use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method retryStrategiesWhenServerFails.
@Test
public void retryStrategiesWhenServerFails() 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);
}
};
int elem = 10;
CountDownLatch latch = new CountDownLatch(elem);
final AtomicInteger j = new AtomicInteger();
NettyContext server = TcpServer.create("localhost").newHandler((in, out) -> out.sendGroups(in.receive().asString().map(jsonDecoder).map(d -> Flux.fromArray(d).doOnNext(pojo -> {
if (j.getAndIncrement() < 2) {
throw new RuntimeException("test");
}
}).retry(2).collectList().map(jsonEncoder)).doOnComplete(() -> System.out.println("wow")).log("flatmap-retry"))).block(Duration.ofSeconds(30));
NettyContext client = TcpClient.create(ops -> ops.connectAddress(() -> server.address())).newHandler((in, out) -> {
in.receive().asString().map(jsonDecoder).concatMap(d -> Flux.fromArray(d)).log("receive").subscribe(c -> latch.countDown());
return out.send(Flux.range(1, elem).map(i -> new Pojo("test" + i)).log("send").collectList().map(jsonEncoder)).neverComplete();
}).block(Duration.ofSeconds(30));
Assertions.assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
Assertions.assertThat(latch.getCount()).isEqualTo(0);
server.dispose();
client.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class UdpServerTests method supportsReceivingDatagrams.
@Test
@Ignore
public void supportsReceivingDatagrams() throws InterruptedException {
final int port = SocketUtils.findAvailableUdpPort();
final CountDownLatch latch = new CountDownLatch(4);
final NettyContext server = UdpServer.create(port).newHandler((in, out) -> {
in.receive().asByteArray().log().subscribe(bytes -> {
if (bytes.length == 1024) {
latch.countDown();
}
});
return Flux.never();
}).doOnSuccess(v -> {
try {
DatagramChannel udp = DatagramChannel.open();
udp.configureBlocking(true);
udp.connect(new InetSocketAddress(InetAddress.getLocalHost(), port));
byte[] data = new byte[1024];
new Random().nextBytes(data);
for (int i = 0; i < 4; i++) {
udp.write(ByteBuffer.wrap(data));
}
udp.close();
} catch (IOException e) {
e.printStackTrace();
}
}).block(Duration.ofSeconds(30));
assertThat("latch was counted down", latch.await(10, TimeUnit.SECONDS));
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpServerTests method releaseInboundChannelOnNonKeepAliveRequest.
@Test
public void releaseInboundChannelOnNonKeepAliveRequest() throws Exception {
NettyContext c = HttpServer.create(0).newHandler((req, resp) -> req.receive().then(resp.status(200).send())).block();
Flux<ByteBuf> src = Flux.range(0, 3).map(n -> Unpooled.wrappedBuffer(Integer.toString(n).getBytes()));
Flux.range(0, 100).concatMap(n -> HttpClient.create(c.address().getPort()).post("/return", r -> r.keepAlive(false).send(src)).map(resp -> {
resp.dispose();
return resp.status().code();
}).log()).collectList().block();
c.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpServerTests method nonContentStatusCodes.
@Test
public void nonContentStatusCodes() {
NettyContext server = HttpServer.create(ops -> ops.host("localhost")).newRouter(r -> r.get("/204-1", (req, res) -> res.status(HttpResponseStatus.NO_CONTENT).sendHeaders()).get("/204-2", (req, res) -> res.status(HttpResponseStatus.NO_CONTENT)).get("/205-1", (req, res) -> res.status(HttpResponseStatus.RESET_CONTENT).sendHeaders()).get("/205-2", (req, res) -> res.status(HttpResponseStatus.RESET_CONTENT)).get("/304-1", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED).sendHeaders()).get("/304-2", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED))).block(Duration.ofSeconds(30));
checkResponse("/204-1", server.address());
checkResponse("/204-2", server.address());
checkResponse("/205-1", server.address());
checkResponse("/205-2", server.address());
checkResponse("/304-1", server.address());
checkResponse("/304-2", server.address());
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpServerTests method keepAlive.
@Test
public void keepAlive() throws URISyntaxException {
Path resource = Paths.get(getClass().getResource("/public").toURI());
NettyContext c = HttpServer.create(0).newRouter(routes -> routes.directory("/test", resource)).block(Duration.ofSeconds(30));
HttpResources.set(PoolResources.fixed("http", 1));
HttpClientResponse response0 = HttpClient.create(c.address().getPort()).get("/test/index.html").block(Duration.ofSeconds(30));
HttpClientResponse response1 = HttpClient.create(c.address().getPort()).get("/test/test.css").block(Duration.ofSeconds(30));
HttpClientResponse response2 = HttpClient.create(c.address().getPort()).get("/test/test1.css").block(Duration.ofSeconds(30));
HttpClientResponse response3 = HttpClient.create(c.address().getPort()).get("/test/test2.css").block(Duration.ofSeconds(30));
HttpClientResponse response4 = HttpClient.create(c.address().getPort()).get("/test/test3.css").block(Duration.ofSeconds(30));
HttpClientResponse response5 = HttpClient.create(c.address().getPort()).get("/test/test4.css").block(Duration.ofSeconds(30));
HttpClientResponse response6 = HttpClient.create(opts -> opts.port(c.address().getPort()).disablePool()).get("/test/test5.css").block(Duration.ofSeconds(30));
Assert.assertEquals(response0.channel(), response1.channel());
Assert.assertEquals(response0.channel(), response2.channel());
Assert.assertEquals(response0.channel(), response3.channel());
Assert.assertEquals(response0.channel(), response4.channel());
Assert.assertEquals(response0.channel(), response5.channel());
Assert.assertNotEquals(response0.channel(), response6.channel());
HttpResources.reset();
response0.dispose();
response1.dispose();
response2.dispose();
response3.dispose();
response4.dispose();
response5.dispose();
response6.dispose();
c.dispose();
}
Aggregations