Example usage for io.netty.channel ChannelPromise await

List of usage examples for io.netty.channel ChannelPromise await

Introduction

In this page you can find the example usage for io.netty.channel ChannelPromise await.

Prototype

boolean await(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Waits for this future to be completed within the specified time limit.

Usage

From source file:io.undertow.server.protocol.http2.HTTP2ViaUpgradeTestCase.java

License:Open Source License

@Test
public void testHttp2WithNettyClient() throws Exception {

    message = "Hello World";

    EventLoopGroup workerGroup = new NioEventLoopGroup();
    Http2ClientInitializer initializer = new Http2ClientInitializer(Integer.MAX_VALUE);

    try {//w  w w  . j av a2 s .  co  m
        // Configure the client.
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        final int port = DefaultServer.getHostPort("default") + 1;
        final String host = DefaultServer.getHostAddress("default");
        b.remoteAddress(host, port);
        b.handler(initializer);

        // Start the client.

        Channel channel = b.connect().syncUninterruptibly().channel();

        Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
        http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS);
        HttpResponseHandler responseHandler = initializer.responseHandler();
        int streamId = 3;
        URI hostName = URI.create("http://" + host + ':' + port);
        System.err.println("Sending request(s)...");
        // Create a simple GET request.
        final ChannelPromise promise = channel.newPromise();
        responseHandler.put(streamId, promise);
        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
                hostName.toString());
        request.headers().add(HttpHeaderNames.HOST, hostName);
        request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
        request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
        channel.writeAndFlush(request);
        streamId += 2;
        promise.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(message, messages.poll());
        System.out.println("Finished HTTP/2 request(s)");

        // Wait until the connection is closed.
        channel.close().syncUninterruptibly();
    } finally {
        workerGroup.shutdownGracefully();
    }
}