use of reactor.netty.NettyOutbound 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.NettyOutbound 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();
}
use of reactor.netty.NettyOutbound in project reactor-netty by reactor.
the class TcpClientTests method testIssue585_2.
@Test
void testIssue585_2() throws Exception {
DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain())).wiretap(true).bindNow();
byte[] bytes = "test".getBytes(Charset.defaultCharset());
ByteBuf b1 = Unpooled.wrappedBuffer(bytes);
ByteBuf b2 = Unpooled.wrappedBuffer(bytes);
ByteBuf b3 = Unpooled.wrappedBuffer(bytes);
WeakReference<ByteBuf> refCheck1 = new WeakReference<>(b1);
WeakReference<ByteBuf> refCheck2 = new WeakReference<>(b2);
WeakReference<ByteBuf> refCheck3 = new WeakReference<>(b3);
Connection conn = TcpClient.create().remoteAddress(server::address).wiretap(true).connectNow();
NettyOutbound out = conn.outbound();
out.sendObject(b1).then().block(Duration.ofSeconds(30));
assertThat(b1.refCnt()).isEqualTo(0);
b1 = null;
checkReference(refCheck1);
out.sendObject(b2).then().block(Duration.ofSeconds(30));
assertThat(b2.refCnt()).isEqualTo(0);
b2 = null;
checkReference(refCheck2);
out.sendObject(b3).then().block(Duration.ofSeconds(30));
assertThat(b3.refCnt()).isEqualTo(0);
b3 = null;
checkReference(refCheck3);
server.disposeNow();
conn.disposeNow();
}
use of reactor.netty.NettyOutbound in project reactor-netty by reactor.
the class TcpClientTests method testIssue585_1.
@Test
void testIssue585_1() throws Exception {
DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain())).wiretap(true).bindNow();
CountDownLatch latch = new CountDownLatch(1);
byte[] bytes = "test".getBytes(Charset.defaultCharset());
ByteBuf b1 = Unpooled.wrappedBuffer(bytes);
ByteBuf b2 = Unpooled.wrappedBuffer(bytes);
ByteBuf b3 = Unpooled.wrappedBuffer(bytes);
WeakReference<ByteBuf> refCheck1 = new WeakReference<>(b1);
WeakReference<ByteBuf> refCheck2 = new WeakReference<>(b2);
WeakReference<ByteBuf> refCheck3 = new WeakReference<>(b3);
Connection conn = TcpClient.create().remoteAddress(server::address).wiretap(true).connectNow();
NettyOutbound out = conn.outbound();
Flux.concatDelayError(out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b1).then(), out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b2).then(), out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b3).then()).doOnError(t -> latch.countDown()).subscribe(conn.disposeSubscriber());
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
assertThat(b1.refCnt()).isEqualTo(0);
b1 = null;
checkReference(refCheck1);
assertThat(b2.refCnt()).isEqualTo(0);
b2 = null;
checkReference(refCheck2);
assertThat(b3.refCnt()).isEqualTo(0);
b3 = null;
checkReference(refCheck3);
server.disposeNow();
conn.disposeNow();
}
use of reactor.netty.NettyOutbound in project reactor-netty by reactor.
the class DefaultHttpServerRoutesTest method directoryRouteTest.
@Test
void directoryRouteTest() throws URISyntaxException {
HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
Mockito.when(request.paramsResolver(Mockito.any())).thenReturn(request);
Mockito.when(request.uri()).thenReturn("/test");
Mockito.when(request.method()).thenReturn(HttpMethod.GET);
Subscription subscription = Mockito.mock(Subscription.class);
NettyOutbound outbound = Mockito.mock(NettyOutbound.class);
Mockito.doAnswer(invocation -> {
Subscriber<Void> subscriber = invocation.getArgument(0);
subscriber.onSubscribe(subscription);
subscriber.onNext(null);
subscriber.onComplete();
return null;
}).when(outbound).subscribe(Mockito.any());
HttpServerResponse response = Mockito.mock(HttpServerResponse.class);
Mockito.when(response.sendFile(Mockito.any())).thenReturn(outbound);
Path resource = Paths.get(getClass().getResource("/public").toURI());
DefaultHttpServerRoutes routes = new DefaultHttpServerRoutes();
HttpServerRoutes route = routes.directory("/test", resource);
Publisher<Void> publisher = route.apply(request, response);
assertThat(publisher).isNotNull();
StepVerifier.create(publisher).expectNextMatches(p -> true).expectComplete().verify(Duration.ofMillis(200));
}
Aggregations