Skip to content

Commit 1783437

Browse files
committed
8264975: java/net/DatagramSocket/DatagramSocketMulticasting.java fails infrequently
Reviewed-by: alanb, chegar
1 parent a52a08d commit 1783437

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

test/jdk/java/net/DatagramSocket/DatagramSocketMulticasting.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@
3838
import java.net.DatagramSocket;
3939
import java.net.InetAddress;
4040
import java.net.InetSocketAddress;
41-
import java.net.MulticastSocket;
4241
import java.net.NetworkInterface;
4342
import java.net.ProtocolFamily;
4443
import java.net.SocketAddress;
4544
import java.net.SocketException;
4645
import java.net.SocketOption;
4746
import java.net.SocketTimeoutException;
48-
import java.nio.channels.DatagramChannel;
4947
import java.util.Arrays;
5048
import java.util.List;
5149
import java.util.stream.Collectors;
@@ -59,6 +57,7 @@
5957
import static java.net.StandardSocketOptions.IP_MULTICAST_LOOP;
6058
import static java.net.StandardSocketOptions.IP_MULTICAST_TTL;
6159
import static java.net.StandardSocketOptions.SO_REUSEADDR;
60+
import static jdk.test.lib.NetworkConfiguration.isSameInterface;
6261

6362
public class DatagramSocketMulticasting {
6463
static final ProtocolFamily UNSPEC = () -> "UNSPEC";
@@ -239,7 +238,7 @@ static void testNetworkInterface(DatagramSocket s,
239238

240239
// setOption(IP_MULTICAST_IF)
241240
s.setOption(IP_MULTICAST_IF, ni);
242-
assertTrue(s.getOption(IP_MULTICAST_IF).equals(ni));
241+
assertTrue(isSameInterface(s.getOption(IP_MULTICAST_IF), ni));
243242

244243
// bad values for IP_MULTICAST_IF
245244
assertThrows(IllegalArgumentException.class,

test/jdk/java/net/DatagramSocket/SendReceiveMaxSize.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void setUp() throws IOException {
7979
IPSupport.throwSkippedExceptionIfNonOperational();
8080
HOST_ADDR = InetAddress.getLocalHost();
8181
BUF_LIMIT = (HOST_ADDR instanceof Inet6Address) ? IPV6_SNDBUF : IPV4_SNDBUF;
82+
System.out.printf("Host address: %s, Buffer limit: %d%n", HOST_ADDR, BUF_LIMIT);
8283
}
8384

8485
@DataProvider

test/lib/jdk/test/lib/NetworkConfiguration.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
3535
import java.util.LinkedList;
3636
import java.util.List;
3737
import java.util.Map;
38+
import java.util.Objects;
3839
import java.util.function.Predicate;
3940
import java.util.stream.Collectors;
4041
import java.util.stream.Stream;
@@ -92,6 +93,38 @@ private static boolean isIPv6LinkLocal(InetAddress a) {
9293
return Inet6Address.class.isInstance(a) && a.isLinkLocalAddress();
9394
}
9495

96+
/**
97+
* Returns true if the two interfaces point to the same network interface.
98+
*
99+
* @implNote
100+
* This method first looks at whether the two interfaces are
101+
* {@linkplain NetworkInterface#equals(Object) equals}, and if so it returns
102+
* true. Otherwise, it looks at whether the two interfaces have the same
103+
* {@linkplain NetworkInterface#getName() name} and
104+
* {@linkplain NetworkInterface#getIndex() index}, and if so returns true.
105+
* Otherwise, it returns false.
106+
*
107+
* @apiNote
108+
* This method ignores differences in the addresses to which the network
109+
* interfaces are bound, to cater for possible reconfiguration that might
110+
* have happened between the time at which each interface configuration
111+
* was looked up.
112+
*
113+
* @param ni1 A network interface, may be {@code null}
114+
* @param ni2 An other network interface, may be {@code null}
115+
* @return {@code true} if the two network interfaces have the same name
116+
* and index, {@code false} otherwise.
117+
*/
118+
public static boolean isSameInterface(NetworkInterface ni1, NetworkInterface ni2) {
119+
if (Objects.equals(ni1, ni2)) return true;
120+
// Objects equals has taken care of the case where
121+
// ni1 == ni2 so either they are both non-null or only
122+
// one of them is null - in which case they can't be equal.
123+
if (ni1 == null || ni2 == null) return false;
124+
if (ni1.getIndex() != ni2.getIndex()) return false;
125+
return Objects.equals(ni1.getName(), ni2.getName());
126+
}
127+
95128
public static boolean isTestable(NetworkInterface nif) {
96129
if (Platform.isOSX()) {
97130
if (nif.getName().contains("awdl")) {

0 commit comments

Comments
 (0)