Search in sources :

Example 66 with Connection

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

the class TcpClientTests method testCancelSend.

@Test
void testCancelSend() throws InterruptedException {
    final CountDownLatch connectionLatch = new CountDownLatch(3);
    TcpClient tcpClient = TcpClient.newConnection().host("localhost").port(echoServerPort);
    Connection c;
    c = tcpClient.handle((i, o) -> {
        o.sendObject(Mono.never().doOnCancel(connectionLatch::countDown).log("uno")).then().subscribe().dispose();
        Schedulers.parallel().schedule(() -> o.sendObject(Mono.never().doOnCancel(connectionLatch::countDown).log("dos")).then().subscribe().dispose());
        o.sendObject(Mono.never().doOnCancel(connectionLatch::countDown).log("tres")).then().subscribe().dispose();
        return Mono.never();
    }).connectNow();
    assertThat(connectionLatch.await(30, TimeUnit.SECONDS)).as("Cancel not propagated").isTrue();
    c.disposeNow();
}
Also used : Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 67 with Connection

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

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

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

the class TcpClientTests method readIdleDoesNotFireWhileDataIsBeingRead.

@Test
void readIdleDoesNotFireWhileDataIsBeingRead() throws InterruptedException, IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    long start = System.currentTimeMillis();
    TcpClient client = TcpClient.create().port(heartbeatServerPort);
    Connection s = client.handle((in, out) -> {
        in.withConnection(c -> c.onReadIdle(200, latch::countDown));
        return Flux.never();
    }).wiretap(true).connectNow();
    assertThat(latch.await(5, TimeUnit.SECONDS)).as("latch await").isTrue();
    heartbeatServer.close();
    long duration = System.currentTimeMillis() - start;
    assertThat(duration).isGreaterThanOrEqualTo(200L);
    s.disposeNow();
}
Also used : Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 70 with Connection

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

the class TcpClientTests method testTcpClient1ThreadAcquire.

@Test
void testTcpClient1ThreadAcquire() {
    LoopResources resources = LoopResources.create("test", 1, true);
    Connection client = TcpClient.create().host("localhost").port(echoServerPort).runOn(resources).wiretap(true).connectNow();
    client.disposeNow();
    resources.dispose();
    assertThat(client).as("client was configured").isInstanceOf(ChannelOperations.class);
}
Also used : LoopResources(reactor.netty.resources.LoopResources) Connection(reactor.netty.Connection) 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