Search in sources :

Example 61 with Connection

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

the class DefaultPooledConnectionProviderTest method testIssue673_TimeoutException.

@Test
void testIssue673_TimeoutException() throws InterruptedException {
    DisposableServer server = TcpServer.create().port(0).handle((in, out) -> out.sendString(Mono.just("test").delayElement(Duration.ofMillis(100)))).wiretap(true).bindNow();
    DefaultPooledConnectionProvider provider = (DefaultPooledConnectionProvider) ConnectionProvider.builder("testIssue673_TimeoutException").maxConnections(1).pendingAcquireMaxCount(4).pendingAcquireTimeout(Duration.ofMillis(10)).build();
    CountDownLatch latch = new CountDownLatch(2);
    try {
        AtomicReference<InstrumentedPool<PooledConnection>> pool = new AtomicReference<>();
        List<? extends Signal<? extends Connection>> list = Flux.range(0, 5).flatMapDelayError(i -> TcpClient.create(provider).port(server.port()).doOnConnected(conn -> {
            ConcurrentMap<PooledConnectionProvider.PoolKey, InstrumentedPool<PooledConnection>> pools = provider.channelPools;
            pool.set(pools.get(pools.keySet().toArray()[0]));
        }).doOnDisconnected(conn -> latch.countDown()).handle((in, out) -> in.receive().then()).wiretap(true).connect().materialize(), 256, 32).collectList().doFinally(fin -> latch.countDown()).block(Duration.ofSeconds(30));
        assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch 30s").isTrue();
        assertThat(list).isNotNull().hasSize(5);
        int onNext = 0;
        int onError = 0;
        String msg = "Pool#acquire(Duration) has been pending for more than the configured timeout of 10ms";
        for (int i = 0; i < 5; i++) {
            Signal<? extends Connection> signal = list.get(i);
            if (signal.isOnNext()) {
                onNext++;
            } else if (signal.getThrowable() instanceof TimeoutException && msg.equals(signal.getThrowable().getMessage())) {
                onError++;
            }
        }
        assertThat(onNext).isEqualTo(1);
        assertThat(onError).isEqualTo(4);
        assertThat(pool.get().metrics().acquiredSize()).as("currently acquired").isEqualTo(0);
        assertThat(pool.get().metrics().idleSize()).as("currently idle").isEqualTo(0);
    } finally {
        server.disposeNow();
        provider.dispose();
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) SocketAddress(java.net.SocketAddress) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) InstrumentedPool(reactor.pool.InstrumentedPool) TimeoutException(java.util.concurrent.TimeoutException) TcpClient(reactor.netty.tcp.TcpClient) ConnectionObserver(reactor.netty.ConnectionObserver) Future(java.util.concurrent.Future) PoolAcquirePendingLimitException(reactor.pool.PoolAcquirePendingLimitException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) NameResolverProvider(reactor.netty.transport.NameResolverProvider) PooledRef(reactor.pool.PooledRef) AddressResolver(io.netty.resolver.AddressResolver) Awaitility.await(org.awaitility.Awaitility.await) AddressUtils(reactor.netty.transport.AddressUtils) Signal(reactor.core.publisher.Signal) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SchedulerClock(reactor.scheduler.clock.SchedulerClock) LockSupport(java.util.concurrent.locks.LockSupport) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) DisposableServer(reactor.netty.DisposableServer) TcpClientTests(reactor.netty.tcp.TcpClientTests) ChannelOption(io.netty.channel.ChannelOption) LoggingHandler(io.netty.handler.logging.LoggingHandler) SocketUtils(reactor.netty.SocketUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) PooledConnection(reactor.netty.resources.DefaultPooledConnectionProvider.PooledConnection) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TcpResources(reactor.netty.tcp.TcpResources) ConnectException(java.net.ConnectException) Connection(reactor.netty.Connection) ClientTransportConfig(reactor.netty.transport.ClientTransportConfig) EventLoopGroup(io.netty.channel.EventLoopGroup) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) NetUtil(io.netty.util.NetUtil) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) ChannelMetricsRecorder(reactor.netty.channel.ChannelMetricsRecorder) TcpServer(reactor.netty.tcp.TcpServer) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) Collections(java.util.Collections) ConcurrentMap(java.util.concurrent.ConcurrentMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) PooledConnection(reactor.netty.resources.DefaultPooledConnectionProvider.PooledConnection) DisposableServer(reactor.netty.DisposableServer) InstrumentedPool(reactor.pool.InstrumentedPool) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 62 with Connection

use of reactor.netty.Connection 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 63 with Connection

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

the class TcpClientTests method testTcpClient.

@Test
void testTcpClient() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    Connection client = TcpClient.create().host("localhost").port(echoServerPort).handle((in, out) -> {
        in.receive().log("conn").subscribe(s -> latch.countDown());
        return out.sendString(Flux.just("Hello World!")).neverComplete();
    }).wiretap(true).connectNow();
    assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    client.disposeNow();
}
Also used : Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 64 with Connection

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

the class TcpClientTests method connectionWillAttemptToReconnectWhenItIsDropped.

@Test
void connectionWillAttemptToReconnectWhenItIsDropped() throws InterruptedException {
    final CountDownLatch connectionLatch = new CountDownLatch(1);
    final CountDownLatch reconnectionLatch = new CountDownLatch(1);
    try {
        TcpClient tcpClient = TcpClient.newConnection().host("localhost").port(abortServerPort);
        Mono<? extends Connection> handler = tcpClient.handle((in, out) -> {
            log.debug("Start");
            connectionLatch.countDown();
            in.receive().subscribe();
            return Flux.never();
        }).wiretap(true).connect();
        Connection c = handler.log().then(handler.doOnSuccess(s -> reconnectionLatch.countDown())).block(Duration.ofSeconds(30));
        assertThat(c).isNotNull();
        c.onDispose();
        assertThat(connectionLatch.await(5, TimeUnit.SECONDS)).as("Initial connection is made").isTrue();
        assertThat(reconnectionLatch.await(5, TimeUnit.SECONDS)).as("A reconnect attempt was made").isTrue();
    } catch (AbortedException e) {
    // ignored
    }
}
Also used : AbortedException(reactor.netty.channel.AbortedException) Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 65 with Connection

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

the class TcpClientTests method testTcpClientWithInetSocketAddress.

@Test
void testTcpClientWithInetSocketAddress() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    TcpClient client = TcpClient.create().port(echoServerPort);
    Connection s = client.handle((in, out) -> {
        in.receive().subscribe(d -> latch.countDown());
        return out.sendString(Flux.just("Hello")).neverComplete();
    }).wiretap(true).connectNow(Duration.ofSeconds(5));
    assertThat(latch.await(5, TimeUnit.SECONDS)).as("latch await").isTrue();
    s.disposeNow();
}
Also used : Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (reactor.netty.Connection)137 Test (org.junit.jupiter.api.Test)69 CountDownLatch (java.util.concurrent.CountDownLatch)52 Duration (java.time.Duration)49 Mono (reactor.core.publisher.Mono)47 InetSocketAddress (java.net.InetSocketAddress)41 TimeUnit (java.util.concurrent.TimeUnit)39 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)38 LoopResources (reactor.netty.resources.LoopResources)38 ByteBuf (io.netty.buffer.ByteBuf)35 Flux (reactor.core.publisher.Flux)35 DisposableServer (reactor.netty.DisposableServer)35 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)33 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 List (java.util.List)31 IOException (java.io.IOException)30 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)30 ChannelOption (io.netty.channel.ChannelOption)29 Unpooled (io.netty.buffer.Unpooled)28 Charset (java.nio.charset.Charset)28