Skip to content

Instantly share code, notes, and snippets.

@lutovich
Created October 17, 2018 14:48
Show Gist options
  • Save lutovich/dc1fe9414e728d12b29b65c15727df93 to your computer and use it in GitHub Desktop.
Save lutovich/dc1fe9414e728d12b29b65c15727df93 to your computer and use it in GitHub Desktop.
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import org.junit.After;
import org.junit.Test;
import java.net.SocketAddress;
import static java.util.concurrent.TimeUnit.*;
public class NettyPoolCloseTest {
private final EventLoopGroup serverGroup = new NioEventLoopGroup();
private final EventLoopGroup clientGroup = new NioEventLoopGroup(1);
@After
public void tearDown() {
serverGroup.shutdownGracefully();
clientGroup.shutdownGracefully();
}
@Test
public void poolClose() throws Exception {
ServerBootstrap serverBootstrap = createServerBootstrap();
Channel serverChannel = serverBootstrap.bind(0).sync().channel();
SocketAddress serverAddress = serverChannel.localAddress();
Bootstrap clientBootstrap = createClientBootstrap(serverAddress);
FixedChannelPool pool = new FixedChannelPool(clientBootstrap, new EmptyChannelPoolHandler(), 5);
Channel clientChannel = pool.acquire().get(60, SECONDS);
ByteBuf msg = clientChannel.alloc().ioBuffer(4).writeInt(42);
clientChannel.writeAndFlush(msg);
pool.release(clientChannel).get(60, SECONDS);
pool.close();
clientGroup.shutdownGracefully();
clientGroup.terminationFuture().awaitUninterruptibly(); // this line is expected to hang
}
private ServerBootstrap createServerBootstrap() {
return new ServerBootstrap()
.group(serverGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addFirst(new LoggingHandler(LogLevel.DEBUG));
ch.pipeline().addLast(new ServerHandler());
}
});
}
private Bootstrap createClientBootstrap(SocketAddress serverAddress) {
return new Bootstrap()
.group(clientGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
}
})
.remoteAddress(serverAddress);
}
private static class ServerHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
msg.readerIndex(msg.readableBytes());
}
}
private static class EmptyChannelPoolHandler implements ChannelPoolHandler {
@Override
public void channelReleased(Channel ch) {
}
@Override
public void channelAcquired(Channel ch) {
}
@Override
public void channelCreated(Channel ch) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment