Search in sources :

Example 51 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class NettyOptionsTest method afterChannelInitThenChannelGroup.

@Test
public void afterChannelInitThenChannelGroup() {
    ChannelGroup group = new DefaultChannelGroup(null);
    List<Channel> initializedChannels = new ArrayList<>();
    NettyContext nettyContext = HttpServer.create(opt -> opt.afterChannelInit(initializedChannels::add).channelGroup(group)).start((req, resp) -> resp.sendNotFound()).getContext();
    HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
    assertThat((Iterable<Channel>) group).hasSize(1).hasSameElementsAs(initializedChannels).doesNotContain(nettyContext.channel());
    resp.dispose();
    nettyContext.dispose();
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpServer(reactor.ipc.netty.http.server.HttpServer) Channel(io.netty.channel.Channel) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 52 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class NettyOptionsTest method afterNettyContextInit.

@Test
public void afterNettyContextInit() {
    AtomicInteger readCount = new AtomicInteger();
    ChannelInboundHandlerAdapter handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            readCount.incrementAndGet();
            super.channelRead(ctx, msg);
        }
    };
    String handlerName = "test";
    NettyContext nettyContext = HttpServer.create(opt -> opt.afterNettyContextInit(c -> c.addHandlerFirst(handlerName, handler))).start((req, resp) -> resp.sendNotFound()).getContext();
    HttpClientResponse response1 = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
    assertThat(response1.status().code()).isEqualTo(404);
    response1.dispose();
    // the "main" context doesn't get enriched with handlers from options...
    assertThat(nettyContext.channel().pipeline().names()).doesNotContain(handlerName);
    // ...but the child channels that are created for requests are
    assertThat(readCount.get()).isEqualTo(1);
    HttpClientResponse response2 = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
    // reactor handler was applied and produced a response
    assertThat(response2.status().code()).isEqualTo(404);
    response2.dispose();
    // BUT channelHandler wasn't applied a second time since not Shareable
    assertThat(readCount.get()).isEqualTo(1);
    nettyContext.dispose();
}
Also used : ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpServer(reactor.ipc.netty.http.server.HttpServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 53 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class TcpClientTests method testTcpClientWithInetSocketAddress.

@Test
public void testTcpClientWithInetSocketAddress() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    TcpClient client = TcpClient.create(echoServerPort);
    NettyContext s = client.newHandler((in, out) -> {
        in.receive().subscribe(d -> latch.countDown());
        return out.sendString(Flux.just("Hello")).neverComplete();
    }).block(Duration.ofSeconds(5));
    latch.await(5, TimeUnit.SECONDS);
    s.dispose();
    assertThat("latch was counted down", latch.getCount(), is(0L));
}
Also used : Arrays(java.util.Arrays) ChannelOption(io.netty.channel.ChannelOption) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) SocketChannel(java.nio.channels.SocketChannel) NettyPipeline(reactor.ipc.netty.NettyPipeline) Duration(java.time.Duration) After(org.junit.After) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Publisher(org.reactivestreams.Publisher) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) List(java.util.List) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyContext(reactor.ipc.netty.NettyContext) ClientOptions(reactor.ipc.netty.options.ClientOptions) Matchers.is(org.hamcrest.Matchers.is) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert.assertEquals(org.junit.Assert.assertEquals) CountDownLatch(java.util.concurrent.CountDownLatch) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 54 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class TcpClientTests method testTcpClient.

@Test
public void testTcpClient() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    NettyContext client = TcpClient.create("localhost", echoServerPort).newHandler((in, out) -> {
        in.receive().log("conn").subscribe(s -> latch.countDown());
        return out.sendString(Flux.just("Hello World!")).neverComplete();
    }).block(Duration.ofSeconds(30));
    latch.await(30, TimeUnit.SECONDS);
    client.dispose();
    assertThat("latch was counted down", latch.getCount(), is(0L));
}
Also used : Arrays(java.util.Arrays) ChannelOption(io.netty.channel.ChannelOption) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) SocketChannel(java.nio.channels.SocketChannel) NettyPipeline(reactor.ipc.netty.NettyPipeline) Duration(java.time.Duration) After(org.junit.After) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Publisher(org.reactivestreams.Publisher) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) List(java.util.List) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) NettyContext(reactor.ipc.netty.NettyContext) ClientOptions(reactor.ipc.netty.options.ClientOptions) Matchers.is(org.hamcrest.Matchers.is) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert.assertEquals(org.junit.Assert.assertEquals) CountDownLatch(java.util.concurrent.CountDownLatch) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 55 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class TcpServerTests method tcpServerCanEncodeAndDecodeJSON.

