use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse-vertx.
the class MetricsContextTest method testFactoryInCluster.
@Test
public void testFactoryInCluster() throws Exception {
AtomicReference<Thread> metricsThread = new AtomicReference<>();
AtomicReference<Context> metricsContext = new AtomicReference<>();
Thread testThread = Thread.currentThread();
VertxMetricsFactory factory = (options) -> {
metricsThread.set(Thread.currentThread());
metricsContext.set(Vertx.currentContext());
return DummyVertxMetrics.INSTANCE;
};
VertxOptions options = new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)).setEventBusOptions(new EventBusOptions());
clusteredVertx(options, onSuccess(vertx -> {
assertSame(testThread, metricsThread.get());
assertNull(metricsContext.get());
testComplete();
}));
await();
}
use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse-vertx.
the class MetricsContextTest method testHttpClientRequest.
private void testHttpClientRequest(Function<Vertx, Context> contextFactory) throws Exception {
AtomicReference<Thread> expectedThread = new AtomicReference<>();
AtomicReference<Context> expectedContext = new AtomicReference<>();
AtomicReference<String> requestBeginCalled = new AtomicReference();
AtomicBoolean responseEndCalled = new AtomicBoolean();
AtomicBoolean socketConnectedCalled = new AtomicBoolean();
AtomicBoolean socketDisconnectedCalled = new AtomicBoolean();
AtomicBoolean bytesReadCalled = new AtomicBoolean();
AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
AtomicBoolean closeCalled = new AtomicBoolean();
VertxMetricsFactory factory = (options) -> new DummyVertxMetrics() {
@Override
public HttpClientMetrics createHttpClientMetrics(HttpClientOptions options) {
return new DummyHttpClientMetrics() {
@Override
public ClientMetrics<Void, Void, HttpRequest, HttpResponse> createEndpointMetrics(SocketAddress remoteAddress, int maxPoolSize) {
return new ClientMetrics<Void, Void, HttpRequest, HttpResponse>() {
@Override
public Void requestBegin(String uri, HttpRequest request) {
requestBeginCalled.set(uri);
return null;
}
@Override
public void responseEnd(Void requestMetric, long bytesRead) {
responseEndCalled.set(true);
}
};
}
@Override
public Void connected(SocketAddress remoteAddress, String remoteName) {
socketConnectedCalled.set(true);
return null;
}
@Override
public void disconnected(Void socketMetric, SocketAddress remoteAddress) {
socketDisconnectedCalled.set(true);
}
@Override
public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
bytesReadCalled.set(true);
}
@Override
public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
bytesWrittenCalled.set(true);
}
@Override
public void close() {
closeCalled.set(true);
}
};
}
};
Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
HttpServer server = vertx.createHttpServer();
server.requestHandler(req -> {
req.endHandler(buf -> {
HttpServerResponse resp = req.response();
resp.setChunked(true).end(Buffer.buffer("bye"));
resp.close();
});
});
CountDownLatch latch = new CountDownLatch(1);
server.listen(8080, "localhost", onSuccess(s -> {
latch.countDown();
}));
awaitLatch(latch);
Context ctx = contextFactory.apply(vertx);
ctx.runOnContext(v1 -> {
expectedThread.set(Thread.currentThread());
expectedContext.set(Vertx.currentContext());
HttpClient client = vertx.createHttpClient();
assertSame(expectedThread.get(), Thread.currentThread());
client.request(HttpMethod.PUT, 8080, "localhost", "/the-uri").compose(req -> req.send(Buffer.buffer("hello")).onComplete(onSuccess(resp -> {
executeInVanillaThread(() -> {
client.close();
vertx.close(v2 -> {
assertEquals("/the-uri", requestBeginCalled.get());
assertTrue(responseEndCalled.get());
assertTrue(socketConnectedCalled.get());
assertTrue(socketDisconnectedCalled.get());
assertTrue(bytesReadCalled.get());
assertTrue(bytesWrittenCalled.get());
assertTrue(closeCalled.get());
testComplete();
});
});
})));
});
await();
}
use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse-vertx.
the class MetricsContextTest method testHttpServerRequestPipelining.
@Test
public void testHttpServerRequestPipelining() throws Exception {
waitFor(2);
AtomicInteger count = new AtomicInteger();
VertxMetricsFactory factory = (options) -> new DummyVertxMetrics() {
@Override
public HttpServerMetrics createHttpServerMetrics(HttpServerOptions options, SocketAddress localAddress) {
return new DummyHttpServerMetrics() {
@Override
public Void requestBegin(Void socketMetric, HttpRequest request) {
switch(request.uri()) {
case "/1":
assertEquals(0, count.get());
break;
case "/2":
assertEquals(1, count.get());
break;
}
return null;
}
@Override
public void requestEnd(Void requestMetric, HttpRequest request, long bytesRead) {
switch(request.uri()) {
case "/1":
assertEquals(1, count.get());
break;
case "/2":
assertEquals(2, count.get());
break;
}
}
@Override
public void responseEnd(Void requestMetric, HttpResponse response, long bytesWritten) {
}
@Override
public Void connected(SocketAddress remoteAddress, String remoteName) {
return null;
}
@Override
public void disconnected(Void socketMetric, SocketAddress remoteAddress) {
}
@Override
public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
}
@Override
public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
}
@Override
public void close() {
}
};
}
};
CountDownLatch latch = new CountDownLatch(1);
Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
HttpServer server = vertx.createHttpServer().requestHandler(req -> {
count.incrementAndGet();
vertx.setTimer(10, id -> {
HttpServerResponse response = req.response();
response.end();
});
});
server.listen(8080, "localhost", onSuccess(s -> {
latch.countDown();
}));
awaitLatch(latch);
HttpClient client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1));
vertx.runOnContext(v -> {
for (int i = 0; i < 2; i++) {
client.request(HttpMethod.GET, 8080, "localhost", "/" + (i + 1), onSuccess(req -> {
req.send().compose(HttpClientResponse::body).onComplete(onSuccess(body -> {
complete();
}));
}));
}
});
await();
}
use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse-vertx.
the class MetricsOptionsTest method testSetMetricsInstance.
@Test
public void testSetMetricsInstance() {
DummyVertxMetrics metrics = DummyVertxMetrics.INSTANCE;
vertx.close();
vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(new SimpleVertxMetricsFactory<>(metrics))));
assertSame(metrics, ((VertxInternal) vertx).metricsSPI());
}
use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse-vertx.
the class MetricsOptionsTest method testCopyOptions.
@Test
public void testCopyOptions() {
MetricsOptions options = new MetricsOptions();
Random rand = new Random();
boolean metricsEnabled = rand.nextBoolean();
options.setEnabled(metricsEnabled);
options = new MetricsOptions(options);
assertEquals(metricsEnabled, options.isEnabled());
}
Aggregations