use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpServerTests method flushEvery5ElementsWithManualDecoding.
@Test
void flushEvery5ElementsWithManualDecoding() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Function<List<Pojo>, ByteBuf> jsonEncoder = pojo -> {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
mapper.writeValue(out, pojo);
} catch (Exception e) {
throw new RuntimeException(e);
}
return Unpooled.copiedBuffer(out.toByteArray());
};
Function<String, Pojo[]> jsonDecoder = s -> {
try {
return mapper.readValue(s, Pojo[].class);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
CountDownLatch dataLatch = new CountDownLatch(10);
DisposableServer server = TcpServer.create().handle((in, out) -> in.withConnection(c -> c.addHandler(new JsonObjectDecoder())).receive().asString().log("serve").map(jsonDecoder).concatMap(Flux::fromArray).window(5).concatMap(w -> out.send(w.collectList().map(jsonEncoder)))).wiretap(true).bindNow();
assertThat(server).isNotNull();
Connection client = TcpClient.create().port(server.port()).handle((in, out) -> {
in.withConnection(c -> c.addHandler(new JsonObjectDecoder())).receive().asString().log("receive").map(jsonDecoder).concatMap(Flux::fromArray).subscribe(c -> dataLatch.countDown());
return out.send(Flux.range(1, 10).map(it -> new Pojo("test" + it)).log("send").collectList().map(jsonEncoder)).neverComplete();
}).wiretap(true).connectNow();
assertThat(client).isNotNull();
assertThat(dataLatch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
assertThat(dataLatch.getCount()).isEqualTo(0);
server.disposeNow();
client.disposeNow();
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpServerTests method exposesNettyPipelineConfiguration.
@Test
void exposesNettyPipelineConfiguration() throws InterruptedException {
final int port = SocketUtils.findAvailableTcpPort();
final CountDownLatch latch = new CountDownLatch(2);
final TcpClient client = TcpClient.create().port(port);
BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> serverHandler = (in, out) -> {
in.receive().asString().subscribe(data -> {
log.info("data " + data + " on " + in);
latch.countDown();
});
return Flux.never();
};
TcpServer server = TcpServer.create().doOnConnection(c -> c.addHandlerLast("codec", new LineBasedFrameDecoder(8 * 1024))).port(port);
DisposableServer connected = server.handle(serverHandler).wiretap(true).bindNow();
assertThat(connected).isNotNull();
Connection clientContext = client.handle((in, out) -> out.sendString(Flux.just("Hello World!\n", "Hello 11!\n"))).wiretap(true).connectNow();
assertThat(clientContext).isNotNull();
assertThat(latch.await(10, TimeUnit.SECONDS)).as("Latch was counted down").isTrue();
connected.disposeNow();
clientContext.disposeNow();
}
use of reactor.netty.Connection 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.Connection 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.Connection in project reactor-netty by reactor.
the class UdpServerTests method portBindingException.
@Test
void portBindingException() {
Connection conn = UdpServer.create().port(0).bindNow(Duration.ofSeconds(30));
try {
UdpServer.create().bindAddress(conn::address).bindNow(Duration.ofSeconds(30));
fail("illegal-success");
} catch (ChannelBindException e) {
assertThat(((InetSocketAddress) conn.address()).getPort()).isEqualTo(e.localPort());
e.printStackTrace();
}
conn.disposeNow();
}
Aggregations