use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ClientTest method testClientRequestWriteability.
@Test
public void testClientRequestWriteability() throws Exception {
Buffer content = Buffer.buffer();
Buffer expected = Buffer.buffer();
String chunk = TestUtils.randomAlphaString(100);
CompletableFuture<Void> done = new CompletableFuture<>();
AtomicBoolean paused = new AtomicBoolean();
AtomicInteger numPause = new AtomicInteger();
server.requestHandler(req -> {
Context ctx = vertx.getOrCreateContext();
done.thenAccept(v1 -> {
paused.set(false);
ctx.runOnContext(v2 -> {
req.resume();
});
});
numPause.incrementAndGet();
req.pause();
paused.set(true);
req.handler(content::appendBuffer);
req.endHandler(v -> {
assertEquals(expected, content);
req.response().end();
});
});
startServer();
HttpClientRequest req = client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
testComplete();
}).setChunked(true).exceptionHandler(err -> {
fail();
});
AtomicInteger sent = new AtomicInteger();
AtomicInteger count = new AtomicInteger();
AtomicInteger drained = new AtomicInteger();
vertx.setPeriodic(1, timerID -> {
Context ctx = vertx.getOrCreateContext();
if (req.writeQueueFull()) {
assertTrue(paused.get());
assertEquals(1, numPause.get());
req.drainHandler(v -> {
assertOnIOContext(ctx);
assertEquals(0, drained.getAndIncrement());
assertEquals(1, numPause.get());
assertFalse(paused.get());
req.end();
});
vertx.cancelTimer(timerID);
done.complete(null);
} else {
count.incrementAndGet();
expected.appendString(chunk);
req.write(chunk);
sent.addAndGet(chunk.length());
}
});
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ClientTest method testServerCloseNetSocket.
@Test
public void testServerCloseNetSocket() throws Exception {
waitFor(2);
AtomicInteger status = new AtomicInteger();
server.requestHandler(req -> {
NetSocket socket = req.netSocket();
socket.handler(buff -> {
switch(status.getAndIncrement()) {
case 0:
assertEquals(Buffer.buffer("some-data"), buff);
socket.end(buff);
break;
case 1:
assertEquals(Buffer.buffer("last-data"), buff);
break;
default:
fail();
break;
}
});
socket.endHandler(v -> {
assertEquals(2, status.getAndIncrement());
});
socket.closeHandler(v -> {
assertEquals(3, status.getAndIncrement());
complete();
});
});
startServer();
client.request(HttpMethod.CONNECT, DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
NetSocket socket = resp.netSocket();
AtomicInteger count = new AtomicInteger();
socket.handler(buff -> {
switch(count.getAndIncrement()) {
case 0:
assertEquals("some-data", buff.toString());
break;
default:
fail();
break;
}
});
socket.endHandler(v -> {
assertEquals(1, count.getAndIncrement());
socket.end(Buffer.buffer("last-data"));
});
socket.closeHandler(v -> {
assertEquals(2, count.getAndIncrement());
complete();
});
socket.write(Buffer.buffer("some-data"));
}).setHost("whatever.com").sendHead();
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ClientTest method testConnectionShutdownInConnectionHandler.
@Test
public void testConnectionShutdownInConnectionHandler() throws Exception {
AtomicInteger serverStatus = new AtomicInteger();
server.connectionHandler(conn -> {
if (serverStatus.getAndIncrement() == 0) {
conn.goAwayHandler(ga -> {
assertEquals(0, ga.getErrorCode());
assertEquals(1, serverStatus.getAndIncrement());
});
conn.shutdownHandler(v -> {
assertEquals(2, serverStatus.getAndIncrement());
});
conn.closeHandler(v -> {
assertEquals(3, serverStatus.getAndIncrement());
});
}
});
server.requestHandler(req -> {
assertEquals(5, serverStatus.getAndIncrement());
req.response().end();
});
startServer();
AtomicInteger clientStatus = new AtomicInteger();
HttpClientRequest req1 = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath");
req1.connectionHandler(conn -> {
Context ctx = Vertx.currentContext();
conn.shutdownHandler(v -> {
assertOnIOContext(ctx);
clientStatus.compareAndSet(1, 2);
});
if (clientStatus.getAndIncrement() == 0) {
conn.shutdown();
}
});
req1.exceptionHandler(err -> {
fail();
});
req1.handler(resp -> {
assertEquals(2, clientStatus.getAndIncrement());
resp.endHandler(v -> {
testComplete();
});
});
req1.end();
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ClientTest method testMaxConcurrency.
private void testMaxConcurrency(int poolSize, int maxConcurrency) throws Exception {
int rounds = 1 + poolSize;
int maxRequests = poolSize * maxConcurrency;
int totalRequests = maxRequests + maxConcurrency;
Set<HttpConnection> serverConns = new HashSet<>();
server.connectionHandler(conn -> {
serverConns.add(conn);
assertTrue(serverConns.size() <= poolSize);
});
ArrayList<HttpServerRequest> requests = new ArrayList<>();
server.requestHandler(req -> {
if (requests.size() < maxRequests) {
requests.add(req);
if (requests.size() == maxRequests) {
vertx.setTimer(300, v -> {
assertEquals(maxRequests, requests.size());
requests.forEach(r -> r.response().end());
});
}
} else {
req.response().end();
}
});
startServer();
client.close();
client = vertx.createHttpClient(new HttpClientOptions(clientOptions).setHttp2MaxPoolSize(poolSize).setHttp2MultiplexingLimit(maxConcurrency));
AtomicInteger respCount = new AtomicInteger();
Set<HttpConnection> clientConnections = Collections.synchronizedSet(new HashSet<>());
for (int j = 0; j < rounds; j++) {
for (int i = 0; i < maxConcurrency; i++) {
client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
resp.endHandler(v -> {
if (respCount.incrementAndGet() == totalRequests) {
testComplete();
}
});
}).connectionHandler(clientConnections::add).end();
}
if (j < poolSize) {
int threshold = j + 1;
waitUntil(() -> clientConnections.size() == threshold);
}
}
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ClientTest method testNetSocketConnect.
@Test
public void testNetSocketConnect() throws Exception {
waitFor(2);
server.requestHandler(req -> {
NetSocket socket = req.netSocket();
AtomicInteger status = new AtomicInteger();
socket.handler(buff -> {
switch(status.getAndIncrement()) {
case 0:
assertEquals(Buffer.buffer("some-data"), buff);
socket.write(buff);
break;
case 1:
assertEquals(Buffer.buffer("last-data"), buff);
break;
default:
fail();
break;
}
});
socket.endHandler(v -> {
assertEquals(2, status.getAndIncrement());
socket.write(Buffer.buffer("last-data"));
});
socket.closeHandler(v -> {
assertEquals(3, status.getAndIncrement());
complete();
});
});
startServer();
client.request(HttpMethod.CONNECT, DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
NetSocket socket = resp.netSocket();
StringBuilder received = new StringBuilder();
AtomicInteger count = new AtomicInteger();
socket.handler(buff -> {
if (buff.length() > 0) {
received.append(buff.toString());
if (received.toString().equals("some-data")) {
received.setLength(0);
socket.end(Buffer.buffer("last-data"));
} else if (received.toString().equals("last-data")) {
assertEquals(0, count.getAndIncrement());
}
}
});
socket.endHandler(v -> {
assertEquals(1, count.getAndIncrement());
});
socket.closeHandler(v -> {
assertEquals(2, count.getAndIncrement());
complete();
});
socket.write(Buffer.buffer("some-data"));
}).setHost("whatever.com").sendHead();
await();
}
Aggregations