use of io.vertx.test.fakemetrics.FakeTCPMetrics in project vert.x by eclipse.
the class HttpMetricsTestBase method testHttpMetricsLifecycle.
@Test
public void testHttpMetricsLifecycle() throws Exception {
int numBuffers = 10;
int chunkSize = 1000;
int contentLength = numBuffers * chunkSize;
AtomicReference<HttpServerMetric> serverMetric = new AtomicReference<>();
server.requestHandler(req -> {
assertEquals(protocol, req.version());
FakeHttpServerMetrics serverMetrics = FakeMetricsBase.getMetrics(server);
assertNotNull(serverMetrics);
HttpServerMetric metric = serverMetrics.getRequestMetric(req);
serverMetric.set(metric);
assertSame(((HttpServerRequestInternal) req).metric(), metric);
assertNotNull(serverMetric.get());
assertNotNull(serverMetric.get().socket);
assertNull(serverMetric.get().response.get());
assertTrue(serverMetric.get().socket.connected.get());
assertNull(serverMetric.get().route.get());
req.routed("/route/:param");
assertEquals("/route/:param", serverMetric.get().route.get());
req.bodyHandler(buff -> {
assertEquals(contentLength, buff.length());
assertTrue(serverMetric.get().requestEnded.get());
assertEquals(contentLength, serverMetric.get().bytesRead.get());
HttpServerResponse resp = req.response().setChunked(true);
AtomicInteger numBuffer = new AtomicInteger(numBuffers);
vertx.setPeriodic(1, timerID -> {
Buffer chunk = TestUtils.randomBuffer(chunkSize);
if (numBuffer.decrementAndGet() == 0) {
resp.end(chunk);
assertTrue(serverMetric.get().responseEnded.get());
assertEquals(contentLength, serverMetric.get().bytesWritten.get());
assertNull(serverMetrics.getRequestMetric(req));
vertx.cancelTimer(timerID);
} else {
resp.write(chunk);
assertSame(serverMetric.get().response.get(), resp);
}
});
});
});
startServer();
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<HttpClientMetric> clientMetric = new AtomicReference<>();
AtomicReference<SocketMetric> clientSocketMetric = new AtomicReference<>();
FakeHttpClientMetrics metrics = FakeMetricsBase.getMetrics(client);
NetClient netClient = ((HttpClientImpl) client).netClient();
FakeTCPMetrics tcpMetrics = FakeMetricsBase.getMetrics(netClient);
assertSame(metrics, tcpMetrics);
Context ctx = vertx.getOrCreateContext();
ctx.runOnContext(v -> {
assertEquals(Collections.emptySet(), metrics.endpoints());
client.request(new RequestOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(TestUtils.randomAlphaString(16))).onComplete(onSuccess(req -> {
req.response(onSuccess(resp -> {
clientSocketMetric.set(metrics.firstMetric(SocketAddress.inetSocketAddress(8080, "localhost")));
assertNotNull(clientSocketMetric.get());
assertEquals(Collections.singleton("localhost:8080"), metrics.endpoints());
clientMetric.set(metrics.getMetric(resp.request()));
assertNotNull(clientMetric.get());
assertEquals(contentLength, clientMetric.get().bytesWritten.get());
// assertNotNull(clientMetric.get().socket);
// assertTrue(clientMetric.get().socket.connected.get());
assertEquals((Integer) 1, metrics.connectionCount("localhost:8080"));
assertEquals((Integer) 1, metrics.connectionCount(SocketAddress.inetSocketAddress(8080, "localhost")));
resp.bodyHandler(buff -> {
assertEquals(contentLength, clientMetric.get().bytesRead.get());
assertNull(metrics.getMetric(resp.request()));
assertEquals(contentLength, buff.length());
latch.countDown();
});
})).exceptionHandler(this::fail).setChunked(true);
assertNull(metrics.getMetric(req));
for (int i = 0; i < numBuffers; i++) {
req.write(TestUtils.randomBuffer(chunkSize));
}
req.end();
}));
});
awaitLatch(latch);
client.close();
AsyncTestBase.assertWaitUntil(() -> metrics.endpoints().isEmpty());
assertEquals(null, metrics.connectionCount("localhost:8080"));
AsyncTestBase.assertWaitUntil(() -> !serverMetric.get().socket.connected.get());
AsyncTestBase.assertWaitUntil(() -> contentLength == serverMetric.get().socket.bytesRead.get());
AsyncTestBase.assertWaitUntil(() -> contentLength == serverMetric.get().socket.bytesWritten.get());
AsyncTestBase.assertWaitUntil(() -> !clientSocketMetric.get().connected.get());
assertEquals(contentLength, clientSocketMetric.get().bytesRead.get());
assertEquals(contentLength, clientSocketMetric.get().bytesWritten.get());
for (Iterator<Long> it : Arrays.asList(clientSocketMetric.get().bytesReadEvents.iterator(), serverMetric.get().socket.bytesWrittenEvents.iterator())) {
while (it.hasNext()) {
long val = it.next();
if (it.hasNext()) {
assertEquals(4096, val);
} else {
assertTrue(val < 4096);
}
}
}
}
Aggregations