changeset 55958:73250862f818

8219804: java/net/MulticastSocket/Promiscuous.java fails intermittently due to NumberFormatException Reviewed-by: chegar, dfuchs
author michaelm
date Mon, 24 Jun 2019 15:10:44 +0100
parents c9e362aef472
children 6ca3526c4e25
files test/jdk/java/net/MulticastSocket/Promiscuous.java
diffstat 1 files changed, 37 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/java/net/MulticastSocket/Promiscuous.java	Mon Jun 24 09:51:45 2019 -0400
+++ b/test/jdk/java/net/MulticastSocket/Promiscuous.java	Mon Jun 24 15:10:44 2019 +0100
@@ -22,7 +22,7 @@
  *
 
 /* @test
- * @bug 8014499
+ * @bug 8014499 8219804
  * @library /test/lib
  * @summary Test for interference when two sockets are bound to the same
  *          port but joined to different multicast groups
@@ -44,11 +44,20 @@
         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
@@ -67,19 +76,36 @@
         }
     }
 
+    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
     {
-        try (MulticastSocket mc1 = new MulticastSocket();
-             MulticastSocket mc2 = new MulticastSocket(mc1.getLocalPort());
+        InetSocketAddress ad1 = new InetSocketAddress(group1, 0);
+        try (MulticastSocket mc1 = new MulticastSocket(ad1);
+             MulticastSocket mc2 = new MulticastSocket(
+                 new InetSocketAddress(group2, mc1.getLocalPort()));
              DatagramSocket ds = new DatagramSocket()) {
+
             final int port = mc1.getLocalPort();
             out.printf("Using port: %d\n", port);
 
             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);
@@ -97,7 +123,7 @@
             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);
@@ -116,7 +142,6 @@
 
     public static void main(String args[]) throws IOException {
         IPSupport.throwSkippedExceptionIfNonOperational();
-
         String os = System.getProperty("os.name");
 
         // Requires IP_MULTICAST_ALL on Linux (new since 2.6.31) so skip
@@ -133,8 +158,8 @@
         }
 
         // 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);
     }