Search in sources :

Example 91 with Connection

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

the class TcpClientTests method noSystemProxySettings.

/* https://github.com/reactor/reactor-netty/issues/1765 */
@Test
void noSystemProxySettings() {
    Properties props = System.getProperties();
    assumeThat(!(props.containsKey("http.proxyHost") || props.containsKey("https.proxyHost") || props.containsKey("socksProxyHost"))).isTrue();
    DisposableServer disposableServer = TcpServer.create().port(0).handle((req, res) -> res.sendString(Mono.just("noSystemProxySettings"))).bindNow();
    AtomicReference<AddressResolverGroup<?>> resolver = new AtomicReference<>();
    Connection conn = null;
    try {
        conn = TcpClient.create().host("localhost").port(disposableServer.port()).proxyWithSystemProperties().doOnConnect(conf -> resolver.set(conf.resolver())).connectNow();
    } finally {
        disposableServer.disposeNow();
        if (conn != null) {
            conn.disposeNow();
        }
    }
    assertThat(resolver.get()).isNull();
}
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) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) Connection(reactor.netty.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 92 with Connection

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

the class TcpClientTests method tcpClientHandlesLineFeedData.

private void tcpClientHandlesLineFeedData(TcpClient client) throws InterruptedException {
    final int messages = 100;
    final CountDownLatch latch = new CountDownLatch(messages);
    final List<String> strings = new ArrayList<>();
    Connection c = client.handle((in, out) -> out.sendString(Flux.range(1, messages).map(i -> "Hello World!" + i + "\n").subscribeOn(Schedulers.parallel())).then(in.receive().asString().take(100).flatMapIterable(s -> Arrays.asList(s.split("\\n"))).doOnNext(s -> {
        strings.add(s);
        latch.countDown();
    }).then())).wiretap(true).connectNow(Duration.ofSeconds(30));
    log.debug("Connected");
    c.onDispose().log().block(Duration.ofSeconds(30));
    assertThat(latch.await(15, TimeUnit.SECONDS)).as("Expected messages not received. Received " + strings.size() + " messages: " + strings).isTrue();
    assertThat(strings).hasSize(messages);
}
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) ArrayList(java.util.ArrayList) Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 93 with Connection

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

the class TcpClientTests method consumerSpecAssignsEventHandlers.

