use of reactor.netty.Connection in project reactor-netty by reactor.
the class UdpServerTests method supportsReceivingDatagrams.
@Test
void supportsReceivingDatagrams() throws InterruptedException {
final Random rndm = new Random();
final int port = SocketUtils.findAvailableUdpPort();
final CountDownLatch latch = new CountDownLatch(4);
final Connection server = UdpServer.create().port(port).handle((in, out) -> {
in.receive().asByteArray().log().subscribe(bytes -> {
if (bytes.length == 1024) {
latch.countDown();
}
});
return Flux.never();
}).bind().doOnSuccess(v -> {
try {
DatagramChannel udp = DatagramChannel.open();
udp.configureBlocking(true);
udp.connect(v.address());
byte[] data = new byte[1024];
rndm.nextBytes(data);
for (int i = 0; i < 4; i++) {
udp.write(ByteBuffer.wrap(data));
}
udp.close();
} catch (IOException e) {
log.error("", e);
}
}).block(Duration.ofSeconds(30));
assertThat(server).isNotNull();
assertThat(latch.await(10, TimeUnit.SECONDS)).as("latch was counted down").isTrue();
server.disposeNow();
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpResourcesTest method blockShouldFail.
@Test
void blockShouldFail() throws InterruptedException {
final int port = SocketUtils.findAvailableTcpPort();
final CountDownLatch latch = new CountDownLatch(2);
DisposableServer server = TcpServer.create().port(port).handle((in, out) -> {
try {
in.receive().blockFirst();
} catch (RuntimeException e) {
latch.countDown();
throw e;
}
return Flux.never();
}).bindNow();
Connection client = TcpClient.newConnection().port(port).handle((in, out) -> {
try {
out.sendString(Flux.just("Hello World!")).then().block();
} catch (RuntimeException e) {
latch.countDown();
throw e;
}
return Mono.never();
}).connectNow();
assertThat(latch.await(5, TimeUnit.SECONDS)).as("latch was counted down").isTrue();
client.dispose();
server.disposeNow();
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class HttpServerTests method testCancelConnectionCloseForWebSocketServer.
@Test
void testCancelConnectionCloseForWebSocketServer() throws Exception {
AtomicReference<WebSocketCloseStatus> statusServer = new AtomicReference<>();
AtomicReference<WebSocketCloseStatus> statusClient = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(2);
disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((in, out) -> {
in.receiveCloseStatus().doOnNext(o -> {
statusServer.set(o);
latch.countDown();
}).subscribe();
in.withConnection(Connection::dispose);
return Mono.never();
})).bindNow();
createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> {
in.receiveCloseStatus().doOnNext(o -> {
statusClient.set(o);
latch.countDown();
}).subscribe();
return Mono.never();
}).subscribe();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(statusClient.get()).isNotNull().isEqualTo(WebSocketCloseStatus.EMPTY);
assertThat(statusServer.get()).isNotNull().isEqualTo(WebSocketCloseStatus.ABNORMAL_CLOSURE);
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class Http2PoolTest method maxLifeTimeMaxConnectionsReached.
@Test
void maxLifeTimeMaxConnectionsReached() throws Exception {
PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.fromSupplier(() -> {
Channel channel = new EmbeddedChannel(new TestChannelId(), Http2FrameCodecBuilder.forClient().build());
return Connection.from(channel);
})).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 1);
Http2Pool http2Pool = poolBuilder.build(config -> new Http2Pool(config, 10));
Connection connection = null;
try {
PooledRef<Connection> acquired1 = http2Pool.acquire().block();
assertThat(acquired1).isNotNull();
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
assertThat(http2Pool.connections.size()).isEqualTo(1);
connection = acquired1.poolable();
Thread.sleep(10);
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
assertThat(http2Pool.connections.size()).isEqualTo(1);
http2Pool.acquire(Duration.ofMillis(10)).as(StepVerifier::create).expectError(PoolAcquireTimeoutException.class).verify(Duration.ofSeconds(1));
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
assertThat(http2Pool.connections.size()).isEqualTo(1);
acquired1.invalidate().block();
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
assertThat(http2Pool.connections.size()).isEqualTo(0);
} finally {
if (connection != null) {
((EmbeddedChannel) connection.channel()).finishAndReleaseAll();
connection.dispose();
}
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class Http2PoolTest method acquireInvalidate.
@Test
void acquireInvalidate() {
EmbeddedChannel channel = new EmbeddedChannel(Http2FrameCodecBuilder.forClient().build(), new Http2MultiplexHandler(new ChannelHandlerAdapter() {
}));
PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.just(Connection.from(channel))).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 1);
InstrumentedPool<Connection> http2Pool = poolBuilder.build(config -> new Http2Pool(config, -1));
try {
List<PooledRef<Connection>> acquired = new ArrayList<>();
http2Pool.acquire().subscribe(acquired::add);
http2Pool.acquire().subscribe(acquired::add);
http2Pool.acquire().subscribe(acquired::add);
assertThat(acquired).hasSize(3);
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(3);
for (PooledRef<Connection> slot : acquired) {
slot.invalidate().block(Duration.ofSeconds(1));
}
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
for (PooledRef<Connection> slot : acquired) {
// second invalidate() should be ignored and ACQUIRED size should remain the same
slot.invalidate().block(Duration.ofSeconds(1));
}
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
} finally {
channel.finishAndReleaseAll();
Connection.from(channel).dispose();
}
}
Aggregations