Search in sources :

Example 16 with PoolResources

use of reactor.ipc.netty.resources.PoolResources in project reactor-netty by reactor.

the class HttpClientTest method abort.

@Test
public void abort() throws Exception {
    NettyContext x = TcpServer.create("localhost", 0).newHandler((in, out) -> in.receive().take(1).thenMany(Flux.defer(() -> out.context(c -> c.addHandlerFirst(new HttpResponseEncoder())).sendObject(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED)).then(Mono.delay(Duration.ofSeconds(2)).then())))).block(Duration.ofSeconds(30));
    PoolResources pool = PoolResources.fixed("test", 1);
    HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").flatMap(r -> {
        r.dispose();
        return Mono.just(r.status().code());
    }).log().block(Duration.ofSeconds(30));
    HttpClientResponse resp = HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").log().block(Duration.ofSeconds(30));
    resp.dispose();
    resp = HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").log().block(Duration.ofSeconds(30));
    resp.dispose();
    x.dispose();
    pool.dispose();
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpVersion(io.netty.handler.codec.http.HttpVersion) StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Unpooled(io.netty.buffer.Unpooled) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) Duration(java.time.Duration) CharsetUtil(io.netty.util.CharsetUtil) Path(java.nio.file.Path) HttpServer(reactor.ipc.netty.http.server.HttpServer) DirectProcessor(reactor.core.publisher.DirectProcessor) SslContext(io.netty.handler.ssl.SslContext) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) SSLException(javax.net.ssl.SSLException) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) TcpServer(reactor.ipc.netty.tcp.TcpServer) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert(org.junit.Assert) InputStream(java.io.InputStream) Proxy(reactor.ipc.netty.options.ClientProxyOptions.Proxy) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) PoolResources(reactor.ipc.netty.resources.PoolResources) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 17 with PoolResources

use of reactor.ipc.netty.resources.PoolResources in project reactor-netty by reactor.

the class HttpRedirectTest method redirectTests.

private void redirectTests(String url) {
    AtomicInteger counter = new AtomicInteger(1);
    NettyContext server = HttpServer.create(0).newHandler((req, res) -> {
        if (req.uri().contains("/login") && req.method().equals(HttpMethod.POST) && counter.getAndDecrement() > 0) {
            return res.sendRedirect(url);
        } else {
            return res.status(200).send();
        }
    }).block(Duration.ofSeconds(30));
    PoolResources pool = PoolResources.fixed("test", 1);
    HttpClient client = HttpClient.create(ops -> ops.connectAddress(() -> server.address()).poolResources(pool));
    try {
        Flux.range(0, this.numberOfTests).concatMap(i -> client.post("/login", r -> r.followRedirect()).flatMap(r -> r.receive().then())).blockLast(Duration.ofSeconds(30));
    } finally {
        server.dispose();
    }
}
Also used : Flux(reactor.core.publisher.Flux) StepVerifier(reactor.test.StepVerifier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) HttpMethod(io.netty.handler.codec.http.HttpMethod) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) PoolResources(reactor.ipc.netty.resources.PoolResources) HttpServer(reactor.ipc.netty.http.server.HttpServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PoolResources(reactor.ipc.netty.resources.PoolResources) NettyContext(reactor.ipc.netty.NettyContext)

Example 18 with PoolResources

use of reactor.ipc.netty.resources.PoolResources in project reactor-netty by reactor.

the class HttpResourcesTest method before.

@Before
public void before() {
    loopDisposed = new AtomicBoolean();
    poolDisposed = new AtomicBoolean();
    loopResources = new LoopResources() {

        @Override
        public EventLoopGroup onServer(boolean useNative) {
            return null;
        }

        @Override
        public Mono<Void> disposeLater() {
            return Mono.<Void>empty().doOnSuccess(c -> loopDisposed.set(true));
        }

        @Override
        public boolean isDisposed() {
            return loopDisposed.get();
        }
    };
    poolResources = new PoolResources() {

        @Override
        public ChannelPool selectOrCreate(SocketAddress address, Supplier<? extends Bootstrap> bootstrap, Consumer<? super Channel> onChannelCreate, EventLoopGroup group) {
            return null;
        }

        public Mono<Void> disposeLater() {
            return Mono.<Void>empty().doOnSuccess(c -> poolDisposed.set(true));
        }

        @Override
        public boolean isDisposed() {
            return poolDisposed.get();
        }
    };
    testResources = new HttpResources(loopResources, poolResources);
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) SocketAddress(java.net.SocketAddress) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) Supplier(java.util.function.Supplier) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelPool(io.netty.channel.pool.ChannelPool) LoopResources(reactor.ipc.netty.resources.LoopResources) Before(org.junit.Before) ChannelPool(io.netty.channel.pool.ChannelPool) Mono(reactor.core.publisher.Mono) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EventLoopGroup(io.netty.channel.EventLoopGroup) PoolResources(reactor.ipc.netty.resources.PoolResources) LoopResources(reactor.ipc.netty.resources.LoopResources) SocketAddress(java.net.SocketAddress) Before(org.junit.Before)

