use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpServerTests method doTestSendFileAsync.
private void doTestSendFileAsync(int chunk) throws IOException, URISyntaxException {
Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
Path tempFile = Files.createTempFile(largeFile.getParent(), "temp", ".txt");
tempFile.toFile().deleteOnExit();
byte[] fileBytes = Files.readAllBytes(largeFile);
for (int i = 0; i < 1000; i++) {
Files.write(tempFile, fileBytes, StandardOpenOption.APPEND);
}
ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
AsynchronousFileChannel channel = AsynchronousFileChannel.open(tempFile, StandardOpenOption.READ);
Flux<ByteBuf> content = Flux.create(fluxSink -> {
fluxSink.onDispose(() -> {
try {
if (channel != null) {
channel.close();
}
} catch (IOException ignored) {
}
});
ByteBuffer buf = ByteBuffer.allocate(chunk);
channel.read(buf, 0, buf, new TestCompletionHandler(channel, fluxSink, allocator, chunk));
});
NettyContext context = HttpServer.create(opt -> opt.host("localhost")).newHandler((req, resp) -> resp.sendByteArray(req.receive().aggregate().asByteArray())).block();
byte[] response = HttpClient.create(opt -> opt.connectAddress(() -> context.address())).request(HttpMethod.POST, "/", req -> req.send(content).then()).flatMap(res -> res.receive().aggregate().asByteArray()).block();
assertThat(response).isEqualTo(Files.readAllBytes(tempFile));
context.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpServerTests method httpPipelining.
@Test
public void httpPipelining() throws Exception {
AtomicInteger i = new AtomicInteger();
NettyContext server = HttpServer.create(0).newHandler((req, resp) -> resp.header(HttpHeaderNames.CONTENT_LENGTH, "1").sendString(Mono.just(i.incrementAndGet()).flatMap(d -> Mono.delay(Duration.ofSeconds(4 - d)).map(x -> d + "\n")))).block(Duration.ofSeconds(30));
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/plaintext");
CountDownLatch latch = new CountDownLatch(6);
NettyContext client = TcpClient.create(server.address().getPort()).newHandler((in, out) -> {
in.context().addHandlerFirst(new HttpClientCodec());
in.receiveObject().ofType(DefaultHttpContent.class).as(ByteBufFlux::fromInbound).asString().log().map(Integer::parseInt).subscribe(d -> {
for (int x = 0; x < d; x++) {
latch.countDown();
}
});
return out.sendObject(Flux.just(request.retain(), request.retain(), request.retain())).neverComplete();
}).block(Duration.ofSeconds(30));
Assert.assertTrue(latch.await(45, TimeUnit.SECONDS));
server.dispose();
client.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpServerTests method testConnectionCloseOnServerError.
@Test
public void testConnectionCloseOnServerError() throws Exception {
Flux<String> content = Flux.range(1, 3).doOnNext(i -> {
if (i == 3) {
throw new RuntimeException("test");
}
}).map(i -> "foo " + i);
NettyContext server = HttpServer.create(0).newHandler((req, res) -> res.sendString(content)).block(Duration.ofSeconds(30));
HttpClientResponse r = HttpClient.create(ops -> ops.port(server.address().getPort())).get("/").block(Duration.ofSeconds(30));
ByteBufFlux response = r.receive();
StepVerifier.create(response).expectNextCount(2).expectError(IOException.class).verify(Duration.ofSeconds(30));
FutureMono.from(r.context().channel().closeFuture()).block(Duration.ofSeconds(30));
r.dispose();
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpServerTests method contextShouldBeTransferredFromDownStreamToUpsream.
@Test
public void contextShouldBeTransferredFromDownStreamToUpsream() {
AtomicReference<Context> context = new AtomicReference<>();
NettyContext server = HttpServer.create(0).newHandler((req, res) -> res.status(200).send()).block(Duration.ofSeconds(300));
HttpClient client = HttpClient.create(ops -> ops.connectAddress(() -> server.address()).poolResources(PoolResources.fixed("test", 1)));
try {
Mono<String> content = client.post("/", req -> req.failOnClientError(false).sendString(Mono.just("bodysample").subscriberContext(c -> {
context.set(c);
return c;
}))).flatMap(res -> res.receive().aggregate().asString()).subscriberContext(c -> c.put("Hello", "World"));
StepVerifier.create(content).expectComplete().verify(Duration.ofSeconds(300));
assertThat(context.get().get("Hello").equals("World")).isTrue();
} finally {
server.dispose();
}
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class NettyOptionsTest method afterChannelInitAfterChannelGroup.
@Test
public void afterChannelInitAfterChannelGroup() {
// this test only differs from afterChannelInitThenChannelGroup in the order of the options
ChannelGroup group = new DefaultChannelGroup(null);
List<Channel> initializedChannels = new ArrayList<>();
NettyContext nettyContext = HttpServer.create(opt -> opt.channelGroup(group).afterChannelInit(initializedChannels::add)).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();
}
Aggregations