use of io.netty.channel.ChannelInboundHandler in project neo4j by neo4j.
the class RequestDecoderDispatcher method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ChannelInboundHandler delegate = protocol.select(decoders);
if (delegate == null) {
log.warn("Unregistered handler for protocol %s", protocol);
/*
* Since we cannot process this message further we need to release the message as per netty doc
* see http://netty.io/wiki/reference-counted-objects.html#inbound-messages
*/
ReferenceCountUtil.release(msg);
return;
}
delegate.channelRead(ctx, msg);
}
use of io.netty.channel.ChannelInboundHandler in project netty by netty.
the class SocketFileRegionTest method testFileRegion0.
private static void testFileRegion0(ServerBootstrap sb, Bootstrap cb, boolean voidPromise, final boolean autoRead, boolean defaultFileRegion) throws Throwable {
sb.childOption(ChannelOption.AUTO_READ, autoRead);
cb.option(ChannelOption.AUTO_READ, autoRead);
final int bufferSize = 1024;
final File file = File.createTempFile("netty-", ".tmp");
file.deleteOnExit();
final FileOutputStream out = new FileOutputStream(file);
final Random random = PlatformDependent.threadLocalRandom();
// Prepend random data which will not be transferred, so that we can test non-zero start offset
final int startOffset = random.nextInt(8192);
for (int i = 0; i < startOffset; i++) {
out.write(random.nextInt());
}
// .. and here comes the real data to transfer.
out.write(data, bufferSize, data.length - bufferSize);
// .. and then some extra data which is not supposed to be transferred.
for (int i = random.nextInt(8192); i > 0; i--) {
out.write(random.nextInt());
}
out.close();
ChannelInboundHandler ch = new SimpleChannelInboundHandler<Object>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
if (!autoRead) {
ctx.read();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
};
TestHandler sh = new TestHandler(autoRead);
sb.childHandler(sh);
cb.handler(ch);
Channel sc = sb.bind().sync().channel();
Channel cc = cb.connect().sync().channel();
FileRegion region = new DefaultFileRegion(new FileInputStream(file).getChannel(), startOffset, data.length - bufferSize);
FileRegion emptyRegion = new DefaultFileRegion(new FileInputStream(file).getChannel(), 0, 0);
if (!defaultFileRegion) {
region = new FileRegionWrapper(region);
emptyRegion = new FileRegionWrapper(emptyRegion);
}
// https://github.com/netty/netty/issues/2964
if (voidPromise) {
assertEquals(cc.voidPromise(), cc.write(Unpooled.wrappedBuffer(data, 0, bufferSize), cc.voidPromise()));
assertEquals(cc.voidPromise(), cc.write(emptyRegion, cc.voidPromise()));
assertEquals(cc.voidPromise(), cc.writeAndFlush(region, cc.voidPromise()));
} else {
assertNotEquals(cc.voidPromise(), cc.write(Unpooled.wrappedBuffer(data, 0, bufferSize)));
assertNotEquals(cc.voidPromise(), cc.write(emptyRegion));
assertNotEquals(cc.voidPromise(), cc.writeAndFlush(region));
}
while (sh.counter < data.length) {
if (sh.exception.get() != null) {
break;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// Ignore.
}
}
sh.channel.close().sync();
cc.close().sync();
sc.close().sync();
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
throw sh.exception.get();
}
if (sh.exception.get() != null) {
throw sh.exception.get();
}
// Make sure we did not receive more than we expected.
assertThat(sh.counter, is(data.length));
}
use of io.netty.channel.ChannelInboundHandler in project neo4j by neo4j.
the class RequestDecoderDispatcherTest method shouldDispatchToRegisteredDecoder.
@Test
public void shouldDispatchToRegisteredDecoder() throws Exception {
// given
RequestDecoderDispatcher<State> dispatcher = new RequestDecoderDispatcher<>(protocol, logProvider);
ChannelInboundHandler delegateOne = mock(ChannelInboundHandler.class);
ChannelInboundHandler delegateTwo = mock(ChannelInboundHandler.class);
ChannelInboundHandler delegateThree = mock(ChannelInboundHandler.class);
dispatcher.register(State.one, delegateOne);
dispatcher.register(State.two, delegateTwo);
dispatcher.register(State.three, delegateThree);
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
Object msg = new Object();
// when
dispatcher.channelRead(ctx, msg);
// then
verify(delegateTwo).channelRead(ctx, msg);
verifyNoMoreInteractions(delegateTwo);
verifyZeroInteractions(delegateOne, delegateThree);
}
use of io.netty.channel.ChannelInboundHandler in project grpc-java by grpc.
the class ProtocolNegotiatorsTest method httpProxy_completes.
@Test(timeout = 5000)
public void httpProxy_completes() throws Exception {
DefaultEventLoopGroup elg = new DefaultEventLoopGroup(1);
// ProxyHandler is incompatible with EmbeddedChannel because when channelRegistered() is called
// the channel is already active.
LocalAddress proxy = new LocalAddress("httpProxy_completes");
SocketAddress host = InetSocketAddress.createUnresolved("specialHost", 314);
ChannelInboundHandler mockHandler = mock(ChannelInboundHandler.class);
Channel serverChannel = new ServerBootstrap().group(elg).channel(LocalServerChannel.class).childHandler(mockHandler).bind(proxy).sync().channel();
ProtocolNegotiator nego = ProtocolNegotiators.httpProxy(proxy, null, null, ProtocolNegotiators.plaintext());
ChannelHandler handler = nego.newHandler(grpcHandler);
Channel channel = new Bootstrap().group(elg).channel(LocalChannel.class).handler(handler).register().sync().channel();
pipeline = channel.pipeline();
// Wait for initialization to complete
channel.eventLoop().submit(NOOP_RUNNABLE).sync();
// The grpcHandler must be in the pipeline, but we don't actually want it during our test
// because it will consume all events since it is a mock. We only use it because it is required
// to construct the Handler.
pipeline.remove(grpcHandler);
channel.connect(host).sync();
serverChannel.close();
ArgumentCaptor<ChannelHandlerContext> contextCaptor = ArgumentCaptor.forClass(ChannelHandlerContext.class);
Mockito.verify(mockHandler).channelActive(contextCaptor.capture());
ChannelHandlerContext serverContext = contextCaptor.getValue();
final String golden = "isThisThingOn?";
ChannelFuture negotiationFuture = channel.writeAndFlush(bb(golden, channel));
// Wait for sending initial request to complete
channel.eventLoop().submit(NOOP_RUNNABLE).sync();
ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
Mockito.verify(mockHandler).channelRead(any(ChannelHandlerContext.class), objectCaptor.capture());
ByteBuf b = (ByteBuf) objectCaptor.getValue();
String request = b.toString(UTF_8);
b.release();
assertTrue("No trailing newline: " + request, request.endsWith("\r\n\r\n"));
assertTrue("No CONNECT: " + request, request.startsWith("CONNECT specialHost:314 "));
assertTrue("No host header: " + request, request.contains("host: specialHost:314"));
assertFalse(negotiationFuture.isDone());
serverContext.writeAndFlush(bb("HTTP/1.1 200 OK\r\n\r\n", serverContext.channel())).sync();
negotiationFuture.sync();
channel.eventLoop().submit(NOOP_RUNNABLE).sync();
objectCaptor.getAllValues().clear();
Mockito.verify(mockHandler, times(2)).channelRead(any(ChannelHandlerContext.class), objectCaptor.capture());
b = (ByteBuf) objectCaptor.getAllValues().get(1);
// If we were using the real grpcHandler, this would have been the HTTP/2 preface
String preface = b.toString(UTF_8);
b.release();
assertEquals(golden, preface);
channel.close();
}
use of io.netty.channel.ChannelInboundHandler in project neo4j by neo4j.
the class RequestDecoderDispatcherTest method shouldLogAWarningIfThereIsNoDecoderForTheMessageType.
@Test
public void shouldLogAWarningIfThereIsNoDecoderForTheMessageType() throws Exception {
// given
RequestDecoderDispatcher<State> dispatcher = new RequestDecoderDispatcher<>(protocol, logProvider);
ChannelInboundHandler delegateOne = mock(ChannelInboundHandler.class);
ChannelInboundHandler delegateThree = mock(ChannelInboundHandler.class);
dispatcher.register(State.one, delegateOne);
dispatcher.register(State.three, delegateThree);
// when
dispatcher.channelRead(mock(ChannelHandlerContext.class), new Object());
// then
AssertableLogProvider.LogMatcher matcher = inLog(RequestDecoderDispatcher.class).warn("Unregistered handler for protocol %s", protocol);
logProvider.assertExactly(matcher);
verifyZeroInteractions(delegateOne, delegateThree);
}
Aggregations