use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.
the class EmbeddedChannelTest method testFlushInbound.
@Test
public void testFlushInbound() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
latch.countDown();
}
});
channel.flushInbound();
if (!latch.await(1L, TimeUnit.SECONDS)) {
fail("Nobody called #channelReadComplete() in time.");
}
}
use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.
the class ServerBootstrapTest method testParentHandler.
private static void testParentHandler(boolean channelInitializer) throws Exception {
final LocalAddress addr = new LocalAddress(UUID.randomUUID().toString());
final CountDownLatch readLatch = new CountDownLatch(1);
final CountDownLatch initLatch = new CountDownLatch(1);
final ChannelHandler handler = new ChannelInboundHandlerAdapter() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
initLatch.countDown();
super.handlerAdded(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
readLatch.countDown();
super.channelRead(ctx, msg);
}
};
EventLoopGroup group = new DefaultEventLoopGroup(1);
Channel sch = null;
Channel cch = null;
try {
ServerBootstrap sb = new ServerBootstrap();
sb.channel(LocalServerChannel.class).group(group).childHandler(new ChannelInboundHandlerAdapter());
if (channelInitializer) {
sb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(handler);
}
});
} else {
sb.handler(handler);
}
Bootstrap cb = new Bootstrap();
cb.group(group).channel(LocalChannel.class).handler(new ChannelInboundHandlerAdapter());
sch = sb.bind(addr).syncUninterruptibly().channel();
cch = cb.connect(addr).syncUninterruptibly().channel();
initLatch.await();
readLatch.await();
} finally {
if (sch != null) {
sch.close().syncUninterruptibly();
}
if (cch != null) {
cch.close().syncUninterruptibly();
}
group.shutdownGracefully();
}
}
use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.
the class ServerBootstrapTest method testHandlerRegister.
@Test(timeout = 5000)
public void testHandlerRegister() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
LocalEventLoopGroup group = new LocalEventLoopGroup(1);
try {
ServerBootstrap sb = new ServerBootstrap();
sb.channel(LocalServerChannel.class).group(group).childHandler(new ChannelInboundHandlerAdapter()).handler(new ChannelHandlerAdapter() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
try {
assertTrue(ctx.executor().inEventLoop());
} catch (Throwable cause) {
error.set(cause);
} finally {
latch.countDown();
}
}
});
sb.register().syncUninterruptibly();
latch.await();
assertNull(error.get());
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.
the class SocketRstTest method testNoRstIfSoLingerOnClose.
public void testNoRstIfSoLingerOnClose(ServerBootstrap sb, Bootstrap cb) throws Throwable {
final AtomicReference<Channel> serverChannelRef = new AtomicReference<Channel>();
final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
serverChannelRef.compareAndSet(null, ch);
latch.countDown();
}
});
cb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
throwableRef.compareAndSet(null, cause);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
latch2.countDown();
}
});
}
});
Channel sc = sb.bind().sync().channel();
cb.connect(sc.localAddress()).syncUninterruptibly();
// Wait for the server to get setup.
latch.await();
// The server has SO_LINGER=0 and so it must send a RST when close is called.
serverChannelRef.get().close();
// Wait for the client to get channelInactive.
latch2.await();
// Verify the client did not received a RST.
assertNull(throwableRef.get());
}
use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.
the class SocketChannelNotYetConnectedTest method testShutdownNotYetConnected.
public void testShutdownNotYetConnected(Bootstrap cb) throws Throwable {
SocketChannel ch = (SocketChannel) cb.handler(new ChannelInboundHandlerAdapter()).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
try {
try {
ch.shutdownInput().syncUninterruptibly();
fail();
} catch (Throwable cause) {
checkThrowable(cause);
}
try {
ch.shutdownOutput().syncUninterruptibly();
fail();
} catch (Throwable cause) {
checkThrowable(cause);
}
} finally {
ch.close().syncUninterruptibly();
}
}
Aggregations