Skip to content
Permalink
Browse files
8219804: java/net/MulticastSocket/Promiscuous.java fails intermittent…
…ly due to NumberFormatException

8226683: Remove review suggestion from fix to 8219804

Reviewed-by: clanger
Backport-of: eccfec4
  • Loading branch information
shipilev committed Aug 24, 2021
1 parent fb886e9 commit b3c293cf4f6539f3d42035585ca48955cda0ba5d
Showing with 32 additions and 9 deletions.
  1. +32 −9 test/jdk/java/net/MulticastSocket/Promiscuous.java
@@ -22,7 +22,7 @@
*
/* @test
* @bug 8014499
* @bug 8014499 8219804
* @summary Test for interference when two sockets are bound to the same
* port but joined to different multicast groups
* @run main Promiscuous
@@ -42,11 +42,20 @@ static void receive(MulticastSocket mc, boolean datagramExpected, int id)
throws IOException
{
byte[] ba = new byte[100];
DatagramPacket p = new DatagramPacket(ba, ba.length);
DatagramPacket p;
try {
mc.receive(p);
int recvId = Integer.parseInt(
new String(p.getData(), 0, p.getLength(), "UTF-8"));
String data = null;
while (true) {
p = new DatagramPacket(ba, ba.length);
mc.receive(p);
data = new String(p.getData(), 0, p.getLength(), "UTF-8");
if (data.length() > UUID.length() && data.startsWith(UUID)) {
data = data.substring(UUID.length());
break;
}
logUnexpected(p);
}
int recvId = Integer.parseInt(data);
if (datagramExpected) {
if (recvId != id)
throw new RuntimeException("Unexpected id, got " + recvId
@@ -65,6 +74,20 @@ static void receive(MulticastSocket mc, boolean datagramExpected, int id)
}
}

static void logUnexpected(DatagramPacket p) {
byte[] ba = p.getData();
System.out.printf("Unexpected packet: length: %d. First three bytes: %d, %d, %d\n",
p.getLength(), ba[0], ba[1], ba[2]);
}

static final String UUID; // process-id : currentTimeMillis

static {
String s1 = Long.toString(ProcessHandle.current().pid());
String s2 = Long.toString(System.currentTimeMillis());
UUID = "<" + s1 + s2 + ">";
}

static void test(InetAddress group1, InetAddress group2)
throws IOException
{
@@ -77,7 +100,7 @@ static void test(InetAddress group1, InetAddress group2)
mc1.setSoTimeout(TIMEOUT);
mc2.setSoTimeout(TIMEOUT);
int nextId = id;
byte[] msg = Integer.toString(nextId).getBytes("UTF-8");
byte[] msg = (UUID + Integer.toString(nextId)).getBytes("UTF-8");
DatagramPacket p = new DatagramPacket(msg, msg.length);
p.setAddress(group1);
p.setPort(port);
@@ -95,7 +118,7 @@ static void test(InetAddress group1, InetAddress group2)
receive(mc2, false, 0);

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

// multicast groups used for the test
InetAddress ip4Group1 = InetAddress.getByName("224.7.8.9");
InetAddress ip4Group2 = InetAddress.getByName("225.4.5.6");
InetAddress ip4Group1 = InetAddress.getByName("224.0.0.120");
InetAddress ip4Group2 = InetAddress.getByName("224.0.0.121");

test(ip4Group1, ip4Group2);
}

1 comment on commit b3c293c

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on b3c293c Aug 24, 2021

Please sign in to comment.