Search in sources :

Example 31 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger 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 32 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class HttpRequestStreamTest method testCloseServerAsynchronously.

@Test
public void testCloseServerAsynchronously() {
    this.server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT));
    AtomicInteger done = new AtomicInteger();
    ReadStream<HttpServerRequest> stream = server.requestStream();
    stream.handler(req -> {
    });
    ThreadLocal<Object> stack = new ThreadLocal<>();
    stack.set(true);
    stream.endHandler(v -> {
        assertTrue(Vertx.currentContext().isEventLoopContext());
        assertNull(stack.get());
        if (done.incrementAndGet() == 2) {
            testComplete();
        }
    });
    server.listen(ar -> {
        assertTrue(Vertx.currentContext().isEventLoopContext());
        assertNull(stack.get());
        ThreadLocal<Object> stack2 = new ThreadLocal<>();
        stack2.set(true);
        server.close(v -> {
            assertTrue(Vertx.currentContext().isEventLoopContext());
            assertNull(stack2.get());
            if (done.incrementAndGet() == 2) {
                testComplete();
            }
        });
        stack2.set(null);
    });
    await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Test(org.junit.Test)

Example 33 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class HttpTest method testResponseDataTimeout.

@Test
public void testResponseDataTimeout() {
    Buffer expected = TestUtils.randomBuffer(1000);
    server.requestHandler(req -> {
        req.response().setChunked(true).write(expected);
    });
    server.listen(onSuccess(s -> {
        HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI);
        Buffer received = Buffer.buffer();
        req.handler(resp -> {
            req.setTimeout(500);
            resp.handler(received::appendBuffer);
        });
        AtomicInteger count = new AtomicInteger();
        req.exceptionHandler(t -> {
            if (count.getAndIncrement() == 0) {
                assertTrue(t instanceof TimeoutException);
                assertEquals(expected, received);
                testComplete();
            }
        });
        req.sendHead();
    }));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 34 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class HttpTest method testFollowRedirectLimit.

@Test
public void testFollowRedirectLimit() throws Exception {
    AtomicInteger redirects = new AtomicInteger();
    server.requestHandler(req -> {
        int val = redirects.incrementAndGet();
        if (val > 16) {
            fail();
        } else {
            req.response().setStatusCode(301).putHeader(HttpHeaders.LOCATION, "http://localhost:8080/otherpath").end();
        }
    });
    startServer();
    client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
        assertEquals(16, redirects.get());
        assertEquals(301, resp.statusCode());
        assertEquals("/otherpath", resp.request().path());
        testComplete();
    }).setFollowRedirects(true).end();
    await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 35 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class HttpTest method testCloseHandlerNotCalledWhenConnectionClosedAfterEnd.

protected void testCloseHandlerNotCalledWhenConnectionClosedAfterEnd(int expected) throws Exception {
    AtomicInteger closeCount = new AtomicInteger();
    AtomicInteger endCount = new AtomicInteger();
    server.requestHandler(req -> {
        req.response().closeHandler(v -> {
            closeCount.incrementAndGet();
        });
        req.response().endHandler(v -> {
            endCount.incrementAndGet();
        });
        req.connection().closeHandler(v -> {
            assertEquals(expected, closeCount.get());
            assertEquals(1, endCount.get());
            testComplete();
        });
        req.response().end("some-data");
    });
    startServer();
    client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
        resp.endHandler(v -> {
            resp.request().connection().close();
        });
    });
    await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8428 Test (org.junit.Test)3991 CountDownLatch (java.util.concurrent.CountDownLatch)1141 ArrayList (java.util.ArrayList)1107 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)906 List (java.util.List)808 IOException (java.io.IOException)751 AtomicReference (java.util.concurrent.atomic.AtomicReference)636 HashMap (java.util.HashMap)570 Map (java.util.Map)519 Test (org.testng.annotations.Test)428 TimeUnit (java.util.concurrent.TimeUnit)377 AtomicLong (java.util.concurrent.atomic.AtomicLong)376 Test (org.junit.jupiter.api.Test)376 HashSet (java.util.HashSet)353 File (java.io.File)350 ExecutorService (java.util.concurrent.ExecutorService)344 Arrays (java.util.Arrays)336 Collections (java.util.Collections)312 Set (java.util.Set)312