use of reactor.netty.Connection in project reactor-netty by reactor.
the class DefaultPooledConnectionProviderTest method testIssue673_TimeoutException.
@Test
void testIssue673_TimeoutException() throws InterruptedException {
DisposableServer server = TcpServer.create().port(0).handle((in, out) -> out.sendString(Mono.just("test").delayElement(Duration.ofMillis(100)))).wiretap(true).bindNow();
DefaultPooledConnectionProvider provider = (DefaultPooledConnectionProvider) ConnectionProvider.builder("testIssue673_TimeoutException").maxConnections(1).pendingAcquireMaxCount(4).pendingAcquireTimeout(Duration.ofMillis(10)).build();
CountDownLatch latch = new CountDownLatch(2);
try {
AtomicReference<InstrumentedPool<PooledConnection>> pool = new AtomicReference<>();
List<? extends Signal<? extends Connection>> list = Flux.range(0, 5).flatMapDelayError(i -> TcpClient.create(provider).port(server.port()).doOnConnected(conn -> {
ConcurrentMap<PooledConnectionProvider.PoolKey, InstrumentedPool<PooledConnection>> pools = provider.channelPools;
pool.set(pools.get(pools.keySet().toArray()[0]));
}).doOnDisconnected(conn -> latch.countDown()).handle((in, out) -> in.receive().then()).wiretap(true).connect().materialize(), 256, 32).collectList().doFinally(fin -> latch.countDown()).block(Duration.ofSeconds(30));
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch 30s").isTrue();
assertThat(list).isNotNull().hasSize(5);
int onNext = 0;
int onError = 0;
String msg = "Pool#acquire(Duration) has been pending for more than the configured timeout of 10ms";
for (int i = 0; i < 5; i++) {
Signal<? extends Connection> signal = list.get(i);
if (signal.isOnNext()) {
onNext++;
} else if (signal.getThrowable() instanceof TimeoutException && msg.equals(signal.getThrowable().getMessage())) {
onError++;
}
}
assertThat(onNext).isEqualTo(1);
assertThat(onError).isEqualTo(4);
assertThat(pool.get().metrics().acquiredSize()).as("currently acquired").isEqualTo(0);
assertThat(pool.get().metrics().idleSize()).as("currently idle").isEqualTo(0);
} finally {
server.disposeNow();
provider.dispose();
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpClientTests method testIssue585_2.
@Test
void testIssue585_2() throws Exception {
DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain())).wiretap(true).bindNow();
byte[] bytes = "test".getBytes(Charset.defaultCharset());
ByteBuf b1 = Unpooled.wrappedBuffer(bytes);
ByteBuf b2 = Unpooled.wrappedBuffer(bytes);
ByteBuf b3 = Unpooled.wrappedBuffer(bytes);
WeakReference<ByteBuf> refCheck1 = new WeakReference<>(b1);
WeakReference<ByteBuf> refCheck2 = new WeakReference<>(b2);
WeakReference<ByteBuf> refCheck3 = new WeakReference<>(b3);
Connection conn = TcpClient.create().remoteAddress(server::address).wiretap(true).connectNow();
NettyOutbound out = conn.outbound();
out.sendObject(b1).then().block(Duration.ofSeconds(30));
assertThat(b1.refCnt()).isEqualTo(0);
b1 = null;
checkReference(refCheck1);
out.sendObject(b2).then().block(Duration.ofSeconds(30));
assertThat(b2.refCnt()).isEqualTo(0);
b2 = null;
checkReference(refCheck2);
out.sendObject(b3).then().block(Duration.ofSeconds(30));
assertThat(b3.refCnt()).isEqualTo(0);
b3 = null;
checkReference(refCheck3);
server.disposeNow();
conn.disposeNow();
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpClientTests method testTcpClient.
@Test
void testTcpClient() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
Connection client = TcpClient.create().host("localhost").port(echoServerPort).handle((in, out) -> {
in.receive().log("conn").subscribe(s -> latch.countDown());
return out.sendString(Flux.just("Hello World!")).neverComplete();
}).wiretap(true).connectNow();
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
client.disposeNow();
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpClientTests method connectionWillAttemptToReconnectWhenItIsDropped.
@Test
void connectionWillAttemptToReconnectWhenItIsDropped() throws InterruptedException {
final CountDownLatch connectionLatch = new CountDownLatch(1);
final CountDownLatch reconnectionLatch = new CountDownLatch(1);
try {
TcpClient tcpClient = TcpClient.newConnection().host("localhost").port(abortServerPort);
Mono<? extends Connection> handler = tcpClient.handle((in, out) -> {
log.debug("Start");
connectionLatch.countDown();
in.receive().subscribe();
return Flux.never();
}).wiretap(true).connect();
Connection c = handler.log().then(handler.doOnSuccess(s -> reconnectionLatch.countDown())).block(Duration.ofSeconds(30));
assertThat(c).isNotNull();
c.onDispose();
assertThat(connectionLatch.await(5, TimeUnit.SECONDS)).as("Initial connection is made").isTrue();
assertThat(reconnectionLatch.await(5, TimeUnit.SECONDS)).as("A reconnect attempt was made").isTrue();
} catch (AbortedException e) {
// ignored
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpClientTests method testTcpClientWithInetSocketAddress.
@Test
void testTcpClientWithInetSocketAddress() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
TcpClient client = TcpClient.create().port(echoServerPort);
Connection s = client.handle((in, out) -> {
in.receive().subscribe(d -> latch.countDown());
return out.sendString(Flux.just("Hello")).neverComplete();
}).wiretap(true).connectNow(Duration.ofSeconds(5));
assertThat(latch.await(5, TimeUnit.SECONDS)).as("latch await").isTrue();
s.disposeNow();
}
Aggregations