From 2c81eec661a82b09e46fc0e0f15422ec26ba762b Mon Sep 17 00:00:00 2001 From: zhangchen351 Date: Mon, 8 Aug 2022 14:21:43 +0800 Subject: [PATCH] [Minor] Fix flaky test testGetHostIp --- .../org/apache/uniffle/common/util/RssUtils.java | 8 ++++---- .../apache/uniffle/common/util/RssUtilsTest.java | 14 ++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/org/apache/uniffle/common/util/RssUtils.java b/common/src/main/java/org/apache/uniffle/common/util/RssUtils.java index 8b7f50081e..1224d8a473 100644 --- a/common/src/main/java/org/apache/uniffle/common/util/RssUtils.java +++ b/common/src/main/java/org/apache/uniffle/common/util/RssUtils.java @@ -97,10 +97,10 @@ public static Map getPropertiesFromFile(String filename) { return result; } - // `InetAddress.getLocalHost().getHostAddress()` could return 127.0.0.1. To avoid - // this situation, we can get current ip through network interface (filtered ipv6, - // loop back, etc.). If the network interface in the machine is more than one, we - // will choose the first IP. + // `InetAddress.getLocalHost().getHostAddress()` could return + // 127.0.0.1 (127.0.1.1 on Debian). To avoid this situation, we can get current + // ip through network interface (filtered ipv6, loop back, etc.). + // If the network interface in the machine is more than one, we will choose the first IP. public static String getHostIp() throws Exception { // For K8S, there are too many IPs, it's hard to decide which we should use. // So we use the environment variable to tell RSS to use which one. diff --git a/common/src/test/java/org/apache/uniffle/common/util/RssUtilsTest.java b/common/src/test/java/org/apache/uniffle/common/util/RssUtilsTest.java index 3c946ebb53..97e732f272 100644 --- a/common/src/test/java/org/apache/uniffle/common/util/RssUtilsTest.java +++ b/common/src/test/java/org/apache/uniffle/common/util/RssUtilsTest.java @@ -18,7 +18,9 @@ package org.apache.uniffle.common.util; import java.lang.reflect.Field; +import java.net.Inet4Address; import java.net.InetAddress; +import java.net.NetworkInterface; import java.nio.ByteBuffer; import java.util.Collections; import java.util.List; @@ -35,7 +37,7 @@ import org.apache.uniffle.common.ShuffleIndexResult; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -57,12 +59,12 @@ public void testGetPropertiesFromFile() { @Test public void testGetHostIp() { try { - String address = InetAddress.getLocalHost().getHostAddress(); String realIp = RssUtils.getHostIp(); - assertNotEquals("127.0.0.1", realIp); - if (!address.equals("127.0.0.1")) { - assertEquals(address, realIp); - } + InetAddress ia = InetAddress.getByName(realIp); + assertTrue(ia instanceof Inet4Address); + assertFalse(ia.isLinkLocalAddress() || ia.isAnyLocalAddress() || ia.isLoopbackAddress()); + assertTrue(NetworkInterface.getByInetAddress(ia) != null); + assertTrue(ia.isReachable(5000)); setEnv("RSS_IP", "8.8.8.8"); assertEquals("8.8.8.8", RssUtils.getHostIp()); setEnv("RSS_IP", "xxxx");