Search in sources :

Example 1 with HttpServerMetric

use of io.vertx.test.fakemetrics.HttpServerMetric in project vert.x by eclipse.

the class HttpMetricsTest method testPushPromise.

@Test
public void testPushPromise() throws Exception {
    waitFor(2);
    int numBuffers = 10;
    int contentLength = numBuffers * 1000;
    server.requestHandler(req -> {
        req.response().push(HttpMethod.GET, "/wibble", ar -> {
            HttpServerResponse pushedResp = ar.result();
            FakeHttpServerMetrics serverMetrics = FakeMetricsBase.getMetrics(server);
            HttpServerMetric serverMetric = serverMetrics.getMetric(pushedResp);
            assertNotNull(serverMetric);
            pushedResp.putHeader("content-length", "" + contentLength);
            AtomicInteger numBuffer = new AtomicInteger(numBuffers);
            vertx.setPeriodic(1, timerID -> {
                if (numBuffer.getAndDecrement() == 0) {
                    pushedResp.end();
                    assertNull(serverMetrics.getMetric(pushedResp));
                    vertx.cancelTimer(timerID);
                    complete();
                } else {
                    pushedResp.write(TestUtils.randomBuffer(1000));
                }
            });
        });
    });
    startServer();
    client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2));
    FakeHttpClientMetrics metrics = FakeMetricsBase.getMetrics(client);
    HttpClientRequest req = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
    });
    req.pushHandler(pushedReq -> {
        HttpClientMetric metric = metrics.getMetric(pushedReq);
        assertNotNull(metric);
        assertSame(pushedReq, metric.request);
        pushedReq.handler(resp -> {
            resp.endHandler(v -> {
                assertNull(metrics.getMetric(pushedReq));
                complete();
            });
        });
    });
    req.end();
    await();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) FakeHttpServerMetrics(io.vertx.test.fakemetrics.FakeHttpServerMetrics) HttpServerMetric(io.vertx.test.fakemetrics.HttpServerMetric) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerResponse(io.vertx.core.http.HttpServerResponse) FakeHttpClientMetrics(io.vertx.test.fakemetrics.FakeHttpClientMetrics) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClientMetric(io.vertx.test.fakemetrics.HttpClientMetric) Test(org.junit.Test)

Example 2 with HttpServerMetric

use of io.vertx.test.fakemetrics.HttpServerMetric 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());
}
Also used : Context(io.vertx.core.Context) HttpServerMetric(io.vertx.test.fakemetrics.HttpServerMetric) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) FakeHttpServerMetrics(io.vertx.test.fakemetrics.FakeHttpServerMetrics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerResponse(io.vertx.core.http.HttpServerResponse) FakeHttpClientMetrics(io.vertx.test.fakemetrics.FakeHttpClientMetrics) HttpClientMetric(io.vertx.test.fakemetrics.HttpClientMetric)

Example 3 with HttpServerMetric

use of io.vertx.test.fakemetrics.HttpServerMetric in project vert.x by eclipse.

the class HttpMetricsTest method testServerConnectionClosed.

private void testServerConnectionClosed(HttpVersion protocol) throws Exception {
    server.close();
    server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setIdleTimeout(2));
    server.requestHandler(req -> {
        FakeHttpServerMetrics metrics = FakeMetricsBase.getMetrics(server);
        HttpServerMetric metric = metrics.getMetric(req);
        assertNotNull(metric);
        assertFalse(metric.failed.get());
        req.response().closeHandler(v -> {
            assertNull(metrics.getMetric(req));
            assertTrue(metric.failed.get());
            testComplete();
        });
    });
    startServer();
    client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(protocol));
    HttpClientRequest req = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
    req.handler(resp -> {
    }).end();
    await();
}
Also used : FakeMetricsBase(io.vertx.test.fakemetrics.FakeMetricsBase) FakeMetricsFactory(io.vertx.test.fakemetrics.FakeMetricsFactory) HttpServerMetric(io.vertx.test.fakemetrics.HttpServerMetric) HttpServer(io.vertx.core.http.HttpServer) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Context(io.vertx.core.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientRequest(io.vertx.core.http.HttpClientRequest) FakeHttpServerMetrics(io.vertx.test.fakemetrics.FakeHttpServerMetrics) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClientMetric(io.vertx.test.fakemetrics.HttpClientMetric) HttpClient(io.vertx.core.http.HttpClient) FakeHttpClientMetrics(io.vertx.test.fakemetrics.FakeHttpClientMetrics) HttpClientRequest(io.vertx.core.http.HttpClientRequest) FakeHttpServerMetrics(io.vertx.test.fakemetrics.FakeHttpServerMetrics) HttpServerMetric(io.vertx.test.fakemetrics.HttpServerMetric) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Aggregations

HttpClientOptions (io.vertx.core.http.HttpClientOptions)3 HttpClientRequest (io.vertx.core.http.HttpClientRequest)3 HttpServerResponse (io.vertx.core.http.HttpServerResponse)3 FakeHttpClientMetrics (io.vertx.test.fakemetrics.FakeHttpClientMetrics)3 FakeHttpServerMetrics (io.vertx.test.fakemetrics.FakeHttpServerMetrics)3 HttpClientMetric (io.vertx.test.fakemetrics.HttpClientMetric)3 HttpServerMetric (io.vertx.test.fakemetrics.HttpServerMetric)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Context (io.vertx.core.Context)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2 VertxOptions (io.vertx.core.VertxOptions)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpClient (io.vertx.core.http.HttpClient)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServer (io.vertx.core.http.HttpServer)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 HttpVersion (io.vertx.core.http.HttpVersion)1 MetricsOptions (io.vertx.core.metrics.MetricsOptions)1