use of io.netty.channel.EventLoop in project netty by netty.
the class DnsNameResolverTest method testMultipleAdditionalRecordsForSameNSRecord.
private static void testMultipleAdditionalRecordsForSameNSRecord(final boolean reversed) throws Exception {
final String domain = "netty.io";
final String hostname = "test.netty.io";
final String ns1Name = "ns1." + domain;
final InetSocketAddress ns1Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 1 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
final InetSocketAddress ns2Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 2 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
final InetSocketAddress ns3Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 3 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
final InetSocketAddress ns4Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 4 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
TestDnsServer redirectServer = new TestDnsServer(new HashSet<String>(asList(hostname, ns1Name))) {
@Override
protected DnsMessage filterMessage(DnsMessage message) {
for (QuestionRecord record : message.getQuestionRecords()) {
if (record.getDomainName().equals(hostname)) {
message.getAdditionalRecords().clear();
message.getAnswerRecords().clear();
message.getAuthorityRecords().add(TestDnsServer.newNsRecord(domain, ns1Name));
message.getAdditionalRecords().add(newARecord(ns1Address));
message.getAdditionalRecords().add(newARecord(ns2Address));
message.getAdditionalRecords().add(newARecord(ns3Address));
message.getAdditionalRecords().add(newARecord(ns4Address));
return message;
}
}
return message;
}
private ResourceRecord newARecord(InetSocketAddress address) {
return newARecord(address.getHostName(), address.getAddress().getHostAddress());
}
};
redirectServer.start();
EventLoopGroup group = new NioEventLoopGroup(1);
final List<InetSocketAddress> cached = new CopyOnWriteArrayList<InetSocketAddress>();
final AuthoritativeDnsServerCache authoritativeDnsServerCache = new AuthoritativeDnsServerCache() {
@Override
public DnsServerAddressStream get(String hostname) {
return null;
}
@Override
public void cache(String hostname, InetSocketAddress address, long originalTtl, EventLoop loop) {
cached.add(address);
}
@Override
public void clear() {
// NOOP
}
@Override
public boolean clear(String hostname) {
return false;
}
};
final AtomicReference<DnsServerAddressStream> redirectedRef = new AtomicReference<DnsServerAddressStream>();
final DnsNameResolver resolver = new DnsNameResolver(group.next(), new ReflectiveChannelFactory<DatagramChannel>(NioDatagramChannel.class), NoopDnsCache.INSTANCE, authoritativeDnsServerCache, NoopDnsQueryLifecycleObserverFactory.INSTANCE, 2000, ResolvedAddressTypes.IPV4_ONLY, true, 10, true, 4096, false, HostsFileEntriesResolver.DEFAULT, new SingletonDnsServerAddressStreamProvider(redirectServer.localAddress()), DnsNameResolver.DEFAULT_SEARCH_DOMAINS, 0, true) {
@Override
protected DnsServerAddressStream newRedirectDnsServerStream(String hostname, List<InetSocketAddress> nameservers) {
if (reversed) {
Collections.reverse(nameservers);
}
DnsServerAddressStream stream = new SequentialDnsServerAddressStream(nameservers, 0);
redirectedRef.set(stream);
return stream;
}
};
try {
Throwable cause = resolver.resolveAll(hostname).await().cause();
assertTrue(cause instanceof UnknownHostException);
DnsServerAddressStream redirected = redirectedRef.get();
assertNotNull(redirected);
assertEquals(4, redirected.size());
assertEquals(4, cached.size());
if (reversed) {
assertEquals(ns4Address, redirected.next());
assertEquals(ns3Address, redirected.next());
assertEquals(ns2Address, redirected.next());
assertEquals(ns1Address, redirected.next());
} else {
assertEquals(ns1Address, redirected.next());
assertEquals(ns2Address, redirected.next());
assertEquals(ns3Address, redirected.next());
assertEquals(ns4Address, redirected.next());
}
// We should always have the same order in the cache.
assertEquals(ns1Address, cached.get(0));
assertEquals(ns2Address, cached.get(1));
assertEquals(ns3Address, cached.get(2));
assertEquals(ns4Address, cached.get(3));
} finally {
resolver.close();
group.shutdownGracefully(0, 0, TimeUnit.SECONDS);
redirectServer.stop();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class DefaultAuthoritativeDnsServerCacheTest method testAddMultipleDnsServerForSameHostname.
@Test
public void testAddMultipleDnsServerForSameHostname() throws Exception {
InetSocketAddress resolved1 = new InetSocketAddress(InetAddress.getByAddress("ns1", new byte[] { 10, 0, 0, 1 }), 53);
InetSocketAddress resolved2 = new InetSocketAddress(InetAddress.getByAddress("ns2", new byte[] { 10, 0, 0, 2 }), 53);
EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultAuthoritativeDnsServerCache cache = new DefaultAuthoritativeDnsServerCache();
cache.cache("netty.io", resolved1, 100, loop);
cache.cache("netty.io", resolved2, 10000, loop);
DnsServerAddressStream entries = cache.get("netty.io");
assertEquals(2, entries.size());
assertEquals(resolved1, entries.next());
assertEquals(resolved2, entries.next());
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class DefaultAuthoritativeDnsServerCacheTest method testUseComparator0.
private static void testUseComparator0(boolean noComparator) throws Exception {
InetSocketAddress unresolved = InetSocketAddress.createUnresolved("ns1", 53);
InetSocketAddress resolved = new InetSocketAddress(InetAddress.getByAddress("ns2", new byte[] { 10, 0, 0, 2 }), 53);
EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultAuthoritativeDnsServerCache cache;
if (noComparator) {
cache = new DefaultAuthoritativeDnsServerCache(10000, 10000, null);
} else {
cache = new DefaultAuthoritativeDnsServerCache(10000, 10000, new Comparator<InetSocketAddress>() {
@Override
public int compare(InetSocketAddress o1, InetSocketAddress o2) {
if (o1.equals(o2)) {
return 0;
}
if (o1.isUnresolved()) {
return 1;
} else {
return -1;
}
}
});
}
cache.cache("netty.io", unresolved, 100, loop);
cache.cache("netty.io", resolved, 10000, loop);
DnsServerAddressStream entries = cache.get("netty.io");
assertEquals(2, entries.size());
if (noComparator) {
assertEquals(unresolved, entries.next());
assertEquals(resolved, entries.next());
} else {
assertEquals(resolved, entries.next());
assertEquals(unresolved, entries.next());
}
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class DefaultDnsCnameCacheTest method testAddSameCnameForSameHostname.
@Test
public void testAddSameCnameForSameHostname() throws Exception {
EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
cache.cache("netty.io", "mapping.netty.io", 10, loop);
cache.cache("netty.io", "mapping.netty.io", 10000, loop);
assertEquals("mapping.netty.io", cache.get("netty.io"));
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class DefaultDnsCnameCacheTest method testMultipleCnamesForSameHostname.
@Test
public void testMultipleCnamesForSameHostname() throws Exception {
EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
cache.cache("netty.io", "mapping1.netty.io", 10, loop);
cache.cache("netty.io", "mapping2.netty.io", 10000, loop);
assertEquals("mapping2.netty.io", cache.get("netty.io"));
} finally {
group.shutdownGracefully();
}
}
Aggregations