|
23 | 23 | import io.netty.channel.socket.nio.NioDatagramChannel; |
24 | 24 | import io.netty.resolver.AddressResolver; |
25 | 25 | import io.netty.resolver.InetSocketAddressResolver; |
26 | | -import io.netty.util.concurrent.Future; |
27 | 26 | import io.netty.util.concurrent.FutureListener; |
28 | 27 | import io.netty.util.concurrent.Promise; |
| 28 | +import org.apache.directory.server.dns.messages.QuestionRecord; |
| 29 | +import org.apache.directory.server.dns.messages.RecordType; |
| 30 | +import org.apache.directory.server.dns.messages.ResourceRecord; |
| 31 | +import org.apache.directory.server.dns.store.RecordStore; |
29 | 32 | import org.junit.jupiter.api.Test; |
30 | 33 |
|
31 | 34 | import java.net.InetAddress; |
32 | 35 | import java.net.InetSocketAddress; |
33 | 36 | import java.net.SocketAddress; |
34 | 37 | import java.nio.channels.UnsupportedAddressTypeException; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.Set; |
35 | 40 | import java.util.concurrent.ExecutionException; |
36 | 41 |
|
| 42 | +import static io.netty.resolver.dns.TestDnsServer.newARecord; |
37 | 43 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
38 | 44 | import static org.junit.jupiter.api.Assertions.assertSame; |
39 | 45 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -71,11 +77,25 @@ public void testUseConfiguredEventLoop() throws InterruptedException { |
71 | 77 | } |
72 | 78 |
|
73 | 79 | @Test |
74 | | - public void testSharedDNSCacheAcrossEventLoops() throws InterruptedException, ExecutionException { |
| 80 | + public void testSharedDNSCacheAcrossEventLoops() throws Exception { |
| 81 | + final String hostname = "netty.io"; |
| 82 | + final String expectedIp = "1.2.3.4"; |
| 83 | + TestDnsServer dnsServer = new TestDnsServer(new RecordStore() { |
| 84 | + @Override |
| 85 | + public Set<ResourceRecord> getRecords(QuestionRecord question) { |
| 86 | + if (question.getRecordType() == RecordType.A && question.getDomainName().equalsIgnoreCase(hostname)) { |
| 87 | + return Collections.singleton(newARecord(hostname, expectedIp)); |
| 88 | + } |
| 89 | + return null; |
| 90 | + } |
| 91 | + }); |
| 92 | + dnsServer.start(); |
75 | 93 | MultiThreadIoEventLoopGroup group = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory()); |
76 | 94 | final EventLoop loop = group.next(); |
77 | 95 | DnsNameResolverBuilder builder = new DnsNameResolverBuilder() |
78 | | - .eventLoop(loop).datagramChannelType(NioDatagramChannel.class); |
| 96 | + .eventLoop(loop).datagramChannelType(NioDatagramChannel.class) |
| 97 | + .optResourceEnabled(false) |
| 98 | + .nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer.localAddress())); |
79 | 99 | DnsAddressResolverGroup resolverGroup = new DnsAddressResolverGroup(builder); |
80 | 100 | EventLoopGroup defaultEventLoopGroup = new MultiThreadIoEventLoopGroup(1, LocalIoHandler.newFactory()); |
81 | 101 | EventLoop eventLoop1 = defaultEventLoopGroup.next(); |
@@ -84,16 +104,17 @@ public void testSharedDNSCacheAcrossEventLoops() throws InterruptedException, Ex |
84 | 104 | final Promise<InetSocketAddress> promise1 = loop.newPromise(); |
85 | 105 | InetSocketAddressResolver resolver1 = (InetSocketAddressResolver) resolverGroup.getResolver(eventLoop1); |
86 | 106 | InetAddress address1 = |
87 | | - resolve(resolver1, InetSocketAddress.createUnresolved("netty.io", 80), promise1); |
| 107 | + resolve(resolver1, InetSocketAddress.createUnresolved(hostname, 80), promise1); |
88 | 108 | final Promise<InetSocketAddress> promise2 = loop.newPromise(); |
89 | 109 | InetSocketAddressResolver resolver2 = (InetSocketAddressResolver) resolverGroup.getResolver(eventLoop2); |
90 | 110 | InetAddress address2 = |
91 | | - resolve(resolver2, InetSocketAddress.createUnresolved("netty.io", 80), promise2); |
| 111 | + resolve(resolver2, InetSocketAddress.createUnresolved(hostname, 80), promise2); |
92 | 112 | assertSame(address1, address2); |
93 | 113 | } finally { |
94 | 114 | resolverGroup.close(); |
95 | 115 | group.shutdownGracefully(); |
96 | 116 | defaultEventLoopGroup.shutdownGracefully(); |
| 117 | + dnsServer.stop(); |
97 | 118 | } |
98 | 119 | } |
99 | 120 |
|
|