use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ServerTest method testClientSendGoAwayInternalError.
@Test
public void testClientSendGoAwayInternalError() throws Exception {
Future<Void> abc = Future.future();
Context ctx = vertx.getOrCreateContext();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
AtomicInteger status = new AtomicInteger();
conn.goAwayHandler(ga -> {
assertOnIOContext(ctx);
assertEquals(0, status.getAndIncrement());
req.response().end();
});
conn.shutdownHandler(v -> {
assertOnIOContext(ctx);
assertEquals(1, status.getAndIncrement());
});
conn.closeHandler(v -> {
assertEquals(2, status.getAndIncrement());
testComplete();
});
abc.complete();
};
server.requestHandler(requestHandler);
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
Http2ConnectionEncoder encoder = request.encoder;
int id = request.nextStreamId();
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
abc.setHandler(ar -> {
encoder.writeGoAway(request.context, id, 3, Unpooled.EMPTY_BUFFER, request.context.newPromise());
request.context.flush();
});
});
fut.sync();
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ServerTest 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.write(buff);
socket.close();
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();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.decoder.frameListener(new Http2EventAdapter() {
int count = 0;
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
int c = count++;
vertx.runOnContext(v -> {
assertEquals(0, c);
});
request.encoder.writeData(request.context, id, Buffer.buffer("some-data").getByteBuf(), 0, false, request.context.newPromise());
request.context.flush();
}
StringBuilder received = new StringBuilder();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
String s = data.toString(StandardCharsets.UTF_8);
received.append(s);
if (endOfStream) {
request.encoder.writeData(request.context, id, Buffer.buffer("last-data").getByteBuf(), 0, true, request.context.newPromise());
vertx.runOnContext(v -> {
assertEquals("some-data", received.toString());
complete();
});
}
return data.readableBytes() + padding;
}
});
request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ServerTest method testNetSocketHandleReset.
@Test
public void testNetSocketHandleReset() throws Exception {
server.requestHandler(req -> {
NetSocket socket = req.netSocket();
AtomicInteger status = new AtomicInteger();
socket.exceptionHandler(err -> {
assertTrue(err instanceof StreamResetException);
StreamResetException ex = (StreamResetException) err;
assertEquals(0, ex.getCode());
assertEquals(0, status.getAndIncrement());
});
socket.endHandler(v -> {
fail();
});
socket.closeHandler(v -> {
assertEquals(1, status.getAndIncrement());
testComplete();
});
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.decoder.frameListener(new Http2EventAdapter() {
int count = 0;
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
int c = count++;
vertx.runOnContext(v -> {
assertEquals(0, c);
});
request.encoder.writeRstStream(ctx, streamId, 0, ctx.newPromise());
request.context.flush();
}
});
request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class Http2ServerTest method testShutdownWithTimeout.
@Test
public void testShutdownWithTimeout() throws Exception {
waitFor(2);
AtomicInteger closed = new AtomicInteger();
AtomicReference<HttpServerRequest> first = new AtomicReference<>();
AtomicInteger status = 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 -> {
fail();
});
req.response().closeHandler(err -> {
closed.incrementAndGet();
});
req.response().endHandler(err -> {
closed.incrementAndGet();
});
HttpConnection conn = req.connection();
conn.closeHandler(v -> {
assertEquals(4, closed.get());
assertEquals(1, status.getAndIncrement());
complete();
});
conn.shutdown(300);
}
};
testServerSendGoAway(requestHandler, 0);
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class NetTest method testTLS.
void testTLS(boolean clientCert, boolean clientTrust, boolean serverCert, boolean serverTrust, boolean requireClientAuth, boolean clientTrustAll, boolean shouldPass, boolean startTLS, String[] enabledCipherSuites, String[] enabledSecureTransportProtocols) throws Exception {
server.close();
NetServerOptions options = new NetServerOptions();
if (!startTLS) {
options.setSsl(true);
}
if (serverTrust) {
options.setTrustStoreOptions(new JksOptions().setPath("tls/server-truststore.jks").setPassword("wibble"));
}
if (serverCert) {
options.setKeyStoreOptions(new JksOptions().setPath("tls/server-keystore.jks").setPassword("wibble"));
}
if (requireClientAuth) {
options.setClientAuth(ClientAuth.REQUIRED);
}
for (String suite : enabledCipherSuites) {
options.addEnabledCipherSuite(suite);
}
for (String protocol : enabledSecureTransportProtocols) {
options.addEnabledSecureTransportProtocol(protocol);
}
Consumer<NetSocket> certificateChainChecker = socket -> {
try {
X509Certificate[] certs = socket.peerCertificateChain();
if (clientCert) {
assertNotNull(certs);
assertEquals(1, certs.length);
} else {
assertNull(certs);
}
} catch (SSLPeerUnverifiedException e) {
assertTrue(clientTrust || clientTrustAll);
}
};
options.setPort(4043);
server = vertx.createNetServer(options);
Handler<NetSocket> serverHandler = socket -> {
if (socket.isSsl()) {
certificateChainChecker.accept(socket);
}
AtomicBoolean upgradedServer = new AtomicBoolean();
AtomicInteger upgradedServerCount = new AtomicInteger();
socket.handler(buff -> {
socket.write(buff);
if (startTLS) {
if (upgradedServer.compareAndSet(false, true)) {
assertFalse(socket.isSsl());
socket.upgradeToSsl(v -> {
certificateChainChecker.accept(socket);
upgradedServerCount.incrementAndGet();
assertTrue(socket.isSsl());
});
} else {
assertTrue(socket.isSsl());
assertEquals(1, upgradedServerCount.get());
}
} else {
assertTrue(socket.isSsl());
}
});
};
server.connectHandler(serverHandler).listen(ar -> {
client.close();
NetClientOptions clientOptions = new NetClientOptions();
if (!startTLS) {
clientOptions.setSsl(true);
}
if (clientTrustAll) {
clientOptions.setTrustAll(true);
}
if (clientTrust) {
clientOptions.setTrustStoreOptions(new JksOptions().setPath("tls/client-truststore.jks").setPassword("wibble"));
}
if (clientCert) {
clientOptions.setKeyStoreOptions(new JksOptions().setPath("tls/client-keystore.jks").setPassword("wibble"));
}
for (String suite : enabledCipherSuites) {
clientOptions.addEnabledCipherSuite(suite);
}
for (String protocol : enabledSecureTransportProtocols) {
clientOptions.addEnabledSecureTransportProtocol(protocol);
}
client = vertx.createNetClient(clientOptions);
client.connect(4043, "localhost", ar2 -> {
if (ar2.succeeded()) {
if (!shouldPass) {
fail("Should not connect");
return;
}
final int numChunks = 100;
final int chunkSize = 100;
final List<Buffer> toSend = new ArrayList<>();
final Buffer expected = Buffer.buffer();
for (int i = 0; i < numChunks; i++) {
Buffer chunk = TestUtils.randomBuffer(chunkSize);
toSend.add(chunk);
expected.appendBuffer(chunk);
}
final Buffer received = Buffer.buffer();
final NetSocket socket = ar2.result();
final AtomicBoolean upgradedClient = new AtomicBoolean();
socket.handler(buffer -> {
received.appendBuffer(buffer);
if (received.length() == expected.length()) {
assertEquals(expected, received);
testComplete();
}
if (startTLS && !upgradedClient.get()) {
upgradedClient.set(true);
assertFalse(socket.isSsl());
socket.upgradeToSsl(v -> {
assertTrue(socket.isSsl());
for (int i = 1; i < numChunks; i++) {
socket.write(toSend.get(i));
}
});
} else {
assertTrue(socket.isSsl());
}
});
int numToSend = startTLS ? 1 : numChunks;
for (int i = 0; i < numToSend; i++) {
socket.write(toSend.get(i));
}
} else {
if (shouldPass) {
fail("Should not fail to connect");
} else {
testComplete();
}
}
});
});
await();
}
Aggregations