use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class UdpClientTest method smokeTest.
@Test
public void smokeTest() throws Exception {
LoopResources resources = LoopResources.create("test");
CountDownLatch latch = new CountDownLatch(4);
NettyContext server = UdpServer.create(ops -> ops.port(0).loopResources(resources)).newHandler((in, out) -> in.receiveObject().map(o -> {
if (o instanceof DatagramPacket) {
DatagramPacket received = (DatagramPacket) o;
System.out.println("Server received " + received.content().toString(CharsetUtil.UTF_8));
ByteBuf buf1 = Unpooled.copiedBuffer("echo ", CharsetUtil.UTF_8);
ByteBuf buf2 = Unpooled.copiedBuffer(buf1, received.content().retain());
return new DatagramPacket(buf2, received.sender());
} else {
return Mono.error(new Exception());
}
}).flatMap(p -> out.sendObject(p))).block(Duration.ofSeconds(30));
NettyContext client1 = UdpClient.create(ops -> ops.port(server.address().getPort()).loopResources(resources)).newHandler((in, out) -> {
in.receive().subscribe(b -> {
System.out.println("Client1 received " + b.toString(CharsetUtil.UTF_8));
latch.countDown();
});
return out.sendString(Mono.just("ping1")).then(out.sendString(Mono.just("ping2"))).neverComplete();
}).block(Duration.ofSeconds(30));
NettyContext client2 = UdpClient.create(ops -> ops.port(server.address().getPort()).loopResources(resources)).newHandler((in, out) -> {
in.receive().subscribe(b -> {
System.out.println("Client2 received " + b.toString(CharsetUtil.UTF_8));
latch.countDown();
});
return out.sendString(Mono.just("ping3")).then(out.sendString(Mono.just("ping4"))).neverComplete();
}).block(Duration.ofSeconds(30));
assertTrue(latch.await(30, TimeUnit.SECONDS));
server.dispose();
client1.dispose();
client2.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class UdpServerTests method supportsUdpMulticast.
@Test
public void supportsUdpMulticast() throws Exception {
final int port = SocketUtils.findAvailableUdpPort();
final CountDownLatch latch = new CountDownLatch(Schedulers.DEFAULT_POOL_SIZE);
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
final InetAddress multicastGroup = InetAddress.getByName("230.0.0.1");
final NetworkInterface multicastInterface = findMulticastEnabledIPv4Interface();
log.info("Using network interface '{}' for multicast", multicastInterface);
final Collection<NettyContext> servers = new ArrayList<>();
LoopResources resources = LoopResources.create("test");
for (int i = 0; i < 4; i++) {
NettyContext server = UdpServer.create(opts -> opts.option(ChannelOption.SO_REUSEADDR, true).connectAddress(() -> new InetSocketAddress(port)).protocolFamily(InternetProtocolFamily.IPv4).loopResources(resources)).newHandler((in, out) -> {
Flux.<NetworkInterface>generate(s -> {
if (ifaces.hasMoreElements()) {
s.next(ifaces.nextElement());
} else {
s.complete();
}
}).flatMap(iface -> {
if (isMulticastEnabledIPv4Interface(iface)) {
return in.join(multicastGroup, iface);
}
return Flux.empty();
}).thenMany(in.receive().asByteArray()).log().subscribe(bytes -> {
if (bytes.length == 1024) {
latch.countDown();
}
});
return Flux.never();
}).block(Duration.ofSeconds(30));
servers.add(server);
}
for (int i = 0; i < Schedulers.DEFAULT_POOL_SIZE; i++) {
threadPool.submit(() -> {
try {
MulticastSocket multicast = new MulticastSocket();
multicast.joinGroup(new InetSocketAddress(multicastGroup, port), multicastInterface);
byte[] data = new byte[1024];
new Random().nextBytes(data);
multicast.send(new DatagramPacket(data, data.length, multicastGroup, port));
multicast.close();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}).get(5, TimeUnit.SECONDS);
}
latch.await(5, TimeUnit.SECONDS);
assertThat("latch was not counted down enough: " + latch.getCount() + " left on " + (4 ^ 2), latch.getCount() == 0);
for (NettyContext s : servers) {
s.dispose();
}
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpClientTest method prematureCancel.
@Test
public void prematureCancel() throws Exception {
DirectProcessor<Void> signal = DirectProcessor.create();
NettyContext x = TcpServer.create("localhost", 0).newHandler((in, out) -> {
signal.onComplete();
return out.context(c -> c.addHandlerFirst(new HttpResponseEncoder())).sendObject(Mono.delay(Duration.ofSeconds(2)).map(t -> new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.PROCESSING))).neverComplete();
}).block(Duration.ofSeconds(30));
StepVerifier.create(createHttpClientForContext(x).get("/").timeout(signal)).verifyError(TimeoutException.class);
// Thread.sleep(1000000);
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpClientTest method testUserAgent.
@Test
public void testUserAgent() {
NettyContext c = HttpServer.create(0).newHandler((req, resp) -> {
Assert.assertTrue("" + req.requestHeaders().get(HttpHeaderNames.USER_AGENT), req.requestHeaders().contains(HttpHeaderNames.USER_AGENT) && req.requestHeaders().get(HttpHeaderNames.USER_AGENT).equals(HttpClient.USER_AGENT));
return resp;
}).block();
HttpClientResponse resp = HttpClient.create(c.address().getPort()).get("/").block();
resp.dispose();
c.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpClientTest method doTestGzip.
private void doTestGzip(boolean gzipEnabled) {
String expectedResponse = gzipEnabled ? "gzip" : "no gzip";
NettyContext server = HttpServer.create(0).newHandler((req, res) -> res.sendString(Mono.just(req.requestHeaders().get(HttpHeaderNames.ACCEPT_ENCODING, "no gzip")))).block(Duration.ofSeconds(30));
StepVerifier.create(HttpClient.create(ops -> ops.port(server.address().getPort()).compression(gzipEnabled)).get("/").flatMap(r -> r.receive().asString().elementAt(0).zipWith(Mono.just(r)))).expectNextMatches(tuple -> {
tuple.getT2().dispose();
return expectedResponse.equals(tuple.getT1());
}).expectComplete().verify(Duration.ofSeconds(30));
server.dispose();
}
Aggregations