|
20 | 20 | import io.netty.channel.nio.NioEventLoopGroup;
|
21 | 21 | import io.netty.channel.socket.nio.NioDatagramChannel;
|
22 | 22 | import io.netty.resolver.AddressResolver;
|
| 23 | +import io.netty.resolver.InetSocketAddressResolver; |
23 | 24 | import io.netty.util.concurrent.Future;
|
24 | 25 | import io.netty.util.concurrent.FutureListener;
|
25 | 26 | import io.netty.util.concurrent.Promise;
|
26 | 27 | import org.junit.jupiter.api.Test;
|
27 | 28 |
|
| 29 | +import java.net.InetAddress; |
| 30 | +import java.net.InetSocketAddress; |
28 | 31 | import java.net.SocketAddress;
|
29 | 32 | import java.nio.channels.UnsupportedAddressTypeException;
|
| 33 | +import java.util.concurrent.ExecutionException; |
30 | 34 |
|
31 | 35 | import static org.hamcrest.MatcherAssert.assertThat;
|
32 | 36 | import static org.hamcrest.Matchers.instanceOf;
|
| 37 | +import static org.junit.jupiter.api.Assertions.assertSame; |
33 | 38 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
34 | 39 |
|
35 | 40 | public class DnsAddressResolverGroupTest {
|
@@ -66,4 +71,50 @@ public void operationComplete(Future<Object> future) {
|
66 | 71 | defaultEventLoopGroup.shutdownGracefully();
|
67 | 72 | }
|
68 | 73 | }
|
| 74 | + |
| 75 | + @Test |
| 76 | + public void testSharedDNSCacheAcrossEventLoops() throws InterruptedException, ExecutionException { |
| 77 | + NioEventLoopGroup group = new NioEventLoopGroup(1); |
| 78 | + final EventLoop loop = group.next(); |
| 79 | + DnsNameResolverBuilder builder = new DnsNameResolverBuilder() |
| 80 | + .eventLoop(loop).channelType(NioDatagramChannel.class); |
| 81 | + DnsAddressResolverGroup resolverGroup = new DnsAddressResolverGroup(builder); |
| 82 | + DefaultEventLoopGroup defaultEventLoopGroup = new DefaultEventLoopGroup(2); |
| 83 | + EventLoop eventLoop1 = defaultEventLoopGroup.next(); |
| 84 | + EventLoop eventLoop2 = defaultEventLoopGroup.next(); |
| 85 | + try { |
| 86 | + final Promise<InetSocketAddress> promise1 = loop.newPromise(); |
| 87 | + InetSocketAddressResolver resolver1 = (InetSocketAddressResolver) resolverGroup.getResolver(eventLoop1); |
| 88 | + InetAddress address1 = |
| 89 | + resolve(resolver1, InetSocketAddress.createUnresolved("netty.io", 80), promise1); |
| 90 | + final Promise<InetSocketAddress> promise2 = loop.newPromise(); |
| 91 | + InetSocketAddressResolver resolver2 = (InetSocketAddressResolver) resolverGroup.getResolver(eventLoop2); |
| 92 | + InetAddress address2 = |
| 93 | + resolve(resolver2, InetSocketAddress.createUnresolved("netty.io", 80), promise2); |
| 94 | + assertSame(address1, address2); |
| 95 | + } finally { |
| 96 | + resolverGroup.close(); |
| 97 | + group.shutdownGracefully(); |
| 98 | + defaultEventLoopGroup.shutdownGracefully(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private InetAddress resolve(InetSocketAddressResolver resolver, SocketAddress socketAddress, |
| 103 | + final Promise<InetSocketAddress> promise) |
| 104 | + throws InterruptedException, ExecutionException { |
| 105 | + resolver.resolve(socketAddress) |
| 106 | + .addListener(new FutureListener<InetSocketAddress>() { |
| 107 | + @Override |
| 108 | + public void operationComplete(Future<InetSocketAddress> future) { |
| 109 | + try { |
| 110 | + promise.setSuccess(future.get()); |
| 111 | + } catch (Throwable cause) { |
| 112 | + promise.setFailure(cause); |
| 113 | + } |
| 114 | + } |
| 115 | + }).await(); |
| 116 | + promise.sync(); |
| 117 | + InetSocketAddress inetSocketAddress = promise.get(); |
| 118 | + return inetSocketAddress.getAddress(); |
| 119 | + } |
69 | 120 | }
|
0 commit comments