use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class NettyOptionsTest method afterChannelInitThenChannelGroup.
@Test
public void afterChannelInitThenChannelGroup() {
ChannelGroup group = new DefaultChannelGroup(null);
List<Channel> initializedChannels = new ArrayList<>();
NettyContext nettyContext = HttpServer.create(opt -> opt.afterChannelInit(initializedChannels::add).channelGroup(group)).start((req, resp) -> resp.sendNotFound()).getContext();
HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
assertThat((Iterable<Channel>) group).hasSize(1).hasSameElementsAs(initializedChannels).doesNotContain(nettyContext.channel());
resp.dispose();
nettyContext.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class NettyOptionsTest method afterNettyContextInit.
@Test
public void afterNettyContextInit() {
AtomicInteger readCount = new AtomicInteger();
ChannelInboundHandlerAdapter handler = new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
readCount.incrementAndGet();
super.channelRead(ctx, msg);
}
};
String handlerName = "test";
NettyContext nettyContext = HttpServer.create(opt -> opt.afterNettyContextInit(c -> c.addHandlerFirst(handlerName, handler))).start((req, resp) -> resp.sendNotFound()).getContext();
HttpClientResponse response1 = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
assertThat(response1.status().code()).isEqualTo(404);
response1.dispose();
// the "main" context doesn't get enriched with handlers from options...
assertThat(nettyContext.channel().pipeline().names()).doesNotContain(handlerName);
// ...but the child channels that are created for requests are
assertThat(readCount.get()).isEqualTo(1);
HttpClientResponse response2 = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
// reactor handler was applied and produced a response
assertThat(response2.status().code()).isEqualTo(404);
response2.dispose();
// BUT channelHandler wasn't applied a second time since not Shareable
assertThat(readCount.get()).isEqualTo(1);
nettyContext.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpClientTests method testTcpClientWithInetSocketAddress.
@Test
public void testTcpClientWithInetSocketAddress() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
TcpClient client = TcpClient.create(echoServerPort);
NettyContext s = client.newHandler((in, out) -> {
in.receive().subscribe(d -> latch.countDown());
return out.sendString(Flux.just("Hello")).neverComplete();
}).block(Duration.ofSeconds(5));
latch.await(5, TimeUnit.SECONDS);
s.dispose();
assertThat("latch was counted down", latch.getCount(), is(0L));
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpClientTests method testTcpClient.
@Test
public void testTcpClient() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
NettyContext client = TcpClient.create("localhost", echoServerPort).newHandler((in, out) -> {
in.receive().log("conn").subscribe(s -> latch.countDown());
return out.sendString(Flux.just("Hello World!")).neverComplete();
}).block(Duration.ofSeconds(30));
latch.await(30, TimeUnit.SECONDS);
client.dispose();
assertThat("latch was counted down", latch.getCount(), is(0L));
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class TcpServerTests method tcpServerCanEncodeAndDecodeJSON.
@Test
public void tcpServerCanEncodeAndDecodeJSON() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Function<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(1);
NettyContext server = TcpServer.create().newHandler((in, out) -> out.send(in.receive().asString().map(jsonDecoder).log().take(1).map(pojo -> {
Assertions.assertThat(pojo.getName()).isEqualTo("John Doe");
return new Pojo("Jane Doe");
}).map(jsonEncoder))).block(Duration.ofSeconds(30));
SimpleClient client = new SimpleClient(server.address().getPort(), dataLatch, "{\"name\":\"John Doe\"}");
client.start();
Assertions.assertThat(dataLatch.await(5, TimeUnit.SECONDS)).isTrue();
Assertions.assertThat(dataLatch.getCount()).isEqualTo(0);
Assertions.assertThat(client.e).isNull();
Assertions.assertThat(client.data).isNotNull();
Assertions.assertThat(client.data.remaining()).isEqualTo(19);
Assertions.assertThat(new String(client.data.array())).isEqualTo("{\"name\":\"Jane Doe\"}");
server.dispose();
}
Aggregations