/** * Start a Server in a blocking fashion, and wait for it to finish initializing. The * returned {@link DisposableServer} offers simple server API, including to {@link * DisposableServer#disposeNow()} shut it down in a blocking fashion. The max startup * timeout is 45 seconds. * * @return a {@link DisposableServer} */ public final DisposableServer bindNow() { return bindNow(Duration.ofSeconds(45)); }
/** * Start a Server in a blocking fashion, and wait for it to finish initializing. The * returned {@link DisposableServer} offers simple server API, including to {@link * DisposableServer#disposeNow()} shut it down in a blocking fashion. The max startup * timeout is 45 seconds. * * @return a {@link DisposableServer} */ public final DisposableServer bindNow() { return bindNow(Duration.ofSeconds(45)); }
/** * Start a Server in a fully blocking fashion, not only waiting for it to initialize * but also blocking during the full lifecycle of the client/server. Since most * servers will be long-lived, this is more adapted to running a server out of a main * method, only allowing shutdown of the servers through sigkill. * <p> * Note that a {@link Runtime#addShutdownHook(Thread) JVM shutdown hook} is added by * this method in order to properly disconnect the client/server upon receiving a * sigkill signal. * * @param timeout a timeout for server shutdown * @param onStart an optional callback on server start */ public final void bindUntilJavaShutdown(Duration timeout, @Nullable Consumer<DisposableServer> onStart) { Objects.requireNonNull(timeout, "timeout"); DisposableServer facade = bindNow(); Objects.requireNonNull(facade, "facade"); if (onStart != null) { onStart.accept(facade); } Runtime.getRuntime() .addShutdownHook(new Thread(() -> facade.disposeNow(timeout))); facade.onDispose() .block(); }
/** * Start a Server in a fully blocking fashion, not only waiting for it to initialize * but also blocking during the full lifecycle of the client/server. Since most * servers will be long-lived, this is more adapted to running a server out of a main * method, only allowing shutdown of the servers through sigkill. * <p> * Note that a {@link Runtime#addShutdownHook(Thread) JVM shutdown hook} is added by * this method in order to properly disconnect the client/server upon receiving a * sigkill signal. * * @param timeout a timeout for server shutdown * @param onStart an optional callback on server start */ public final void bindUntilJavaShutdown(Duration timeout, @Nullable Consumer<DisposableServer> onStart) { Objects.requireNonNull(timeout, "timeout"); DisposableServer facade = bindNow(); Objects.requireNonNull(facade, "facade"); if (onStart != null) { onStart.accept(facade); } Runtime.getRuntime() .addShutdownHook(new Thread(() -> facade.disposeNow(timeout))); facade.onDispose() .block(); }
.map(jsonEncoder))) .wiretap(true) .bindNow();
.log("flatmap-retry"))) .wiretap(true) .bindNow();
.concatMap(w -> out.send(w.collectList().map(jsonEncoder)))) .wiretap(true) .bindNow();
@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)); }
}) .wiretap(true) .bindNow();
@Test public void prematureCancel() { DirectProcessor<Void> signal = DirectProcessor.create(); DisposableServer x = TcpServer.create() .host("localhost") .port(0) .handle((in, out) -> { signal.onComplete(); return out.withConnection(c -> c.addHandlerFirst(new HttpResponseEncoder())) .sendObject(Mono.delay(Duration.ofSeconds(2)) .map(t -> new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.PROCESSING))) .neverComplete(); }) .wiretap(true) .bindNow(Duration.ofSeconds(30)); StepVerifier.create( createHttpClientForContextWithAddress(x) .get() .uri("/") .responseContent() .timeout(signal)) .verifyError(TimeoutException.class); }
@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(); }
.then(Mono.delay(Duration.ofSeconds(2)).then())))) .wiretap(true) .bindNow();
out.sendString(Mono.just("NOPE")))) .wiretap(true) .bindNow();
out.sendString(Mono.just("NOPE")))) .wiretap(true) .bindNow();
.neverComplete()) .wiretap(true) .bindNow();