use of reactor.netty.Connection in project reactor-netty by reactor.
the class UdpClientTest method testIssue192.
@Test
void testIssue192() throws Exception {
LoopResources resources = LoopResources.create("testIssue192");
NioEventLoopGroup loop = new NioEventLoopGroup(1);
UdpServer server = UdpServer.create().runOn(resources);
UdpClient client = UdpClient.create().resolver(spec -> spec.runOn(loop)).runOn(resources);
assertThat(Thread.getAllStackTraces().keySet().stream().noneMatch(t -> t.getName().startsWith("testIssue192"))).isTrue();
Connection conn1 = null;
Connection conn2 = null;
try {
conn1 = server.bindNow();
conn2 = client.connectNow();
assertThat(conn1).isNotNull();
assertThat(conn2).isNotNull();
assertThat(Thread.getAllStackTraces().keySet().stream().anyMatch(t -> t.getName().startsWith("testIssue192"))).isTrue();
} finally {
if (conn1 != null) {
conn1.disposeNow();
}
if (conn2 != null) {
conn2.disposeNow();
}
resources.disposeLater().block(Duration.ofSeconds(5));
loop.shutdownGracefully().get(5, TimeUnit.SECONDS);
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpServerTests method assertSendFile.
private void assertSendFile(Function<NettyOutbound, NettyOutbound> fn) throws Exception {
DisposableServer context = TcpServer.create().handle((in, out) -> in.receive().asString().flatMap(word -> "GOGOGO".equals(word) ? fn.apply(out).then() : out.sendString(Mono.just("NOPE")))).wiretap(true).bindNow();
assertThat(context).isNotNull();
AtomicReference<String> m1 = new AtomicReference<>();
AtomicReference<String> m2 = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(2);
Connection client1 = TcpClient.create().port(context.port()).handle((in, out) -> {
in.receive().asString().log("-----------------CLIENT1").subscribe(s -> {
m1.set(s);
latch.countDown();
});
return out.sendString(Mono.just("gogogo")).neverComplete();
}).wiretap(true).connectNow();
Connection client2 = TcpClient.create().port(context.port()).option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 1024, 65536)).handle((in, out) -> {
in.receive().asString(StandardCharsets.UTF_8).take(2).reduceWith(String::new, String::concat).log("-----------------CLIENT2").subscribe(s -> {
m2.set(s);
latch.countDown();
});
return out.sendString(Mono.just("GOGOGO")).neverComplete();
}).wiretap(true).connectNow();
assertThat(client2).isNotNull();
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
client1.disposeNow();
client2.disposeNow();
context.disposeNow();
assertThat(m1.get()).isNotNull().isEqualTo("NOPE");
assertThat(m2.get()).isNotNull().startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here ->").contains("<- 1024 mark here").endsWith("End of File");
}
use of reactor.netty.Connection 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.Connection 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.Connection 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();
}
Aggregations