Search in sources :

Example 6 with HttpRequest

use of io.vertx.core.spi.observability.HttpRequest in project vert.x by eclipse.

the class HttpTracerTestBase method testHttpClientRequest.

private void testHttpClientRequest(RequestOptions request, String expectedOperation) throws Exception {
    String key = TestUtils.randomAlphaString(10);
    Object val = new Object();
    AtomicInteger seq = new AtomicInteger();
    tracer = new VertxTracer() {

        @Override
        public Object sendRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, BiConsumer headers, TagExtractor tagExtractor) {
            assertSame(val, context.getLocal(key));
            assertTrue(seq.compareAndSet(0, 1));
            headers.accept("header-key", "header-value");
            assertNotNull(request);
            assertTrue(request instanceof HttpRequest);
            assertEquals(expectedOperation, operation);
            return request;
        }

        @Override
        public void receiveResponse(Context context, Object response, Object payload, Throwable failure, TagExtractor tagExtractor) {
            assertSame(val, context.getLocal(key));
            assertTrue(context.removeLocal(key));
            assertNotNull(response);
            assertTrue(response instanceof HttpResponse);
            assertNull(failure);
            assertTrue(seq.compareAndSet(1, 2));
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    server.requestHandler(req -> {
        assertEquals("header-value", req.getHeader("header-key"));
        req.response().end();
    }).listen(8080, "localhost", onSuccess(v -> {
        latch.countDown();
    }));
    awaitLatch(latch);
    Context ctx = vertx.getOrCreateContext();
    ctx.runOnContext(v1 -> {
        ConcurrentMap<Object, Object> tracerMap = ((ContextInternal) ctx).localContextData();
        tracerMap.put(key, val);
        client.request(request, onSuccess(req -> {
            req.send(onSuccess(resp -> {
                resp.endHandler(v2 -> {
                    // Updates are done on the HTTP client context, so we need to run task on this context
                    // to avoid data race
                    ctx.runOnContext(v -> {
                        assertNull(tracerMap.get(key));
                        testComplete();
                    });
                });
            }));
        }));
    });
    await();
}
Also used : Context(io.vertx.core.Context) HttpRequest(io.vertx.core.spi.observability.HttpRequest) HttpResponse(io.vertx.core.spi.observability.HttpResponse) Vertx(io.vertx.core.Vertx) ContextInternal(io.vertx.core.impl.ContextInternal) HttpRequest(io.vertx.core.spi.observability.HttpRequest) Test(org.junit.Test) Context(io.vertx.core.Context) io.vertx.core.http(io.vertx.core.http) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) TracingPolicy(io.vertx.core.tracing.TracingPolicy) HttpResponse(io.vertx.core.spi.observability.HttpResponse) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingPolicy(io.vertx.core.tracing.TracingPolicy) BiConsumer(java.util.function.BiConsumer)

Aggregations

io.vertx.core.http (io.vertx.core.http)6 HttpRequest (io.vertx.core.spi.observability.HttpRequest)6 HttpResponse (io.vertx.core.spi.observability.HttpResponse)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 BiConsumer (java.util.function.BiConsumer)6 Test (org.junit.Test)6 io.vertx.core (io.vertx.core)5 Buffer (io.vertx.core.buffer.Buffer)5 DatagramSocket (io.vertx.core.datagram.DatagramSocket)5 DatagramSocketOptions (io.vertx.core.datagram.DatagramSocketOptions)5 EventBus (io.vertx.core.eventbus.EventBus)5 EventBusOptions (io.vertx.core.eventbus.EventBusOptions)5 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)5 MetricsOptions (io.vertx.core.metrics.MetricsOptions)5 DummyVertxMetrics (io.vertx.core.metrics.impl.DummyVertxMetrics)5 io.vertx.core.net (io.vertx.core.net)5 VertxMetricsFactory (io.vertx.core.spi.VertxMetricsFactory)5 VertxTestBase (io.vertx.test.core.VertxTestBase)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5