MINOR: Fix `testResolveDnsLookup` by using a mocked dns resolver (#11091)
This focuses on the currently failing test, #9315 is a more complete fix
that we should also review and merge.
Reviewers: David Jacot <djacot@confluent.io>
diff --git a/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java b/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java
index 5772946..ad6c6a4 100644
--- a/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java
+++ b/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java
@@ -25,13 +25,13 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
-import java.util.Arrays;
+import static java.util.Arrays.asList;
import java.util.List;
import java.util.stream.Collectors;
public class ClientUtilsTest {
- private HostResolver hostResolver = new DefaultHostResolver();
+ private final HostResolver hostResolver = new DefaultHostResolver();
@Test
public void testParseAndValidateAddresses() throws UnknownHostException {
@@ -55,18 +55,18 @@
// With lookup of example.com, either one or two addresses are expected depending on
// whether ipv4 and ipv6 are enabled
- List<InetSocketAddress> validatedAddresses = checkWithLookup(Arrays.asList("example.com:10000"));
+ List<InetSocketAddress> validatedAddresses = checkWithLookup(asList("example.com:10000"));
assertTrue("Unexpected addresses " + validatedAddresses, validatedAddresses.size() >= 1);
List<String> validatedHostNames = validatedAddresses.stream().map(InetSocketAddress::getHostName)
.collect(Collectors.toList());
- List<String> expectedHostNames = Arrays.asList("93.184.216.34", "2606:2800:220:1:248:1893:25c8:1946");
+ List<String> expectedHostNames = asList("93.184.216.34", "2606:2800:220:1:248:1893:25c8:1946");
assertTrue("Unexpected addresses " + validatedHostNames, expectedHostNames.containsAll(validatedHostNames));
validatedAddresses.forEach(address -> assertEquals(10000, address.getPort()));
}
@Test(expected = IllegalArgumentException.class)
public void testInvalidConfig() {
- ClientUtils.parseAndValidateAddresses(Arrays.asList("localhost:10000"), "random.value");
+ ClientUtils.parseAndValidateAddresses(asList("localhost:10000"), "random.value");
}
@Test(expected = ConfigException.class)
@@ -104,16 +104,24 @@
@Test
public void testResolveDnsLookup() throws UnknownHostException {
- assertEquals(1, ClientUtils.resolve("localhost", ClientDnsLookup.DEFAULT, hostResolver).size());
+ assertEquals(1, resolveToTwoIps(ClientDnsLookup.DEFAULT).size());
}
@Test
public void testResolveDnsLookupAllIps() throws UnknownHostException {
- assertTrue(ClientUtils.resolve("kafka.apache.org", ClientDnsLookup.USE_ALL_DNS_IPS, hostResolver).size() > 1);
+ assertEquals(2, resolveToTwoIps(ClientDnsLookup.USE_ALL_DNS_IPS).size());
+ }
+
+ private List<InetAddress> resolveToTwoIps(ClientDnsLookup dnsLookup) throws UnknownHostException {
+ InetAddress[] addresses = new InetAddress[] {
+ InetAddress.getByName("198.51.100.0"), InetAddress.getByName("198.51.100.5")
+ };
+ HostResolver hostResolver = new AddressChangeHostResolver(addresses, addresses);
+ return ClientUtils.resolve("kafka.apache.org", dnsLookup, hostResolver);
}
private List<InetSocketAddress> checkWithoutLookup(String... url) {
- return ClientUtils.parseAndValidateAddresses(Arrays.asList(url), ClientDnsLookup.DEFAULT);
+ return ClientUtils.parseAndValidateAddresses(asList(url), ClientDnsLookup.DEFAULT);
}
private List<InetSocketAddress> checkWithLookup(List<String> url) {