List of usage examples for io.netty.channel ChannelFuture sync
@Override
ChannelFuture sync() throws InterruptedException;
From source file:io.vertx.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testResetActivePushPromise() throws Exception { Context ctx = vertx.getOrCreateContext(); server.requestHandler(req -> {/*w w w . ja va2 s .c o m*/ req.response().push(HttpMethod.GET, "/wibble", ar -> { assertTrue(ar.succeeded()); assertOnIOContext(ctx); HttpServerResponse response = ar.result(); response.exceptionHandler(err -> { testComplete(); }); response.setChunked(true).write("some_content"); }); }); 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, true, request.context.newPromise()); request.decoder.frameListener(new Http2FrameAdapter() { @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { request.encoder.writeRstStream(ctx, streamId, Http2Error.CANCEL.code(), ctx.newPromise()); request.context.flush(); 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 testStreamError() throws Exception { waitFor(5);//from www .ja v a 2s .c om Future<Void> when = Future.future(); Context ctx = vertx.getOrCreateContext(); server.requestHandler(req -> { req.exceptionHandler(err -> { // Called twice : reset + close assertEquals(ctx, Vertx.currentContext()); complete(); }); req.response().exceptionHandler(err -> { assertEquals(ctx, Vertx.currentContext()); complete(); }); req.response().closeHandler(v -> { assertEquals(ctx, Vertx.currentContext()); complete(); }); req.response().endHandler(v -> { assertEquals(ctx, Vertx.currentContext()); complete(); }); when.complete(); }); 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()); request.context.flush(); when.setHandler(ar -> { // Send a corrupted frame on purpose to check we get the corresponding error in the request exception handler // the error is : greater padding value 0c -> 1F // ChannelFuture a = encoder.frameWriter().writeData(request.context, id, Buffer.buffer("hello").getByteBuf(), 12, false, request.context.newPromise()); // normal frame : 00 00 12 00 08 00 00 00 03 0c 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 // corrupted frame : 00 00 12 00 08 00 00 00 03 1F 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 request.channel.write(Buffer.buffer(new byte[] { 0x00, 0x00, 0x12, 0x00, 0x08, 0x00, 0x00, 0x00, (byte) (id & 0xFF), 0x1F, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }).getByteBuf()); request.context.flush(); }); }); fut.sync(); await(); }
From source file:io.vertx.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testPromiseStreamError() throws Exception { Context ctx = vertx.getOrCreateContext(); waitFor(3);/*from w ww. j av a 2s . c o m*/ Future<Void> when = Future.future(); server.requestHandler(req -> { req.response().push(HttpMethod.GET, "/wibble", ar -> { assertTrue(ar.succeeded()); assertOnIOContext(ctx); when.complete(); HttpServerResponse resp = ar.result(); resp.exceptionHandler(err -> { assertSame(ctx, Vertx.currentContext()); complete(); }); resp.closeHandler(v -> { assertSame(ctx, Vertx.currentContext()); complete(); }); resp.endHandler(v -> { assertSame(ctx, Vertx.currentContext()); complete(); }); resp.setChunked(true).write("whatever"); // Transition to half-closed remote }); }); 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 onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception { when.setHandler(ar -> { Http2ConnectionEncoder encoder = request.encoder; encoder.frameWriter().writeHeaders(request.context, promisedStreamId, GET("/"), 0, false, request.context.newPromise()); request.context.flush(); }); } }); int id = request.nextStreamId(); Http2ConnectionEncoder encoder = request.encoder; encoder.writeHeaders(request.context, id, GET("/"), 0, false, 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 testConnectionDecodeError() throws Exception { Context ctx = vertx.getOrCreateContext(); waitFor(6);// ww w.java 2 s . c o m Future<Void> when = Future.future(); server.requestHandler(req -> { req.exceptionHandler(err -> { // Called twice : reset + close assertSame(ctx, Vertx.currentContext()); complete(); }); req.response().exceptionHandler(err -> { // Called once : reset assertSame(ctx, Vertx.currentContext()); complete(); }); req.response().closeHandler(v -> { // Called once : close assertSame(ctx, Vertx.currentContext()); complete(); }); req.response().endHandler(v -> { // Called once : close assertSame(ctx, Vertx.currentContext()); complete(); }); req.connection().exceptionHandler(err -> { assertSame(ctx, Vertx.currentContext()); complete(); }); when.complete(); }); 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; when.setHandler(ar -> { encoder.frameWriter().writeRstStream(request.context, 10, 0, request.context.newPromise()); request.context.flush(); }); encoder.writeHeaders(request.context, id, GET("/"), 0, false, 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 testNetSocketConnect() throws Exception { waitFor(2);// w w w . j av a 2 s . c om 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); break; case 1: assertEquals(Buffer.buffer("last-data"), buff); break; default: fail(); break; } }); socket.endHandler(v -> { assertEquals(2, status.getAndIncrement()); socket.write(Buffer.buffer("last-data")); }); socket.closeHandler(v -> { assertEquals(3, status.getAndIncrement()); 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.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testServerCloseNetSocket() throws Exception { waitFor(2);//from w ww . j a va 2 s.co m 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); 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(); }); }); 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.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testIdleTimeout() throws Exception { waitFor(5);/*from w ww . j a v a2 s .c o m*/ server.close(); server = vertx.createHttpServer(serverOptions.setIdleTimeout(2)); 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.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testSendPing() throws Exception { waitFor(2);// ww w .jav a 2 s. c om Buffer expected = TestUtils.randomBuffer(8); Context ctx = vertx.getOrCreateContext(); server.close(); 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, ByteBuf data) throws Http2Exception { Buffer buffer = Buffer.buffer(data.copy()); vertx.runOnContext(v -> { assertEquals(expected, buffer); complete(); }); } }); }); fut.sync(); await(); }
From source file:io.vertx.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testReceivePing() throws Exception { Buffer expected = TestUtils.randomBuffer(8); Context ctx = vertx.getOrCreateContext(); server.close();/* w w w. j a va2 s. c o m*/ server.connectionHandler(conn -> { conn.pingHandler(buff -> { 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.getByteBuf(), request.context.newPromise()); }); fut.sync(); await(); }
From source file:jgnash.engine.concurrent.DistributedLockServer.java
License:Open Source License
public boolean startServer(final char[] password) { boolean result = false; // If a password has been specified, create an EncryptionManager if (password != null && password.length > 0) { encryptionManager = new EncryptionManager(password); }/*from w w w .jav a 2s . c o m*/ eventLoopGroup = new NioEventLoopGroup(); final ServerBootstrap bootstrap = new ServerBootstrap(); try { bootstrap.group(eventLoopGroup).channel(NioServerSocketChannel.class).childHandler(new Initializer()) .childOption(ChannelOption.SO_KEEPALIVE, true); final ChannelFuture future = bootstrap.bind(port); future.sync(); if (future.isDone() && future.isSuccess()) { logger.info("Distributed Lock Server started successfully"); result = true; } else { logger.info("Failed to start the Distributed Lock Server"); } } catch (final InterruptedException e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); stopServer(); } return result; }