use of io.netty.channel.EventLoop in project netty by netty.
the class DefaultDnsCacheTest method testExpire.
@Test
public void testExpire() throws Throwable {
InetAddress addr1 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 1 });
InetAddress addr2 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 2 });
EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultDnsCache cache = new DefaultDnsCache();
cache.cache("netty.io", null, addr1, 1, loop);
cache.cache("netty.io", null, addr2, 10000, loop);
Throwable error = loop.schedule(new Callable<Throwable>() {
@Override
public Throwable call() {
try {
assertNull(cache.get("netty.io", null));
return null;
} catch (Throwable cause) {
return cause;
}
}
}, 1, TimeUnit.SECONDS).get();
if (error != null) {
throw error;
}
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class DnsAddressResolverGroupTest method testUseConfiguredEventLoop.
@Test
public void testUseConfiguredEventLoop() throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup(1);
final EventLoop loop = group.next();
DefaultEventLoopGroup defaultEventLoopGroup = new DefaultEventLoopGroup(1);
DnsNameResolverBuilder builder = new DnsNameResolverBuilder().eventLoop(loop).channelType(NioDatagramChannel.class);
DnsAddressResolverGroup resolverGroup = new DnsAddressResolverGroup(builder);
try {
final Promise<?> promise = loop.newPromise();
AddressResolver<?> resolver = resolverGroup.getResolver(defaultEventLoopGroup.next());
resolver.resolve(new SocketAddress() {
private static final long serialVersionUID = 3169703458729818468L;
}).addListener(new FutureListener<Object>() {
@Override
public void operationComplete(Future<Object> future) {
try {
assertThat(future.cause(), instanceOf(UnsupportedAddressTypeException.class));
assertTrue(loop.inEventLoop());
promise.setSuccess(null);
} catch (Throwable cause) {
promise.setFailure(cause);
}
}
}).await();
promise.sync();
} finally {
resolverGroup.close();
group.shutdownGracefully();
defaultEventLoopGroup.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class Bootstrap method doResolveAndConnect0.
private ChannelFuture doResolveAndConnect0(final Channel channel, SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
try {
final EventLoop eventLoop = channel.eventLoop();
AddressResolver<SocketAddress> resolver;
try {
resolver = this.resolver.getResolver(eventLoop);
} catch (Throwable cause) {
channel.close();
return promise.setFailure(cause);
}
if (!resolver.isSupported(remoteAddress) || resolver.isResolved(remoteAddress)) {
// Resolver has no idea about what to do with the specified remote address or it's resolved already.
doConnect(remoteAddress, localAddress, promise);
return promise;
}
final Future<SocketAddress> resolveFuture = resolver.resolve(remoteAddress);
if (resolveFuture.isDone()) {
final Throwable resolveFailureCause = resolveFuture.cause();
if (resolveFailureCause != null) {
// Failed to resolve immediately
channel.close();
promise.setFailure(resolveFailureCause);
} else {
// Succeeded to resolve immediately; cached? (or did a blocking lookup)
doConnect(resolveFuture.getNow(), localAddress, promise);
}
return promise;
}
// Wait until the name resolution is finished.
resolveFuture.addListener(new FutureListener<SocketAddress>() {
@Override
public void operationComplete(Future<SocketAddress> future) throws Exception {
if (future.cause() != null) {
channel.close();
promise.setFailure(future.cause());
} else {
doConnect(future.getNow(), localAddress, promise);
}
}
});
} catch (Throwable cause) {
promise.tryFailure(cause);
}
return promise;
}
use of io.netty.channel.EventLoop in project zuul by Netflix.
the class PerServerConnectionPool method remove.
@Override
public boolean remove(PooledConnection conn) {
if (conn == null) {
return false;
}
if (!conn.isInPool()) {
return false;
}
// Get the eventloop for this channel.
EventLoop eventLoop = conn.getChannel().eventLoop();
// Attempt to return connection to the pool.
Deque<PooledConnection> connections = getPoolForEventLoop(eventLoop);
if (connections.remove(conn)) {
conn.setInPool(false);
connsInPool.decrementAndGet();
return true;
} else {
return false;
}
}
use of io.netty.channel.EventLoop in project grpc-java by grpc.
the class NettyHandlerTestBase method newMockContext.
protected final ChannelHandlerContext newMockContext() {
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
EventLoop eventLoop = mock(EventLoop.class);
when(ctx.executor()).thenReturn(eventLoop);
when(ctx.channel()).thenReturn(channel);
return ctx;
}
Aggregations