From d7e95b4aec03a000e6a21203487c055ce440bc75 Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Tue, 29 Jan 2019 15:26:28 +0800 Subject: [PATCH] enhance unit test and logging (#3374) * enhance unit test and logging * enhance logging message * fix unit test * make code clean --- .../apache/dubbo/common/utils/NetUtils.java | 11 ++--- .../registry/multicast/MulticastRegistry.java | 19 +++++-- .../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 4c6a56a7c37..06450e2ef72 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 static String toURL(String protocol, String host, int port, String path) } 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 static void setInterface (MulticastSocket multicastSocket, InetAddress mu } } -} \ 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 33c42577ee5..338b556f193 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 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 MulticastRegistry(URL url) { } 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 void run() { } } + 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 743ff37d1de..93c59bce316 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 void testMulticastAddress() { 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(); + } } + } }