Search in sources :

Example 36 with NettyContext

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();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpClient(reactor.ipc.netty.http.client.HttpClient) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 37 with NettyContext

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();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) NettyContext(reactor.ipc.netty.NettyContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpClient(reactor.ipc.netty.http.client.HttpClient) Test(org.junit.Test)

Example 38 with NettyContext

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);
}
Also used : Flux(reactor.core.publisher.Flux) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Random(java.util.Random) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ResourceLeakDetector(io.netty.util.ResourceLeakDetector) Level(io.netty.util.ResourceLeakDetector.Level) HttpServer(reactor.ipc.netty.http.server.HttpServer) Random(java.util.Random) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 39 with NettyContext

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();
}
Also used : Loggers(reactor.util.Loggers) Duration(java.time.Duration) Logger(reactor.util.Logger) NettyContext(reactor.ipc.netty.NettyContext) TimeoutException(java.util.concurrent.TimeoutException) Mono(reactor.core.publisher.Mono) InetSocketAddress(java.net.InetSocketAddress) TimeoutException(java.util.concurrent.TimeoutException)

Example 40 with NettyContext

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();
}
Also used : ReactorHttpHandlerAdapter(org.springframework.http.server.reactive.ReactorHttpHandlerAdapter) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) POST(org.springframework.web.reactive.function.server.RequestPredicates.POST) Mono(reactor.core.publisher.Mono) AfterAll(org.junit.jupiter.api.AfterAll) Flux(reactor.core.publisher.Flux) HttpHandler(org.springframework.http.server.reactive.HttpHandler) BeforeAll(org.junit.jupiter.api.BeforeAll) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) Duration(java.time.Duration) Task(com.baeldung.web.reactive.Task) NettyContext(reactor.ipc.netty.NettyContext) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Task(com.baeldung.web.reactive.Task) HttpServer(reactor.ipc.netty.http.server.HttpServer) ReactorHttpHandlerAdapter(org.springframework.http.server.reactive.ReactorHttpHandlerAdapter) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

NettyContext (reactor.ipc.netty.NettyContext)92 Test (org.junit.Test)87 Mono (reactor.core.publisher.Mono)86 Duration (java.time.Duration)83 Flux (reactor.core.publisher.Flux)78 InetSocketAddress (java.net.InetSocketAddress)65 HttpServer (reactor.ipc.netty.http.server.HttpServer)63 HttpClient (reactor.ipc.netty.http.client.HttpClient)61 CountDownLatch (java.util.concurrent.CountDownLatch)60 StepVerifier (reactor.test.StepVerifier)60 AtomicReference (java.util.concurrent.atomic.AtomicReference)56 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)53 Unpooled (io.netty.buffer.Unpooled)50 TimeUnit (java.util.concurrent.TimeUnit)44 StandardCharsets (java.nio.charset.StandardCharsets)43 Ignore (org.junit.Ignore)43 SslContext (io.netty.handler.ssl.SslContext)42 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)42 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)42 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)42