@Test public 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)); latch.await(5, TimeUnit.SECONDS); s.disposeNow(); assertThat("latch was counted down", latch.getCount(), is(0L)); }
@Test public void testTimeoutOnStop() { Connection c = new TcpClient(){ @Override public Mono<? extends Connection> connect(Bootstrap b) { return Mono.just(NEVER_STOP_CONTEXT); } }.connectNow(); assertThatExceptionOfType(RuntimeException.class) .isThrownBy(() -> c.disposeNow(Duration.ofMillis(100))) .withMessage("Socket couldn't be stopped within 100ms"); }
@Test public void portBindingException() { Connection conn = UdpServer.create() .port(0) .bindNow(Duration.ofSeconds(30)); try { UdpServer.create() .addressSupplier(conn::address) .bindNow(Duration.ofSeconds(30)); Assert.fail("illegal-success"); } catch (ChannelBindException e) { Assert.assertEquals(e.localPort(), conn.address().getPort()); e.printStackTrace(); } conn.disposeNow(); } }
@Test public void closingPromiseIsFulfilled() { TcpClient client = TcpClient.newConnection() .host("localhost") .port(abortServerPort); client.handle((in, out) -> Mono.empty()) .wiretap(true) .connectNow() .disposeNow(); }
@Test public void writeIdleDoesNotFireWhileDataIsBeingSent() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); long start = System.currentTimeMillis(); Connection client = TcpClient.create() .host("localhost") .port(echoServerPort) .handle((in, out) -> { System.out.println("hello"); out.withConnection(c -> c.onWriteIdle(500, latch::countDown)); List<Publisher<Void>> allWrites = new ArrayList<>(); for (int i = 0; i < 5; i++) { allWrites.add(out.sendString(Flux.just("a") .delayElements(Duration.ofMillis(750)))); } return Flux.merge(allWrites); }) .wiretap(true) .connectNow(); System.out.println("Started"); assertTrue(latch.await(5, TimeUnit.SECONDS)); long duration = System.currentTimeMillis() - start; assertThat(duration, is(greaterThanOrEqualTo(500L))); client.disposeNow(); }
@Ignore public void consumerSpecAssignsEventHandlers() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(2); final CountDownLatch close = new CountDownLatch(1); final AtomicLong totalDelay = new AtomicLong(); final long start = System.currentTimeMillis(); TcpClient client = TcpClient.create() .host("localhost") .port(timeoutServerPort); Connection s = client.handle((in, out) -> { in.withConnection(c -> c.onDispose(close::countDown)); out.withConnection(c -> c.onWriteIdle(500, () -> { totalDelay.addAndGet(System.currentTimeMillis() - start); latch.countDown(); })); return Mono.delay(Duration.ofSeconds(3)) .then() .log(); }) .wiretap(true) .connectNow(); assertTrue("latch was counted down", latch.await(5, TimeUnit.SECONDS)); assertTrue("close was counted down", close.await(30, TimeUnit.SECONDS)); assertThat("totalDelay was >500ms", totalDelay.get(), greaterThanOrEqualTo(500L)); s.disposeNow(); }
@Test public 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(); latch.await(30, TimeUnit.SECONDS); client.disposeNow(); assertThat("latch was counted down", latch.getCount(), is(0L)); }
@Test @Ignore public void readIdleDoesNotFireWhileDataIsBeingRead() throws InterruptedException, IOException { final CountDownLatch latch = new CountDownLatch(1); long start = System.currentTimeMillis(); TcpClient client = TcpClient.create() .port(heartbeatServerPort); Connection s = client.handle((in, out) -> { in.withConnection(c -> c.onReadIdle(500, latch::countDown)); return Flux.never(); }) .wiretap(true) .connectNow(); assertTrue(latch.await(15, TimeUnit.SECONDS)); heartbeatServer.close(); long duration = System.currentTimeMillis() - start; assertThat(duration, is(greaterThanOrEqualTo(500L))); s.disposeNow(); }
server.disposeNow();
server.disposeNow(); client1.disposeNow(); client2.disposeNow();
@Test public void testTcpClient1ThreadAcquire() { LoopResources resources = LoopResources.create("test", 1, true); Connection client = TcpClient.create() .host("localhost") .port(echoServerPort) .runOn(resources) .wiretap(true) .connectNow(); client.disposeNow(); resources.dispose(); assertThat("client was configured", client instanceof ChannelOperations); }
client.disposeNow();
client.disposeNow();
@Test public void testIssue462() throws InterruptedException { final CountDownLatch countDownLatch = new CountDownLatch(1); DisposableServer server = TcpServer.create() .port(0) .handle((in, out) -> { in.receive() .log("channel") .subscribe(trip -> countDownLatch.countDown()); return Flux.never(); }) .wiretap(true) .bindNow(); assertNotNull(server); System.out.println("PORT +" + server.address() .getPort()); Connection client = TcpClient.create() .port(server.address() .getPort()) .handle((in, out) -> out.sendString(Flux.just("test"))) .wiretap(true) .connectNow(); assertNotNull(client); client.disposeNow(); server.disposeNow(); assertThat("Latch was counted down", countDownLatch.await(5, TimeUnit.SECONDS)); }
client.disposeNow();
String client2Response = m2.block(); client1.disposeNow(); client2.disposeNow(); context.disposeNow();
String client2Response = m2.block(); client1.disposeNow(); client2.disposeNow(); context.disposeNow();
@Test public void exposesRemoteAddress() throws InterruptedException { final int port = SocketUtils.findAvailableTcpPort(); final CountDownLatch latch = new CountDownLatch(1); DisposableServer server = TcpServer.create() .port(port) .handle((in, out) -> { in.withConnection(c -> { InetSocketAddress addr = c.address(); assertNotNull("remote address is not null", addr.getAddress()); latch.countDown(); }); return Flux.never(); }) .wiretap(true) .bindNow(); assertNotNull(server); Connection client = TcpClient.create().port(port) .handle((in, out) -> out.sendString(Flux.just("Hello World!"))) .wiretap(true) .connectNow(); assertNotNull(client); assertTrue("Latch was counted down", latch.await(5, TimeUnit.SECONDS)); client.disposeNow(); server.disposeNow(); }
clientContext.disposeNow();