Search in sources :

Example 86 with ChannelHandlerContext

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

the class EmbeddedChannelTest method testFlushOutbound.

@Test
public void testFlushOutbound() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            latch.countDown();
        }
    });
    channel.flushOutbound();
    if (!latch.await(1L, TimeUnit.SECONDS)) {
        fail("Nobody called #flush() in time.");
    }
}
Also used : ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Example 87 with ChannelHandlerContext

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

the class LocalChannelTest method testWriteInWritePromiseCompletePreservesOrder.

@Test
public void testWriteInWritePromiseCompletePreservesOrder() throws InterruptedException {
    Bootstrap cb = new Bootstrap();
    ServerBootstrap sb = new ServerBootstrap();
    final CountDownLatch messageLatch = new CountDownLatch(2);
    final ByteBuf data = Unpooled.wrappedBuffer(new byte[1024]);
    final ByteBuf data2 = Unpooled.wrappedBuffer(new byte[512]);
    try {
        cb.group(group1).channel(LocalChannel.class).handler(new TestHandler());
        sb.group(group2).channel(LocalServerChannel.class).childHandler(new ChannelInboundHandlerAdapter() {

            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                final long count = messageLatch.getCount();
                if ((data.equals(msg) && count == 2) || (data2.equals(msg) && count == 1)) {
                    ReferenceCountUtil.safeRelease(msg);
                    messageLatch.countDown();
                } else {
                    super.channelRead(ctx, msg);
                }
            }
        });
        Channel sc = null;
        Channel cc = null;
        try {
            // Start server
            sc = sb.bind(TEST_ADDRESS).syncUninterruptibly().channel();
            // Connect to the server
            cc = cb.connect(sc.localAddress()).syncUninterruptibly().channel();
            final Channel ccCpy = cc;
            // Make sure a write operation is executed in the eventloop
            cc.pipeline().lastContext().executor().execute(new Runnable() {

                @Override
                public void run() {
                    ChannelPromise promise = ccCpy.newPromise();
                    promise.addListener(new ChannelFutureListener() {

                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            ccCpy.writeAndFlush(data2.retainedDuplicate(), ccCpy.newPromise());
                        }
                    });
                    ccCpy.writeAndFlush(data.retainedDuplicate(), promise);
                }
            });
            assertTrue(messageLatch.await(5, SECONDS));
        } finally {
            closeChannel(cc);
            closeChannel(sc);
        }
    } finally {
        data.release();
        data2.release();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 88 with ChannelHandlerContext

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

the class FragmentedMessageDeliveryTest method testPermutation.

private void testPermutation(byte[] unfragmented, ByteBuf[] fragments) throws Exception {
    // Given
    BoltStateMachine machine = mock(BoltStateMachine.class);
    Channel ch = mock(Channel.class);
    when(ch.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(ch);
    BoltProtocolV1 protocol = new BoltProtocolV1(new SynchronousBoltWorker(machine), ch, NullLogService.getInstance());
    // When data arrives split up according to the current permutation
    for (ByteBuf fragment : fragments) {
        fragment.readerIndex(0).retain();
        protocol.handle(ctx, fragment);
    }
    // Then the session should've received the specified messages, and the protocol should be in a nice clean state
    try {
        verify(machine).run(eq("Mjölnir"), anyMapOf(String.class, Object.class), any(BoltResponseHandler.class));
    } catch (AssertionError e) {
        throw new AssertionError("Failed to handle fragmented delivery.\n" + "Messages: " + Arrays.toString(messages) + "\n" + "Chunk size: " + chunkSize + "\n" + "Serialized data delivered in fragments: " + describeFragments(fragments) + "\n" + "Unfragmented data: " + HexPrinter.hex(unfragmented) + "\n", e);
    } finally {
        // To avoid buffer leak errors
        protocol.close();
    }
}
Also used : SynchronousBoltWorker(org.neo4j.bolt.v1.runtime.SynchronousBoltWorker) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) RecordingByteChannel(org.neo4j.bolt.v1.messaging.RecordingByteChannel) Channel(io.netty.channel.Channel) BoltResponseHandler(org.neo4j.bolt.v1.runtime.BoltResponseHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1) ByteBuf(io.netty.buffer.ByteBuf)

Example 89 with ChannelHandlerContext

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

the class SocketTransportHandlerTest method shouldInitializeProtocolOnFirstMessage.

@Test
public void shouldInitializeProtocolOnFirstMessage() throws Exception {
    BoltStateMachine machine = mock(BoltStateMachine.class);
    ProtocolChooser chooser = protocolChooser(machine);
    ChannelHandlerContext context = channelHandlerContextMock();
    SocketTransportHandler handler = new SocketTransportHandler(chooser, NullLogProvider.getInstance());
    handler.channelRead(context, handshake());
    BoltProtocol protocol1 = chooser.chosenProtocol();
    handler.channelRead(context, handshake());
    BoltProtocol protocol2 = chooser.chosenProtocol();
    assertSame(protocol1, protocol2);
}
Also used : BoltProtocol(org.neo4j.bolt.transport.BoltProtocol) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) SocketTransportHandler(org.neo4j.bolt.transport.SocketTransportHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ProtocolChooser(org.neo4j.bolt.transport.ProtocolChooser) Test(org.junit.Test)

Example 90 with ChannelHandlerContext

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

the class SocketTransportHandlerTest method shouldCloseContextWhenProtocolNotInitializedOnHandlerRemoved.

@Test
public void shouldCloseContextWhenProtocolNotInitializedOnHandlerRemoved() throws Throwable {
    // Given
    ChannelHandlerContext context = mock(ChannelHandlerContext.class);
    SocketTransportHandler handler = newSocketTransportHandler(mock(ProtocolChooser.class));
    // When
    handler.handlerRemoved(context);
    // Then
    verify(context).close();
}
Also used : SocketTransportHandler(org.neo4j.bolt.transport.SocketTransportHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ProtocolChooser(org.neo4j.bolt.transport.ProtocolChooser) 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