use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpServerTests 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 disposableServer = TcpServer.create().port(0).runOn(loop).doOnConnection(c -> {
c.onDispose().subscribe(null, null, latch2::countDown);
latch1.countDown();
}).channelGroup(new DefaultChannelGroup(new DefaultEventExecutor())).handle((in, out) -> out.sendString(Mono.just("delay1000").delayElement(Duration.ofSeconds(1)))).wiretap(true).bindNow(Duration.ofSeconds(30));
TcpClient client = TcpClient.create().remoteAddress(disposableServer::address).wiretap(true);
AtomicReference<String> result = new AtomicReference<>();
Flux.merge(client.connect(), client.connect()).flatMap(conn -> conn.inbound().receive().asString()).collect(Collectors.joining()).subscribe(s -> {
result.set(s);
latch3.countDown();
});
assertThat(latch1.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
// Stop accepting incoming requests, wait at most 3s for the active requests to finish
disposableServer.disposeNow();
// Dispose the event loop
loop.disposeLater().block(Duration.ofSeconds(30));
assertThat(latch2.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
assertThat(latch3.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
assertThat(result.get()).isNotNull().isEqualTo("delay1000delay1000");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpServerTests method testEchoWithLineBasedFrameDecoder.
@Test
void testEchoWithLineBasedFrameDecoder() throws Exception {
CountDownLatch latch = new CountDownLatch(2);
DisposableServer server = TcpServer.create().port(0).doOnConnection(c -> c.addHandlerLast("codec", new LineBasedFrameDecoder(256))).handle((in, out) -> out.sendString(in.receive().asString().doOnNext(s -> {
if ("4".equals(s)) {
latch.countDown();
}
}).map(s -> s + "\n"))).bindNow();
assertThat(server).isNotNull();
Connection client = TcpClient.create().port(server.port()).doOnConnected(c -> c.addHandlerLast("codec", new LineBasedFrameDecoder(256))).handle((in, out) -> out.sendString(Flux.just("1\n", "2\n", "3\n", "4\n")).then(in.receive().asString().doOnNext(s -> {
if ("4".equals(s)) {
latch.countDown();
}
}).then())).connectNow();
assertThat(client).isNotNull();
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TransportEventLoopMetricsTest method testEventLoopMetrics.
@Test
void testEventLoopMetrics() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
DisposableServer server = null;
Connection client = null;
LoopResources loop = null;
try {
loop = LoopResources.create(TransportEventLoopMetricsTest.class.getName(), 3, true);
server = TcpServer.create().port(0).metrics(true).runOn(loop).doOnConnection(c -> {
EventLoop eventLoop = c.channel().eventLoop();
IntStream.range(0, 10).forEach(i -> eventLoop.execute(() -> {
}));
if (eventLoop instanceof SingleThreadEventExecutor) {
SingleThreadEventExecutor singleThreadEventExecutor = (SingleThreadEventExecutor) eventLoop;
String[] tags = new String[] { NAME, singleThreadEventExecutor.threadProperties().name() };
assertThat(getGaugeValue(EVENT_LOOP_PREFIX + PENDING_TASKS, tags)).isEqualTo(10);
latch.countDown();
}
}).wiretap(true).bindNow();
assertThat(server).isNotNull();
client = TcpClient.create().port(server.port()).wiretap(true).connectNow();
assertThat(client).isNotNull();
assertThat(latch.await(5, TimeUnit.SECONDS)).as("Did not find 10 pending tasks from meter").isTrue();
} finally {
if (client != null) {
client.disposeNow();
}
if (server != null) {
server.disposeNow();
}
if (loop != null) {
loop.disposeLater().block(Duration.ofSeconds(10));
}
}
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpEmissionTest method testBackpressure.
@ParameterizedTest
@ValueSource(booleans = { true, false })
void testBackpressure(boolean flushOnEach) throws Exception {
byte[] array = new byte[32];
Random random = new Random();
final int emissionCount = 130;
DisposableServer server = TcpServer.create().handle((inbound, outbound) -> outbound.send(Flux.fromStream(IntStream.range(0, emissionCount).mapToObj(i -> {
random.nextBytes(array);
return Unpooled.copiedBuffer(array);
})), b -> flushOnEach)).host("localhost").port(0).bindNow();
CountDownLatch latch = new CountDownLatch(1);
AtomicLong cnt = new AtomicLong();
try (Socket socket = new Socket("localhost", server.port());
InputStream is = socket.getInputStream()) {
byte[] buffer = new byte[32];
while (true) {
is.read(buffer, 0, 4);
if (cnt.incrementAndGet() >= emissionCount * 8) {
latch.countDown();
break;
}
}
} finally {
server.disposeNow();
}
assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpResourcesTest method blockShouldFail.
@Test
void blockShouldFail() throws InterruptedException {
final int port = SocketUtils.findAvailableTcpPort();
final CountDownLatch latch = new CountDownLatch(2);
DisposableServer server = TcpServer.create().port(port).handle((in, out) -> {
try {
in.receive().blockFirst();
} catch (RuntimeException e) {
latch.countDown();
throw e;
}
return Flux.never();
}).bindNow();
Connection client = TcpClient.newConnection().port(port).handle((in, out) -> {
try {
out.sendString(Flux.just("Hello World!")).then().block();
} catch (RuntimeException e) {
latch.countDown();
throw e;
}
return Mono.never();
}).connectNow();
assertThat(latch.await(5, TimeUnit.SECONDS)).as("latch was counted down").isTrue();
client.dispose();
server.disposeNow();
}
Aggregations