use of io.vertx.test.core.CheckingSender in project vert.x by eclipse.
the class WebSocketTest method testDontReceiveMessagerAfterCloseHandlerCalled.
@Test
public void testDontReceiveMessagerAfterCloseHandlerCalled() {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
boolean[] closed = new boolean[1];
ws.handler(msg -> {
// We will still receive messages after the close frame is sent
if (closed[0]) {
fail("Should not receive a message after close handler callback");
}
});
ws.closeHandler(v -> {
closed[0] = true;
// Let some time to let message arrive in the handler
vertx.setTimer(10, id -> {
testComplete();
});
});
vertx.setTimer(500, id -> {
// Fill the buffer, so the close frame will be delayed
while (!ws.writeQueueFull()) {
ws.write(TestUtils.randomBuffer(1000));
}
// Send the close frame, the TCP connection will be closed after that frame is sent
ws.close();
});
});
server.listen(onSuccess(s -> {
// Create a new client that will use the same event-loop than the server
// so the ws.writeQueueFull() will return true since the client won't be able to read the socket
// when the server is busy writing the WebSocket
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", onSuccess(ws -> {
CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), ws);
ws.closeHandler(v -> sender.close());
sender.send();
}));
}));
await();
}
use of io.vertx.test.core.CheckingSender in project vert.x by eclipse.
the class WebSocketTest method testServerWebSocketShouldBeClosedWhenTheClosedHandlerIsCalled.
@Test
public void testServerWebSocketShouldBeClosedWhenTheClosedHandlerIsCalled() {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), ws);
sender.send();
ws.closeHandler(v -> {
Throwable failure = sender.close();
if (failure != null) {
fail(failure);
} else {
testComplete();
}
});
});
server.listen(onSuccess(s -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", onSuccess(ws -> {
vertx.setTimer(1000, id -> {
ws.close();
});
}));
}));
await();
}
use of io.vertx.test.core.CheckingSender in project vert.x by eclipse.
the class Http1xTest method testHttpClientRequestShouldCallExceptionHandlerWhenTheClosedHandlerIsCalled.
@Test
public void testHttpClientRequestShouldCallExceptionHandlerWhenTheClosedHandlerIsCalled() throws Exception {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
vertx.setTimer(1000, id -> {
req.response().close();
});
});
startServer(testAddress);
client.request(new RequestOptions(requestOptions).setMethod(PUT)).onComplete(onSuccess(req -> {
req.setChunked(true);
CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), req);
AtomicBoolean connected = new AtomicBoolean();
AtomicBoolean done = new AtomicBoolean();
req.exceptionHandler(err -> {
assertTrue(connected.get());
Throwable failure = sender.close();
if (failure != null) {
fail(failure);
} else if (done.compareAndSet(false, true)) {
testComplete();
}
});
req.sendHead(v -> {
connected.set(true);
sender.send();
});
}));
await();
}
use of io.vertx.test.core.CheckingSender in project vert.x by eclipse.
the class WebSocketTest method testClientWebSocketShouldBeClosedWhenTheClosedHandlerIsCalled.
@Test
public void testClientWebSocketShouldBeClosedWhenTheClosedHandlerIsCalled() {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
vertx.setTimer(1000, id -> {
ws.close();
});
});
server.listen(onSuccess(s -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", onSuccess(ws -> {
CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), ws);
sender.send();
ws.closeHandler(v -> {
Throwable failure = sender.close();
if (failure != null) {
fail(failure);
} else {
testComplete();
}
});
}));
}));
await();
}
use of io.vertx.test.core.CheckingSender in project vert.x by eclipse.
the class Http1xTest method testHttpServerRequestShouldCallExceptionHandlerWhenTheClosedHandlerIsCalled.
@Test
public void testHttpServerRequestShouldCallExceptionHandlerWhenTheClosedHandlerIsCalled() {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.setChunked(true);
CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), resp);
sender.send();
resp.closeHandler(v -> {
Throwable failure = sender.close();
if (failure != null) {
fail(failure);
} else {
testComplete();
}
});
});
server.listen(testAddress, onSuccess(s -> {
client.request(requestOptions).compose(HttpClientRequest::send).onComplete(onSuccess(resp -> {
vertx.setTimer(1000, id -> {
resp.request().connection().close();
});
}));
}));
await();
}
Aggregations