Search in sources :

Example 6 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2ClientTest method testClientRequestWriteability.

@Test
public void testClientRequestWriteability() throws Exception {
    Buffer content = Buffer.buffer();
    Buffer expected = Buffer.buffer();
    String chunk = TestUtils.randomAlphaString(100);
    CompletableFuture<Void> done = new CompletableFuture<>();
    AtomicBoolean paused = new AtomicBoolean();
    AtomicInteger numPause = new AtomicInteger();
    server.requestHandler(req -> {
        Context ctx = vertx.getOrCreateContext();
        done.thenAccept(v1 -> {
            paused.set(false);
            ctx.runOnContext(v2 -> {
                req.resume();
            });
        });
        numPause.incrementAndGet();
        req.pause();
        paused.set(true);
        req.handler(content::appendBuffer);
        req.endHandler(v -> {
            assertEquals(expected, content);
            req.response().end();
        });
    });
    startServer();
    HttpClientRequest req = client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        testComplete();
    }).setChunked(true).exceptionHandler(err -> {
        fail();
    });
    AtomicInteger sent = new AtomicInteger();
    AtomicInteger count = new AtomicInteger();
    AtomicInteger drained = new AtomicInteger();
    vertx.setPeriodic(1, timerID -> {
        Context ctx = vertx.getOrCreateContext();
        if (req.writeQueueFull()) {
            assertTrue(paused.get());
            assertEquals(1, numPause.get());
            req.drainHandler(v -> {
                assertOnIOContext(ctx);
                assertEquals(0, drained.getAndIncrement());
                assertEquals(1, numPause.get());
                assertFalse(paused.get());
                req.end();
            });
            vertx.cancelTimer(timerID);
            done.complete(null);
        } else {
            count.incrementAndGet();
            expected.appendString(chunk);
            req.write(chunk);
            sent.addAndGet(chunk.length());
        }
    });
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 7 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2ClientTest method testResetPendingPushPromise.

@Test
public void testResetPendingPushPromise() throws Exception {
    server.requestHandler(req -> {
        req.response().push(HttpMethod.GET, "/wibble", ar -> {
            assertFalse(ar.succeeded());
            testComplete();
        });
    });
    startServer();
    client.close();
    client = vertx.createHttpClient(clientOptions.setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(0L)));
    HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        fail();
    });
    req.pushHandler(pushedReq -> {
        pushedReq.reset(Http2Error.CANCEL.code());
    });
    req.end();
    await();
}
Also used : io.vertx.core(io.vertx.core) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Http2Settings(io.netty.handler.codec.http2.Http2Settings) Test(org.junit.Test)

Example 8 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2ClientTest method testReceivePing.

@Test
public void testReceivePing() throws Exception {
    Buffer expected = TestUtils.randomBuffer(8);
    Context ctx = vertx.getOrCreateContext();
    server.close();
    server.connectionHandler(conn -> {
        conn.ping(expected, ar -> {
        });
    });
    server.requestHandler(req -> {
    });
    startServer(ctx);
    HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
    });
    req.connectionHandler(conn -> {
        conn.pingHandler(data -> {
            assertEquals(expected, data);
            complete();
        });
    });
    req.end();
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Test(org.junit.Test)

Example 9 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2ClientTest method testConnectionShutdownInConnectionHandler.

@Test
public void testConnectionShutdownInConnectionHandler() throws Exception {
    AtomicInteger serverStatus = new AtomicInteger();
    server.connectionHandler(conn -> {
        if (serverStatus.getAndIncrement() == 0) {
            conn.goAwayHandler(ga -> {
                assertEquals(0, ga.getErrorCode());
                assertEquals(1, serverStatus.getAndIncrement());
            });
            conn.shutdownHandler(v -> {
                assertEquals(2, serverStatus.getAndIncrement());
            });
            conn.closeHandler(v -> {
                assertEquals(3, serverStatus.getAndIncrement());
            });
        }
    });
    server.requestHandler(req -> {
        assertEquals(5, serverStatus.getAndIncrement());
        req.response().end();
    });
    startServer();
    AtomicInteger clientStatus = new AtomicInteger();
    HttpClientRequest req1 = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath");
    req1.connectionHandler(conn -> {
        Context ctx = Vertx.currentContext();
        conn.shutdownHandler(v -> {
            assertOnIOContext(ctx);
            clientStatus.compareAndSet(1, 2);
        });
        if (clientStatus.getAndIncrement() == 0) {
            conn.shutdown();
        }
    });
    req1.exceptionHandler(err -> {
        fail();
    });
    req1.handler(resp -> {
        assertEquals(2, clientStatus.getAndIncrement());
        resp.endHandler(v -> {
            testComplete();
        });
    });
    req1.end();
    await();
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 10 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2ClientTest method testResetActivePushPromise.

@Test
public void testResetActivePushPromise() throws Exception {
    server.requestHandler(req -> {
        req.response().push(HttpMethod.GET, "/wibble", ar -> {
            assertTrue(ar.succeeded());
            HttpServerResponse response = ar.result();
            response.exceptionHandler(err -> {
                if (err instanceof StreamResetException) {
                    assertEquals(Http2Error.CANCEL.code(), ((StreamResetException) err).getCode());
                    testComplete();
                }
            });
            response.setChunked(true).write("some_content");
        });
    });
    startServer();
    HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        fail();
    });
    req.pushHandler(pushedReq -> {
        pushedReq.handler(pushedResp -> {
            pushedResp.handler(buff -> {
                pushedReq.reset(Http2Error.CANCEL.code());
            });
        });
    });
    req.end();
    await();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) StreamResetException(io.vertx.core.http.StreamResetException) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Test(org.junit.Test)

Aggregations

HttpClientRequest (io.vertx.core.http.HttpClientRequest)73 Test (org.junit.Test)59 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)42 Buffer (io.vertx.core.buffer.Buffer)39 HttpServerResponse (io.vertx.core.http.HttpServerResponse)36 HttpClientOptions (io.vertx.core.http.HttpClientOptions)34 HttpMethod (io.vertx.core.http.HttpMethod)32 CountDownLatch (java.util.concurrent.CountDownLatch)32 HttpServerOptions (io.vertx.core.http.HttpServerOptions)31 CompletableFuture (java.util.concurrent.CompletableFuture)31 AtomicReference (java.util.concurrent.atomic.AtomicReference)31 HttpVersion (io.vertx.core.http.HttpVersion)29 HttpConnection (io.vertx.core.http.HttpConnection)28 TestUtils.assertIllegalStateException (io.vertx.test.core.TestUtils.assertIllegalStateException)28 TimeUnit (java.util.concurrent.TimeUnit)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)28 HttpClient (io.vertx.core.http.HttpClient)27 NetSocket (io.vertx.core.net.NetSocket)27 ArrayList (java.util.ArrayList)26 Collections (java.util.Collections)26