Fix flaky DnsNameResolverTest.testTruncatedWithTcpFallback (#9262)

Motivation:

testTruncatedWithTcpFallback was flacky as we may end up closing the socket before we could read all data. We should only close the socket after we succesfully read all data.

Modifications:

Move socket.close() to finally block

Result:

Fix flaky test and so make the CI more stable again.
This commit is contained in:
Norman Maurer 2019-06-21 09:28:51 +02:00
parent ea2e4a7b3d
commit 780536ba72

View File

@ -2768,6 +2768,7 @@ 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)
@ -2787,7 +2788,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 socket = serverSocket.accept(); 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());
@ -2806,7 +2807,6 @@ public class DnsNameResolverTest {
} }
socket.getOutputStream().flush(); socket.getOutputStream().flush();
socket.getOutputStream().close(); socket.getOutputStream().close();
socket.close();
} }
AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = envelopeFuture.syncUninterruptibly().getNow(); AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = envelopeFuture.syncUninterruptibly().getNow();
@ -2831,6 +2831,9 @@ public class DnsNameResolverTest {
envelope.release(); envelope.release();
} finally { } finally {
dnsServer2.stop(); dnsServer2.stop();
if (socket != null) {
socket.close();
}
if (serverSocket != null) { if (serverSocket != null) {
serverSocket.close(); serverSocket.close();
} }