Search in sources :

Example 66 with EventLoop

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();
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DnsMessage(org.apache.directory.server.dns.messages.DnsMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) EventLoop(io.netty.channel.EventLoop) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 67 with EventLoop

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();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoop(io.netty.channel.EventLoop) InetSocketAddress(java.net.InetSocketAddress) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 68 with EventLoop

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();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoop(io.netty.channel.EventLoop) InetSocketAddress(java.net.InetSocketAddress) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Comparator(java.util.Comparator)

Example 69 with EventLoop

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();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoop(io.netty.channel.EventLoop) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 70 with EventLoop

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();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoop(io.netty.channel.EventLoop) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test)

Aggregations

EventLoop (io.netty.channel.EventLoop)77 EventLoopGroup (io.netty.channel.EventLoopGroup)27 Test (org.junit.jupiter.api.Test)27 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)18 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)17 Channel (io.netty.channel.Channel)10 InetSocketAddress (java.net.InetSocketAddress)10 Bootstrap (io.netty.bootstrap.Bootstrap)9 InetAddress (java.net.InetAddress)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelPromise (io.netty.channel.ChannelPromise)6 LocalAddress (io.netty.channel.local.LocalAddress)5 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 SingleThreadEventLoop (io.netty.channel.SingleThreadEventLoop)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Callable (java.util.concurrent.Callable)3