Search in sources :

Example 91 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project neo4j by neo4j.

the class SocketTransportHandlerTest method shouldCloseProtocolOnChannelInactive.

@Test
public void shouldCloseProtocolOnChannelInactive() throws Throwable {
    // Given
    BoltStateMachine machine = mock(BoltStateMachine.class);
    ChannelHandlerContext ctx = channelHandlerContextMock();
    SocketTransportHandler handler = newSocketTransportHandler(protocolChooser(machine));
    // And Given a session has been established
    handler.channelRead(ctx, handshake());
    // When
    handler.channelInactive(ctx);
    // Then
    verify(machine).close();
}
Also used : BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) SocketTransportHandler(org.neo4j.bolt.transport.SocketTransportHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.Test)

Example 92 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project neo4j by neo4j.

the class SocketTransportHandlerTest method channelHandlerContextMock.

private static ChannelHandlerContext channelHandlerContextMock() {
    Channel channel = mock(Channel.class);
    ChannelHandlerContext context = mock(ChannelHandlerContext.class);
    when(context.channel()).thenReturn(channel);
    when(channel.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(context.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    return context;
}
Also used : Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Example 93 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class HttpClientCodecTest method testServerCloseSocketInputProvidesData.

@Test
public void testServerCloseSocketInputProvidesData() throws InterruptedException {
    ServerBootstrap sb = new ServerBootstrap();
    Bootstrap cb = new Bootstrap();
    final CountDownLatch serverChannelLatch = new CountDownLatch(1);
    final CountDownLatch responseRecievedLatch = new CountDownLatch(1);
    try {
        sb.group(new NioEventLoopGroup(2));
        sb.channel(NioServerSocketChannel.class);
        sb.childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                // Don't use the HttpServerCodec, because we don't want to have content-length or anything added.
                ch.pipeline().addLast(new HttpRequestDecoder(4096, 8192, 8192, true));
                ch.pipeline().addLast(new HttpObjectAggregator(4096));
                ch.pipeline().addLast(new SimpleChannelInboundHandler<FullHttpRequest>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) {
                        // This is just a simple demo...don't block in IO
                        assertTrue(ctx.channel() instanceof SocketChannel);
                        final SocketChannel sChannel = (SocketChannel) ctx.channel();
                        /**
                             * The point of this test is to not add any content-length or content-encoding headers
                             * and the client should still handle this.
                             * See <a href="https://tools.ietf.org/html/rfc7230#section-3.3.3">RFC 7230, 3.3.3</a>.
                             */
                        sChannel.writeAndFlush(Unpooled.wrappedBuffer(("HTTP/1.0 200 OK\r\n" + "Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n" + "Content-Type: text/html\r\n\r\n").getBytes(CharsetUtil.ISO_8859_1))).addListener(new ChannelFutureListener() {

                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                assertTrue(future.isSuccess());
                                sChannel.writeAndFlush(Unpooled.wrappedBuffer("<html><body>hello half closed!</body></html>\r\n".getBytes(CharsetUtil.ISO_8859_1))).addListener(new ChannelFutureListener() {

                                    @Override
                                    public void operationComplete(ChannelFuture future) throws Exception {
                                        assertTrue(future.isSuccess());
                                        sChannel.shutdownOutput();
                                    }
                                });
                            }
                        });
                    }
                });
                serverChannelLatch.countDown();
            }
        });
        cb.group(new NioEventLoopGroup(1));
        cb.channel(NioSocketChannel.class);
        cb.option(ChannelOption.ALLOW_HALF_CLOSURE, true);
        cb.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new HttpClientCodec(4096, 8192, 8192, true, true));
                ch.pipeline().addLast(new HttpObjectAggregator(4096));
                ch.pipeline().addLast(new SimpleChannelInboundHandler<FullHttpResponse>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) {
                        responseRecievedLatch.countDown();
                    }
                });
            }
        });
        Channel serverChannel = sb.bind(new InetSocketAddress(0)).sync().channel();
        int port = ((InetSocketAddress) serverChannel.localAddress()).getPort();
        ChannelFuture ccf = cb.connect(new InetSocketAddress(NetUtil.LOCALHOST, port));
        assertTrue(ccf.awaitUninterruptibly().isSuccess());
        Channel clientChannel = ccf.channel();
        assertTrue(serverChannelLatch.await(5, SECONDS));
        clientChannel.writeAndFlush(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
        assertTrue(responseRecievedLatch.await(5, SECONDS));
    } finally {
        sb.config().group().shutdownGracefully();
        sb.config().childGroup().shutdownGracefully();
        cb.config().group().shutdownGracefully();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) PrematureChannelClosureException(io.netty.handler.codec.PrematureChannelClosureException) CodecException(io.netty.handler.codec.CodecException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 94 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class HttpContentDecoderTest method testExpectContinueResetHttpObjectDecoder.

@Test
public void testExpectContinueResetHttpObjectDecoder() {
    // request with header "Expect: 100-continue" must be replied with one "100 Continue" response
    // case 5: Test that HttpObjectDecoder correctly resets its internal state after a failed expectation.
    HttpRequestDecoder decoder = new HttpRequestDecoder();
    final int maxBytes = 10;
    HttpObjectAggregator aggregator = new HttpObjectAggregator(maxBytes);
    final AtomicReference<FullHttpRequest> secondRequestRef = new AtomicReference<FullHttpRequest>();
    EmbeddedChannel channel = new EmbeddedChannel(decoder, aggregator, new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            if (msg instanceof FullHttpRequest) {
                if (!secondRequestRef.compareAndSet(null, (FullHttpRequest) msg)) {
                    ((FullHttpRequest) msg).release();
                }
            } else {
                ReferenceCountUtil.release(msg);
            }
        }
    });
    String req1 = "POST /1 HTTP/1.1\r\n" + "Content-Length: " + (maxBytes + 1) + "\r\n" + "Expect: 100-continue\r\n" + "\r\n";
    assertFalse(channel.writeInbound(Unpooled.wrappedBuffer(req1.getBytes(CharsetUtil.US_ASCII))));
    FullHttpResponse resp = channel.readOutbound();
    assertEquals(HttpStatusClass.CLIENT_ERROR, resp.status().codeClass());
    resp.release();
    String req2 = "POST /2 HTTP/1.1\r\n" + "Content-Length: " + maxBytes + "\r\n" + "Expect: 100-continue\r\n" + "\r\n";
    assertFalse(channel.writeInbound(Unpooled.wrappedBuffer(req2.getBytes(CharsetUtil.US_ASCII))));
    resp = channel.readOutbound();
    assertEquals(100, resp.status().code());
    resp.release();
    byte[] content = new byte[maxBytes];
    assertFalse(channel.writeInbound(Unpooled.wrappedBuffer(content)));
    FullHttpRequest req = secondRequestRef.get();
    assertNotNull(req);
    assertEquals("/2", req.uri());
    assertEquals(10, req.content().readableBytes());
    req.release();
    assertHasInboundMessages(channel, false);
    assertHasOutboundMessages(channel, false);
    assertFalse(channel.finish());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 95 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class HttpObjectAggregatorTest method testSetMaxCumulationBufferComponentsAfterInit.

@Test(expected = IllegalStateException.class)
public void testSetMaxCumulationBufferComponentsAfterInit() throws Exception {
    HttpObjectAggregator aggr = new HttpObjectAggregator(Integer.MAX_VALUE);
    ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
    aggr.handlerAdded(ctx);
    Mockito.verifyNoMoreInteractions(ctx);
    aggr.setMaxCumulationBufferComponents(10);
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.Test)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)266 Test (org.junit.Test)150 Channel (io.netty.channel.Channel)106 ByteBuf (io.netty.buffer.ByteBuf)94 ChannelFuture (io.netty.channel.ChannelFuture)87 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)77 Bootstrap (io.netty.bootstrap.Bootstrap)69 ChannelPipeline (io.netty.channel.ChannelPipeline)68 ClosedChannelException (java.nio.channels.ClosedChannelException)63 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)57 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)56 InetSocketAddress (java.net.InetSocketAddress)54 AtomicReference (java.util.concurrent.atomic.AtomicReference)53 EventLoopGroup (io.netty.channel.EventLoopGroup)48 CountDownLatch (java.util.concurrent.CountDownLatch)47 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)45 ArrayList (java.util.ArrayList)45 List (java.util.List)43 IOException (java.io.IOException)42 Map (java.util.Map)40