use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpResourcesTest method blockShouldFail.
@Test
public void blockShouldFail() throws InterruptedException {
final int port = SocketUtils.findAvailableTcpPort();
final CountDownLatch latch = new CountDownLatch(2);
NettyContext server = TcpServer.create(port).newHandler((in, out) -> {
try {
in.receive().blockFirst();
} catch (RuntimeException e) {
latch.countDown();
throw e;
}
return Flux.never();
}).block(Duration.ofSeconds(30));
NettyContext client = TcpClient.create(port).newHandler((in, out) -> {
try {
out.sendString(Flux.just("Hello World!")).then().block();
} catch (RuntimeException e) {
latch.countDown();
throw e;
}
return Mono.empty();
}).block(Duration.ofSeconds(30));
assertTrue("latch was counted down", latch.await(5, TimeUnit.SECONDS));
client.dispose();
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method flushEvery5ElementsWithManualDecoding.
@Test
public 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);
NettyContext server = TcpServer.create().newHandler((in, out) -> in.context(c -> c.addHandler(new JsonObjectDecoder())).receive().asString().log("serve").map(jsonDecoder).concatMap(d -> Flux.fromArray(d)).window(5).concatMap(w -> out.send(w.collectList().map(jsonEncoder)))).block(Duration.ofSeconds(30));
NettyContext client = TcpClient.create(o -> o.port(server.address().getPort())).newHandler((in, out) -> {
in.context(c -> c.addHandler(new JsonObjectDecoder())).receive().asString().log("receive").map(jsonDecoder).concatMap(d -> Flux.fromArray(d)).subscribe(c -> dataLatch.countDown());
return out.send(Flux.range(1, 10).map(it -> new Pojo("test" + it)).log("send").collectList().map(jsonEncoder)).neverComplete();
}).block(Duration.ofSeconds(30));
Assertions.assertThat(dataLatch.await(30, TimeUnit.SECONDS)).isTrue();
Assertions.assertThat(dataLatch.getCount()).isEqualTo(0);
server.dispose();
client.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method testHang.
@Test(timeout = 10000)
public void testHang() throws Exception {
NettyContext httpServer = HttpServer.create(opts -> opts.host("0.0.0.0").port(0)).newRouter(r -> r.get("/data", (request, response) -> {
return response.send(Mono.empty());
})).block(Duration.ofSeconds(30));
httpServer.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method testIssue462.
@Test
public void testIssue462() throws InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(1);
NettyContext server = TcpServer.create(0).newHandler((in, out) -> {
in.receive().log("channel").subscribe(trip -> {
countDownLatch.countDown();
});
return Flux.never();
}).block(Duration.ofSeconds(30));
System.out.println("PORT +" + server.address().getPort());
NettyContext client = TcpClient.create(server.address().getPort()).newHandler((in, out) -> out.sendString(Flux.just("test"))).block(Duration.ofSeconds(30));
client.dispose();
server.dispose();
assertThat("countDownLatch counted down", countDownLatch.await(5, TimeUnit.SECONDS));
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method sendFileSecure.
@Test
public void sendFileSecure() throws CertificateException, SSLException, InterruptedException, URISyntaxException {
Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
SelfSignedCertificate ssc = new SelfSignedCertificate();
SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
SslContext sslClient = SslContextBuilder.forClient().trustManager(ssc.cert()).build();
NettyContext context = TcpServer.create(opt -> opt.sslContext(sslServer)).newHandler((in, out) -> in.receive().asString().flatMap(word -> "GOGOGO".equals(word) ? out.sendFile(largeFile).then() : out.sendString(Mono.just("NOPE")))).block();
MonoProcessor<String> m1 = MonoProcessor.create();
MonoProcessor<String> m2 = MonoProcessor.create();
NettyContext client1 = TcpClient.create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).newHandler((in, out) -> {
in.receive().asString().log("-----------------CLIENT1").subscribe(m1::onNext);
return out.sendString(Mono.just("gogogo")).neverComplete();
}).block();
NettyContext client2 = TcpClient.create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).newHandler((in, out) -> {
in.receive().asString(StandardCharsets.UTF_8).take(2).reduceWith(String::new, String::concat).log("-----------------CLIENT2").subscribe(m2::onNext);
return out.sendString(Mono.just("GOGOGO")).neverComplete();
}).block();
String client1Response = m1.block();
String client2Response = m2.block();
client1.dispose();
client1.onClose().block();
client2.dispose();
client2.onClose().block();
context.dispose();
context.onClose().block();
Assertions.assertThat(client1Response).isEqualTo("NOPE");
Assertions.assertThat(client2Response).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");
}
Aggregations