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();
}
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();
}
}
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);
}
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();
}
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());
}
});
}
Aggregations