Skip to content

Commit

Permalink
Improve Http1xTest#testTimedOutWaiterDoesntConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Sep 2, 2018
1 parent 8d0db4e commit 530ec73
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/test/java/io/vertx/test/core/Http1xTest.java
Expand Up @@ -1110,29 +1110,48 @@ public void testTimedOutWaiterDoesntConnect() throws Exception {
awaitLatch(firstCloseLatch); awaitLatch(firstCloseLatch);


client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false).setMaxPoolSize(1)); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false).setMaxPoolSize(1));
AtomicInteger connectCount = new AtomicInteger(0); AtomicInteger requestCount = new AtomicInteger(0);
// We need a net server because we need to intercept the socket connection, not just full http requests // We need a net server because we need to intercept the socket connection, not just full http requests
NetServer server = vertx.createNetServer(new NetServerOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT)); NetServer server = vertx.createNetServer(new NetServerOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT));
server.connectHandler(socket -> { server.connectHandler(socket -> {
connectCount.incrementAndGet(); Buffer content = Buffer.buffer();
// Delay and write a proper http response AtomicBoolean closed = new AtomicBoolean();
vertx.setTimer(responseDelay, time -> socket.write("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")); socket.closeHandler(v -> closed.set(true));
socket.handler(buff -> {
content.appendBuffer(buff);
if (buff.toString().endsWith("\r\n\r\n")) {
// Delay and write a proper http response
vertx.setTimer(responseDelay, time -> {
if (!closed.get()) {
requestCount.incrementAndGet();
socket.write("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK");
}
});
}
});
}); });


CountDownLatch latch = new CountDownLatch(requests); CountDownLatch latch = new CountDownLatch(requests);


server.listen(onSuccess(s -> { server.listen(onSuccess(s -> {
for(int count = 0; count < requests; count++) { for(int count = 0; count < requests; count++) {
HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
resp.bodyHandler(buff -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI);
assertEquals("OK", buff.toString()); if (count % 2 == 0) {
latch.countDown(); req.handler(resp -> {
resp.bodyHandler(buff -> {
assertEquals("OK", buff.toString());
latch.countDown();
});
});
req.exceptionHandler(this::fail);
} else {
// Odd requests get a timeout less than the responseDelay, since we have a pool size of one and a delay all but
// the first request should end up in the wait queue, the odd numbered requests should time out so we should get
// (requests + 1 / 2) connect attempts
req.handler(resp -> {
fail("Was not expecting a response");
}); });
});
// Odd requests get a timeout less than the responseDelay, since we have a pool size of one and a delay all but
// the first request should end up in the wait queue, the odd numbered requests should time out so we should get
// (requests + 1 / 2) connect attempts
if (count % 2 == 1) {
req.setTimeout(responseDelay / 2); req.setTimeout(responseDelay / 2);
req.exceptionHandler(ex -> { req.exceptionHandler(ex -> {
latch.countDown(); latch.countDown();
Expand All @@ -1144,7 +1163,7 @@ public void testTimedOutWaiterDoesntConnect() throws Exception {


awaitLatch(latch); awaitLatch(latch);


assertEquals("Incorrect number of connect attempts.", (requests + 1) / 2, connectCount.get()); assertEquals("Incorrect number of connect attempts.", (requests + 1) / 2, requestCount.get());
server.close(); server.close();
} }


Expand Down

0 comments on commit 530ec73

Please sign in to comment.