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.");
}
}
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();
}
}
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();
}
}
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);
}
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();
}
Aggregations