Search in sources :

Example 6 with DisposableServer

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();
}
Also used : DisposableServer(reactor.netty.DisposableServer) Connection(reactor.netty.Connection) Test(org.junit.jupiter.api.Test)

Example 7 with DisposableServer

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();
}
Also used : AttributeKey(io.netty.util.AttributeKey) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) SocketAddress(java.net.SocketAddress) AbortedException(reactor.netty.channel.AbortedException) Retry(reactor.util.retry.Retry) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Future(java.util.concurrent.Future) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Duration(java.time.Duration) Logger(reactor.util.Logger) NameResolverProvider(reactor.netty.transport.NameResolverProvider) LoopResources(reactor.netty.resources.LoopResources) ChannelOperations(reactor.netty.channel.ChannelOperations) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyOutbound(reactor.netty.NettyOutbound) DisposableServer(reactor.netty.DisposableServer) ChannelOption(io.netty.channel.ChannelOption) SocketUtils(reactor.netty.SocketUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) Properties(java.util.Properties) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) Publisher(org.reactivestreams.Publisher) NetUtil(io.netty.util.NetUtil) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Bootstrap(io.netty.bootstrap.Bootstrap) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) NettyOutbound(reactor.netty.NettyOutbound) WeakReference(java.lang.ref.WeakReference) Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 8 with DisposableServer

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();
}
Also used : DisposableServer(reactor.netty.DisposableServer)

Example 9 with DisposableServer

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);
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) Nullable(reactor.util.annotation.Nullable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(java.time.Duration) BiConsumer(java.util.function.BiConsumer) AfterClass(org.junit.AfterClass) ChannelGroup(io.netty.channel.group.ChannelGroup) CLIENT(brave.Span.Kind.CLIENT) ITHttpAsyncClient(brave.test.http.ITHttpAsyncClient) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) TraceContext(brave.propagation.TraceContext) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServer(reactor.netty.http.server.HttpServer) Assertions.fail(org.assertj.core.api.Assertions.fail) Scope(brave.propagation.CurrentTraceContext.Scope) SamplingFlags(brave.propagation.SamplingFlags) DisposableServer(reactor.netty.DisposableServer) HttpClient(reactor.netty.http.client.HttpClient) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Scope(brave.propagation.CurrentTraceContext.Scope) DisposableServer(reactor.netty.DisposableServer) TraceContext(brave.propagation.TraceContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 10 with DisposableServer

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();
}
Also used : HttpServer(reactor.netty.http.server.HttpServer) SocketAddress(java.net.SocketAddress) HttpServerMetricsRecorder(reactor.netty.http.server.HttpServerMetricsRecorder) Duration(java.time.Duration) Mono(reactor.core.publisher.Mono) DisposableServer(reactor.netty.DisposableServer) DisposableServer(reactor.netty.DisposableServer)

Aggregations

DisposableServer (reactor.netty.DisposableServer)137 Mono (reactor.core.publisher.Mono)84 Test (org.junit.jupiter.api.Test)79 Duration (java.time.Duration)71 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)71 AtomicReference (java.util.concurrent.atomic.AtomicReference)68 Flux (reactor.core.publisher.Flux)68 Connection (reactor.netty.Connection)67 TimeUnit (java.util.concurrent.TimeUnit)65 LoopResources (reactor.netty.resources.LoopResources)60 List (java.util.List)58 CountDownLatch (java.util.concurrent.CountDownLatch)57 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)56 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)55 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)54 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)54 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)53 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)53 Assumptions.assumeThat (org.assertj.core.api.Assumptions.assumeThat)53 StepVerifier (reactor.test.StepVerifier)53