DnsNameResolverTest.testTruncated0(...) should only close socket once envelope is received (#9469)

Motivation:

We should only ever close the underlying tcp socket once we received the envelope to ensure we never race in the test.

Modifications:

- Only close socket once we received the envelope
- Set REUSE_ADDR

Result:

More robust test
This commit is contained in:
Norman Maurer 2019-08-15 16:28:22 +02:00 committed by GitHub
parent 299954e138
commit 04d71b3773
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2796,7 +2796,6 @@ public class DnsNameResolverTest {
dnsServer2.start(); dnsServer2.start();
DnsNameResolver resolver = null; DnsNameResolver resolver = null;
ServerSocket serverSocket = null; ServerSocket serverSocket = null;
Socket socket = null;
try { try {
DnsNameResolverBuilder builder = newResolver() DnsNameResolverBuilder builder = newResolver()
.queryTimeoutMillis(10000) .queryTimeoutMillis(10000)
@ -2807,6 +2806,7 @@ public class DnsNameResolverTest {
if (tcpFallback) { if (tcpFallback) {
// If we are configured to use TCP as a fallback also bind a TCP socket // If we are configured to use TCP as a fallback also bind a TCP socket
serverSocket = new ServerSocket(dnsServer2.localAddress().getPort()); serverSocket = new ServerSocket(dnsServer2.localAddress().getPort());
serverSocket.setReuseAddress(true);
builder.socketChannelType(NioSocketChannel.class); builder.socketChannelType(NioSocketChannel.class);
} }
@ -2816,7 +2816,7 @@ public class DnsNameResolverTest {
if (tcpFallback) { if (tcpFallback) {
// If we are configured to use TCP as a fallback lets replay the dns message over TCP // If we are configured to use TCP as a fallback lets replay the dns message over TCP
socket = serverSocket.accept(); Socket socket = serverSocket.accept();
IoBuffer ioBuffer = IoBuffer.allocate(1024); IoBuffer ioBuffer = IoBuffer.allocate(1024);
new DnsMessageEncoder().encode(ioBuffer, messageRef.get()); new DnsMessageEncoder().encode(ioBuffer, messageRef.get());
@ -2834,7 +2834,11 @@ public class DnsNameResolverTest {
socket.getOutputStream().write(ioBuffer.get()); socket.getOutputStream().write(ioBuffer.get());
} }
socket.getOutputStream().flush(); socket.getOutputStream().flush();
socket.getOutputStream().close(); // Let's wait until we received the envelope before closing the socket.
envelopeFuture.syncUninterruptibly();
socket.close();
serverSocket.close();
} }
AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = envelopeFuture.syncUninterruptibly().getNow(); AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = envelopeFuture.syncUninterruptibly().getNow();
@ -2859,12 +2863,6 @@ public class DnsNameResolverTest {
envelope.release(); envelope.release();
} finally { } finally {
dnsServer2.stop(); dnsServer2.stop();
if (socket != null) {
socket.close();
}
if (serverSocket != null) {
serverSocket.close();
}
if (resolver != null) { if (resolver != null) {
resolver.close(); resolver.close();
} }