use of io.netty.channel.local.LocalAddress in project netty by netty.
the class SimpleChannelPoolTest method testChannelAcquiredException.
@Test
public void testChannelAcquiredException() throws InterruptedException {
final LocalAddress addr = new LocalAddress(getLocalAddrId());
final EventLoopGroup group = new DefaultEventLoopGroup();
// Start server
final ServerBootstrap sb = new ServerBootstrap().group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
}
});
final Channel sc = sb.bind(addr).syncUninterruptibly().channel();
// Create pool, acquire and return channels
final Bootstrap bootstrap = new Bootstrap().channel(LocalChannel.class).group(group).remoteAddress(addr);
final NullPointerException exception = new NullPointerException();
final SimpleChannelPool pool = new SimpleChannelPool(bootstrap, new ChannelPoolHandler() {
@Override
public void channelReleased(Channel ch) {
}
@Override
public void channelAcquired(Channel ch) {
throw exception;
}
@Override
public void channelCreated(Channel ch) {
}
});
try {
pool.acquire().sync();
} catch (NullPointerException e) {
assertSame(e, exception);
}
sc.close().sync();
pool.close();
group.shutdownGracefully();
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class SimpleChannelPoolTest method testAcquire.
@Test
public void testAcquire() throws Exception {
EventLoopGroup group = new DefaultEventLoopGroup();
LocalAddress addr = new LocalAddress(getLocalAddrId());
Bootstrap cb = new Bootstrap();
cb.remoteAddress(addr);
cb.group(group).channel(LocalChannel.class);
ServerBootstrap sb = new ServerBootstrap();
sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
}
});
// Start server
Channel sc = sb.bind(addr).sync().channel();
CountingChannelPoolHandler handler = new CountingChannelPoolHandler();
final ChannelPool pool = new SimpleChannelPool(cb, handler);
Channel channel = pool.acquire().sync().getNow();
pool.release(channel).syncUninterruptibly();
final Channel channel2 = pool.acquire().sync().getNow();
assertSame(channel, channel2);
assertEquals(1, handler.channelCount());
pool.release(channel2).syncUninterruptibly();
// Should fail on multiple release calls.
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() throws Throwable {
pool.release(channel2).syncUninterruptibly();
}
});
assertFalse(channel.isActive());
assertEquals(2, handler.acquiredCount());
assertEquals(2, handler.releasedCount());
sc.close().sync();
pool.close();
group.shutdownGracefully();
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class SimpleChannelPoolTest method testUnhealthyChannelIsOfferedWhenNoHealthCheckRequested.
/**
* Tests that if channel was unhealthy it is was offered back to the pool because
* it was requested not to validate channel health on release.
*
* @throws Exception
*/
@Test
public void testUnhealthyChannelIsOfferedWhenNoHealthCheckRequested() throws Exception {
EventLoopGroup group = new DefaultEventLoopGroup();
LocalAddress addr = new LocalAddress(getLocalAddrId());
Bootstrap cb = new Bootstrap();
cb.remoteAddress(addr);
cb.group(group).channel(LocalChannel.class);
ServerBootstrap sb = new ServerBootstrap();
sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
}
});
// Start server
Channel sc = sb.bind(addr).syncUninterruptibly().channel();
ChannelPoolHandler handler = new CountingChannelPoolHandler();
ChannelPool pool = new SimpleChannelPool(cb, handler, ChannelHealthChecker.ACTIVE, false);
Channel channel1 = pool.acquire().syncUninterruptibly().getNow();
channel1.close().syncUninterruptibly();
Future<Void> releaseFuture = pool.release(channel1, channel1.eventLoop().<Void>newPromise()).syncUninterruptibly();
assertThat(releaseFuture.isSuccess(), CoreMatchers.is(true));
Channel channel2 = pool.acquire().syncUninterruptibly().getNow();
// verifying that in fact the channel2 is different that means is not pulled from the pool
assertNotSame(channel1, channel2);
sc.close().syncUninterruptibly();
channel2.close().syncUninterruptibly();
pool.close();
group.shutdownGracefully();
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class SimpleChannelPoolTest method testUnhealthyChannelIsNotOffered.
/**
* Tests that if channel was unhealthy it is not offered back to the pool.
*
* @throws Exception
*/
@Test
public void testUnhealthyChannelIsNotOffered() throws Exception {
EventLoopGroup group = new DefaultEventLoopGroup();
LocalAddress addr = new LocalAddress(getLocalAddrId());
Bootstrap cb = new Bootstrap();
cb.remoteAddress(addr);
cb.group(group).channel(LocalChannel.class);
ServerBootstrap sb = new ServerBootstrap();
sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
}
});
// Start server
Channel sc = sb.bind(addr).syncUninterruptibly().channel();
ChannelPoolHandler handler = new CountingChannelPoolHandler();
ChannelPool pool = new SimpleChannelPool(cb, handler);
Channel channel1 = pool.acquire().syncUninterruptibly().getNow();
pool.release(channel1).syncUninterruptibly();
Channel channel2 = pool.acquire().syncUninterruptibly().getNow();
// first check that when returned healthy then it actually offered back to the pool.
assertSame(channel1, channel2);
channel1.close().syncUninterruptibly();
pool.release(channel1).syncUninterruptibly();
Channel channel3 = pool.acquire().syncUninterruptibly().getNow();
// channel1 was not healthy anymore so it should not get acquired anymore.
assertNotSame(channel1, channel3);
sc.close().syncUninterruptibly();
channel3.close().syncUninterruptibly();
pool.close();
group.shutdownGracefully();
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class DefaultChannelPipelineTest method testCancelBind.
// Tests for https://github.com/netty/netty/issues/2349
@Test
public void testCancelBind() {
ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel());
ChannelPromise promise = pipeline.channel().newPromise();
assertTrue(promise.cancel(false));
ChannelFuture future = pipeline.bind(new LocalAddress("test"), promise);
assertTrue(future.isCancelled());
}
Aggregations