Skip to content

Commit d7e95b4

Browse files
authored
enhance unit test and logging (#3374)
* enhance unit test and logging * enhance logging message * fix unit test * make code clean
1 parent 58e35b2 commit d7e95b4

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,25 +346,24 @@ public static String toURL(String protocol, String host, int port, String path)
346346
}
347347

348348
public static void joinMulticastGroup (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException {
349-
setInterface(multicastSocket, multicastAddress);
349+
setInterface(multicastSocket, multicastAddress instanceof Inet6Address);
350350
multicastSocket.setLoopbackMode(false);
351351
multicastSocket.joinGroup(multicastAddress);
352352
}
353353

354-
public static void setInterface (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException{
354+
public static void setInterface (MulticastSocket multicastSocket, boolean preferIpv6) throws IOException{
355355
boolean interfaceSet = false;
356-
boolean ipV6 = multicastAddress instanceof Inet6Address;
357356
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
358357
while (interfaces.hasMoreElements()) {
359358
NetworkInterface i = (NetworkInterface) interfaces.nextElement();
360359
Enumeration addresses = i.getInetAddresses();
361360
while (addresses.hasMoreElements()) {
362361
InetAddress address = (InetAddress) addresses.nextElement();
363-
if (ipV6 && address instanceof Inet6Address) {
362+
if (preferIpv6 && address instanceof Inet6Address) {
364363
multicastSocket.setInterface(address);
365364
interfaceSet = true;
366365
break;
367-
} else if (!ipV6 && address instanceof Inet4Address) {
366+
} else if (!preferIpv6 && address instanceof Inet4Address) {
368367
multicastSocket.setInterface(address);
369368
interfaceSet = true;
370369
break;
@@ -376,4 +375,4 @@ public static void setInterface (MulticastSocket multicastSocket, InetAddress mu
376375
}
377376
}
378377

379-
}
378+
}

dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.io.IOException;
3333
import java.net.DatagramPacket;
34+
import java.net.Inet4Address;
3435
import java.net.InetAddress;
3536
import java.net.InetSocketAddress;
3637
import java.net.MulticastSocket;
@@ -81,9 +82,8 @@ public MulticastRegistry(URL url) {
8182
}
8283
try {
8384
multicastAddress = InetAddress.getByName(url.getHost());
84-
if (!multicastAddress.isMulticastAddress()) {
85-
throw new IllegalArgumentException("Invalid multicast address " + url.getHost() + ", ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255.");
86-
}
85+
checkMulticastAddress(multicastAddress);
86+
8787
multicastPort = url.getPort() <= 0 ? DEFAULT_MULTICAST_PORT : url.getPort();
8888
multicastSocket = new MulticastSocket(multicastPort);
8989
NetUtils.joinMulticastGroup(multicastSocket, multicastAddress);
@@ -132,6 +132,19 @@ public void run() {
132132
}
133133
}
134134

135+
private void checkMulticastAddress(InetAddress multicastAddress) {
136+
if (!multicastAddress.isMulticastAddress()) {
137+
String message = "Invalid multicast address " + multicastAddress;
138+
if (!(multicastAddress instanceof Inet4Address)) {
139+
throw new IllegalArgumentException(message + ", " +
140+
"ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255.");
141+
} else {
142+
throw new IllegalArgumentException(message + ", " + "ipv6 multicast address must start with ff, " +
143+
"for example: ff01::1");
144+
}
145+
}
146+
}
147+
135148
/**
136149
* Remove the expired providers, only when "clean" parameter is true.
137150
*/

dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -227,34 +227,35 @@ public void testMulticastAddress() {
227227
MulticastSocket multicastSocket = null;
228228
try {
229229
// ipv4 multicast address
230-
try {
231-
multicastAddress = InetAddress.getByName("224.55.66.77");
232-
multicastSocket = new MulticastSocket(2345);
233-
multicastSocket.setLoopbackMode(false);
234-
NetUtils.setInterface(multicastSocket, multicastAddress);
235-
multicastSocket.joinGroup(multicastAddress);
236-
} finally {
237-
if (multicastSocket != null) {
238-
multicastSocket.close();
239-
}
230+
multicastAddress = InetAddress.getByName("224.55.66.77");
231+
multicastSocket = new MulticastSocket(2345);
232+
multicastSocket.setLoopbackMode(false);
233+
NetUtils.setInterface(multicastSocket, false);
234+
multicastSocket.joinGroup(multicastAddress);
235+
} catch (Exception e) {
236+
Assertions.fail(e);
237+
} finally {
238+
if (multicastSocket != null) {
239+
multicastSocket.close();
240240
}
241+
}
241242

242-
// multicast ipv6 address,
243-
/*try {
244-
multicastAddress = InetAddress.getByName("ff01::1");
245-
multicastSocket = new MulticastSocket();
246-
multicastSocket.setLoopbackMode(false);
247-
NetUtils.setInterface(multicastSocket, multicastAddress);
248-
multicastSocket.joinGroup(multicastAddress);
249-
} finally {
250-
if (multicastSocket != null) {
251-
multicastSocket.close();
252-
}
253-
}*/
243+
// multicast ipv6 address,
244+
try {
245+
multicastAddress = InetAddress.getByName("ff01::1");
254246

255-
} catch (Exception e) {
256-
Assertions.fail(e);
247+
multicastSocket = new MulticastSocket();
248+
multicastSocket.setLoopbackMode(false);
249+
NetUtils.setInterface(multicastSocket, true);
250+
multicastSocket.joinGroup(multicastAddress);
251+
} catch (Throwable t) {
252+
t.printStackTrace();
253+
} finally {
254+
if (multicastSocket != null) {
255+
multicastSocket.close();
256+
}
257257
}
258+
258259
}
259260

260261
}

0 commit comments

Comments
 (0)