Search in sources :

Example 96 with DisposableServer

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

the class TcpClientTests method doTestSharedNameResolver.

private void doTestSharedNameResolver(TcpClient client, boolean sharedClient) throws InterruptedException {
    DisposableServer disposableServer = TcpServer.create().port(0).handle((req, res) -> res.sendString(Mono.just("testNoOpenedFileDescriptors"))).bindNow(Duration.ofSeconds(30));
    LoopResources loop = LoopResources.create("doTestSharedNameResolver", 4, true);
    AtomicReference<List<AddressResolverGroup<?>>> resolvers = new AtomicReference<>(new ArrayList<>());
    try {
        int count = 8;
        CountDownLatch latch = new CountDownLatch(count);
        TcpClient localClient = null;
        if (sharedClient) {
            localClient = client.runOn(loop).port(disposableServer.port()).doOnConnect(config -> resolvers.get().add(config.resolver())).doOnConnected(conn -> conn.onDispose(latch::countDown));
        }
        for (int i = 0; i < count; i++) {
            if (!sharedClient) {
                localClient = client.runOn(loop).port(disposableServer.port()).doOnConnect(config -> resolvers.get().add(config.resolver())).doOnConnected(conn -> conn.onDispose(latch::countDown));
            }
            localClient.handle((in, out) -> in.receive().then()).connect().subscribe();
        }
        assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
        assertThat(resolvers.get().size()).isEqualTo(count);
        AddressResolverGroup<?> resolver = resolvers.get().get(0);
        assertThat(resolvers.get()).allMatch(addressResolverGroup -> addressResolverGroup == resolver);
    } finally {
        disposableServer.disposeNow();
        loop.disposeLater().block();
    }
}
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) LoopResources(reactor.netty.resources.LoopResources) List(java.util.List) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 97 with DisposableServer

use of reactor.netty.DisposableServer 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 98 with DisposableServer

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

the class BlockingConnectionTest method getContextAddressAndHost.

@Test
void getContextAddressAndHost() {
    DisposableServer c = new TcpServer() {

        @Override
        public TcpServerConfig configuration() {
            return null;
        }

        @Override
        protected TcpServer duplicate() {
            return null;
        }

        @Override
        public Mono<? extends DisposableServer> bind() {
            return Mono.just(NEVER_STOP_SERVER);
        }
    }.bindNow();
    assertThat(c).isSameAs(NEVER_STOP_SERVER);
    assertThat(c.port()).isEqualTo(((InetSocketAddress) NEVER_STOP_CONTEXT.address()).getPort());
    assertThat(c.host()).isEqualTo(((InetSocketAddress) NEVER_STOP_CONTEXT.address()).getHostString());
}
Also used : DisposableServer(reactor.netty.DisposableServer) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test)

Example 99 with DisposableServer

use of reactor.netty.DisposableServer 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)

Example 100 with DisposableServer

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

the class ServerTransport method bind.

/**
 * Binds the {@link ServerTransport} and returns a {@link Mono} of {@link DisposableServer}. If
 * {@link Mono} is cancelled, the underlying binding will be aborted. Once the {@link
 * DisposableServer} has been emitted and is not necessary anymore, disposing the main server
 * loop must be done by the user via {@link DisposableServer#dispose()}.
 *
 * @return a {@link Mono} of {@link DisposableServer}
 */
public Mono<? extends DisposableServer> bind() {
    CONF config = configuration();
    Objects.requireNonNull(config.bindAddress(), "bindAddress");
    Mono<? extends DisposableServer> mono = Mono.create(sink -> {
        SocketAddress local = Objects.requireNonNull(config.bindAddress().get(), "Bind Address supplier returned null");
        if (local instanceof InetSocketAddress) {
            InetSocketAddress localInet = (InetSocketAddress) local;
            if (localInet.isUnresolved()) {
                local = AddressUtils.createResolved(localInet.getHostName(), localInet.getPort());
            }
        }
        boolean isDomainSocket = false;
        DisposableBind disposableServer;
        if (local instanceof DomainSocketAddress) {
            isDomainSocket = true;
            disposableServer = new UdsDisposableBind(sink, config, local);
        } else {
            disposableServer = new InetDisposableBind(sink, config, local);
        }
        ConnectionObserver childObs = new ChildObserver(config.defaultChildObserver().then(config.childObserver()));
        Acceptor acceptor = new Acceptor(config.childEventLoopGroup(), config.channelInitializer(childObs, null, true), config.childOptions, config.childAttrs, isDomainSocket);
        TransportConnector.bind(config, new AcceptorInitializer(acceptor), local, isDomainSocket).subscribe(disposableServer);
    });
    if (config.doOnBind() != null) {
        mono = mono.doOnSubscribe(s -> config.doOnBind().accept(config));
    }
    return mono;
}
Also used : AttributeKey(io.netty.util.AttributeKey) SocketAddress(java.net.SocketAddress) ChannelOption(io.netty.channel.ChannelOption) AbortedException(reactor.netty.channel.AbortedException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) ReactorNetty.format(reactor.netty.ReactorNetty.format) MonoSink(reactor.core.publisher.MonoSink) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ConnectionObserver(reactor.netty.ConnectionObserver) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Duration(java.time.Duration) Map(java.util.Map) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Logger(reactor.util.Logger) Connection(reactor.netty.Connection) LoopResources(reactor.netty.resources.LoopResources) DecoderException(io.netty.handler.codec.DecoderException) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelInitializer(io.netty.channel.ChannelInitializer) Operators(reactor.core.publisher.Operators) ChannelOperations(reactor.netty.channel.ChannelOperations) Context(reactor.util.context.Context) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) InetSocketAddress(java.net.InetSocketAddress) ChannelBindException(reactor.netty.ChannelBindException) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) List(java.util.List) ChannelConfig(io.netty.channel.ChannelConfig) Subscription(org.reactivestreams.Subscription) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelHandler(io.netty.channel.ChannelHandler) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) InetSocketAddress(java.net.InetSocketAddress) ConnectionObserver(reactor.netty.ConnectionObserver) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) SocketAddress(java.net.SocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) InetSocketAddress(java.net.InetSocketAddress)

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