Search in sources :

Example 1 with NettyContext

use of reactor.ipc.netty.NettyContext in project spring-framework by spring-projects.

the class ReactorHttpServer method startInternal.

@Override
protected void startInternal() {
    NettyContext nettyContext = this.reactorServer.newHandler(this.reactorHandler).block();
    setPort(nettyContext.address().getPort());
    this.nettyContext.set(nettyContext);
}
Also used : NettyContext(reactor.ipc.netty.NettyContext)

Example 2 with NettyContext

use of reactor.ipc.netty.NettyContext in project spring-boot by spring-projects.

the class NettyWebServer method stop.

@Override
public void stop() throws WebServerException {
    NettyContext context = this.nettyContext.getAndSet(null);
    if (context != null) {
        context.dispose();
    }
    latch.countDown();
}
Also used : NettyContext(reactor.ipc.netty.NettyContext)

Example 3 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class TcpClientTests method consumerSpecAssignsEventHandlers.

@Test
public void consumerSpecAssignsEventHandlers() throws InterruptedException, IOException {
    final CountDownLatch latch = new CountDownLatch(2);
    final CountDownLatch close = new CountDownLatch(1);
    final AtomicLong totalDelay = new AtomicLong();
    final long start = System.currentTimeMillis();
    TcpClient client = TcpClient.create(opts -> opts.host("localhost").port(timeoutServerPort));
    NettyContext s = client.newHandler((in, out) -> {
        in.onReadIdle(500, () -> {
            totalDelay.addAndGet(System.currentTimeMillis() - start);
            latch.countDown();
        }).context().onClose(close::countDown);
        out.onWriteIdle(500, () -> {
            totalDelay.addAndGet(System.currentTimeMillis() - start);
            latch.countDown();
        });
        return Mono.delay(Duration.ofSeconds(3)).then().log();
    }).block(Duration.ofSeconds(30));
    assertTrue("latch was counted down", latch.await(5, TimeUnit.SECONDS));
    assertTrue("close was counted down", close.await(30, TimeUnit.SECONDS));
    assertThat("totalDelay was >500ms", totalDelay.get(), greaterThanOrEqualTo(500L));
    s.dispose();
}
Also used : Arrays(java.util.Arrays) ChannelOption(io.netty.channel.ChannelOption) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) SocketChannel(java.nio.channels.SocketChannel) NettyPipeline(reactor.ipc.netty.NettyPipeline) Duration(java.time.Duration) After(org.junit.After) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Publisher(org.reactivestreams.Publisher) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) List(java.util.List) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyContext(reactor.ipc.netty.NettyContext) ClientOptions(reactor.ipc.netty.options.ClientOptions) Matchers.is(org.hamcrest.Matchers.is) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) CountDownLatch(java.util.concurrent.CountDownLatch) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 4 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class TcpClientTests method readIdleDoesNotFireWhileDataIsBeingRead.

@Test
public void readIdleDoesNotFireWhileDataIsBeingRead() throws InterruptedException, IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    long start = System.currentTimeMillis();
    TcpClient client = TcpClient.create("localhost", heartbeatServerPort);
    NettyContext s = client.newHandler((in, out) -> {
        in.onReadIdle(500, latch::countDown);
        return Flux.never();
    }).block(Duration.ofSeconds(30));
    assertTrue(latch.await(15, TimeUnit.SECONDS));
    heartbeatServer.close();
    long duration = System.currentTimeMillis() - start;
    assertThat(duration, is(greaterThanOrEqualTo(500L)));
    s.dispose();
}
Also used : Arrays(java.util.Arrays) ChannelOption(io.netty.channel.ChannelOption) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) SocketChannel(java.nio.channels.SocketChannel) NettyPipeline(reactor.ipc.netty.NettyPipeline) Duration(java.time.Duration) After(org.junit.After) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Publisher(org.reactivestreams.Publisher) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) List(java.util.List) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyContext(reactor.ipc.netty.NettyContext) ClientOptions(reactor.ipc.netty.options.ClientOptions) Matchers.is(org.hamcrest.Matchers.is) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert.assertEquals(org.junit.Assert.assertEquals) CountDownLatch(java.util.concurrent.CountDownLatch) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 5 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class TcpClientTests method writeIdleDoesNotFireWhileDataIsBeingSent.

@Test
public void writeIdleDoesNotFireWhileDataIsBeingSent() throws InterruptedException, IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    long start = System.currentTimeMillis();
    NettyContext client = TcpClient.create("localhost", echoServerPort).newHandler((in, out) -> {
        System.out.println("hello");
        out.onWriteIdle(500, latch::countDown);
        List<Publisher<Void>> allWrites = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            allWrites.add(out.sendString(Flux.just("a").delayElements(Duration.ofMillis(750))));
        }
        return Flux.merge(allWrites);
    }).block(Duration.ofSeconds(30));
    System.out.println("Started");
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    long duration = System.currentTimeMillis() - start;
    assertThat(duration, is(greaterThanOrEqualTo(500l)));
    client.dispose();
}
Also used : Arrays(java.util.Arrays) ChannelOption(io.netty.channel.ChannelOption) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) SocketChannel(java.nio.channels.SocketChannel) NettyPipeline(reactor.ipc.netty.NettyPipeline) Duration(java.time.Duration) After(org.junit.After) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Publisher(org.reactivestreams.Publisher) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) List(java.util.List) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyContext(reactor.ipc.netty.NettyContext) ClientOptions(reactor.ipc.netty.options.ClientOptions) Matchers.is(org.hamcrest.Matchers.is) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

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