8229348: java/net/DatagramSocket/UnreferencedDatagramSockets.java fails intermittently
authordfuchs
Tue, 13 Aug 2019 16:18:43 +0100
changeset 57731 a414a1b8884a
parent 57730 0ec272e1822e
child 57732 23e13076e102
8229348: java/net/DatagramSocket/UnreferencedDatagramSockets.java fails intermittently Summary: The test was observed blocking on receive and is updated to avoid using the wildcard address Reviewed-by: chegar, msheppar
test/jdk/java/net/DatagramSocket/UnreferencedDatagramSockets.java
--- a/test/jdk/java/net/DatagramSocket/UnreferencedDatagramSockets.java	Tue Aug 13 16:11:28 2019 +0100
+++ b/test/jdk/java/net/DatagramSocket/UnreferencedDatagramSockets.java	Tue Aug 13 16:18:43 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -49,6 +49,7 @@
 import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.CountDownLatch;
 
 import com.sun.management.UnixOperatingSystemMXBean;
 
@@ -70,9 +71,10 @@
     static class Server implements Runnable {
 
         DatagramSocket ss;
+        CountDownLatch latch = new CountDownLatch(1);
 
         Server() throws IOException {
-            ss = new DatagramSocket(0);
+            ss = new DatagramSocket(0, getHost());
             System.out.printf("  DatagramServer addr: %s: %d%n",
                     this.getHost(), this.getPort());
             pendingSockets.add(new NamedWeak(ss, pendingQueue, "serverDatagramSocket"));
@@ -80,7 +82,7 @@
         }
 
         InetAddress getHost() throws UnknownHostException {
-            InetAddress localhost = InetAddress.getByName("localhost"); //.getLocalHost();
+            InetAddress localhost = lookupLocalHost();
             return localhost;
         }
 
@@ -96,7 +98,7 @@
                 ss.receive(p);
                 buffer[0] += 1;
                 ss.send(p);         // send back +1
-
+                latch.await();      // wait for the client to receive the packet
                 // do NOT close but 'forget' the datagram socket reference
                 ss = null;
             } catch (Exception ioe) {
@@ -105,11 +107,15 @@
         }
     }
 
+    static InetAddress lookupLocalHost() throws UnknownHostException {
+        return InetAddress.getByName("localhost"); //.getLocalHost();
+    }
+
     public static void main(String args[]) throws Exception {
         IPSupport.throwSkippedExceptionIfNonOperational();
 
         // Create and close a DatagramSocket to warm up the FD count for side effects.
-        try (DatagramSocket s = new DatagramSocket(0)) {
+        try (DatagramSocket s = new DatagramSocket(0, lookupLocalHost())) {
             // no-op; close immediately
             s.getLocalPort();   // no-op
         }
@@ -122,7 +128,7 @@
         Thread thr = new Thread(svr);
         thr.start();
 
-        DatagramSocket client = new DatagramSocket(0);
+        DatagramSocket client = new DatagramSocket(0, lookupLocalHost());
         client.connect(svr.getHost(), svr.getPort());
         pendingSockets.add(new NamedWeak(client, pendingQueue, "clientDatagramSocket"));
         extractRefs(client, "clientDatagramSocket");
@@ -134,6 +140,8 @@
 
         p = new DatagramPacket(msg, msg.length);
         client.receive(p);
+        svr.latch.countDown(); // unblock the server
+
 
         System.out.printf("echo received from: %s%n", p.getSocketAddress());
         if (msg[0] != 2) {