Example 19 with PoolResources

use of reactor.ipc.netty.resources.PoolResources in project reactor-netty by reactor.

the class ChannelOperationsHandlerTest method testIssue196.

@Test
@Ignore
public void testIssue196() throws Exception {
    ExecutorService threadPool = Executors.newCachedThreadPool();
    int testServerPort = SocketUtils.findAvailableTcpPort();
    TestServer testServer = new TestServer(testServerPort);
    threadPool.submit(testServer);
    if (!testServer.await(10, TimeUnit.SECONDS)) {
        throw new IOException("Fail to start test server");
    }
    HttpClient client = HttpClient.create(opt -> opt.port(testServerPort).poolResources(PoolResources.fixed("test", 1)));
    Flux.range(0, 2).flatMap(i -> client.get("/205").flatMap(res -> res.receive().aggregate().asString())).blockLast(Duration.ofSeconds(100));
    testServer.close();
}
Also used : StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuffer(java.nio.ByteBuffer) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) Duration(java.time.Duration) Logger(reactor.util.Logger) HttpClient(reactor.ipc.netty.http.client.HttpClient) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) ExecutorService(java.util.concurrent.ExecutorService) HttpServer(reactor.ipc.netty.http.server.HttpServer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FutureMono(reactor.ipc.netty.FutureMono) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) Executors(java.util.concurrent.Executors) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with PoolResources

use of reactor.ipc.netty.resources.PoolResources in project reactor-netty by reactor.

the class TcpClient method newHandler.

/**
 * @param handler
 * @param address
 * @param secure
 * @param onSetup
 *
 * @return a new Mono to connect on subscribe
 */
protected Mono<NettyContext> newHandler(BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> handler, InetSocketAddress address, boolean secure, Consumer<? super Channel> onSetup) {
    final BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> targetHandler = null == handler ? ChannelOperations.noopHandler() : handler;
    return Mono.create(sink -> {
        SocketAddress remote = address != null ? address : options.getAddress();
        ChannelPool pool = null;
        PoolResources poolResources = options.getPoolResources();
        if (poolResources != null) {
            pool = poolResources.selectOrCreate(remote, options, doHandler(null, sink, secure, remote, null, null), options.getLoopResources().onClient(options.preferNative()));
        }
        ContextHandler<SocketChannel> contextHandler = doHandler(targetHandler, sink, secure, remote, pool, onSetup);
        sink.onCancel(contextHandler);
        if (pool == null) {
            Bootstrap b = options.get();
            b.remoteAddress(remote);
            b.handler(contextHandler);
            contextHandler.setFuture(b.connect());
        } else {
            contextHandler.setFuture(pool.acquire());
        }
    });
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool) SocketChannel(io.netty.channel.socket.SocketChannel) PoolResources(reactor.ipc.netty.resources.PoolResources) Bootstrap(io.netty.bootstrap.Bootstrap) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

PoolResources (reactor.ipc.netty.resources.PoolResources)21 Test (org.junit.Test)18 Mono (reactor.core.publisher.Mono)17 Duration (java.time.Duration)16 Flux (reactor.core.publisher.Flux)15 NettyContext (reactor.ipc.netty.NettyContext)15 InetSocketAddress (java.net.InetSocketAddress)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 StepVerifier (reactor.test.StepVerifier)12 SslContext (io.netty.handler.ssl.SslContext)11 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 FutureMono (reactor.ipc.netty.FutureMono)10 HttpServer (reactor.ipc.netty.http.server.HttpServer)10 Unpooled (io.netty.buffer.Unpooled)9 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)9 HttpVersion (io.netty.handler.codec.http.HttpVersion)9 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)9 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)9