You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/07/23 02:23:46 UTC
[GitHub] [shardingsphere-elasticjob] sunkai-cai commented on a change in pull request #1938: support setting the system variable `elasticjob.preferred.network.ip`
sunkai-cai commented on a change in pull request #1938:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/1938#discussion_r675270873
##########
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:
fine
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org