Skip to content

Commit

Permalink
Make test more robust by binding the ServerSocket first (netty#13061)
Browse files Browse the repository at this point in the history
Motivation:

Sometimes we saw failures due port already in use. Let's ensure we can bind to it before bootstrap the DnsServer

Modifications:

First bind the TCP socket

Result:

Hopefully less flaky test
  • Loading branch information
normanmaurer authored Dec 19, 2022
1 parent 350bd0f commit 0a68907
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3079,6 +3079,14 @@ private static DnsMessageModifier modifierFrom(DnsMessage message) {
}

private static void testTruncated0(boolean tcpFallback, final boolean truncatedBecauseOfMtu) throws IOException {
ServerSocket serverSocket = null;
if (tcpFallback) {
// If we are configured to use TCP as a fallback also bind a TCP socket
serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(NetUtil.LOCALHOST4, 0));
}

final String host = "somehost.netty.io";
final String txt = "this is a txt record";
final AtomicReference<DnsMessage> messageRef = new AtomicReference<DnsMessage>();
Expand Down Expand Up @@ -3110,24 +3118,22 @@ protected DnsMessage filterMessage(DnsMessage message) {
return message;
}
};
dnsServer2.start();
DnsNameResolver resolver = null;
ServerSocket serverSocket = null;
try {
DnsNameResolverBuilder builder = newResolver()
.queryTimeoutMillis(10000)
.resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
.maxQueriesPerResolve(16)
.nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
DnsNameResolverBuilder builder = newResolver();

if (tcpFallback) {
// If we are configured to use TCP as a fallback also bind a TCP socket
serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(dnsServer2.localAddress().getPort()));
dnsServer2.start(false, (InetSocketAddress) serverSocket.getLocalSocketAddress());

// If we are configured to use TCP as a fallback also bind a TCP socket
builder.socketChannelType(NioSocketChannel.class);
} else {
dnsServer2.start();
}
builder.queryTimeoutMillis(10000)
.resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
.maxQueriesPerResolve(16)
.nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
resolver = builder.build();
if (truncatedBecauseOfMtu) {
resolver.ch.pipeline().addFirst(new ChannelInboundHandlerAdapter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public void start() throws IOException {
* Start the {@link TestDnsServer} but drop all {@code AAAA} queries and not send any response to these at all.
*/
public void start(final boolean dropAAAAQueries) throws IOException {
InetSocketAddress address = new InetSocketAddress(NetUtil.LOCALHOST4, 0);
start(dropAAAAQueries, new InetSocketAddress(NetUtil.LOCALHOST4, 0));
}

public void start(final boolean dropAAAAQueries, InetSocketAddress address) throws IOException {
UdpTransport transport = new UdpTransport(address.getHostName(), address.getPort());
setTransports(transport);

Expand Down

0 comments on commit 0a68907

Please sign in to comment.