Search in sources :

Example 1 with BlockingNettyContext

use of reactor.ipc.netty.tcp.BlockingNettyContext in project reactor-netty by reactor.

the class HttpServerTests method defaultHttpPortWithAddress.

@Test
public void defaultHttpPortWithAddress() {
    BlockingNettyContext blockingFacade = HttpServer.create("localhost").start((req, resp) -> resp.sendNotFound());
    blockingFacade.shutdown();
    assertThat(blockingFacade.getPort()).isEqualTo(8080).isEqualTo(blockingFacade.getContext().address().getPort());
}
Also used : BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Test(org.junit.Test)

Example 2 with BlockingNettyContext

use of reactor.ipc.netty.tcp.BlockingNettyContext in project reactor-netty by reactor.

the class HttpServerTests method defaultHttpPort.

@Test
public void defaultHttpPort() {
    BlockingNettyContext blockingFacade = HttpServer.create().start((req, resp) -> resp.sendNotFound());
    blockingFacade.shutdown();
    assertThat(blockingFacade.getPort()).isEqualTo(8080).isEqualTo(blockingFacade.getContext().address().getPort());
}
Also used : BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Test(org.junit.Test)

Example 3 with BlockingNettyContext

use of reactor.ipc.netty.tcp.BlockingNettyContext in project reactor-netty by reactor.

the class HttpServerTests method httpPortOptionTakesPrecedenceOverBuilderField.

@Test
public void httpPortOptionTakesPrecedenceOverBuilderField() {
    HttpServer.Builder builder = HttpServer.builder().options(o -> o.port(9081)).port(9080);
    HttpServer binding = builder.build();
    BlockingNettyContext blockingFacade = binding.start((req, resp) -> resp.sendNotFound());
    blockingFacade.shutdown();
    assertThat(builder).hasFieldOrPropertyWithValue("port", 9080);
    assertThat(blockingFacade.getPort()).isEqualTo(9081).isEqualTo(blockingFacade.getContext().address().getPort());
    assertThat(binding.options().getAddress()).isInstanceOf(InetSocketAddress.class).hasFieldOrPropertyWithValue("port", 9081);
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Path(java.nio.file.Path) Context(reactor.util.context.Context) StandardOpenOption(java.nio.file.StandardOpenOption) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ByteBufFlux(reactor.ipc.netty.ByteBufFlux) HttpVersion(io.netty.handler.codec.http.HttpVersion) FluxSink(reactor.core.publisher.FluxSink) Tuple3(reactor.util.function.Tuple3) Tuple2(reactor.util.function.Tuple2) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteBuf(io.netty.buffer.ByteBuf) Assert(org.testng.Assert) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ExecutorService(java.util.concurrent.ExecutorService) TcpClient(reactor.ipc.netty.tcp.TcpClient) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) CompletionHandler(java.nio.channels.CompletionHandler) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) FileSystems(java.nio.file.FileSystems) InetSocketAddress(java.net.InetSocketAddress) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Test(org.junit.Test)

Example 4 with BlockingNettyContext

use of reactor.ipc.netty.tcp.BlockingNettyContext in project micrometer by micrometer-metrics.

the class AtlasMeterRegistryTest method publishOneLastTimeOnClose.

@Issue("#484")
@Test
void publishOneLastTimeOnClose() throws InterruptedException {
    URI uri = URI.create(config.uri());
    CountDownLatch latch = new CountDownLatch(1);
    BlockingNettyContext blockingFacade = HttpServer.create(uri.getHost(), uri.getPort()).start((req, resp) -> {
        latch.countDown();
        return resp.send();
    });
    try {
        registry.close();
        latch.await(10, TimeUnit.SECONDS);
        assertThat(latch.getCount()).isZero();
    } finally {
        blockingFacade.shutdown();
    }
}
Also used : BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Issue(io.micrometer.core.Issue) Test(org.junit.jupiter.api.Test)

Example 5 with BlockingNettyContext

use of reactor.ipc.netty.tcp.BlockingNettyContext in project reactor-netty by reactor.

the class NettyConnector method startAndAwait.

/**
 * Start a Client or 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 handler the handler to execute.
 * @param onStart an optional callback to be invoked once the client/server has finished
 * initializing.
 */
default <T extends BiFunction<INBOUND, OUTBOUND, ? extends Publisher<Void>>> void startAndAwait(T handler, @Nullable Consumer<BlockingNettyContext> onStart) {
    BlockingNettyContext facade = new BlockingNettyContext(newHandler(handler), getClass().getSimpleName());
    facade.installShutdownHook();
    if (onStart != null) {
        onStart.accept(facade);
    }
    facade.getContext().onClose().block();
}
Also used : BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext)

Aggregations

BlockingNettyContext (reactor.ipc.netty.tcp.BlockingNettyContext)7 CountDownLatch (java.util.concurrent.CountDownLatch)4 ByteBuf (io.netty.buffer.ByteBuf)3 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)3 Unpooled (io.netty.buffer.Unpooled)3 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)3 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)3 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 HttpMethod (io.netty.handler.codec.http.HttpMethod)3 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)3 HttpVersion (io.netty.handler.codec.http.HttpVersion)3 SslContext (io.netty.handler.ssl.SslContext)3 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)3 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)3 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3