This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new d7e95b4  enhance unit test and logging (#3374)
d7e95b4 is described below

commit d7e95b4aec03a000e6a21203487c055ce440bc75
Author: Ian Luo <[email protected]>
AuthorDate: Tue Jan 29 15:26:28 2019 +0800

    enhance unit test and logging (#3374)
    
    * enhance unit test and logging
    
    * enhance logging message
    
    * fix unit test
    
    * make code clean
---
 .../org/apache/dubbo/common/utils/NetUtils.java    | 11 +++--
 .../registry/multicast/MulticastRegistry.java      | 19 +++++++--
 .../registry/multicast/MulticastRegistryTest.java  | 49 +++++++++++-----------
 3 files changed, 46 insertions(+), 33 deletions(-)

diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
index 4c6a56a..06450e2 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
@@ -346,25 +346,24 @@ public class NetUtils {
     }
 
     public static void joinMulticastGroup (MulticastSocket multicastSocket, 
InetAddress multicastAddress) throws IOException {
-        setInterface(multicastSocket, multicastAddress);
+        setInterface(multicastSocket, multicastAddress instanceof 
Inet6Address);
         multicastSocket.setLoopbackMode(false);
         multicastSocket.joinGroup(multicastAddress);
     }
 
-    public static void setInterface (MulticastSocket multicastSocket, 
InetAddress multicastAddress) throws IOException{
+    public static void setInterface (MulticastSocket multicastSocket, boolean 
preferIpv6) throws IOException{
         boolean interfaceSet = false;
-        boolean ipV6 = multicastAddress instanceof Inet6Address;
         Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
         while (interfaces.hasMoreElements()) {
             NetworkInterface i = (NetworkInterface) interfaces.nextElement();
             Enumeration addresses = i.getInetAddresses();
             while (addresses.hasMoreElements()) {
                 InetAddress address = (InetAddress) addresses.nextElement();
-                if (ipV6 && address instanceof Inet6Address) {
+                if (preferIpv6 && address instanceof Inet6Address) {
                     multicastSocket.setInterface(address);
                     interfaceSet = true;
                     break;
-                } else if (!ipV6 && address instanceof Inet4Address) {
+                } else if (!preferIpv6 && address instanceof Inet4Address) {
                     multicastSocket.setInterface(address);
                     interfaceSet = true;
                     break;
@@ -376,4 +375,4 @@ public class NetUtils {
         }
     }
 
-}
\ No newline at end of file
+}
diff --git 
a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java
 
b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java
index 33c4257..338b556 100644
--- 
a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java
+++ 
b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java
@@ -31,6 +31,7 @@ import org.apache.dubbo.registry.support.FailbackRegistry;
 
 import java.io.IOException;
 import java.net.DatagramPacket;
+import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.MulticastSocket;
@@ -81,9 +82,8 @@ public class MulticastRegistry extends FailbackRegistry {
         }
         try {
             multicastAddress = InetAddress.getByName(url.getHost());
-            if (!multicastAddress.isMulticastAddress()) {
-                throw new IllegalArgumentException("Invalid multicast address 
" + url.getHost() + ", ipv4 multicast address scope: 224.0.0.0 - 
239.255.255.255.");
-            }
+            checkMulticastAddress(multicastAddress);
+
             multicastPort = url.getPort() <= 0 ? DEFAULT_MULTICAST_PORT : 
url.getPort();
             multicastSocket = new MulticastSocket(multicastPort);
             NetUtils.joinMulticastGroup(multicastSocket, multicastAddress);
@@ -132,6 +132,19 @@ public class MulticastRegistry extends FailbackRegistry {
         }
     }
 
+    private void checkMulticastAddress(InetAddress multicastAddress) {
+        if (!multicastAddress.isMulticastAddress()) {
+            String message = "Invalid multicast address " + multicastAddress;
+            if (!(multicastAddress instanceof Inet4Address)) {
+                throw new IllegalArgumentException(message + ", " +
+                        "ipv4 multicast address scope: 224.0.0.0 - 
239.255.255.255.");
+            } else {
+                throw new IllegalArgumentException(message + ", " + "ipv6 
multicast address must start with ff, " +
+                        "for example: ff01::1");
+            }
+        }
+    }
+
     /**
      * Remove the expired providers, only when "clean" parameter is true.
      */
diff --git 
a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java
 
b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java
index 743ff37..93c59bc 100644
--- 
a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java
+++ 
b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java
@@ -227,34 +227,35 @@ public class MulticastRegistryTest {
         MulticastSocket multicastSocket = null;
         try {
             // ipv4 multicast address
-            try {
-                multicastAddress = InetAddress.getByName("224.55.66.77");
-                multicastSocket = new MulticastSocket(2345);
-                multicastSocket.setLoopbackMode(false);
-                NetUtils.setInterface(multicastSocket, multicastAddress);
-                multicastSocket.joinGroup(multicastAddress);
-            } finally {
-                if (multicastSocket != null) {
-                    multicastSocket.close();
-                }
+            multicastAddress = InetAddress.getByName("224.55.66.77");
+            multicastSocket = new MulticastSocket(2345);
+            multicastSocket.setLoopbackMode(false);
+            NetUtils.setInterface(multicastSocket, false);
+            multicastSocket.joinGroup(multicastAddress);
+        } catch (Exception e) {
+            Assertions.fail(e);
+        } finally {
+            if (multicastSocket != null) {
+                multicastSocket.close();
             }
+        }
 
-            // multicast ipv6 address,
-            /*try {
-                multicastAddress = InetAddress.getByName("ff01::1");
-                multicastSocket = new MulticastSocket();
-                multicastSocket.setLoopbackMode(false);
-                NetUtils.setInterface(multicastSocket, multicastAddress);
-                multicastSocket.joinGroup(multicastAddress);
-            } finally {
-                if (multicastSocket != null) {
-                    multicastSocket.close();
-                }
-            }*/
+        // multicast ipv6 address,
+        try {
+            multicastAddress = InetAddress.getByName("ff01::1");
 
-        } catch (Exception e) {
-            Assertions.fail(e);
+            multicastSocket = new MulticastSocket();
+            multicastSocket.setLoopbackMode(false);
+            NetUtils.setInterface(multicastSocket, true);
+            multicastSocket.joinGroup(multicastAddress);
+        } catch (Throwable t) {
+            t.printStackTrace();
+        } finally {
+            if (multicastSocket != null) {
+                multicastSocket.close();
+            }
         }
+
     }
 
 }

Reply via email to