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());
}
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();
}
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();
}
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();
}
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();
}
Aggregations