@Test
public void tcpServerCanEncodeAndDecodeJSON() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Function<Pojo, ByteBuf> jsonEncoder = pojo -> {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            mapper.writeValue(out, pojo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Unpooled.copiedBuffer(out.toByteArray());
    };
    Function<String, Pojo> jsonDecoder = s -> {
        try {
            return mapper.readValue(s, Pojo.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
    CountDownLatch dataLatch = new CountDownLatch(1);
    NettyContext server = TcpServer.create().newHandler((in, out) -> out.send(in.receive().asString().map(jsonDecoder).log().take(1).map(pojo -> {
        Assertions.assertThat(pojo.getName()).isEqualTo("John Doe");
        return new Pojo("Jane Doe");
    }).map(jsonEncoder))).block(Duration.ofSeconds(30));
    SimpleClient client = new SimpleClient(server.address().getPort(), dataLatch, "{\"name\":\"John Doe\"}");
    client.start();
    Assertions.assertThat(dataLatch.await(5, TimeUnit.SECONDS)).isTrue();
    Assertions.assertThat(dataLatch.getCount()).isEqualTo(0);
    Assertions.assertThat(client.e).isNull();
    Assertions.assertThat(client.data).isNotNull();
    Assertions.assertThat(client.data.remaining()).isEqualTo(19);
    Assertions.assertThat(new String(client.data.array())).isEqualTo("{\"name\":\"Jane Doe\"}");
    server.dispose();
}
Also used : URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Processor(org.reactivestreams.Processor) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) After(org.junit.After) Logger(reactor.util.Logger) Assertions(org.assertj.core.api.Assertions) SocketUtils(reactor.ipc.netty.SocketUtils) Path(java.nio.file.Path) JsonObjectDecoder(io.netty.handler.codec.json.JsonObjectDecoder) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) Exceptions(reactor.core.Exceptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) WorkQueueProcessor(reactor.core.publisher.WorkQueueProcessor) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) NettyInbound(reactor.ipc.netty.NettyInbound) NettyPipeline(reactor.ipc.netty.NettyPipeline) EmitterProcessor(reactor.core.publisher.EmitterProcessor) HttpClient(reactor.ipc.netty.http.client.HttpClient) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) HttpServer(reactor.ipc.netty.http.server.HttpServer) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Assert.assertNotNull(org.junit.Assert.assertNotNull) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MonoProcessor(reactor.core.publisher.MonoProcessor) NetUtil(io.netty.util.NetUtil) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) FileSystems(java.nio.file.FileSystems) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) URISyntaxException(java.net.URISyntaxException) SSLException(javax.net.ssl.SSLException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NettyContext(reactor.ipc.netty.NettyContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

NettyContext (reactor.ipc.netty.NettyContext)92 Test (org.junit.Test)87 Mono (reactor.core.publisher.Mono)86 Duration (java.time.Duration)83 Flux (reactor.core.publisher.Flux)78 InetSocketAddress (java.net.InetSocketAddress)65 HttpServer (reactor.ipc.netty.http.server.HttpServer)63 HttpClient (reactor.ipc.netty.http.client.HttpClient)61 CountDownLatch (java.util.concurrent.CountDownLatch)60 StepVerifier (reactor.test.StepVerifier)60 AtomicReference (java.util.concurrent.atomic.AtomicReference)56 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)53 Unpooled (io.netty.buffer.Unpooled)50 TimeUnit (java.util.concurrent.TimeUnit)44 StandardCharsets (java.nio.charset.StandardCharsets)43 Ignore (org.junit.Ignore)43 SslContext (io.netty.handler.ssl.SslContext)42 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)42 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)42 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)42