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 testNetSocketConnect() throws Exception { waitFor(2);/*from w ww. j av a 2 s .co m*/ List<Integer> callbacks = Collections.synchronizedList(new ArrayList<>()); server.requestHandler(req -> { NetSocket socket = req.netSocket(); AtomicInteger status = new AtomicInteger(); socket.handler(buff -> { switch (status.getAndIncrement()) { case 0: assertEquals(Buffer.buffer("some-data"), buff); socket.write(buff, onSuccess(v2 -> callbacks.add(0))); break; case 1: assertEquals(Buffer.buffer("last-data"), buff); break; default: fail(); break; } }); socket.endHandler(v1 -> { assertEquals(2, status.getAndIncrement()); socket.write(Buffer.buffer("last-data"), onSuccess(v2 -> callbacks.add(1))); }); socket.closeHandler(v -> { assertEquals(2, callbacks.size()); assertEquals(Arrays.asList(0, 1), callbacks); complete(); }); }); startServer(); 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("200", headers.status().toString()); assertFalse(endStream); }); request.encoder.writeData(request.context, id, Buffer.buffer("some-data").getByteBuf(), 0, false, request.context.newPromise()); request.context.flush(); } StringBuilder received = new StringBuilder(); @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { String s = data.toString(StandardCharsets.UTF_8); received.append(s); if (received.toString().equals("some-data")) { received.setLength(0); vertx.runOnContext(v -> { assertFalse(endOfStream); }); request.encoder.writeData(request.context, id, Buffer.buffer("last-data").getByteBuf(), 0, true, request.context.newPromise()); } else if (endOfStream) { vertx.runOnContext(v -> { assertEquals("last-data", received.toString()); complete(); }); } return data.readableBytes() + padding; } }); request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise()); request.context.flush(); }); fut.sync(); await(); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
private void testNetSocketSendFile(Buffer expected, String path, long offset, long length) throws Exception { server.requestHandler(req -> {// w w w . j a v a 2 s . com NetSocket socket = req.netSocket(); socket.sendFile(path, offset, length, ar -> { assertTrue(ar.succeeded()); socket.end(); }); }); startServer(); 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("200", headers.status().toString()); assertFalse(endStream); }); } Buffer received = Buffer.buffer(); @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { received.appendBuffer(Buffer.buffer(data.copy())); if (endOfStream) { vertx.runOnContext(v -> { assertEquals(received, expected); testComplete(); }); } return data.readableBytes() + padding; } }); 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 testServerCloseNetSocket() throws Exception { waitFor(2);/*from ww w . ja v a 2 s . c o m*/ final AtomicInteger writeAcks = new AtomicInteger(0); AtomicInteger status = new AtomicInteger(); server.requestHandler(req -> { NetSocket socket = req.netSocket(); socket.handler(buff -> { switch (status.getAndIncrement()) { case 0: assertEquals(Buffer.buffer("some-data"), buff); socket.write(buff, onSuccess(v -> writeAcks.incrementAndGet())); socket.close(); break; case 1: assertEquals(Buffer.buffer("last-data"), buff); break; default: fail(); break; } }); socket.endHandler(v -> { assertEquals(2, status.getAndIncrement()); }); socket.closeHandler(v -> { assertEquals(3, status.getAndIncrement()); complete(); assertEquals(1, writeAcks.get()); }); }); startServer(); TestClient client = new TestClient(); ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> { int id = request.nextStreamId(); request.decoder.frameListener(new Http2EventAdapter() { int count = 0; @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { int c = count++; vertx.runOnContext(v -> { assertEquals(0, c); }); request.encoder.writeData(request.context, id, Buffer.buffer("some-data").getByteBuf(), 0, false, request.context.newPromise()); request.context.flush(); } StringBuilder received = new StringBuilder(); @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { String s = data.toString(StandardCharsets.UTF_8); received.append(s); if (endOfStream) { request.encoder.writeData(request.context, id, Buffer.buffer("last-data").getByteBuf(), 0, true, request.context.newPromise()); vertx.runOnContext(v -> { assertEquals("some-data", received.toString()); complete(); }); } return data.readableBytes() + padding; } }); request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, 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 testNetSocketHandleReset() throws Exception { server.requestHandler(req -> {// ww w . jav a 2 s . c o m NetSocket socket = req.netSocket(); AtomicInteger status = new AtomicInteger(); socket.exceptionHandler(err -> { assertTrue(err instanceof StreamResetException); StreamResetException ex = (StreamResetException) err; assertEquals(0, ex.getCode()); assertEquals(0, status.getAndIncrement()); }); socket.endHandler(v -> { fail(); }); socket.closeHandler(v -> { assertEquals(1, status.getAndIncrement()); testComplete(); }); }); startServer(); TestClient client = new TestClient(); ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> { int id = request.nextStreamId(); request.decoder.frameListener(new Http2EventAdapter() { int count = 0; @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { int c = count++; vertx.runOnContext(v -> { assertEquals(0, c); }); request.encoder.writeRstStream(ctx, streamId, 0, ctx.newPromise()); request.context.flush(); } }); request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, 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 testUnknownFrame() throws Exception { Buffer expectedSend = TestUtils.randomBuffer(500); Buffer expectedRecv = TestUtils.randomBuffer(500); Context ctx = vertx.getOrCreateContext(); server.requestHandler(req -> {//from w w w. j a v a 2s .c om req.customFrameHandler(frame -> { assertOnIOContext(ctx); assertEquals(10, frame.type()); assertEquals(253, frame.flags()); assertEquals(expectedSend, frame.payload()); req.response().writeCustomFrame(12, 134, expectedRecv).end(); }); }); 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() { int status = 0; @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { int s = status++; vertx.runOnContext(v -> { assertEquals(0, s); }); } @Override public void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int streamId, Http2Flags flags, ByteBuf payload) { int s = status++; Buffer recv = Buffer.buffer(payload.copy()); vertx.runOnContext(v -> { assertEquals(1, s); assertEquals(12, frameType); assertEquals(134, flags.value()); assertEquals(expectedRecv, recv); }); } @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { int len = data.readableBytes(); int s = status++; vertx.runOnContext(v -> { assertEquals(2, s); assertEquals(0, len); assertTrue(endOfStream); testComplete(); }); return data.readableBytes() + padding; } }); request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise()); request.encoder.writeFrame(request.context, (byte) 10, id, new Http2Flags((short) 253), expectedSend.getByteBuf(), 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 testIdleTimeout() throws Exception { waitFor(5);/*from w w w . ja v a 2s . c om*/ server.close(); server = vertx .createHttpServer(serverOptions.setIdleTimeoutUnit(TimeUnit.MILLISECONDS).setIdleTimeout(2000)); server.requestHandler(req -> { req.exceptionHandler(err -> { assertTrue(err instanceof ClosedChannelException); complete(); }); req.response().closeHandler(v -> { complete(); }); req.response().endHandler(v -> { complete(); }); req.connection().closeHandler(v -> { complete(); }); }); startServer(); TestClient client = new TestClient(); ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> { int id = request.nextStreamId(); request.decoder.frameListener(new Http2EventAdapter() { }); request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise()); request.context.flush(); }); fut.sync(); fut.channel().closeFuture().addListener(v1 -> { vertx.runOnContext(v2 -> { complete(); }); }); await(); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testSendPing() throws Exception { waitFor(2);/*from w w w . ja v a 2 s .co m*/ Buffer expected = TestUtils.randomBuffer(8); Context ctx = vertx.getOrCreateContext(); server.connectionHandler(conn -> { conn.ping(expected, ar -> { assertSame(ctx, Vertx.currentContext()); assertTrue(ar.succeeded()); assertEquals(expected, ar.result()); complete(); }); }); server.requestHandler(req -> fail()); 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 onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { Buffer buffer = Buffer.buffer().appendLong(data); vertx.runOnContext(v -> { assertEquals(expected, buffer); complete(); }); } }); }); fut.sync(); await(); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testReceivePing() throws Exception { Buffer expected = TestUtils.randomBuffer(8); Context ctx = vertx.getOrCreateContext(); server.connectionHandler(conn -> { conn.pingHandler(buff -> {/*from w w w. j a v a 2s . c om*/ assertOnIOContext(ctx); assertEquals(expected, buff); testComplete(); }); }); server.requestHandler(req -> fail()); startServer(ctx); TestClient client = new TestClient(); ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> { request.encoder.writePing(request.context, false, expected.getLong(0), request.context.newPromise()); }); fut.sync(); await(); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testPriorKnowledge() throws Exception { server.close();/*from www . ja v a 2 s . c o m*/ server = vertx .createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST)); server.requestHandler(req -> { req.response().end("Hello World"); }); startServer(); TestClient client = new TestClient() { @Override protected ChannelInitializer channelInitializer(int port, String host, Consumer<Connection> handler) { return new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline p = ch.pipeline(); Http2Connection connection = new DefaultHttp2Connection(false); TestClientHandlerBuilder clientHandlerBuilder = new TestClientHandlerBuilder(handler); TestClientHandler clientHandler = clientHandlerBuilder.build(connection); p.addLast(clientHandler); } }; } }; ChannelFuture fut = client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_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 -> { 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 testConnectionWindowSize() throws Exception { server.close();// w ww . j a v a 2 s . c om server = vertx.createHttpServer(createHttp2ServerOptions(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST) .setHttp2ConnectionWindowSize(65535 + 65535)); server.requestHandler(req -> { 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(); }