Example usage for io.netty.channel.embedded EmbeddedChannel pipeline

List of usage examples for io.netty.channel.embedded EmbeddedChannel pipeline

Introduction

In this page you can find the example usage for io.netty.channel.embedded EmbeddedChannel pipeline.

Prototype

@Override
    public ChannelPipeline pipeline() 

Source Link

Usage

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

@Test
public void shouldFireKeepAlive() throws Exception {
    final AtomicInteger keepAliveEventCounter = new AtomicInteger();
    final AtomicReference<ChannelHandlerContext> ctxRef = new AtomicReference();

    QueryHandler testHandler = new QueryHandler(endpoint, responseRingBuffer, queue, false) {
        @Override/*  w  w w.j a v  a 2 s.c  om*/
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            super.channelRegistered(ctx);
            ctxRef.compareAndSet(null, ctx);
        }

        @Override
        protected void onKeepAliveFired(ChannelHandlerContext ctx, CouchbaseRequest keepAliveRequest) {
            assertEquals(1, keepAliveEventCounter.incrementAndGet());
        }

        @Override
        protected void onKeepAliveResponse(ChannelHandlerContext ctx, CouchbaseResponse keepAliveResponse) {
            assertEquals(2, keepAliveEventCounter.incrementAndGet());
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(testHandler);

    //test idle event triggers a query keepAlive request and hook is called
    testHandler.userEventTriggered(ctxRef.get(), IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT);

    assertEquals(1, keepAliveEventCounter.get());
    assertTrue(queue.peek() instanceof QueryHandler.KeepAliveRequest);
    QueryHandler.KeepAliveRequest keepAliveRequest = (QueryHandler.KeepAliveRequest) queue.peek();

    //test responding to the request with http response is interpreted into a KeepAliveResponse and hook is called
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    LastHttpContent responseEnd = new DefaultLastHttpContent();
    channel.writeInbound(response, responseEnd);
    QueryHandler.KeepAliveResponse keepAliveResponse = keepAliveRequest.observable()
            .cast(QueryHandler.KeepAliveResponse.class).timeout(1, TimeUnit.SECONDS).toBlocking().single();

    channel.pipeline().remove(testHandler);
    assertEquals(2, keepAliveEventCounter.get());
    assertEquals(ResponseStatus.NOT_EXISTS, keepAliveResponse.status());
    assertEquals(0, responseEnd.refCnt());
}

From source file:com.couchbase.client.core.endpoint.search.SearchHandlerTest.java

License:Apache License

@Test
public void shouldFireKeepAlive() throws Exception {
    //similar test to query
    final AtomicInteger keepAliveEventCounter = new AtomicInteger();
    final AtomicReference<ChannelHandlerContext> ctxRef = new AtomicReference();

    SearchHandler searchKeepAliveHandler = new SearchHandler(endpoint, responseRingBuffer, queue, false,
            false) {//from  ww w.ja  v  a2  s  .  c om
        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            super.channelRegistered(ctx);
            ctxRef.compareAndSet(null, ctx);
        }

        @Override
        protected void onKeepAliveFired(ChannelHandlerContext ctx, CouchbaseRequest keepAliveRequest) {
            assertEquals(1, keepAliveEventCounter.incrementAndGet());
        }

        @Override
        protected void onKeepAliveResponse(ChannelHandlerContext ctx, CouchbaseResponse keepAliveResponse) {
            assertEquals(2, keepAliveEventCounter.incrementAndGet());
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(searchKeepAliveHandler);

    //test idle event triggers a query keepAlive request and hook is called
    searchKeepAliveHandler.userEventTriggered(ctxRef.get(), IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT);

    assertEquals(1, keepAliveEventCounter.get());
    assertTrue(queue.peek() instanceof SearchHandler.KeepAliveRequest);
    SearchHandler.KeepAliveRequest keepAliveRequest = (SearchHandler.KeepAliveRequest) queue.peek();

    //test responding to the request with http response is interpreted into a KeepAliveResponse and hook is called
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    LastHttpContent responseEnd = new DefaultLastHttpContent();
    channel.writeInbound(response, responseEnd);
    SearchHandler.KeepAliveResponse keepAliveResponse = keepAliveRequest.observable()
            .cast(SearchHandler.KeepAliveResponse.class).timeout(1, TimeUnit.SECONDS).toBlocking().single();

    channel.pipeline().remove(searchKeepAliveHandler);
    assertEquals(2, keepAliveEventCounter.get());
    assertEquals(ResponseStatus.NOT_EXISTS, keepAliveResponse.status());
    assertEquals(0, responseEnd.refCnt());
}

From source file:com.uber.tchannel.handlers.InitDefaultRequestHandlerTest.java

License:Open Source License

@Test
public void testInitHandlerRemovesItself() throws Exception {

    // Given//  ww w  . ja  v a2 s.  c om
    EmbeddedChannel channel = new EmbeddedChannel(new InitRequestHandler(new PeerManager(new Bootstrap())));

    assertEquals(3, channel.pipeline().names().size());

    InitRequestFrame initRequestFrame = new InitRequestFrame(42, InitFrame.DEFAULT_VERSION,
            new HashMap<String, String>() {
                {
                    put(InitFrame.HOST_PORT_KEY, "0.0.0.0:0");
                    put(InitFrame.PROCESS_NAME_KEY, "test-process");
                }
            });

    channel.writeInbound(
            MessageCodec.encode(MessageCodec.encode(PooledByteBufAllocator.DEFAULT, initRequestFrame)));
    channel.writeOutbound(channel.readInbound());

    // Then
    TFrame tFrame = MessageCodec.decode((ByteBuf) channel.readOutbound());
    InitResponseFrame initResponseFrame = (InitResponseFrame) MessageCodec.decode(tFrame);
    tFrame.release();

    // Assert
    assertNotNull(initResponseFrame);
    assertEquals(initRequestFrame.getId(), initResponseFrame.getId());
    assertEquals(initRequestFrame.getVersion(), initResponseFrame.getVersion());
    assertEquals(initRequestFrame.getHostPort(), initResponseFrame.getHostPort());

    // Assert Pipeline is empty
    assertEquals(2, channel.pipeline().names().size());

    // Make sure Messages are still passed through
    channel.writeInbound(initRequestFrame);
    channel.writeOutbound(channel.readInbound());
    InitRequestFrame sameInitRequestFrame = channel.readOutbound();
    assertEquals(initRequestFrame.getId(), sameInitRequestFrame.getId());
    assertEquals(initRequestFrame.getVersion(), sameInitRequestFrame.getVersion());
    assertEquals(initRequestFrame.getHostPort(), sameInitRequestFrame.getHostPort());
}

From source file:com.uber.tchannel.handlers.InitRequestFrameInitiatorTest.java

License:Open Source License

@Test
public void testValidInitResponse() throws Exception {
    // Given//from w  w  w.  ja v a  2  s .c  o m
    PeerManager manager = new PeerManager(new Bootstrap());
    manager.setHostPort(String.format("%s:%d", "127.0.0.1", 8888));
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelRegistrar(manager));

    channel.pipeline().addFirst("InitRequestInitiator", new InitRequestInitiator(manager));
    assertEquals(4, channel.pipeline().names().size());

    TFrame frame = MessageCodec.decode((ByteBuf) channel.readOutbound());
    // Then
    InitRequestFrame initRequestFrame = (InitRequestFrame) MessageCodec.decode(frame);

    frame.release();

    // Assert
    assertNotNull(initRequestFrame);
    // Headers as expected
    assertEquals(initRequestFrame.getHeaders().get("host_port"), "127.0.0.1:8888");
    assertEquals(initRequestFrame.getHeaders().get("process_name"), "java-process");

    channel.writeInbound(MessageCodec.encode(MessageCodec.encode(new InitResponseFrame(initRequestFrame.getId(),
            initRequestFrame.getVersion(), initRequestFrame.getHeaders()))));

    Object obj = channel.readOutbound();
    assertNull(obj);

    assertEquals(3, channel.pipeline().names().size());

}

From source file:io.grpc.alts.internal.GoogleDefaultProtocolNegotiatorTest.java

License:Apache License

@Test
public void altsHandler() {
    Attributes eagAttributes = Attributes.newBuilder().set(GrpcAttributes.ATTR_LB_PROVIDED_BACKEND, true)
            .build();//from w w  w  . j  av a2s  .com
    GrpcHttp2ConnectionHandler mockHandler = mock(GrpcHttp2ConnectionHandler.class);
    when(mockHandler.getEagAttributes()).thenReturn(eagAttributes);

    final AtomicReference<Throwable> failure = new AtomicReference<>();
    ChannelHandler exceptionCaught = new ChannelInboundHandlerAdapter() {
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            failure.set(cause);
            super.exceptionCaught(ctx, cause);
        }
    };
    ChannelHandler h = googleProtocolNegotiator.newHandler(mockHandler);
    EmbeddedChannel chan = new EmbeddedChannel(exceptionCaught);
    // Add the negotiator handler last, but to the front.  Putting this in ctor above would make it
    // throw early.
    chan.pipeline().addFirst(h);
    chan.pipeline().fireUserEventTriggered(InternalProtocolNegotiationEvent.getDefault());

    // Check that the message complained about the ALTS code, rather than SSL.  ALTS throws on
    // being added, so it's hard to catch it at the right time to make this assertion.
    assertThat(failure.get()).hasMessageThat().contains("TsiHandshakeHandler");
}

