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 testResetActivePushPromise() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    server.requestHandler(req -> {/*w  w w  .  ja v a  2  s .  c  o m*/
        req.response().push(HttpMethod.GET, "/wibble", ar -> {
            assertTrue(ar.succeeded());
            assertOnIOContext(ctx);
            HttpServerResponse response = ar.result();
            AtomicInteger resets = new AtomicInteger();
            response.exceptionHandler(err -> {
                if (err instanceof StreamResetException) {
                    assertEquals(8, ((StreamResetException) err).getCode());
                    resets.incrementAndGet();
                }
            });
            response.closeHandler(v -> {
                testComplete();
                assertEquals(1, resets.get());
            });
            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.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testQueuePushPromise() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    int numPushes = 10;
    Set<String> pushSent = new HashSet<>();
    server.requestHandler(req -> {//w  w  w.j av a2  s  .  com
        req.response().setChunked(true).write("abc");
        for (int i = 0; i < numPushes; i++) {
            int val = i;
            String path = "/wibble" + val;
            req.response().push(HttpMethod.GET, path, ar -> {
                assertTrue(ar.succeeded());
                assertSame(ctx, Vertx.currentContext());
                pushSent.add(path);
                vertx.setTimer(10, id -> {
                    ar.result().end("wibble-" + val);
                });
            });
        }
    });
    startServer(ctx);
    TestClient client = new TestClient();
    client.settings.maxConcurrentStreams(3);
    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() {
            int count = numPushes;
            Set<String> pushReceived = new HashSet<>();

            @Override
            public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId,
                    Http2Headers headers, int padding) throws Http2Exception {
                pushReceived.add(headers.path().toString());
            }

            @Override
            public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding,
                    boolean endOfStream) throws Http2Exception {
                if (count-- == 0) {
                    vertx.runOnContext(v -> {
                        assertEquals(numPushes, pushSent.size());
                        assertEquals(pushReceived, pushSent);
                        testComplete();
                    });
                }
                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 testResetPendingPushPromise() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    server.requestHandler(req -> {/*from   w  w w.  j a  va  2s .  c  om*/
        req.response().push(HttpMethod.GET, "/wibble", ar -> {
            assertFalse(ar.succeeded());
            assertOnIOContext(ctx);
            testComplete();
        });
    });
    startServer(ctx);
    TestClient client = new TestClient();
    client.settings.maxConcurrentStreams(0);
    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 void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId,
                    Http2Headers headers, int padding) throws Http2Exception {
                request.encoder.writeRstStream(request.context, promisedStreamId, Http2Error.CANCEL.code(),
                        request.context.newPromise());
                request.context.flush();
            }
        });
    });
    fut.sync();
    await();
}

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

License:Open Source License

private void testMalformedRequestHeaders(Http2Headers headers) throws Exception {
    server.requestHandler(req -> fail());
    startServer();/*from w  w  w .j a va2  s .co m*/
    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, headers, 0, true, request.context.newPromise());
        request.decoder.frameListener(new Http2FrameAdapter() {
            @Override
            public void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode)
                    throws Http2Exception {
                vertx.runOnContext(v -> {
                    testComplete();
                });
            }
        });
    });
    fut.sync();
    await();
}

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

License:Open Source License

private void testHandlerFailure(boolean data, BiConsumer<RuntimeException, HttpServer> configurator)
        throws Exception {
    RuntimeException failure = new RuntimeException();
    io.vertx.core.http.Http2Settings settings = TestUtils.randomHttp2Settings();
    server.close();/*from  w  ww .  jav a2  s  .  c o  m*/
    server = vertx.createHttpServer(serverOptions.setInitialSettings(settings));
    configurator.accept(failure, server);
    Context ctx = vertx.getOrCreateContext();
    ctx.exceptionHandler(err -> {
        assertSame(err, failure);
        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, GET("/"), 0, !data, request.context.newPromise());
        if (data) {
            request.encoder.writeData(request.context, id, Buffer.buffer("hello").getByteBuf(), 0, true,
                    request.context.newPromise());
        }
    });
    fut.sync();
    await();
}

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

License:Open Source License

