List of usage examples for io.netty.channel ChannelFuture sync
@Override
ChannelFuture sync() throws InterruptedException;
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testUpdateConnectionWindowSize() throws Exception { server.connectionHandler(conn -> { assertEquals(65535, conn.getWindowSize()); conn.setWindowSize(65535 + 10000); assertEquals(65535 + 10000, conn.getWindowSize()); conn.setWindowSize(65535 + 65535); assertEquals(65535 + 65535, conn.getWindowSize()); }).requestHandler(req -> {//from ww w . ja va2 s . com req.response().end(); }); startServer(); TestClient client = new TestClient(); ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> { request.decoder.frameListener(new Http2EventAdapter() { @Override public void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement) throws Http2Exception { vertx.runOnContext(v -> { assertEquals(65535, windowSizeIncrement); testComplete(); }); } }); }); fut.sync(); await(); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testStreamPriority() throws Exception { StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45) .setExclusive(true);/*from w w w.j a v a 2 s . c om*/ StreamPriority responseStreamPriority = new StreamPriority().setDependency(153).setWeight((short) 75) .setExclusive(false); waitFor(3); server.requestHandler(req -> { HttpServerResponse resp = req.response(); assertEquals(requestStreamPriority, req.streamPriority()); resp.setStatusCode(200); resp.setStreamPriority(responseStreamPriority); resp.end("data"); complete(); }); 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("/"), requestStreamPriority.getDependency(), requestStreamPriority.getWeight(), requestStreamPriority.isExclusive(), 0, true, request.context.newPromise()); request.context.flush(); request.decoder.frameListener(new Http2FrameAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { super.onHeadersRead(ctx, streamId, headers, streamDependency, weight, exclusive, padding, endStream); vertx.runOnContext(v -> { assertEquals(id, streamId); assertEquals(responseStreamPriority.getDependency(), streamDependency); assertEquals(responseStreamPriority.getWeight(), weight); assertEquals(responseStreamPriority.isExclusive(), exclusive); complete(); }); } @Override public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive) throws Http2Exception { vertx.runOnContext(v -> { fail("Priority frame should not be sent"); }); } @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { if (endOfStream) { vertx.runOnContext(v -> { complete(); }); } return super.onDataRead(ctx, streamId, data, padding, endOfStream); } }); }); fut.sync(); await(); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testStreamPriorityChange() throws Exception { StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45) .setExclusive(true);/*from w w w . j av a 2 s. c om*/ StreamPriority requestStreamPriority2 = new StreamPriority().setDependency(223).setWeight((short) 145) .setExclusive(false); StreamPriority responseStreamPriority = new StreamPriority().setDependency(153).setWeight((short) 75) .setExclusive(false); StreamPriority responseStreamPriority2 = new StreamPriority().setDependency(253).setWeight((short) 175) .setExclusive(true); waitFor(5); server.requestHandler(req -> { HttpServerResponse resp = req.response(); assertEquals(requestStreamPriority, req.streamPriority()); req.bodyHandler(b -> { assertEquals(requestStreamPriority2, req.streamPriority()); resp.setStatusCode(200); resp.setStreamPriority(responseStreamPriority); resp.write("hello"); resp.setStreamPriority(responseStreamPriority2); resp.end("world"); complete(); }); req.streamPriorityHandler(streamPriority -> { assertEquals(requestStreamPriority2, streamPriority); assertEquals(requestStreamPriority2, req.streamPriority()); complete(); }); }); 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("/"), requestStreamPriority.getDependency(), requestStreamPriority.getWeight(), requestStreamPriority.isExclusive(), 0, false, request.context.newPromise()); request.context.flush(); request.encoder.writePriority(request.context, id, requestStreamPriority2.getDependency(), requestStreamPriority2.getWeight(), requestStreamPriority2.isExclusive(), request.context.newPromise()); request.context.flush(); request.encoder.writeData(request.context, id, Buffer.buffer("hello").getByteBuf(), 0, true, request.context.newPromise()); request.context.flush(); request.decoder.frameListener(new Http2FrameAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { super.onHeadersRead(ctx, streamId, headers, streamDependency, weight, exclusive, padding, endStream); vertx.runOnContext(v -> { assertEquals(id, streamId); assertEquals(responseStreamPriority.getDependency(), streamDependency); assertEquals(responseStreamPriority.getWeight(), weight); assertEquals(responseStreamPriority.isExclusive(), exclusive); complete(); }); } @Override public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive) throws Http2Exception { vertx.runOnContext(v -> { assertEquals(id, streamId); assertEquals(responseStreamPriority2.getDependency(), streamDependency); assertEquals(responseStreamPriority2.getWeight(), weight); assertEquals(responseStreamPriority2.isExclusive(), exclusive); complete(); }); } @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { if (endOfStream) { vertx.runOnContext(v -> { complete(); }); } return super.onDataRead(ctx, streamId, data, padding, endOfStream); } }); }); fut.sync(); await(); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testStreamPriorityNoChange() throws Exception { StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45) .setExclusive(true);//w ww. j a v a2s . com StreamPriority responseStreamPriority = new StreamPriority().setDependency(153).setWeight((short) 75) .setExclusive(false); waitFor(3); server.requestHandler(req -> { HttpServerResponse resp = req.response(); assertEquals(requestStreamPriority, req.streamPriority()); req.bodyHandler(b -> { assertEquals(requestStreamPriority, req.streamPriority()); resp.setStatusCode(200); resp.setStreamPriority(responseStreamPriority); resp.write("hello"); resp.setStreamPriority(responseStreamPriority); resp.end("world"); complete(); }); req.streamPriorityHandler(streamPriority -> { fail("Stream priority handler should not be called"); }); }); 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("/"), requestStreamPriority.getDependency(), requestStreamPriority.getWeight(), requestStreamPriority.isExclusive(), 0, false, request.context.newPromise()); request.context.flush(); request.encoder.writePriority(request.context, id, requestStreamPriority.getDependency(), requestStreamPriority.getWeight(), requestStreamPriority.isExclusive(), request.context.newPromise()); request.context.flush(); request.encoder.writeData(request.context, id, Buffer.buffer("hello").getByteBuf(), 0, true, request.context.newPromise()); request.context.flush(); request.decoder.frameListener(new Http2FrameAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { super.onHeadersRead(ctx, streamId, headers, streamDependency, weight, exclusive, padding, endStream); vertx.runOnContext(v -> { assertEquals(id, streamId); assertEquals(responseStreamPriority.getDependency(), streamDependency); assertEquals(responseStreamPriority.getWeight(), weight); assertEquals(responseStreamPriority.isExclusive(), exclusive); complete(); }); } @Override public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive) throws Http2Exception { vertx.runOnContext(v -> { fail("Priority frame should not be sent"); }); } @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { if (endOfStream) { vertx.runOnContext(v -> { complete(); }); } return super.onDataRead(ctx, streamId, data, padding, endOfStream); } }); }); fut.sync(); await(); }
From source file:io.vertx.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testConnectionHandler() throws Exception { waitFor(2);/*w w w .ja va 2 s . c o m*/ Context ctx = vertx.getOrCreateContext(); server.close(); 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.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testServerSettings() throws Exception { waitFor(2);/*from w w w . j a va2s . c o m*/ io.vertx.core.http.Http2Settings expectedSettings = TestUtils.randomHttp2Settings(); expectedSettings.setHeaderTableSize((int) io.vertx.core.http.Http2Settings.DEFAULT_HEADER_TABLE_SIZE); server.close(); server = vertx.createHttpServer(serverOptions); Context otherContext = vertx.getOrCreateContext(); server.connectionHandler(conn -> { otherContext.runOnContext(v -> { conn.updateSettings(expectedSettings, ar -> { assertSame(otherContext, 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(); @Override public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings newSettings) throws Http2Exception { vertx.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.test.core.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(); Future<Void> settingsRead = Future.future(); AtomicInteger count = new AtomicInteger(); server.connectionHandler(conn -> { io.vertx.core.http.Http2Settings settings = conn.remoteSettings(); assertEquals(true, settings.isPushEnabled()); // Netty bug ? // Nothing has been yet received so we should get Integer.MAX_VALUE // assertEquals(Integer.MAX_VALUE, settings.getMaxHeaderListSize()); assertEquals(io.vertx.core.http.Http2Settings.DEFAULT_MAX_FRAME_SIZE, settings.getMaxFrameSize()); assertEquals(io.vertx.core.http.Http2Settings.DEFAULT_INITIAL_WINDOW_SIZE, settings.getInitialWindowSize()); assertEquals((Long) (long) Integer.MAX_VALUE, (Long) (long) settings.getMaxConcurrentStreams()); assertEquals(io.vertx.core.http.Http2Settings.DEFAULT_HEADER_TABLE_SIZE, settings.getHeaderTableSize()); conn.remoteSettingsHandler(update -> { assertOnIOContext(ctx);//www . j av a2 s .c o m switch (count.getAndIncrement()) { case 0: assertEquals(initialSettings.isPushEnabled(), update.isPushEnabled()); assertEquals(initialSettings.getMaxHeaderListSize(), update.getMaxHeaderListSize()); assertEquals(initialSettings.getMaxFrameSize(), update.getMaxFrameSize()); assertEquals(initialSettings.getInitialWindowSize(), update.getInitialWindowSize()); assertEquals(initialSettings.getMaxConcurrentStreams(), update.getMaxConcurrentStreams()); assertEquals(initialSettings.getHeaderTableSize(), update.getHeaderTableSize()); assertEquals(initialSettings.get('\u0007'), update.get(7)); settingsRead.complete(); break; case 1: 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; } }); }); 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 -> { settingsRead.setHandler(ar -> { request.encoder.writeSettings(request.context, HttpUtils.fromVertxSettings(updatedSettings), request.context.newPromise()); request.context.flush(); }); }); fut.sync(); await(); }
From source file:io.vertx.test.core.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 -> {// w w w . j av a 2 s .c om 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()); assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.getHeader(":authority")); assertTrue(req.isSSL()); assertEquals("https", req.getHeader(":scheme")); assertEquals("/", req.getHeader(":path")); assertEquals("GET", req.getHeader(":method")); 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.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testURI() throws Exception { server.requestHandler(req -> {//from w w w .java2s . c o m 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("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.getHeader(":path")); 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_2", 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.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testClientResetServerStream() throws Exception { Context ctx = vertx.getOrCreateContext(); Future<Void> bufReceived = Future.future(); AtomicInteger resetCount = new AtomicInteger(); server.requestHandler(req -> {//from w w w . j a va2 s. co m req.handler(buf -> { bufReceived.complete(); }); req.exceptionHandler(err -> { assertOnIOContext(ctx); assertTrue(err instanceof StreamResetException); assertEquals(10L, ((StreamResetException) err).getCode()); assertEquals(0, resetCount.getAndIncrement()); }); req.response().exceptionHandler(err -> { assertOnIOContext(ctx); assertTrue(err instanceof StreamResetException); assertEquals(10L, ((StreamResetException) err).getCode()); assertEquals(1, resetCount.getAndIncrement()); }); req.endHandler(v -> { assertOnIOContext(ctx); assertEquals(2, resetCount.get()); testComplete(); }); }); startServer(ctx); TestClient client = new TestClient(); ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> { int id = request.nextStreamId(); Http2ConnectionEncoder encoder = request.encoder; encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise()); encoder.writeData(request.context, id, Buffer.buffer("hello").getByteBuf(), 0, false, request.context.newPromise()); bufReceived.setHandler(ar -> { encoder.writeRstStream(request.context, id, 10, request.context.newPromise()); request.context.flush(); }); }); fut.sync(); await(); }