@Test
void consumerSpecAssignsEventHandlers() throws InterruptedException {
    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().host("localhost").port(timeoutServerPort);
    Connection s = client.handle((in, out) -> {
        in.withConnection(c -> c.onDispose(close::countDown));
        out.withConnection(c -> c.onWriteIdle(200, () -> {
            totalDelay.addAndGet(System.currentTimeMillis() - start);
            latch.countDown();
        }));
        return Mono.delay(Duration.ofSeconds(1)).then().log();
    }).wiretap(true).connectNow();
    assertThat(latch.await(5, TimeUnit.SECONDS)).as("latch was counted down").isTrue();
    assertThat(close.await(30, TimeUnit.SECONDS)).as("close was counted down").isTrue();
    assertThat(totalDelay.get()).as("totalDelay was > 200ms").isGreaterThanOrEqualTo(200L);
    s.disposeNow();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 94 with Connection

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

the class NewConnectionProviderTest method testDisposableConnectBindException.

@Test
@SuppressWarnings("unchecked")
void testDisposableConnectBindException() {
    MonoSink<Connection> sink = Mockito.mock(MonoSink.class);
    NewConnectionProvider.DisposableConnect connect = new NewConnectionProvider.DisposableConnect(sink, () -> new InetSocketAddress("test1", 6956));
    connect.onError(new UnsupportedOperationException());
    Mockito.verify(sink).error(Mockito.argThat(a -> a instanceof UnsupportedOperationException));
    connect = new NewConnectionProvider.DisposableConnect(sink, () -> new InetSocketAddress("test2", 4956));
    // Not possible to mock io.netty.channel.unix.Errors.NativeIoException or create a new instance because of Jni
    // error:
    // java.lang.UnsatisfiedLinkError: 'int io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoENOENT()'
    connect.onError(new IOException("bind(..) failed: Address already in use"));
    Mockito.verify(sink).error(Mockito.argThat(a -> a instanceof ChannelBindException && ((ChannelBindException) a).localHost().equals("test2") && ((ChannelBindException) a).localPort() == 4956));
    // Issue-1668
    connect = new NewConnectionProvider.DisposableConnect(sink, () -> new InetSocketAddress("test3", 7956));
    connect.onError(new IOException("bind(..) failed: Die Adresse wird bereits verwendet"));
    Mockito.verify(sink).error(Mockito.argThat(a -> a instanceof ChannelBindException && ((ChannelBindException) a).localHost().equals("test3") && ((ChannelBindException) a).localPort() == 7956));
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) MonoSink(reactor.core.publisher.MonoSink) IOException(java.io.IOException) Connection(reactor.netty.Connection) InetSocketAddress(java.net.InetSocketAddress) ChannelBindException(reactor.netty.ChannelBindException) InetSocketAddress(java.net.InetSocketAddress) Connection(reactor.netty.Connection) IOException(java.io.IOException) ChannelBindException(reactor.netty.ChannelBindException) Test(org.junit.jupiter.api.Test)

Example 95 with Connection

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

the class BlockingConnectionTest method simpleServerFromAsyncServer.

@Test
void simpleServerFromAsyncServer() {
    DisposableServer simpleServer = TcpServer.create().handle((in, out) -> out.sendString(in.receive().asString().takeUntil(s -> s.endsWith("CONTROL")).map(s -> "ECHO: " + s.replaceAll("CONTROL", "")).concatWith(Mono.just("DONE"))).neverComplete()).wiretap(true).bindNow();
    InetSocketAddress address = (InetSocketAddress) simpleServer.address();
    AtomicReference<List<String>> data1 = new AtomicReference<>();
    AtomicReference<List<String>> data2 = new AtomicReference<>();
    Connection simpleClient1 = TcpClient.create().port(address.getPort()).handle((in, out) -> out.sendString(Flux.just("Hello", "World", "CONTROL")).then(in.receive().asString().takeUntil(s -> s.endsWith("DONE")).map(s -> s.replaceAll("DONE", "")).filter(s -> !s.isEmpty()).collectList().doOnNext(data1::set).doOnNext(System.err::println).then())).wiretap(true).connectNow();
    Connection simpleClient2 = TcpClient.create().port(address.getPort()).handle((in, out) -> out.sendString(Flux.just("How", "Are", "You?", "CONTROL")).then(in.receive().asString().takeUntil(s -> s.endsWith("DONE")).map(s -> s.replaceAll("DONE", "")).filter(s -> !s.isEmpty()).collectList().doOnNext(data2::set).doOnNext(System.err::println).then())).wiretap(true).connectNow();
    simpleClient1.onDispose().block(Duration.ofSeconds(30));
    System.err.println("STOPPED 1");
    simpleClient2.onDispose().block(Duration.ofSeconds(30));
    System.err.println("STOPPED 2");
    System.err.println("STOPPING SERVER");
    simpleServer.disposeNow();
    assertThat(data1.get()).allSatisfy(s -> assertThat(s).startsWith("ECHO: "));
    assertThat(data2.get()).allSatisfy(s -> assertThat(s).startsWith("ECHO: "));
    assertThat(data1.get().toString().replaceAll("ECHO: ", "").replaceAll(", ", "")).isEqualTo("[HelloWorld]");
    assertThat(data2.get().toString().replaceAll("ECHO: ", "").replaceAll(", ", "")).isEqualTo("[HowAreYou?]");
}
Also used : LoopResources(reactor.netty.resources.LoopResources) EventLoopGroup(io.netty.channel.EventLoopGroup) LoggingHandler(io.netty.handler.logging.LoggingHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) ClientTransport(reactor.netty.transport.ClientTransport) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) Channel(io.netty.channel.Channel) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) ChannelMetricsRecorder(reactor.netty.channel.ChannelMetricsRecorder) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) Connection(reactor.netty.Connection) ClientTransportConfig(reactor.netty.transport.ClientTransportConfig) DisposableServer(reactor.netty.DisposableServer) DisposableServer(reactor.netty.DisposableServer) InetSocketAddress(java.net.InetSocketAddress) Connection(reactor.netty.Connection) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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