use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.
the class HttpMetricsTest method testHttpMetricsLifecycle.
private void testHttpMetricsLifecycle(HttpVersion protocol) throws Exception {
waitFor(2);
int numBuffers = 10;
int contentLength = numBuffers * 1000;
AtomicReference<HttpServerMetric> serverMetric = new AtomicReference<>();
server.requestHandler(req -> {
assertEquals(protocol, req.version());
FakeHttpServerMetrics serverMetrics = FakeMetricsBase.getMetrics(server);
assertNotNull(serverMetrics);
serverMetric.set(serverMetrics.getMetric(req));
assertNotNull(serverMetric.get());
assertNotNull(serverMetric.get().socket);
assertTrue(serverMetric.get().socket.connected.get());
req.bodyHandler(buff -> {
assertEquals(contentLength, buff.length());
HttpServerResponse resp = req.response().putHeader("Content-Length", "" + contentLength);
AtomicInteger numBuffer = new AtomicInteger(numBuffers);
vertx.setPeriodic(1, timerID -> {
if (numBuffer.getAndDecrement() == 0) {
resp.end();
assertNull(serverMetrics.getMetric(req));
vertx.cancelTimer(timerID);
} else {
resp.write(TestUtils.randomBuffer(1000));
}
});
});
});
startServer();
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<HttpClientMetric> clientMetric = new AtomicReference<>();
client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(protocol));
FakeHttpClientMetrics metrics = FakeMetricsBase.getMetrics(client);
Context ctx = vertx.getOrCreateContext();
ctx.runOnContext(v -> {
HttpClientRequest req = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath").exceptionHandler(this::fail);
assertNull(metrics.getMetric(req));
req.setChunked(true).handler(resp -> {
clientMetric.set(metrics.getMetric(req));
assertNotNull(clientMetric.get());
assertNotNull(clientMetric.get().socket);
assertTrue(clientMetric.get().socket.connected.get());
resp.bodyHandler(buff -> {
assertNull(metrics.getMetric(req));
assertEquals(contentLength, buff.length());
latch.countDown();
});
});
for (int i = 0; i < numBuffers; i++) {
req.write(TestUtils.randomBuffer(1000));
}
req.end();
});
awaitLatch(latch);
client.close();
waitUntil(() -> !serverMetric.get().socket.connected.get());
assertEquals(contentLength, serverMetric.get().socket.bytesRead.get());
assertEquals(contentLength, serverMetric.get().socket.bytesWritten.get());
waitUntil(() -> !clientMetric.get().socket.connected.get());
assertEquals(contentLength, clientMetric.get().socket.bytesRead.get());
assertEquals(contentLength, clientMetric.get().socket.bytesWritten.get());
}
use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.
the class HttpMetricsTest method testHttpClientLifecycle.
private void testHttpClientLifecycle(HttpVersion protocol) throws Exception {
HttpServer server = vertx.createHttpServer();
CountDownLatch requestBeginLatch = new CountDownLatch(1);
CountDownLatch requestBodyLatch = new CountDownLatch(1);
CountDownLatch requestEndLatch = new CountDownLatch(1);
CompletableFuture<Void> beginResponse = new CompletableFuture<>();
CompletableFuture<Void> endResponse = new CompletableFuture<>();
server.requestHandler(req -> {
assertEquals(protocol, req.version());
requestBeginLatch.countDown();
req.handler(buff -> {
requestBodyLatch.countDown();
});
req.endHandler(v -> {
requestEndLatch.countDown();
});
Context ctx = vertx.getOrCreateContext();
beginResponse.thenAccept(v1 -> {
ctx.runOnContext(v2 -> {
req.response().setChunked(true).write(TestUtils.randomAlphaString(1024));
});
});
endResponse.thenAccept(v1 -> {
ctx.runOnContext(v2 -> {
req.response().end();
});
});
});
CountDownLatch listenLatch = new CountDownLatch(1);
server.listen(8080, "localhost", onSuccess(s -> {
listenLatch.countDown();
}));
awaitLatch(listenLatch);
HttpClient client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(protocol));
FakeHttpClientMetrics clientMetrics = FakeMetricsBase.getMetrics(client);
CountDownLatch responseBeginLatch = new CountDownLatch(1);
CountDownLatch responseEndLatch = new CountDownLatch(1);
HttpClientRequest req = client.post(8080, "localhost", "/somepath", resp -> {
responseBeginLatch.countDown();
resp.endHandler(v -> {
responseEndLatch.countDown();
});
}).setChunked(true);
req.sendHead();
awaitLatch(requestBeginLatch);
HttpClientMetric reqMetric = clientMetrics.getMetric(req);
assertEquals(0, reqMetric.requestEnded.get());
assertEquals(0, reqMetric.responseBegin.get());
req.write(TestUtils.randomAlphaString(1024));
awaitLatch(requestBodyLatch);
assertEquals(0, reqMetric.requestEnded.get());
assertEquals(0, reqMetric.responseBegin.get());
req.end();
awaitLatch(requestEndLatch);
assertEquals(1, reqMetric.requestEnded.get());
assertEquals(0, reqMetric.responseBegin.get());
beginResponse.complete(null);
awaitLatch(responseBeginLatch);
assertEquals(1, reqMetric.requestEnded.get());
assertEquals(1, reqMetric.responseBegin.get());
endResponse.complete(null);
awaitLatch(responseEndLatch);
assertNull(clientMetrics.getMetric(req));
assertEquals(1, reqMetric.requestEnded.get());
assertEquals(1, reqMetric.responseBegin.get());
}
use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.
the class HttpMetricsTest method testClientConnectionClosed.
private void testClientConnectionClosed(HttpVersion protocol) throws Exception {
server.requestHandler(req -> {
req.response().setChunked(true).write(Buffer.buffer("some-data"));
});
startServer();
client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(protocol).setIdleTimeout(2));
FakeHttpClientMetrics metrics = FakeMetricsBase.getMetrics(client);
HttpClientRequest req = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
req.handler(resp -> {
HttpClientMetric metric = metrics.getMetric(req);
assertNotNull(metric);
assertFalse(metric.failed.get());
resp.exceptionHandler(err -> {
assertNull(metrics.getMetric(req));
assertTrue(metric.failed.get());
testComplete();
});
});
req.end();
await();
}
use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.
the class HttpRequestStreamTest method testReadStreamPauseResume.
@Test
public void testReadStreamPauseResume() {
String path = "/some/path";
this.server = vertx.createHttpServer(new HttpServerOptions().setAcceptBacklog(10).setPort(HttpTestBase.DEFAULT_HTTP_PORT));
ReadStream<HttpServerRequest> httpStream = server.requestStream();
AtomicBoolean paused = new AtomicBoolean();
httpStream.handler(req -> {
assertFalse(paused.get());
HttpServerResponse response = req.response();
response.setStatusCode(200).end();
response.close();
});
server.listen(listenAR -> {
assertTrue(listenAR.succeeded());
paused.set(true);
httpStream.pause();
netClient = vertx.createNetClient(new NetClientOptions().setConnectTimeout(1000));
netClient.connect(HttpTestBase.DEFAULT_HTTP_PORT, "localhost", socketAR -> {
assertTrue(socketAR.succeeded());
NetSocket socket = socketAR.result();
Buffer buffer = Buffer.buffer();
socket.handler(buffer::appendBuffer);
socket.closeHandler(v -> {
assertEquals(0, buffer.length());
paused.set(false);
httpStream.resume();
client = vertx.createHttpClient(new HttpClientOptions());
client.request(HttpMethod.GET, HttpTestBase.DEFAULT_HTTP_PORT, "localhost", path, resp -> {
assertEquals(200, resp.statusCode());
testComplete();
}).end();
});
});
});
await();
}
use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.
the class HttpTest method testInVerticle.
private void testInVerticle(boolean worker) throws Exception {
client.close();
server.close();
class MyVerticle extends AbstractVerticle {
Context ctx;
@Override
public void start() {
ctx = Vertx.currentContext();
if (worker) {
assertTrue(ctx instanceof WorkerContext);
} else {
assertTrue(ctx instanceof EventLoopContext);
}
Thread thr = Thread.currentThread();
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
server.requestHandler(req -> {
req.response().end();
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
});
server.listen(ar -> {
assertTrue(ar.succeeded());
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
client = vertx.createHttpClient(new HttpClientOptions());
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> {
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
assertEquals(200, res.statusCode());
testComplete();
}).end();
});
}
}
MyVerticle verticle = new MyVerticle();
vertx.deployVerticle(verticle, new DeploymentOptions().setWorker(worker));
await();
}
Aggregations