use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpServerTests method testHalfClosedConnection.
@Test
@SuppressWarnings("FutureReturnValueIgnored")
void testHalfClosedConnection() throws Exception {
DisposableServer server = TcpServer.create().port(0).childOption(ChannelOption.ALLOW_HALF_CLOSURE, true).wiretap(true).handle((in, out) -> in.receive().asString().doOnNext(s -> {
if (s.endsWith("257\n")) {
out.sendString(Mono.just("END").delayElement(Duration.ofMillis(100))).then().subscribe();
}
}).then()).bindNow();
Connection conn = TcpClient.create().remoteAddress(server::address).wiretap(true).connectNow();
CountDownLatch latch = new CountDownLatch(1);
conn.inbound().receive().asString().subscribe(s -> {
if ("END".equals(s)) {
latch.countDown();
}
});
conn.outbound().sendString(Flux.range(1, 257).map(count -> count + "\n")).then().subscribe(null, null, // FutureReturnValueIgnored
() -> ((io.netty.channel.socket.SocketChannel) conn.channel()).shutdownOutput());
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
conn.disposeNow();
server.disposeNow();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpServerTests method exposesRemoteAddress.
@Test
void exposesRemoteAddress() throws InterruptedException {
final int port = SocketUtils.findAvailableTcpPort();
final CountDownLatch latch = new CountDownLatch(1);
DisposableServer server = TcpServer.create().port(port).handle((in, out) -> {
in.withConnection(c -> {
InetSocketAddress addr = (InetSocketAddress) c.address();
assertThat(addr.getAddress()).as("remote address is not null").isNotNull();
latch.countDown();
});
return Flux.never();
}).wiretap(true).bindNow();
assertThat(server).isNotNull();
Connection client = TcpClient.create().port(port).handle((in, out) -> out.sendString(Flux.just("Hello World!"))).wiretap(true).connectNow();
assertThat(client).isNotNull();
assertThat(latch.await(5, TimeUnit.SECONDS)).as("Latch was counted down").isTrue();
client.disposeNow();
server.disposeNow();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpServerTests method tcpServerHandlesJsonPojosOverSsl.
@Test
void tcpServerHandlesJsonPojosOverSsl() throws Exception {
final CountDownLatch latch = new CountDownLatch(2);
SslContext serverOptions = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(SslProvider.JDK).build();
SslContext clientOptions = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(SslProvider.JDK).build();
log.debug("Using SslContext: {}", clientOptions);
final TcpServer server = TcpServer.create().host("localhost").secure(sslContextSpec -> sslContextSpec.sslContext(serverOptions));
ObjectMapper m = new ObjectMapper();
DisposableServer connectedServer = server.handle((in, out) -> {
in.receive().asByteArray().map(bb -> {
try {
return m.readValue(bb, Pojo.class);
} catch (IOException io) {
throw Exceptions.propagate(io);
}
}).log("conn").subscribe(data -> {
if ("John Doe".equals(data.getName())) {
latch.countDown();
}
});
return out.sendString(Mono.just("Hi")).neverComplete();
}).wiretap(true).bindNow();
assertThat(connectedServer).isNotNull();
final TcpClient client = TcpClient.create().host("localhost").port(connectedServer.port()).secure(spec -> spec.sslContext(clientOptions));
Connection connectedClient = client.handle((in, out) -> {
// in
in.receive().asString().log("receive").subscribe(data -> {
if (data.equals("Hi")) {
latch.countDown();
}
});
// out
return out.send(Flux.just(new Pojo("John" + " Doe")).map(s -> {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
m.writeValue(os, s);
return out.alloc().buffer().writeBytes(os.toByteArray());
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
})).neverComplete();
}).wiretap(true).connectNow();
assertThat(connectedClient).isNotNull();
assertThat(latch.await(5, TimeUnit.SECONDS)).as("Latch was counted down").isTrue();
connectedClient.disposeNow();
connectedServer.disposeNow();
}
use of reactor.netty.DisposableServer 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.DisposableServer 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();
}
Aggregations