Search in sources :

Example 71 with DisposableServer

use of reactor.netty.DisposableServer in project reactor-netty by reactor.

the class TcpClientTests method doTestIssue600.

private void doTestIssue600(boolean withLoop) {
    DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain().delaySubscription(Duration.ofSeconds(1)))).wiretap(true).bindNow();
    ConnectionProvider pool = ConnectionProvider.create("doTestIssue600", 10);
    LoopResources loop = LoopResources.create("test", 4, true);
    TcpClient client;
    if (withLoop) {
        client = TcpClient.create(pool).remoteAddress(server::address).runOn(loop);
    } else {
        client = TcpClient.create(pool).remoteAddress(server::address);
    }
    Set<String> threadNames = new ConcurrentSkipListSet<>();
    Flux.range(1, 4).flatMap(i -> client.handle((in, out) -> {
        threadNames.add(Thread.currentThread().getName());
        return out.send(Flux.empty());
    }).connect()).as(StepVerifier::create).expectNextCount(4).expectComplete().verify(Duration.ofSeconds(30));
    pool.dispose();
    loop.dispose();
    server.disposeNow();
    assertThat(threadNames.size()).isGreaterThan(1);
}
Also used : DisposableServer(reactor.netty.DisposableServer) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) LoopResources(reactor.netty.resources.LoopResources) ConnectionProvider(reactor.netty.resources.ConnectionProvider)

Example 72 with DisposableServer

use of reactor.netty.DisposableServer 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();
}
Also used : DisposableServer(reactor.netty.DisposableServer) NettyOutbound(reactor.netty.NettyOutbound) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 73 with DisposableServer

use of reactor.netty.DisposableServer in project reactor-netty by reactor.

the class TcpClientTests method testBootstrap.

@Test
@SuppressWarnings("deprecation")
void testBootstrap() {
    DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain())).wiretap(true).bindNow();
    AtomicInteger invoked = new AtomicInteger();
    Connection conn = TcpClient.create().bootstrap(b -> b.attr(AttributeKey.valueOf("testBootstrap"), "testBootstrap").group(new NioEventLoopGroup()).option(ChannelOption.valueOf("testBootstrap"), "testBootstrap").remoteAddress(server.address()).resolver(DefaultAddressResolverGroup.INSTANCE).handler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            invoked.set(1);
            super.channelActive(ctx);
        }
    })).connectNow();
    conn.outbound().sendString(Mono.just("testBootstrap")).then().subscribe();
    String result = conn.inbound().receive().asString().blockFirst();
    assertThat(result).isEqualTo("testBootstrap");
    assertThat(invoked.get()).isEqualTo(1);
    conn.disposeNow();
    server.disposeNow();
}
Also used : AttributeKey(io.netty.util.AttributeKey) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) SocketAddress(java.net.SocketAddress) AbortedException(reactor.netty.channel.AbortedException) Retry(reactor.util.retry.Retry) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Future(java.util.concurrent.Future) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Duration(java.time.Duration) Logger(reactor.util.Logger) NameResolverProvider(reactor.netty.transport.NameResolverProvider) LoopResources(reactor.netty.resources.LoopResources) ChannelOperations(reactor.netty.channel.ChannelOperations) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyOutbound(reactor.netty.NettyOutbound) DisposableServer(reactor.netty.DisposableServer) ChannelOption(io.netty.channel.ChannelOption) SocketUtils(reactor.netty.SocketUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) Properties(java.util.Properties) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) Publisher(org.reactivestreams.Publisher) NetUtil(io.netty.util.NetUtil) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Bootstrap(io.netty.bootstrap.Bootstrap) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(reactor.netty.Connection) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) AbortedException(reactor.netty.channel.AbortedException) IOException(java.io.IOException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 74 with DisposableServer

use of reactor.netty.DisposableServer in project reactor-netty by reactor.

the class TcpClientTests method testAddressSupplier.

@Test
@SuppressWarnings("deprecation")
void testAddressSupplier() {
    DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain())).wiretap(true).bindNow();
    Connection conn = TcpClient.create().addressSupplier(server::address).connectNow();
    conn.outbound().sendString(Mono.just("testAddressSupplier")).then().subscribe();
    String result = conn.inbound().receive().asString().blockFirst();
    assertThat(result).isEqualTo("testAddressSupplier");
    conn.disposeNow();
    server.disposeNow();
}
Also used : DisposableServer(reactor.netty.DisposableServer) Connection(reactor.netty.Connection) Test(org.junit.jupiter.api.Test)

Example 75 with DisposableServer

use of reactor.netty.DisposableServer 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();
}
Also used : AttributeKey(io.netty.util.AttributeKey) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) SocketAddress(java.net.SocketAddress) AbortedException(reactor.netty.channel.AbortedException) Retry(reactor.util.retry.Retry) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Future(java.util.concurrent.Future) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Duration(java.time.Duration) Logger(reactor.util.Logger) NameResolverProvider(reactor.netty.transport.NameResolverProvider) LoopResources(reactor.netty.resources.LoopResources) ChannelOperations(reactor.netty.channel.ChannelOperations) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyOutbound(reactor.netty.NettyOutbound) DisposableServer(reactor.netty.DisposableServer) ChannelOption(io.netty.channel.ChannelOption) SocketUtils(reactor.netty.SocketUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) Properties(java.util.Properties) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) Publisher(org.reactivestreams.Publisher) NetUtil(io.netty.util.NetUtil) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Bootstrap(io.netty.bootstrap.Bootstrap) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) NettyOutbound(reactor.netty.NettyOutbound) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Aggregations

DisposableServer (reactor.netty.DisposableServer)137 Mono (reactor.core.publisher.Mono)84 Test (org.junit.jupiter.api.Test)79 Duration (java.time.Duration)71 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)71 AtomicReference (java.util.concurrent.atomic.AtomicReference)68 Flux (reactor.core.publisher.Flux)68 Connection (reactor.netty.Connection)67 TimeUnit (java.util.concurrent.TimeUnit)65 LoopResources (reactor.netty.resources.LoopResources)60 List (java.util.List)58 CountDownLatch (java.util.concurrent.CountDownLatch)57 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)56 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)55 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)54 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)54 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)53 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)53 Assumptions.assumeThat (org.assertj.core.api.Assumptions.assumeThat)53 StepVerifier (reactor.test.StepVerifier)53