TeslaCN commented on a change in pull request #1938:
URL:
https://github.com/apache/shardingsphere-elasticjob/pull/1938#discussion_r674728052
##########
File path:
elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
##########
@@ -113,7 +116,8 @@ private static NetworkInterface
getFirstNetworkInterface(final List<NetworkInter
}
return result;
}
-
+
+ // For testing.
Review comment:
What is `for testing`?
##########
File path:
elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java
##########
@@ -21,18 +21,105 @@
import org.junit.Test;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Vector;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public final class IpUtilsTest {
@Test
public void assertGetIp() {
assertNotNull(IpUtils.getIp());
}
-
+
+ @Test
+ @SneakyThrows
+ public void assertPreferredNetworkInterface() {
+ System.setProperty(IpUtils.PREFERRED_NETWORK_INTERFACE, "eth0");
+ Method declaredMethod =
IpUtils.class.getDeclaredMethod("isPreferredNetworkInterface",
NetworkInterface.class);
+ declaredMethod.setAccessible(true);
+ NetworkInterface mockNetworkInterface = mock(NetworkInterface.class);
+ when(mockNetworkInterface.getDisplayName()).thenReturn("eth0");
+ boolean result = (boolean)
declaredMethod.invoke("isPreferredNetworkInterface", mockNetworkInterface);
+ assertTrue(result);
+ System.clearProperty(IpUtils.PREFERRED_NETWORK_INTERFACE);
+ }
+
+ @Test
+ @SneakyThrows
+ public void assertPreferredNetworkAddress() {
+ Method declaredMethod =
IpUtils.class.getDeclaredMethod("isPreferredAddress", InetAddress.class);
+ declaredMethod.setAccessible(true);
+ InetAddress inetAddress = mock(InetAddress.class);
+
+ System.setProperty(IpUtils.PREFERRED_NETWORK_IP, "192.168");
+ when(inetAddress.getHostAddress()).thenReturn("192.168.0.100");
+ assertTrue((boolean) declaredMethod.invoke("isPreferredAddress",
inetAddress));
+ when(inetAddress.getHostAddress()).thenReturn("10.10.0.100");
+ assertFalse((boolean) declaredMethod.invoke("isPreferredAddress",
inetAddress));
+ System.clearProperty(IpUtils.PREFERRED_NETWORK_IP);
+
+ System.setProperty(IpUtils.PREFERRED_NETWORK_IP, "10.10.*");
+ when(inetAddress.getHostAddress()).thenReturn("10.10.0.100");
+ assertTrue((boolean) declaredMethod.invoke("isPreferredAddress",
inetAddress));
+ when(inetAddress.getHostAddress()).thenReturn("10.0.0.100");
+ assertFalse((boolean) declaredMethod.invoke("isPreferredAddress",
inetAddress));
+ System.clearProperty(IpUtils.PREFERRED_NETWORK_IP);
+ }
+
+ @Test
+ @SneakyThrows
+ public void assertGetFirstNetworkInterface() {
+ InetAddress address1 = mock(Inet4Address.class);
+ when(address1.isLoopbackAddress()).thenReturn(false);
+ when(address1.isAnyLocalAddress()).thenReturn(false);
+ when(address1.isReachable(100)).thenReturn(true);
+ when(address1.getHostAddress()).thenReturn("10.10.0.1");
+ Vector<InetAddress> addresses1 = new Vector<>();
+ addresses1.add(address1);
+
Review comment:
Remove redundant blank lines.
##########
File path:
elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
##########
@@ -95,14 +97,15 @@ private static NetworkInterface findNetworkInterface() {
}
return result;
}
-
+
+ // For testing.
Review comment:
What is `for testing`?
##########
File path:
elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
##########
@@ -129,7 +133,18 @@ private static boolean ignoreNetworkInterface(final
NetworkInterface networkInte
return true;
}
}
-
+
+ // For testing.
Review comment:
What is `for testing`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]