From source file:io.grpc.alts.internal.GoogleDefaultProtocolNegotiatorTest.java

License:Apache License

@Test
public void tlsHandler() {
    Attributes eagAttributes = Attributes.EMPTY;
    GrpcHttp2ConnectionHandler mockHandler = mock(GrpcHttp2ConnectionHandler.class);
    when(mockHandler.getEagAttributes()).thenReturn(eagAttributes);
    when(mockHandler.getAuthority()).thenReturn("authority");

    ChannelHandler h = googleProtocolNegotiator.newHandler(mockHandler);
    EmbeddedChannel chan = new EmbeddedChannel(h);
    chan.pipeline().fireUserEventTriggered(InternalProtocolNegotiationEvent.getDefault());

    assertThat(chan.pipeline().first().getClass().getSimpleName()).isEqualTo("SslHandler");
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testHttp1xOrH2CHandlerHttp1xRequest() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler());
    ByteBuf buff = HTTP_1_1_POST.copy(0, HTTP_1_1_POST.readableBytes());
    ch.writeInbound(buff);/*from  w w w.  j ava 2 s  .  co  m*/
    assertEquals(0, buff.refCnt());
    assertEquals(1, ch.outboundMessages().size());
    HttpRequest req = (HttpRequest) ch.outboundMessages().poll();
    assertEquals("POST", req.method().name());
    assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class));
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testHttp1xOrH2CHandlerFragmentedHttp1xRequest() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler());
    ByteBuf buff = HTTP_1_1_POST.copy(0, 1);
    ch.writeInbound(buff);/*from  www .  j a va  2  s . c  o m*/
    assertEquals(0, buff.refCnt());
    assertEquals(0, ch.outboundMessages().size());
    buff = HTTP_1_1_POST.copy(1, HTTP_1_1_POST.readableBytes() - 1);
    ch.writeInbound(buff);
    assertEquals(0, buff.refCnt());
    assertEquals(1, ch.outboundMessages().size());
    HttpRequest req = (HttpRequest) ch.outboundMessages().poll();
    assertEquals("POST", req.method().name());
    assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class));
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testHttp1xOrH2CHandlerHttp2Request() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler());
    ByteBuf expected = Unpooled.copiedBuffer(Http1xOrH2CHandler.HTTP_2_PREFACE, StandardCharsets.UTF_8);
    ch.writeInbound(expected);//from  w  ww.  j  a v  a 2 s .  c  om
    assertEquals(1, expected.refCnt());
    assertEquals(1, ch.outboundMessages().size());
    ByteBuf res = (ByteBuf) ch.outboundMessages().poll();
    assertEquals(Http1xOrH2CHandler.HTTP_2_PREFACE, res.toString(StandardCharsets.UTF_8));
    assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class));
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testHttp1xOrH2CHandlerFragmentedHttp2Request() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler());
    ByteBuf expected = Unpooled.copiedBuffer(Http1xOrH2CHandler.HTTP_2_PREFACE, StandardCharsets.UTF_8);
    ByteBuf buff = expected.copy(0, 1);/*from w ww  .  jav a  2 s.c o m*/
    ch.writeInbound(buff);
    assertEquals(0, buff.refCnt());
    assertEquals(0, ch.outboundMessages().size());
    buff = expected.copy(1, expected.readableBytes() - 1);
    ch.writeInbound(buff);
    assertEquals(0, buff.refCnt());
    assertEquals(1, ch.outboundMessages().size());
    ByteBuf res = (ByteBuf) ch.outboundMessages().poll();
    assertEquals(1, res.refCnt());
    assertEquals(Http1xOrH2CHandler.HTTP_2_PREFACE, res.toString(StandardCharsets.UTF_8));
    assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class));
}