Example usage for io.netty.channel ChannelFuture sync

List of usage examples for io.netty.channel ChannelFuture sync

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture sync.

Prototype

@Override
    ChannelFuture sync() throws InterruptedException;

Source Link

Usage

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testConnectionHandler() throws Exception {
    waitFor(2);//from w w w  . j av a2s  .com
    Context ctx = vertx.getOrCreateContext();
    server.connectionHandler(conn -> {
        assertOnIOContext(ctx);
        complete();
    });
    server.requestHandler(req -> fail());
    startServer(ctx);
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        vertx.runOnContext(v -> {
            complete();
        });
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testServerInitialSettings() throws Exception {
    io.vertx.core.http.Http2Settings settings = TestUtils.randomHttp2Settings();
    server.close();//from  w  ww  .  j a va 2 s.  c  o  m
    server = vertx.createHttpServer(serverOptions.setInitialSettings(settings));
    server.requestHandler(req -> fail());
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        request.decoder.frameListener(new Http2FrameAdapter() {
            @Override
            public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings newSettings)
                    throws Http2Exception {
                vertx.runOnContext(v -> {
                    assertEquals((Long) settings.getHeaderTableSize(), newSettings.headerTableSize());
                    assertEquals((Long) settings.getMaxConcurrentStreams(), newSettings.maxConcurrentStreams());
                    assertEquals((Integer) settings.getInitialWindowSize(), newSettings.initialWindowSize());
                    assertEquals((Integer) settings.getMaxFrameSize(), newSettings.maxFrameSize());
                    assertEquals((Long) settings.getMaxHeaderListSize(), newSettings.maxHeaderListSize());
                    assertEquals(settings.get('\u0007'), newSettings.get('\u0007'));
                    testComplete();
                });
            }
        });
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testServerSettings() throws Exception {
    waitFor(2);//  www  .  j  a v  a  2 s.com
    io.vertx.core.http.Http2Settings expectedSettings = TestUtils.randomHttp2Settings();
    expectedSettings.setHeaderTableSize((int) io.vertx.core.http.Http2Settings.DEFAULT_HEADER_TABLE_SIZE);
    Context otherContext = vertx.getOrCreateContext();
    server.connectionHandler(conn -> {
        Context ctx = Vertx.currentContext();
        otherContext.runOnContext(v -> {
            conn.updateSettings(expectedSettings, ar -> {
                assertSame(ctx, Vertx.currentContext());
                io.vertx.core.http.Http2Settings ackedSettings = conn.settings();
                assertEquals(expectedSettings.getMaxHeaderListSize(), ackedSettings.getMaxHeaderListSize());
                assertEquals(expectedSettings.getMaxFrameSize(), ackedSettings.getMaxFrameSize());
                assertEquals(expectedSettings.getInitialWindowSize(), ackedSettings.getInitialWindowSize());
                assertEquals(expectedSettings.getMaxConcurrentStreams(),
                        ackedSettings.getMaxConcurrentStreams());
                assertEquals(expectedSettings.getHeaderTableSize(), ackedSettings.getHeaderTableSize());
                assertEquals(expectedSettings.get('\u0007'), ackedSettings.get(7));
                complete();
            });
        });
    });
    server.requestHandler(req -> {
        fail();
    });
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        request.decoder.frameListener(new Http2FrameAdapter() {
            AtomicInteger count = new AtomicInteger();
            Context context = vertx.getOrCreateContext();

            @Override
            public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings newSettings)
                    throws Http2Exception {
                context.runOnContext(v -> {
                    switch (count.getAndIncrement()) {
                    case 0:
                        // Initial settings
                        break;
                    case 1:
                        // Server sent settings
                        assertEquals((Long) expectedSettings.getMaxHeaderListSize(),
                                newSettings.maxHeaderListSize());
                        assertEquals((Integer) expectedSettings.getMaxFrameSize(), newSettings.maxFrameSize());
                        assertEquals((Integer) expectedSettings.getInitialWindowSize(),
                                newSettings.initialWindowSize());
                        assertEquals((Long) expectedSettings.getMaxConcurrentStreams(),
                                newSettings.maxConcurrentStreams());
                        assertEquals(null, newSettings.headerTableSize());
                        complete();
                        break;
                    default:
                        fail();
                    }
                });
            }
        });
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testClientSettings() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    io.vertx.core.http.Http2Settings initialSettings = TestUtils.randomHttp2Settings();
    io.vertx.core.http.Http2Settings updatedSettings = TestUtils.randomHttp2Settings();
    AtomicInteger count = new AtomicInteger();
    server.connectionHandler(conn -> {
        io.vertx.core.http.Http2Settings settings = conn.remoteSettings();
        assertEquals(initialSettings.isPushEnabled(), settings.isPushEnabled());

        // Netty bug ?
        // Nothing has been yet received so we should get Integer.MAX_VALUE
        // assertEquals(Integer.MAX_VALUE, settings.getMaxHeaderListSize());

        assertEquals(initialSettings.getMaxFrameSize(), settings.getMaxFrameSize());
        assertEquals(initialSettings.getInitialWindowSize(), settings.getInitialWindowSize());
        assertEquals((Long) (long) initialSettings.getMaxConcurrentStreams(),
                (Long) (long) settings.getMaxConcurrentStreams());
        assertEquals(initialSettings.getHeaderTableSize(), settings.getHeaderTableSize());

        conn.remoteSettingsHandler(update -> {
            assertOnIOContext(ctx);//  w  w  w . jav a 2  s .  com
            switch (count.getAndIncrement()) {
            case 0:
                assertEquals(updatedSettings.isPushEnabled(), update.isPushEnabled());
                assertEquals(updatedSettings.getMaxHeaderListSize(), update.getMaxHeaderListSize());
                assertEquals(updatedSettings.getMaxFrameSize(), update.getMaxFrameSize());
                assertEquals(updatedSettings.getInitialWindowSize(), update.getInitialWindowSize());
                assertEquals(updatedSettings.getMaxConcurrentStreams(), update.getMaxConcurrentStreams());
                assertEquals(updatedSettings.getHeaderTableSize(), update.getHeaderTableSize());
                assertEquals(updatedSettings.get('\u0007'), update.get(7));
                testComplete();
                break;
            default:
                fail();
            }
        });
    });
    server.requestHandler(req -> {
        fail();
    });
    startServer(ctx);
    TestClient client = new TestClient();
    client.settings.putAll(HttpUtils.fromVertxSettings(initialSettings));
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        request.encoder.writeSettings(request.context, HttpUtils.fromVertxSettings(updatedSettings),
                request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testGet() throws Exception {
    String expected = TestUtils.randomAlphaString(1000);
    AtomicBoolean requestEnded = new AtomicBoolean();
    Context ctx = vertx.getOrCreateContext();
    server.requestHandler(req -> {/*from w  w  w  . j a va 2s. co  m*/
        assertOnIOContext(ctx);
        req.endHandler(v -> {
            assertOnIOContext(ctx);
            requestEnded.set(true);
        });
        HttpServerResponse resp = req.response();
        assertEquals(HttpMethod.GET, req.method());
        assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.host());
        assertEquals("/", req.path());
        assertTrue(req.isSSL());
        assertEquals("https", req.scheme());
        assertEquals("/", req.uri());
        assertEquals("foo_request_value", req.getHeader("Foo_request"));
        assertEquals("bar_request_value", req.getHeader("bar_request"));
        assertEquals(2, req.headers().getAll("juu_request").size());
        assertEquals("juu_request_value_1", req.headers().getAll("juu_request").get(0));
        assertEquals("juu_request_value_2", req.headers().getAll("juu_request").get(1));
        assertEquals(Collections.singletonList("cookie_1; cookie_2; cookie_3"), req.headers().getAll("cookie"));
        resp.putHeader("content-type", "text/plain");
        resp.putHeader("Foo_response", "foo_response_value");
        resp.putHeader("bar_response", "bar_response_value");
        resp.putHeader("juu_response",
                (List<String>) Arrays.asList("juu_response_value_1", "juu_response_value_2"));
        resp.end(expected);
    });
    startServer(ctx);
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        int id = request.nextStreamId();
        request.decoder.frameListener(new Http2EventAdapter() {
            @Override
            public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
                    int streamDependency, short weight, boolean exclusive, int padding, boolean endStream)
                    throws Http2Exception {
                vertx.runOnContext(v -> {
                    assertEquals(id, streamId);
                    assertEquals("200", headers.status().toString());
                    assertEquals("text/plain", headers.get("content-type").toString());
                    assertEquals("foo_response_value", headers.get("foo_response").toString());
                    assertEquals("bar_response_value", headers.get("bar_response").toString());
                    assertEquals(2, headers.getAll("juu_response").size());
                    assertEquals("juu_response_value_1", headers.getAll("juu_response").get(0).toString());
                    assertEquals("juu_response_value_2", headers.getAll("juu_response").get(1).toString());
                    assertFalse(endStream);
                });
            }

            @Override
            public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding,
                    boolean endOfStream) throws Http2Exception {
                String actual = data.toString(StandardCharsets.UTF_8);
                vertx.runOnContext(v -> {
                    assertEquals(id, streamId);
                    assertEquals(expected, actual);
                    assertTrue(endOfStream);
                    testComplete();
                });
                return super.onDataRead(ctx, streamId, data, padding, endOfStream);
            }
        });
        Http2Headers headers = GET("/").authority(DEFAULT_HTTPS_HOST_AND_PORT);
        headers.set("foo_request", "foo_request_value");
        headers.set("bar_request", "bar_request_value");
        headers.set("juu_request", "juu_request_value_1", "juu_request_value_2");
        headers.set("cookie", Arrays.asList("cookie_1", "cookie_2", "cookie_3"));
        request.encoder.writeHeaders(request.context, id, headers, 0, true, request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testStatusMessage() throws Exception {
    server.requestHandler(req -> {/*from w  w  w.jav  a2  s  .  c o m*/
        HttpServerResponse resp = req.response();
        resp.setStatusCode(404);
        assertEquals("Not Found", resp.getStatusMessage());
        resp.setStatusMessage("whatever");
        assertEquals("whatever", resp.getStatusMessage());
        testComplete();
    });
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        int id = request.nextStreamId();
        request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testURI() throws Exception {
    server.requestHandler(req -> {//w  w w  .  ja va2s. c om
        assertEquals("/some/path", req.path());
        assertEquals("foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.query());
        assertEquals("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.uri());
        assertEquals("http://whatever.com/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2",
                req.absoluteURI());
        assertEquals("whatever.com", req.host());
        MultiMap params = req.params();
        Set<String> names = params.names();
        assertEquals(2, names.size());
        assertTrue(names.contains("foo"));
        assertTrue(names.contains("bar"));
        assertEquals("foo_value", params.get("foo"));
        assertEquals(Collections.singletonList("foo_value"), params.getAll("foo"));
        assertEquals("bar_value_1", params.get("bar"));
        assertEquals(Arrays.asList("bar_value_1", "bar_value_2"), params.getAll("bar"));
        testComplete();
    });
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        int id = request.nextStreamId();
        Http2Headers headers = new DefaultHttp2Headers().method("GET").scheme("http").authority("whatever.com")
                .path("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2");
        request.encoder.writeHeaders(request.context, id, headers, 0, true, request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testHeadersEndHandler() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    server.requestHandler(req -> {//  ww w.j  a v a  2s  . c  o  m
        HttpServerResponse resp = req.response();
        resp.setChunked(true);
        resp.putHeader("some", "some-header");
        resp.headersEndHandler(v -> {
            assertOnIOContext(ctx);
            assertFalse(resp.headWritten());
            resp.putHeader("extra", "extra-header");
        });
        resp.write("something");
        assertTrue(resp.headWritten());
        resp.end();
    });
    startServer(ctx);
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        request.decoder.frameListener(new Http2EventAdapter() {
            @Override
            public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
                    int streamDependency, short weight, boolean exclusive, int padding, boolean endStream)
                    throws Http2Exception {
                vertx.runOnContext(v -> {
                    assertEquals("some-header", headers.get("some").toString());
                    assertEquals("extra-header", headers.get("extra").toString());
                    testComplete();
                });
            }
        });
        int id = request.nextStreamId();
        request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testBodyEndHandler() throws Exception {
    server.requestHandler(req -> {//w  w w . ja  va2s  . c  om
        HttpServerResponse resp = req.response();
        resp.setChunked(true);
        AtomicInteger count = new AtomicInteger();
        resp.bodyEndHandler(v -> {
            assertEquals(0, count.getAndIncrement());
            assertTrue(resp.ended());
        });
        resp.write("something");
        assertEquals(0, count.get());
        resp.end();
        assertEquals(1, count.get());
        testComplete();
    });
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        int id = request.nextStreamId();
        request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testPost() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    Buffer expectedContent = TestUtils.randomBuffer(1000);
    Buffer postContent = Buffer.buffer();
    server.requestHandler(req -> {/*w ww.j av  a2s  .c  o  m*/
        assertOnIOContext(ctx);
        req.handler(buff -> {
            assertOnIOContext(ctx);
            postContent.appendBuffer(buff);
        });
        req.endHandler(v -> {
            assertOnIOContext(ctx);
            req.response().putHeader("content-type", "text/plain").end("");
            assertEquals(expectedContent, postContent);
            testComplete();
        });
    });
    startServer(ctx);
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        int id = request.nextStreamId();
        request.encoder.writeHeaders(request.context, id, POST("/").set("content-type", "text/plain"), 0, false,
                request.context.newPromise());
        request.encoder.writeData(request.context, id, expectedContent.getByteBuf(), 0, true,
                request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}