Search in sources :

Example 6 with BlockingNettyContext

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

the class HttpServerTests method startRouter.

@Test
public void startRouter() {
    BlockingNettyContext facade = HttpServer.create(0).startRouter(routes -> routes.get("/hello", (req, resp) -> resp.sendString(Mono.just("hello!"))));
    try {
        HttpClientResponse res = HttpClient.create(facade.getPort()).get("/hello").block();
        assertThat(res.status().code()).isEqualTo(200);
        res.dispose();
        res = HttpClient.create(facade.getPort()).get("/helloMan", req -> req.failOnClientError(false)).block();
        assertThat(res.status().code()).isEqualTo(404);
        res.dispose();
    } finally {
        facade.shutdown();
    }
}
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) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Test(org.junit.Test)

Example 7 with BlockingNettyContext

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

the class HttpServerTests method startRouterAndAwait.

@Test
public void startRouterAndAwait() throws InterruptedException {
    ExecutorService ex = Executors.newSingleThreadExecutor();
    AtomicReference<BlockingNettyContext> ref = new AtomicReference<>();
    Future<?> f = ex.submit(() -> HttpServer.create(0).startRouterAndAwait(routes -> routes.get("/hello", (req, resp) -> resp.sendString(Mono.just("hello!"))), ref::set));
    // if the server cannot be started, a ExecutionException will be thrown instead
    assertThatExceptionOfType(TimeoutException.class).isThrownBy(() -> f.get(1, TimeUnit.SECONDS));
    // the router is not done and is still blocking the thread
    assertThat(f.isDone()).isFalse();
    assertThat(ref.get()).isNotNull().withFailMessage("Server is not initialized after 1s");
    // shutdown the router to unblock the thread
    ref.get().shutdown();
    Thread.sleep(100);
    assertThat(f.isDone()).isTrue();
}
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) ExecutorService(java.util.concurrent.ExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

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