use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpTests method httpRespondsToRequestsFromClients.
@Test
public void httpRespondsToRequestsFromClients() {
NettyContext server = HttpServer.create(0).newRouter(r -> r.post("/test/{param}", (req, res) -> res.sendString(req.receive().asString().log("server-received").map(it -> it + ' ' + req.param("param") + '!').log("server-reply")))).block(Duration.ofSeconds(30));
HttpClient client = HttpClient.create("localhost", server.address().getPort());
Mono<String> content = client.post("/test/World", req -> req.header("Content-Type", "text/plain").sendString(Flux.just("Hello").log("client-send"))).flatMap(res -> res.receive().aggregate().asString().log("client-received")).doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
StepVerifier.create(content).expectNextMatches(s -> s.equals("Hello World!")).expectComplete().verify(Duration.ofSeconds(5000));
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpTests method httpErrorWithRequestsFromClients.
@Test
public void httpErrorWithRequestsFromClients() throws Exception {
CountDownLatch errored1 = new CountDownLatch(1);
CountDownLatch errored2 = new CountDownLatch(1);
CountDownLatch errored3 = new CountDownLatch(1);
CountDownLatch errored4 = new CountDownLatch(1);
CountDownLatch errored5 = new CountDownLatch(1);
Flux<ByteBuf> flux1 = Flux.range(0, 257).flatMap(i -> {
if (i == 4) {
throw new RuntimeException("test");
}
return Mono.just(Unpooled.copyInt(i));
});
Flux<ByteBuf> flux2 = Flux.range(0, 257).flatMap(i -> {
if (i == 4) {
return Mono.error(new Exception("test"));
}
return Mono.just(Unpooled.copyInt(i));
});
NettyContext server = HttpServer.create(0).newRouter(r -> r.get("/test", (req, res) -> {
throw new RuntimeException();
}).get("/test2", (req, res) -> res.send(Flux.error(new Exception())).then().log("send-1").doOnError(t -> errored1.countDown())).get("/test3", (req, res) -> Flux.error(new Exception())).get("/issue231_1", (req, res) -> res.send(flux1).then().log("send-2").doOnError(t -> errored2.countDown())).get("/issue231_2", (req, res) -> res.send(flux2).then().log("send-3").doOnError(t -> errored3.countDown())).get("/issue237_1", (req, res) -> res.send(flux1).then().log("send-4").doOnError(t -> errored4.countDown())).get("/issue237_2", (req, res) -> res.send(flux2).then().log("send-5").doOnError(t -> errored5.countDown()))).block(Duration.ofSeconds(30));
HttpClient client = HttpClient.create("localhost", server.address().getPort());
Mono<Integer> code = client.get("/test").flatMap(res -> {
res.dispose();
return Mono.just(res.status().code());
}).log("received-status-1");
StepVerifier.create(code).expectError(HttpClientException.class).verify(Duration.ofSeconds(30));
Mono<ByteBuf> content = client.get("/test2").flatMapMany(res -> res.receive().log("received-status-2")).next();
StepVerifier.create(content).expectError(IOException.class).verify(Duration.ofSeconds(30));
Assertions.assertThat(errored1.await(30, TimeUnit.SECONDS)).isTrue();
ByteBuf content1 = client.get("/issue231_1").flatMapMany(res -> res.receive().log("received-status-4")).next().block(Duration.ofSeconds(30));
Assertions.assertThat(errored2.await(30, TimeUnit.SECONDS)).isTrue();
content1 = client.get("/issue231_2").flatMapMany(res -> res.receive().log("received-status-4")).next().block(Duration.ofSeconds(30));
Assertions.assertThat(errored3.await(30, TimeUnit.SECONDS)).isTrue();
Flux<ByteBuf> content2 = client.get("/issue237_1").flatMapMany(res -> res.receive().log("received-status-5"));
StepVerifier.create(content2).expectNextCount(4).expectError(IOException.class).verify(Duration.ofSeconds(30));
Assertions.assertThat(errored4.await(30, TimeUnit.SECONDS)).isTrue();
content2 = client.get("/issue237_2").flatMapMany(res -> res.receive().log("received-status-6"));
StepVerifier.create(content2).expectNextCount(4).expectError(IOException.class).verify(Duration.ofSeconds(30));
Assertions.assertThat(errored5.await(30, TimeUnit.SECONDS)).isTrue();
code = client.get("/test3").flatMapMany(res -> {
res.dispose();
return Flux.just(res.status().code()).log("received-status-3");
}).next();
StepVerifier.create(code).expectError(HttpClientException.class).verify(Duration.ofSeconds(30));
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class FluxReceiveTest method testByteBufsReleasedWhenTimeout.
@Test
public void testByteBufsReleasedWhenTimeout() {
ResourceLeakDetector.setLevel(Level.PARANOID);
byte[] content = new byte[1024 * 8];
Random rndm = new Random();
rndm.nextBytes(content);
NettyContext server1 = HttpServer.create(0).newRouter(routes -> routes.get("/target", (req, res) -> res.sendByteArray(Flux.just(content).delayElements(Duration.ofMillis(100))))).block(Duration.ofSeconds(30));
NettyContext server2 = HttpServer.create(0).newRouter(routes -> routes.get("/forward", (req, res) -> HttpClient.create(server1.address().getPort()).get("/target").log().delayElement(Duration.ofMillis(50)).flatMap(response -> response.receive().aggregate().asString()).timeout(Duration.ofMillis(50)).then())).block(Duration.ofSeconds(30));
Flux.range(0, 50).flatMap(i -> HttpClient.create(server2.address().getPort()).get("/forward").log().onErrorResume(t -> Mono.empty())).blockLast(Duration.ofSeconds(30));
server1.dispose();
server2.dispose();
ResourceLeakDetector.setLevel(Level.SIMPLE);
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class BlockingNettyContext method shutdown.
/**
* Shut down the {@link NettyContext} and wait for its termination, up to the
* {@link #setLifecycleTimeout(Duration) lifecycle timeout}.
*/
public void shutdown() {
if (context.isDisposed()) {
return;
}
// only applies if not called from the hook's thread
removeShutdownHook();
context.dispose();
context.onClose().doOnError(e -> LOG.error("Stopped {} on {} with an error {}", description, context.address(), e)).doOnTerminate(() -> LOG.info("Stopped {} on {}", description, context.address())).timeout(lifecycleTimeout, Mono.error(new TimeoutException(description + " couldn't be stopped within " + lifecycleTimeout.toMillis() + "ms"))).block();
}
use of reactor.ipc.netty.NettyContext in project tutorials by eugenp.
the class Spring5ReactiveServerClientIntegrationTest method setUp.
@BeforeAll
public static void setUp() throws Exception {
HttpServer server = HttpServer.create("localhost", 8080);
RouterFunction<?> route = RouterFunctions.route(POST("/task/process"), request -> ServerResponse.ok().body(request.bodyToFlux(Task.class).map(ll -> new Task("TaskName", 1)), Task.class)).and(RouterFunctions.route(GET("/task"), request -> ServerResponse.ok().body(Mono.just("server is alive"), String.class)));
HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
nettyContext = server.newHandler(adapter).block();
}
Aggregations