use of io.netty.channel.EventLoop in project netty by netty.
the class DefaultDnsCnameCacheTest method testClear.
@Test
public void testClear() throws Exception {
EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
cache.cache("x.netty.io", "mapping.netty.io", 100000, loop);
cache.cache("y.netty.io", "mapping.netty.io", 100000, loop);
assertEquals("mapping.netty.io", cache.get("x.netty.io"));
assertEquals("mapping.netty.io", cache.get("y.netty.io"));
assertTrue(cache.clear("x.netty.io"));
assertNull(cache.get("x.netty.io"));
assertEquals("mapping.netty.io", cache.get("y.netty.io"));
cache.clear();
assertNull(cache.get("y.netty.io"));
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class AbstractSingleThreadEventLoopTest method gracefulShutdownAfterStart.
@Test
public void gracefulShutdownAfterStart() throws Exception {
EventLoop loop = newEventLoopGroup().next();
final CountDownLatch latch = new CountDownLatch(1);
loop.execute(new Runnable() {
@Override
public void run() {
latch.countDown();
}
});
// Wait for the event loop thread to start.
latch.await();
// Request the event loop thread to stop.
loop.shutdownGracefully(200L, 3000L, TimeUnit.MILLISECONDS);
// Wait until the event loop is terminated.
assertTrue(loop.awaitTermination(500L, TimeUnit.MILLISECONDS));
assertRejection(loop);
}
use of io.netty.channel.EventLoop in project netty by netty.
the class NioEventLoop method processSelectedKey.
private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
if (!k.isValid()) {
final EventLoop eventLoop;
try {
eventLoop = ch.eventLoop();
} catch (Throwable ignored) {
// to close ch.
return;
}
// See https://github.com/netty/netty/issues/5125
if (eventLoop == this) {
// close the channel if the key is not valid anymore
unsafe.close(unsafe.voidPromise());
}
return;
}
try {
int readyOps = k.readyOps();
// the NIO JDK channel implementation may throw a NotYetConnectedException.
if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
// remove OP_CONNECT as otherwise Selector.select(..) will always return without blocking
// See https://github.com/netty/netty/issues/924
int ops = k.interestOps();
ops &= ~SelectionKey.OP_CONNECT;
k.interestOps(ops);
unsafe.finishConnect();
}
// Process OP_WRITE first as we may be able to write some queued buffers and so free memory.
if ((readyOps & SelectionKey.OP_WRITE) != 0) {
// Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write
ch.unsafe().forceFlush();
}
// to a spin loop
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
unsafe.read();
}
} catch (CancelledKeyException ignored) {
unsafe.close(unsafe.voidPromise());
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class NioEventLoopTest method testScheduleBigDelayNotOverflow.
@Test
public void testScheduleBigDelayNotOverflow() {
EventLoopGroup group = new NioEventLoopGroup(1);
final EventLoop el = group.next();
Future<?> future = el.schedule(new Runnable() {
@Override
public void run() {
// NOOP
}
}, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
assertFalse(future.awaitUninterruptibly(1000));
assertTrue(future.cancel(true));
group.shutdownGracefully();
}
use of io.netty.channel.EventLoop in project netty by netty.
the class NioSocketChannelTest method testChannelReRegisterRead.
private static void testChannelReRegisterRead(final boolean sameEventLoop) throws Exception {
final EventLoopGroup group = new NioEventLoopGroup(2);
final CountDownLatch latch = new CountDownLatch(1);
// Just some random bytes
byte[] bytes = new byte[1024];
PlatformDependent.threadLocalRandom().nextBytes(bytes);
Channel sc = null;
Channel cc = null;
ServerBootstrap b = new ServerBootstrap();
try {
b.group(group).channel(NioServerSocketChannel.class).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) {
// We was able to read something from the Channel after reregister.
latch.countDown();
}
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
final EventLoop loop = group.next();
if (sameEventLoop) {
deregister(ctx, loop);
} else {
loop.execute(new Runnable() {
@Override
public void run() {
deregister(ctx, loop);
}
});
}
}
private void deregister(ChannelHandlerContext ctx, final EventLoop loop) {
// As soon as the channel becomes active re-register it to another
// EventLoop. After this is done we should still receive the data that
// was written to the channel.
ctx.deregister().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture cf) {
Channel channel = cf.channel();
assertNotSame(loop, channel.eventLoop());
group.next().register(channel);
}
});
}
});
}
});
sc = b.bind(0).syncUninterruptibly().channel();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInboundHandlerAdapter());
cc = bootstrap.connect(sc.localAddress()).syncUninterruptibly().channel();
cc.writeAndFlush(Unpooled.wrappedBuffer(bytes)).syncUninterruptibly();
latch.await();
} finally {
if (cc != null) {
cc.close();
}
if (sc != null) {
sc.close();
}
group.shutdownGracefully();
}
}
Aggregations