use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpClientTests method testAddressSupplier.
@Test
@SuppressWarnings("deprecation")
void testAddressSupplier() {
DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain())).wiretap(true).bindNow();
Connection conn = TcpClient.create().addressSupplier(server::address).connectNow();
conn.outbound().sendString(Mono.just("testAddressSupplier")).then().subscribe();
String result = conn.inbound().receive().asString().blockFirst();
assertThat(result).isEqualTo("testAddressSupplier");
conn.disposeNow();
server.disposeNow();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpClientTests method testIssue585_1.
@Test
void testIssue585_1() throws Exception {
DisposableServer server = TcpServer.create().port(0).handle((req, res) -> res.send(req.receive().retain())).wiretap(true).bindNow();
CountDownLatch latch = new CountDownLatch(1);
byte[] bytes = "test".getBytes(Charset.defaultCharset());
ByteBuf b1 = Unpooled.wrappedBuffer(bytes);
ByteBuf b2 = Unpooled.wrappedBuffer(bytes);
ByteBuf b3 = Unpooled.wrappedBuffer(bytes);
WeakReference<ByteBuf> refCheck1 = new WeakReference<>(b1);
WeakReference<ByteBuf> refCheck2 = new WeakReference<>(b2);
WeakReference<ByteBuf> refCheck3 = new WeakReference<>(b3);
Connection conn = TcpClient.create().remoteAddress(server::address).wiretap(true).connectNow();
NettyOutbound out = conn.outbound();
Flux.concatDelayError(out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b1).then(), out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b2).then(), out.sendObject(Mono.error(new RuntimeException("test"))).sendObject(b3).then()).doOnError(t -> latch.countDown()).subscribe(conn.disposeSubscriber());
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
assertThat(b1.refCnt()).isEqualTo(0);
b1 = null;
checkReference(refCheck1);
assertThat(b2.refCnt()).isEqualTo(0);
b2 = null;
checkReference(refCheck2);
assertThat(b3.refCnt()).isEqualTo(0);
b3 = null;
checkReference(refCheck3);
server.disposeNow();
conn.disposeNow();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class ServerTransport method bindUntilJavaShutdown.
/**
* Start the server in a fully blocking fashion, not only waiting for it to initialize
* but also blocking during the full lifecycle of the 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 {@code sigkill}.
* <p>
* Note: {@link Runtime#addShutdownHook(Thread) JVM shutdown hook} is added by
* this method in order to properly disconnect the server upon receiving a
* {@code sigkill} signal.</p>
*
* @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 = Objects.requireNonNull(bindNow(), "facade");
if (onStart != null) {
onStart.accept(facade);
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> facade.disposeNow(timeout)));
facade.onDispose().block();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class ITTracingHttpClientDecoratorTest method currentSpanVisibleToUserHandler.
@Test
@SuppressWarnings("try")
public void currentSpanVisibleToUserHandler() {
AtomicReference<HttpHeaders> headers = new AtomicReference<>();
DisposableServer disposableServer = null;
TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
try (Scope scope = currentTraceContext.newScope(parent)) {
disposableServer = HttpServer.create().port(0).handle((req, res) -> {
headers.set(req.requestHeaders());
return res.sendString(Mono.just("test"));
}).bindNow();
client.port(disposableServer.port()).request(HttpMethod.GET).uri("/").send((req, out) -> {
req.header("test-id", currentTraceContext.get().traceIdString());
return out;
}).responseContent().aggregate().block(Duration.ofSeconds(30));
assertThat(headers.get()).isNotNull();
assertThat(headers.get().get("x-b3-traceId")).isEqualTo(headers.get().get("test-id"));
} finally {
if (disposableServer != null) {
disposableServer.disposeNow();
}
}
testSpanHandler.takeRemoteSpan(CLIENT);
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class Application method main.
public static void main(String[] args) {
DisposableServer server = HttpServer.create().metrics(true, // <1>
CustomHttpServerMetricsRecorder::new).route(r -> r.get("/stream/{n}", (req, res) -> res.sendString(Mono.just(req.param("n")))).get("/bytes/{n}", (req, res) -> res.sendString(Mono.just(req.param("n"))))).bindNow();
server.onDispose().block();
}
Aggregations