use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testCancelReceivingForWebSocketServer.
@Test
void testCancelReceivingForWebSocketServer() throws Exception {
AtomicReference<WebSocketCloseStatus> statusServer = new AtomicReference<>();
AtomicReference<WebSocketCloseStatus> statusClient = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(2);
disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((in, out) -> {
in.receiveCloseStatus().doOnNext(o -> {
statusServer.set(o);
latch.countDown();
}).subscribe();
in.receive().take(1).subscribe();
return Mono.never();
})).bindNow();
createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> {
in.receiveCloseStatus().doOnNext(o -> {
statusClient.set(o);
latch.countDown();
}).subscribe();
return out.sendString(Flux.interval(Duration.ofMillis(10)).map(l -> l + ""));
}).subscribe();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(statusClient.get()).isNotNull().isEqualTo(WebSocketCloseStatus.EMPTY);
assertThat(statusServer.get()).isNotNull().isEqualTo(WebSocketCloseStatus.ABNORMAL_CLOSURE);
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method nonContentStatusCodes.
@Test
void nonContentStatusCodes() {
disposableServer = createServer().host("localhost").route(r -> r.get("/204-1", (req, res) -> res.status(HttpResponseStatus.NO_CONTENT).sendHeaders()).get("/204-2", (req, res) -> res.status(HttpResponseStatus.NO_CONTENT)).get("/205-1", (req, res) -> res.status(HttpResponseStatus.RESET_CONTENT).sendHeaders()).get("/205-2", (req, res) -> res.status(HttpResponseStatus.RESET_CONTENT)).get("/304-1", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED).sendHeaders()).get("/304-2", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED)).get("/304-3", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED).send())).bindNow();
InetSocketAddress address = (InetSocketAddress) disposableServer.address();
checkResponse("/204-1", address);
checkResponse("/204-2", address);
checkResponse("/205-1", address);
checkResponse("/205-2", address);
checkResponse("/304-1", address);
checkResponse("/304-2", address);
checkResponse("/304-3", address);
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method doTestIdleTimeout_DelayFirstRequest.
private void doTestIdleTimeout_DelayFirstRequest(boolean withSecurity) {
HttpServer server = createServer().idleTimeout(Duration.ofMillis(200)).handle((req, resp) -> resp.send(req.receive().retain()));
HttpClient client = createClient(() -> disposableServer.address()).disableRetry(true);
if (withSecurity) {
Http11SslContextSpec serverCtx = Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey());
Http11SslContextSpec clientCtx = Http11SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
server = server.secure(spec -> spec.sslContext(serverCtx));
client = client.secure(spec -> spec.sslContext(clientCtx));
}
disposableServer = server.bindNow(Duration.ofSeconds(30));
client.post().uri("/").send((req, out) -> out.sendString(Mono.just("doTestIdleTimeout_DelayFirstRequest").delaySubscription(Duration.ofMillis(500)))).responseContent().aggregate().as(StepVerifier::create).expectErrorMatches(t -> t instanceof IOException || t instanceof AbortedException).verify(Duration.ofSeconds(30));
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testGracefulShutdown.
@Test
void testGracefulShutdown() throws Exception {
CountDownLatch latch1 = new CountDownLatch(2);
CountDownLatch latch2 = new CountDownLatch(2);
CountDownLatch latch3 = new CountDownLatch(1);
LoopResources loop = LoopResources.create("testGracefulShutdown");
disposableServer = createServer().runOn(loop).doOnConnection(c -> {
c.onDispose().subscribe(null, null, latch2::countDown);
latch1.countDown();
}).channelGroup(new DefaultChannelGroup(new DefaultEventExecutor())).route(r -> r.get("/delay500", (req, res) -> res.sendString(Mono.just("delay500").delayElement(Duration.ofMillis(500)))).get("/delay1000", (req, res) -> res.sendString(Mono.just("delay1000").delayElement(Duration.ofSeconds(1))))).bindNow(Duration.ofSeconds(30));
HttpClient client = createClient(disposableServer::address);
AtomicReference<String> result = new AtomicReference<>();
Flux.just("/delay500", "/delay1000").flatMap(s -> client.get().uri(s).responseContent().aggregate().asString()).collect(Collectors.joining()).subscribe(s -> {
result.set(s);
latch3.countDown();
});
assertThat(latch1.await(30, TimeUnit.SECONDS)).isTrue();
// Stop accepting incoming requests, wait at most 3s for the active requests to finish
disposableServer.disposeNow();
assertThat(latch2.await(30, TimeUnit.SECONDS)).isTrue();
// Dispose the event loop
loop.disposeLater().block(Duration.ofSeconds(30));
assertThat(latch3.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(result.get()).isNotNull().isEqualTo("delay500delay1000");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testCustomMetricsRecorderWithUriMapper.
@Test
void testCustomMetricsRecorderWithUriMapper() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(5);
List<String> collectedUris = new CopyOnWriteArrayList<>();
disposableServer = createServer().handle((req, resp) -> resp.sendString(Mono.just("OK"))).metrics(true, () -> new HttpServerMetricsRecorder() {
@Override
public void recordDataReceived(SocketAddress remoteAddress, String uri, long bytes) {
collectedUris.add(uri);
latch.countDown();
}
@Override
public void recordDataSent(SocketAddress remoteAddress, String uri, long bytes) {
collectedUris.add(uri);
latch.countDown();
}
@Override
public void incrementErrorsCount(SocketAddress remoteAddress, String uri) {
collectedUris.add(uri);
latch.countDown();
}
@Override
public void recordDataReceived(SocketAddress remoteAddress, long bytes) {
}
@Override
public void recordDataSent(SocketAddress remoteAddress, long bytes) {
}
@Override
public void incrementErrorsCount(SocketAddress remoteAddress) {
}
@Override
public void recordTlsHandshakeTime(SocketAddress remoteAddress, Duration time, String status) {
}
@Override
public void recordConnectTime(SocketAddress remoteAddress, Duration time, String status) {
}
@Override
public void recordResolveAddressTime(SocketAddress remoteAddress, Duration time, String status) {
}
@Override
public void recordDataReceivedTime(String uri, String method, Duration time) {
collectedUris.add(uri);
latch.countDown();
}
@Override
public void recordDataSentTime(String uri, String method, String status, Duration time) {
collectedUris.add(uri);
latch.countDown();
}
@Override
public void recordResponseTime(String uri, String method, String status, Duration time) {
collectedUris.add(uri);
latch.countDown();
}
}, s -> s.startsWith("/stream/") ? "/stream/{n}" : s).bindNow();
HttpClient.create().get().uri("http://localhost:" + disposableServer.port() + "/stream/1024").responseContent().aggregate().block(Duration.ofSeconds(30));
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(collectedUris).isNotEmpty().containsOnly("/stream/{n}");
}
Aggregations