This is an automated email from the ASF dual-hosted git repository.

ijuma pushed a commit to branch 2.8
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/2.8 by this push:
     new 1fb225c  MINOR: Fix `testResolveDnsLookup` by using a mocked dns 
resolver (#11091)
1fb225c is described below

commit 1fb225c1b03cf13179ef9c403ecfaebc2ffe08a8
Author: Ismael Juma <[email protected]>
AuthorDate: Tue Jul 20 07:40:33 2021 -0700

    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 <[email protected]>
---
 .../org/apache/kafka/clients/ClientUtilsTest.java  | 27 +++++++++++++---------
 1 file changed, 16 insertions(+), 11 deletions(-)

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 2f0a590..23d2ba4 100644
--- a/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java
+++ b/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java
@@ -19,13 +19,13 @@ package org.apache.kafka.clients;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 import org.apache.kafka.common.config.ConfigException;
 import org.junit.jupiter.api.Test;
 
+import static java.util.Arrays.asList;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -33,7 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class ClientUtilsTest {
 
-    private HostResolver hostResolver = new DefaultHostResolver();
+    private final HostResolver hostResolver = new DefaultHostResolver();
 
     @Test
     public void testParseAndValidateAddresses() throws UnknownHostException {
@@ -57,11 +57,11 @@ public class ClientUtilsTest {
 
         // 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(validatedAddresses.size() >= 1, "Unexpected addresses " + 
validatedAddresses);
         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(expectedHostNames.containsAll(validatedHostNames), 
"Unexpected addresses " + validatedHostNames);
         validatedAddresses.forEach(address -> assertEquals(10000, 
address.getPort()));
     }
@@ -108,24 +108,29 @@ public class ClientUtilsTest {
 
     @Test
     public void testResolveDnsLookup() throws UnknownHostException {
-        // Note that kafka.apache.org resolves to at least 2 IP addresses
-        assertEquals(1, ClientUtils.resolve("kafka.apache.org", 
ClientDnsLookup.DEFAULT, hostResolver).size());
+        assertEquals(1, resolveToTwoIps(ClientDnsLookup.DEFAULT).size());
     }
 
     @Test
     public void testResolveDnsLookupAllIps() throws UnknownHostException {
-        // Note that kafka.apache.org resolves to at least 2 IP addresses
-        assertTrue(ClientUtils.resolve("kafka.apache.org", 
ClientDnsLookup.USE_ALL_DNS_IPS, hostResolver).size() > 1);
+        assertEquals(2, 
resolveToTwoIps(ClientDnsLookup.USE_ALL_DNS_IPS).size());
     }
 
     @Test
     public void testResolveDnsLookupResolveCanonicalBootstrapServers() throws 
UnknownHostException {
-        // Note that kafka.apache.org resolves to at least 2 IP addresses
-        assertTrue(ClientUtils.resolve("kafka.apache.org", 
ClientDnsLookup.RESOLVE_CANONICAL_BOOTSTRAP_SERVERS_ONLY, hostResolver).size() 
> 1);
+        assertEquals(2, 
resolveToTwoIps(ClientDnsLookup.RESOLVE_CANONICAL_BOOTSTRAP_SERVERS_ONLY).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.USE_ALL_DNS_IPS);
+        return ClientUtils.parseAndValidateAddresses(asList(url), 
ClientDnsLookup.USE_ALL_DNS_IPS);
     }
 
     private List<InetSocketAddress> checkWithLookup(List<String> url) {

Reply via email to