use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class HttpTest method testResponseEndHandlersSendFile.
@Test
public void testResponseEndHandlersSendFile() throws Exception {
waitFor(2);
AtomicInteger cnt = new AtomicInteger();
String content = "iqdioqwdqwiojqwijdwqd";
File toSend = setupFile("somefile.txt", content);
server.requestHandler(req -> {
req.response().headersEndHandler(v -> {
req.response().putHeader("extraheader", "wibble");
assertEquals(0, cnt.getAndIncrement());
});
req.response().bodyEndHandler(v -> {
assertEquals(content.length(), req.response().bytesWritten());
assertEquals(1, cnt.getAndIncrement());
complete();
});
req.response().sendFile(toSend.getAbsolutePath());
}).listen(onSuccess(server -> {
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> {
assertEquals(200, res.statusCode());
assertEquals("wibble", res.headers().get("extraheader"));
res.bodyHandler(buff -> {
assertEquals(Buffer.buffer(content), buff);
complete();
});
}).end();
}));
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ServerTest method testServerSendGoAwayInteralError.
@Test
public void testServerSendGoAwayInteralError() throws Exception {
waitFor(3);
AtomicReference<HttpServerRequest> first = new AtomicReference<>();
AtomicInteger status = new AtomicInteger();
AtomicInteger closed = new AtomicInteger();
Handler<HttpServerRequest> requestHandler = req -> {
if (first.compareAndSet(null, req)) {
req.exceptionHandler(err -> {
fail();
});
req.response().closeHandler(err -> {
closed.incrementAndGet();
});
req.response().endHandler(err -> {
closed.incrementAndGet();
});
} else {
assertEquals(0, status.getAndIncrement());
req.exceptionHandler(err -> {
closed.incrementAndGet();
});
req.response().closeHandler(err -> {
closed.incrementAndGet();
});
req.response().endHandler(err -> {
closed.incrementAndGet();
});
HttpConnection conn = req.connection();
conn.closeHandler(v -> {
assertEquals(5, closed.get());
assertEquals(1, status.get());
complete();
});
conn.shutdownHandler(v -> {
assertEquals(1, status.get());
complete();
});
conn.goAway(2, first.get().response().streamId());
}
};
testServerSendGoAway(requestHandler, 2);
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http1xTest method testCloseTheConnectionAfterResetPersistentClientResponse.
private void testCloseTheConnectionAfterResetPersistentClientResponse(boolean pipelined) throws Exception {
waitFor(2);
server.close();
NetServer server = vertx.createNetServer();
try {
AtomicInteger count = new AtomicInteger();
AtomicBoolean closed = new AtomicBoolean();
server.connectHandler(so -> {
Buffer total = Buffer.buffer();
switch(count.getAndIncrement()) {
case 0:
so.handler(buff -> {
total.appendBuffer(buff);
if (total.toString().equals("GET /somepath HTTP/1.1\r\n" + "Host: localhost:8080\r\n" + "\r\n")) {
so.write(Buffer.buffer("HTTP/1.1 200 OK\r\n" + "Content-Length: 200\r\n" + "\r\n" + "Some-Buffer"));
so.closeHandler(v -> {
closed.set(true);
});
}
});
break;
case 1:
so.handler(buff -> {
total.appendBuffer(buff);
if (total.toString().equals("GET /somepath HTTP/1.1\r\n" + "Host: localhost:8080\r\n" + "\r\n")) {
so.write("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 11\r\n" + "\r\n" + "Hello world");
complete();
}
});
break;
default:
fail("Invalid count");
break;
}
});
CountDownLatch listenLatch = new CountDownLatch(1);
server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v -> {
listenLatch.countDown();
}));
awaitLatch(listenLatch);
client.close();
client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setPipelining(pipelined).setKeepAlive(true));
HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
if (pipelined) {
req1.handler(resp1 -> {
resp1.handler(buff -> {
req1.reset();
client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
resp.bodyHandler(body -> {
assertEquals("Hello world", body.toString());
complete();
});
});
});
});
req1.end();
} else {
req1.handler(resp -> {
resp.handler(buff -> {
req1.reset();
});
});
req1.end();
client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
resp.bodyHandler(body -> {
assertEquals("Hello world", body.toString());
complete();
});
});
}
await();
} finally {
server.close();
}
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http1xTest method testResetClientRequestNotYetSent.
private void testResetClientRequestNotYetSent(boolean keepAlive, boolean pipelined) throws Exception {
waitFor(2);
server.close();
NetServer server = vertx.createNetServer();
try {
AtomicInteger numReq = new AtomicInteger();
server.connectHandler(conn -> {
assertEquals(0, numReq.getAndIncrement());
StringBuilder sb = new StringBuilder();
conn.handler(buff -> {
sb.append(buff);
String content = sb.toString();
if (content.startsWith("GET some-uri HTTP/1.1\r\n") && content.endsWith("\r\n\r\n")) {
conn.write("HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n");
complete();
}
});
});
CountDownLatch latch = new CountDownLatch(1);
server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v -> {
latch.countDown();
}));
awaitLatch(latch);
client.close();
client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(keepAlive).setPipelining(pipelined));
HttpClientRequest post = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
fail();
});
post.setChunked(true).write(TestUtils.randomBuffer(1024));
assertTrue(post.reset());
client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
assertEquals(1, numReq.get());
complete();
});
await();
} finally {
server.close();
}
}
use of java.util.concurrent.atomic.AtomicInteger in project elasticsearch by elastic.
the class SearchPhaseControllerTests method testConsumerConcurrently.
public void testConsumerConcurrently() throws InterruptedException {
int expectedNumResults = randomIntBetween(1, 100);
int bufferSize = randomIntBetween(2, 200);
SearchRequest request = new SearchRequest();
request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("foo")));
request.setBatchedReduceSize(bufferSize);
InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> consumer = searchPhaseController.newSearchPhaseResults(request, expectedNumResults);
AtomicInteger max = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(expectedNumResults);
for (int i = 0; i < expectedNumResults; i++) {
int id = i;
Thread t = new Thread(() -> {
int number = randomIntBetween(1, 1000);
max.updateAndGet(prev -> Math.max(prev, number));
QuerySearchResult result = new QuerySearchResult(id, new SearchShardTarget("node", new Index("a", "b"), id));
result.topDocs(new TopDocs(id, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
InternalAggregations aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", (double) number, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
result.aggregations(aggs);
consumer.consumeResult(id, result);
latch.countDown();
});
t.start();
}
latch.await();
SearchPhaseController.ReducedQueryPhase reduce = consumer.reduce();
InternalMax internalMax = (InternalMax) reduce.aggregations.asList().get(0);
assertEquals(max.get(), internalMax.getValue(), 0.0D);
}
Aggregations