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

This commit is contained in:
Akira Ajisaka 2021-06-30 19:06:29 +09:00 committed by GitHub
parent 4cac6ec405
commit 20a4b1ae36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 23 deletions

View File

@ -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 static List<InetAddress> getIPsAsInetAddressList(String strInterface,
}
return new Vector<InetAddress>(allAddrs);
}
@VisibleForTesting
static String getCachedHostname() {
return cachedHostname;
}
@VisibleForTesting
static void setCachedHostname(String hostname) {
cachedHostname = hostname;
}
}

View File

@ -198,9 +198,9 @@ public void testRDNS() throws Exception {
@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 void testLookupWithHostsFallback() throws Exception {
Assertions.assertThat(hostname).isNotEqualTo(DUMMY_HOSTNAME);
} finally {
// Restore DNS#cachedHostname for subsequent tests.
changeDnsCachedHostname(oldHostname);
DNS.setCachedHostname(oldHostname);
}
}
@ -220,9 +220,9 @@ public void testLookupWithHostsFallback() throws Exception {
*/
@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 void testLookupWithoutHostsFallback() throws Exception {
Assertions.assertThat(hostname).isEqualTo(DUMMY_HOSTNAME);
} finally {
// Restore DNS#cachedHostname for subsequent tests.
changeDnsCachedHostname(oldHostname);
DNS.setCachedHostname(oldHostname);
}
}
@ -240,22 +240,6 @@ private String getLoopbackInterface() throws SocketException {
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.
*