private void testSendFile(Buffer expected, String path, long offset, long length) throws Exception {
    waitFor(2);// w ww . j a va 2s  .  c o  m
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        resp.bodyEndHandler(v -> {
            assertEquals(resp.bytesWritten(), length);
            complete();
        });
        resp.sendFile(path, offset, length);
    });
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        request.decoder.frameListener(new Http2EventAdapter() {
            Buffer buffer = Buffer.buffer();
            Http2Headers responseHeaders;

            @Override
            public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
                    int streamDependency, short weight, boolean exclusive, int padding, boolean endStream)
                    throws Http2Exception {
                responseHeaders = headers;
            }

            @Override
            public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding,
                    boolean endOfStream) throws Http2Exception {
                buffer.appendBuffer(Buffer.buffer(data.duplicate()));
                if (endOfStream) {
                    vertx.runOnContext(v -> {
                        assertEquals("" + length, responseHeaders.get("content-length").toString());
                        assertEquals(expected, buffer);
                        complete();
                    });
                }
                return data.readableBytes() + padding;
            }
        });
        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 testStreamError() throws Exception {
    waitFor(2);//from  w ww .j  a  v a  2s .  c  o m
    Future<Void> when = Future.future();
    Context ctx = vertx.getOrCreateContext();
    server.requestHandler(req -> {
        AtomicInteger reqErrors = new AtomicInteger();
        req.exceptionHandler(err -> {
            // Called twice : reset + close
            assertOnIOContext(ctx);
            reqErrors.incrementAndGet();
        });
        AtomicInteger respErrors = new AtomicInteger();
        req.response().exceptionHandler(err -> {
            assertOnIOContext(ctx);
            respErrors.incrementAndGet();
        });
        req.response().closeHandler(v -> {
            assertOnIOContext(ctx);
            assertTrue("Was expecting reqErrors to be > 0", reqErrors.get() > 0);
            assertTrue("Was expecting respErrors to be > 0", respErrors.get() > 0);
            complete();
        });
        req.response().endHandler(v -> {
            assertOnIOContext(ctx);
            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.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testPromiseStreamError() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    waitFor(2);/*w w  w  .j av a 2  s. 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();
            AtomicInteger erros = new AtomicInteger();
            resp.exceptionHandler(err -> {
                assertOnIOContext(ctx);
                erros.incrementAndGet();
            });
            resp.closeHandler(v -> {
                assertOnIOContext(ctx);
                assertTrue("Was expecting errors to be > 0", erros.get() > 0);
                complete();
            });
            resp.endHandler(v -> {
                assertOnIOContext(ctx);
                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.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testConnectionDecodeError() throws Exception {
    Context ctx = vertx.getOrCreateContext();
    waitFor(3);/*w  w w  .ja v a  2 s. co  m*/
    Future<Void> when = Future.future();
    server.requestHandler(req -> {
        AtomicInteger reqFailures = new AtomicInteger();
        AtomicInteger respFailures = new AtomicInteger();
        req.exceptionHandler(err -> {
            assertOnIOContext(ctx);
            reqFailures.incrementAndGet();
        });
        req.response().exceptionHandler(err -> {
            assertOnIOContext(ctx);
            respFailures.incrementAndGet();
        });
        req.response().closeHandler(v -> {
            assertOnIOContext(ctx);
            complete();
        });
        req.response().endHandler(v -> {
            assertOnIOContext(ctx);
            assertTrue(reqFailures.get() > 0);
            assertTrue(respFailures.get() > 0);
            complete();
        });
        HttpConnection conn = req.connection();
        AtomicInteger connFailures = new AtomicInteger();
        conn.exceptionHandler(err -> {
            assertOnIOContext(ctx);
            connFailures.incrementAndGet();
        });
        conn.closeHandler(v -> {
            assertTrue(connFailures.get() > 0);
            assertOnIOContext(ctx);
            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 -> {
            // Send a stream ID that does not exists
            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.core.http.Http2ServerTest.java

License:Open Source License

private void testServerSendGoAway(Handler<HttpServerRequest> requestHandler, int expectedError)
        throws Exception {
    server.requestHandler(requestHandler);
    startServer();/*from   w w  w. j a  v  a  2  s . co m*/
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        request.decoder.frameListener(new Http2EventAdapter() {
            @Override
            public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
                    ByteBuf debugData) throws Http2Exception {
                vertx.runOnContext(v -> {
                    assertEquals(expectedError, errorCode);
                    complete();
                });
            }
        });
        Http2ConnectionEncoder encoder = request.encoder;
        int id1 = request.nextStreamId();
        encoder.writeHeaders(request.context, id1, GET("/"), 0, true, request.context.newPromise());
        int id2 = request.nextStreamId();
        encoder.writeHeaders(request.context, id2, GET("/"), 0, true, request.context.newPromise());
        request.context.flush();

    });
    fut.sync();
    await();
}