Skip to content

Commit b3c293c

Browse files
committed
8219804: java/net/MulticastSocket/Promiscuous.java fails intermittently due to NumberFormatException
8226683: Remove review suggestion from fix to 8219804 Reviewed-by: clanger Backport-of: eccfec4
1 parent fb886e9 commit b3c293c

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

test/jdk/java/net/MulticastSocket/Promiscuous.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
2424
/* @test
25-
* @bug 8014499
25+
* @bug 8014499 8219804
2626
* @summary Test for interference when two sockets are bound to the same
2727
* port but joined to different multicast groups
2828
* @run main Promiscuous
@@ -42,11 +42,20 @@ static void receive(MulticastSocket mc, boolean datagramExpected, int id)
4242
throws IOException
4343
{
4444
byte[] ba = new byte[100];
45-
DatagramPacket p = new DatagramPacket(ba, ba.length);
45+
DatagramPacket p;
4646
try {
47-
mc.receive(p);
48-
int recvId = Integer.parseInt(
49-
new String(p.getData(), 0, p.getLength(), "UTF-8"));
47+
String data = null;
48+
while (true) {
49+
p = new DatagramPacket(ba, ba.length);
50+
mc.receive(p);
51+
data = new String(p.getData(), 0, p.getLength(), "UTF-8");
52+
if (data.length() > UUID.length() && data.startsWith(UUID)) {
53+
data = data.substring(UUID.length());
54+
break;
55+
}
56+
logUnexpected(p);
57+
}
58+
int recvId = Integer.parseInt(data);
5059
if (datagramExpected) {
5160
if (recvId != id)
5261
throw new RuntimeException("Unexpected id, got " + recvId
@@ -65,6 +74,20 @@ static void receive(MulticastSocket mc, boolean datagramExpected, int id)
6574
}
6675
}
6776

77+
static void logUnexpected(DatagramPacket p) {
78+
byte[] ba = p.getData();
79+
System.out.printf("Unexpected packet: length: %d. First three bytes: %d, %d, %d\n",
80+
p.getLength(), ba[0], ba[1], ba[2]);
81+
}
82+
83+
static final String UUID; // process-id : currentTimeMillis
84+
85+
static {
86+
String s1 = Long.toString(ProcessHandle.current().pid());
87+
String s2 = Long.toString(System.currentTimeMillis());
88+
UUID = "<" + s1 + s2 + ">";
89+
}
90+
6891
static void test(InetAddress group1, InetAddress group2)
6992
throws IOException
7093
{
@@ -77,7 +100,7 @@ static void test(InetAddress group1, InetAddress group2)
77100
mc1.setSoTimeout(TIMEOUT);
78101
mc2.setSoTimeout(TIMEOUT);
79102
int nextId = id;
80-
byte[] msg = Integer.toString(nextId).getBytes("UTF-8");
103+
byte[] msg = (UUID + Integer.toString(nextId)).getBytes("UTF-8");
81104
DatagramPacket p = new DatagramPacket(msg, msg.length);
82105
p.setAddress(group1);
83106
p.setPort(port);
@@ -95,7 +118,7 @@ static void test(InetAddress group1, InetAddress group2)
95118
receive(mc2, false, 0);
96119

97120
nextId = ++id;
98-
msg = Integer.toString(nextId).getBytes("UTF-8");
121+
msg = (UUID + Integer.toString(nextId)).getBytes("UTF-8");
99122
p = new DatagramPacket(msg, msg.length);
100123
p.setAddress(group2);
101124
p.setPort(port);
@@ -129,8 +152,8 @@ public static void main(String args[]) throws IOException {
129152
}
130153

131154
// multicast groups used for the test
132-
InetAddress ip4Group1 = InetAddress.getByName("224.7.8.9");
133-
InetAddress ip4Group2 = InetAddress.getByName("225.4.5.6");
155+
InetAddress ip4Group1 = InetAddress.getByName("224.0.0.120");
156+
InetAddress ip4Group2 = InetAddress.getByName("224.0.0.121");
134157

135158
test(ip4Group1, ip4Group2);
136159
}

0 commit comments

Comments
 (0)