Procházet zdrojové kódy

HADOOP-17331. [JDK 16] TestDNS fails (#2884)

Akira Ajisaka před 3 roky
rodič
revize
20a4b1ae36

+ 12 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java

@@ -18,6 +18,7 @@
 
 package org.apache.hadoop.net;
 
+import org.apache.hadoop.thirdparty.com.google.common.annotations.VisibleForTesting;
 import org.apache.hadoop.thirdparty.com.google.common.net.InetAddresses;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
@@ -58,7 +59,7 @@ public class DNS {
    * The cached hostname -initially null.
    */
 
-  private static final String cachedHostname = resolveLocalHostname();
+  private static String cachedHostname = resolveLocalHostname();
   private static final String cachedHostAddress = resolveLocalHostIPAddress();
   private static final String LOCALHOST = "localhost";
 
@@ -448,4 +449,14 @@ public class DNS {
     }
     return new Vector<InetAddress>(allAddrs);
   }
+
+  @VisibleForTesting
+  static String getCachedHostname() {
+    return cachedHostname;
+  }
+
+  @VisibleForTesting
+  static void setCachedHostname(String hostname) {
+    cachedHostname = hostname;
+  }
 }

+ 6 - 22
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java

@@ -198,9 +198,9 @@ public class TestDNS {
   @Test (timeout=60000)
   public void testLookupWithHostsFallback() throws Exception {
     assumeNotWindows();
-    final String oldHostname = changeDnsCachedHostname(DUMMY_HOSTNAME);
-
+    final String oldHostname = DNS.getCachedHostname();
     try {
+      DNS.setCachedHostname(DUMMY_HOSTNAME);
       String hostname = DNS.getDefaultHost(
           getLoopbackInterface(), INVALID_DNS_SERVER, true);
 
@@ -208,7 +208,7 @@ public class TestDNS {
       Assertions.assertThat(hostname).isNotEqualTo(DUMMY_HOSTNAME);
     } finally {
       // Restore DNS#cachedHostname for subsequent tests.
-      changeDnsCachedHostname(oldHostname);
+      DNS.setCachedHostname(oldHostname);
     }
   }
 
@@ -220,9 +220,9 @@ public class TestDNS {
    */
   @Test(timeout=60000)
   public void testLookupWithoutHostsFallback() throws Exception {
-    final String oldHostname = changeDnsCachedHostname(DUMMY_HOSTNAME);
-
+    final String oldHostname = DNS.getCachedHostname();
     try {
+      DNS.setCachedHostname(DUMMY_HOSTNAME);
       String hostname = DNS.getDefaultHost(
           getLoopbackInterface(), INVALID_DNS_SERVER, false);
 
@@ -231,7 +231,7 @@ public class TestDNS {
       Assertions.assertThat(hostname).isEqualTo(DUMMY_HOSTNAME);
     } finally {
       // Restore DNS#cachedHostname for subsequent tests.
-      changeDnsCachedHostname(oldHostname);
+      DNS.setCachedHostname(oldHostname);
     }
   }
 
@@ -240,22 +240,6 @@ public class TestDNS {
         InetAddress.getLoopbackAddress()).getName();
   }
 
-  /**
-   * Change DNS#cachedHostName to something which cannot be a real
-   * host name. Uses reflection since it is a 'private final' field.
-   */
-  private String changeDnsCachedHostname(final String newHostname)
-      throws Exception {
-    final String oldCachedHostname = DNS.getDefaultHost(DEFAULT);
-    Field field = DNS.class.getDeclaredField("cachedHostname");
-    field.setAccessible(true);
-    Field modifiersField = Field.class.getDeclaredField("modifiers");
-    modifiersField.setAccessible(true);
-    modifiersField.set(field, field.getModifiers() & ~Modifier.FINAL);
-    field.set(null, newHostname);
-    return oldCachedHostname;
-  }
-
   /**
    * Test that the name "localhost" resolves to something.
    *