/** * Send String to the peer, listen for any error on write and close on terminal signal * (complete|error). If more than one publisher is attached (multiple calls to send()) * completion occurs after all publishers complete. * Note: Nesting any send* method is not supported. * * @param dataStream the dataStream publishing Buffer items to write on this channel * * @return A Publisher to signal successful sequence write (e.g. after "flush") or any * error during write */ default NettyOutbound sendString(Publisher<? extends String> dataStream) { return sendString(dataStream, Charset.defaultCharset()); }
/** * Send String to the peer, listen for any error on write and close on terminal signal * (complete|error). If more than one publisher is attached (multiple calls to send()) * completion occurs after all publishers complete. * Note: Nesting any send* method is not supported. * * @param dataStream the dataStream publishing Buffer items to write on this channel * * @return A Publisher to signal successful sequence write (e.g. after "flush") or any * error during write */ default NettyOutbound sendString(Publisher<? extends String> dataStream) { return sendString(dataStream, Charset.defaultCharset()); }
@Test(timeout = 2000) public void startAndAwait() throws InterruptedException { AtomicReference<DisposableServer> conn = new AtomicReference<>(); CountDownLatch startLatch = new CountDownLatch(1); Thread t = new Thread(() -> TcpServer.create() .handle((in, out) -> out.sendString(Mono.just("foo"))) .bindUntilJavaShutdown(Duration.ofMillis(200), c -> { conn.set(c); startLatch.countDown(); })); t.start(); //let the server initialize startLatch.await(); //check nothing happens for 200ms t.join(200); Assertions.assertThat(t.isAlive()).isTrue(); //check that stopping the bnc stops the server conn.get().disposeNow(); t.join(); Assertions.assertThat(t.isAlive()).isFalse(); }
@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)); }
private void tcpClientHandlesLineFeedData(TcpClient client) throws InterruptedException { final int messages = 100; final CountDownLatch latch = new CountDownLatch(messages); final List<String> strings = new ArrayList<>(); Connection c = client.handle((in, out) -> out.sendString(Flux.range(1, messages) .map(i -> "Hello World!" + i + "\n") .subscribeOn(Schedulers.parallel())) .then( in.receive() .asString() .take(100) .flatMapIterable(s -> Arrays.asList(s.split("\\n"))) .doOnNext(s -> { strings.add(s); latch.countDown(); }).then()) ) .wiretap(true) .connectNow(Duration.ofSeconds(30)); System.out.println("Connected"); c.onDispose() .log() .block(Duration.ofSeconds(30)); assertTrue("Expected messages not received. Received " + strings.size() + " messages: " + strings, latch.await(15, TimeUnit.SECONDS)); assertEquals(messages, strings.size()); }
@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(); }
@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)); }
new LineBasedFrameDecoder(8 * 1024))) .handle((in, out) -> out.sendString(Flux.range(1, messages) .map(i -> "Hello World!" + i + "\n") .subscribeOn(Schedulers.parallel()))
@Test public void testChannelInactiveThrowsIOException() throws Exception { ExecutorService threadPool = Executors.newCachedThreadPool(); int abortServerPort = SocketUtils.findAvailableTcpPort(); ConnectionAbortServer abortServer = new ConnectionAbortServer(abortServerPort); Future<?> f = threadPool.submit(abortServer); if(!abortServer.await(10, TimeUnit.SECONDS)){ throw new IOException("Fail to start test server"); } ByteBufFlux response = HttpClient.create() .port(abortServerPort) .wiretap(true) .request(HttpMethod.GET) .uri("/") .send((req, out) -> out.sendString(Flux.just("a", "b", "c"))) .responseContent(); StepVerifier.create(response.log()) .expectError(IOException.class) .verify(); abortServer.close(); assertThat(f.get()).isNull(); }
@Test @Ignore public void wsTest() { HttpServer server = HttpServer.create(); server.route(r -> r.get("/search/{search}", (in, out) -> HttpClient.create() .wiretap(true) .post() .uri("ws://localhost:3000") .send((requestOut, o) -> o.sendString(Mono.just("ping"))) .response((repliesOut, buf) -> out.sendGroups(buf.window(100))))) .wiretap(true) .bindNow() .onDispose() .block(Duration.ofSeconds(30)); }
@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)); }
.handle((in, out) -> out.options(o -> o.flushOnEach(false)) .sendString(in.receive() .asString() .doOnNext(s -> { .handle((in, out) -> out.options(sendOptions -> sendOptions.flushOnEach(false)) .sendString(Flux.just("1\n", "2\n", "3\n", "4\n")) .then(in.receive() .asString()
.handle((in, out) -> { try { out.sendString(Flux.just("Hello World!")) .then() .block();
.handle((in, out) -> out.sendWebsocket( (i, o) -> o.options(NettyPipeline.SendOptions::flushOnEach) .sendString( Mono.just("test") .delayElement(Duration.ofMillis(100))
.post() .send((req, out) -> out.sendString(Mono.subscriberContext() .map(ctx -> ctx.getOrDefault("test", "fail")))) .responseContent()
@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(); }
.uri("/test") .handle((i, o) -> o.options(NettyPipeline.SendOptions::flushOnEach) .sendString(i.receive() .asString() .subscribeWith(client)))
.handle((in, out) -> out.sendWebsocket( (i, o) -> o.options(NettyPipeline.SendOptions::flushOnEach) .sendString( Mono.just("test") .delayElement(Duration.ofMillis(100))
client.handle((in, out) -> out.sendString(Flux.just("Hello World!\n", "Hello 11!\n"))) .wiretap(true) .connectNow();
.wiretap(true) .handle((req, res) -> res.sendHeaders().sendString(Flux.just("test", "test", "test", "test"))) .bindNow();