use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testSharedServersRoundRobin.
@Test
public void testSharedServersRoundRobin() throws Exception {
int numServers = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE / 2 - 1;
int numConnections = numServers * 100;
List<HttpServer> servers = new ArrayList<>();
Set<HttpServer> connectedServers = new ConcurrentHashSet<>();
Map<HttpServer, Integer> connectCount = new ConcurrentHashMap<>();
CountDownLatch latchListen = new CountDownLatch(numServers);
CountDownLatch latchConns = new CountDownLatch(numConnections);
for (int i = 0; i < numServers; i++) {
HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
servers.add(theServer);
theServer.webSocketHandler(ws -> {
connectedServers.add(theServer);
Integer cnt = connectCount.get(theServer);
int icnt = cnt == null ? 0 : cnt;
icnt++;
connectCount.put(theServer, icnt);
latchConns.countDown();
}).listen(ar -> {
if (ar.succeeded()) {
latchListen.countDown();
} else {
fail("Failed to bind server");
}
});
}
assertTrue(latchListen.await(10, TimeUnit.SECONDS));
// Create a bunch of connections
client = vertx.createHttpClient();
CountDownLatch latchClient = new CountDownLatch(numConnections);
for (int i = 0; i < numConnections; i++) {
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", onSuccess(ws -> {
ws.closeHandler(v -> latchClient.countDown());
ws.close();
}));
}
assertTrue(latchClient.await(10, TimeUnit.SECONDS));
assertTrue(latchConns.await(10, TimeUnit.SECONDS));
assertEquals(numServers, connectedServers.size());
for (HttpServer server : servers) {
assertTrue(connectedServers.contains(server));
}
assertEquals(numServers, connectCount.size());
for (int cnt : connectCount.values()) {
assertEquals(numConnections / numServers, cnt);
}
CountDownLatch closeLatch = new CountDownLatch(numServers);
for (HttpServer server : servers) {
server.close(ar -> {
assertTrue(ar.succeeded());
closeLatch.countDown();
});
}
assertTrue(closeLatch.await(10, TimeUnit.SECONDS));
testComplete();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testClientWebSocketSendPongExceeds125Bytes.
@Test
public void testClientWebSocketSendPongExceeds125Bytes() {
// Netty will prevent us from encoding a pingBody greater than 126 bytes by silently throwing an error in the background
String pingBody = randomAlphaString(126);
int maxFrameSize = 256;
server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
server.webSocketHandler(ws -> {
ws.pongHandler(buff -> fail());
vertx.setTimer(2000, id -> testComplete());
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
ws.writeFrame(WebSocketFrame.pongFrame(Buffer.buffer(pingBody)));
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testClose.
public void testClose(boolean closeClient, boolean closeServer, boolean regularClose) {
waitFor(4);
Consumer<WebSocketBase> test = ws -> {
assertFalse(ws.isClosed());
AtomicInteger cnt = new AtomicInteger();
ws.exceptionHandler(err -> {
if (regularClose) {
fail();
} else if (cnt.getAndIncrement() == 0) {
complete();
}
});
ws.endHandler(v -> {
if (regularClose) {
complete();
} else {
fail();
}
});
ws.closeHandler(v -> {
assertTrue(ws.isClosed());
try {
ws.close();
} catch (Exception e) {
fail();
}
complete();
});
};
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
test.accept(ws);
if (closeServer) {
ws.close();
}
});
server.listen(onSuccess(s -> {
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/some/path", onSuccess(ws -> {
test.accept(ws);
if (closeClient) {
ws.close();
}
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testClearClientHandlersOnEnd.
@Test
public void testClearClientHandlersOnEnd() {
String path = "/some/path";
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(WebSocketBase::close);
server.listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, onSuccess(ws -> {
ws.endHandler(v2 -> {
try {
ws.endHandler(null);
ws.exceptionHandler(null);
ws.handler(null);
} catch (Exception e) {
fail("Was expecting to set to null the handlers when the socket is closed");
return;
}
testComplete();
});
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testServerWebSocketSendPingExceeds125Bytes.
@Test
public void testServerWebSocketSendPingExceeds125Bytes() {
// Netty will prevent us from encoding a pingBody greater than 126 bytes by silently throwing an error in the background
String pingBody = randomAlphaString(126);
int maxFrameSize = 256;
server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
server.webSocketHandler(ws -> {
ws.pongHandler(buff -> fail());
ws.writeFrame(WebSocketFrame.pingFrame(Buffer.buffer(pingBody)));
vertx.setTimer(2000, id -> testComplete());
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
}));
}));
await();
}
Aggregations