use of io.netty.channel.local.LocalAddress in project netty by netty.
the class SimpleChannelPoolTest method testBoundedChannelPoolSegment.
@Test
public void testBoundedChannelPoolSegment() 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, ChannelHealthChecker.ACTIVE) {
private final Queue<Channel> queue = new LinkedBlockingQueue<Channel>(1);
@Override
protected Channel pollChannel() {
return queue.poll();
}
@Override
protected boolean offerChannel(Channel ch) {
return queue.offer(ch);
}
};
Channel channel = pool.acquire().sync().getNow();
final Channel channel2 = pool.acquire().sync().getNow();
pool.release(channel).syncUninterruptibly().getNow();
assertThrows(IllegalStateException.class, new Executable() {
@Override
public void execute() throws Throwable {
pool.release(channel2).syncUninterruptibly();
}
});
channel2.close().sync();
assertEquals(2, handler.channelCount());
assertEquals(2, handler.acquiredCount());
assertEquals(1, handler.releasedCount());
sc.close().sync();
channel.close().sync();
channel2.close().sync();
pool.close();
group.shutdownGracefully();
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class SimpleChannelPoolTest method testCloseAsync.
@Test
public void testCloseAsync() throws Exception {
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 SimpleChannelPool pool = new SimpleChannelPool(bootstrap, new CountingChannelPoolHandler());
Channel ch1 = pool.acquire().syncUninterruptibly().getNow();
Channel ch2 = pool.acquire().syncUninterruptibly().getNow();
pool.release(ch1).get(1, TimeUnit.SECONDS);
pool.release(ch2).get(1, TimeUnit.SECONDS);
// Assert that returned channels are open before close
assertTrue(ch1.isOpen());
assertTrue(ch2.isOpen());
// Close asynchronously with timeout
pool.closeAsync().get(1, TimeUnit.SECONDS);
// Assert channels were indeed closed
assertFalse(ch1.isOpen());
assertFalse(ch2.isOpen());
sc.close().sync();
pool.close();
group.shutdownGracefully();
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class DefaultChannelPipelineTest method testCancelConnect.
@Test
public void testCancelConnect() {
ChannelPipeline pipeline = new LocalChannel().pipeline();
group.register(pipeline.channel());
ChannelPromise promise = pipeline.channel().newPromise();
assertTrue(promise.cancel(false));
ChannelFuture future = pipeline.connect(new LocalAddress("test"), promise);
assertTrue(future.isCancelled());
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class AbstractChannelPoolMapTest method testMap.
@Test
public void testMap() throws Exception {
EventLoopGroup group = new LocalEventLoopGroup();
LocalAddress addr = new LocalAddress(getLocalAddrId());
final Bootstrap cb = new Bootstrap();
cb.remoteAddress(addr);
cb.group(group).channel(LocalChannel.class);
AbstractChannelPoolMap<EventLoop, SimpleChannelPool> poolMap = new AbstractChannelPoolMap<EventLoop, SimpleChannelPool>() {
@Override
protected SimpleChannelPool newPool(EventLoop key) {
return new SimpleChannelPool(cb.clone(key), new TestChannelPoolHandler());
}
};
EventLoop loop = group.next();
assertFalse(poolMap.iterator().hasNext());
assertEquals(0, poolMap.size());
final SimpleChannelPool pool = poolMap.get(loop);
assertEquals(1, poolMap.size());
assertTrue(poolMap.iterator().hasNext());
assertSame(pool, poolMap.get(loop));
assertTrue(poolMap.remove(loop));
assertFalse(poolMap.remove(loop));
assertFalse(poolMap.iterator().hasNext());
assertEquals(0, poolMap.size());
assertThrows(ConnectException.class, new Executable() {
@Override
public void execute() throws Throwable {
pool.acquire().syncUninterruptibly();
}
});
poolMap.close();
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class FixedChannelPoolTest method testCloseAsync.
@Test
public void testCloseAsync() throws ExecutionException, InterruptedException {
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
final Channel sc = sb.bind(addr).syncUninterruptibly().channel();
final FixedChannelPool pool = new FixedChannelPool(cb, new TestChannelPoolHandler(), 2);
pool.acquire().get();
pool.acquire().get();
final ChannelPromise closePromise = sc.newPromise();
pool.closeAsync().addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> future) throws Exception {
assertEquals(0, pool.acquiredChannelCount());
sc.close(closePromise).syncUninterruptibly();
}
}).awaitUninterruptibly();
closePromise.awaitUninterruptibly();
}
Aggregations