More test updates niosocketimpl-branch
authoralanb
Tue, 09 Apr 2019 12:02:23 +0100
branchniosocketimpl-branch
changeset 57313 14b02c7b27b8
parent 57312 36c96936c5bc
child 57321 eef9324f94cc
More test updates
test/jdk/java/net/Socket/Timeouts.java
test/jdk/java/net/Socket/UdpSocket.java
--- a/test/jdk/java/net/Socket/Timeouts.java	Tue Apr 09 11:48:54 2019 +0100
+++ b/test/jdk/java/net/Socket/Timeouts.java	Tue Apr 09 12:02:23 2019 +0100
@@ -131,13 +131,13 @@
     public void testTimedRead3() throws IOException {
         withConnection((s1, s2) -> {
             s2.setSoTimeout(2000);
-            long start = System.currentTimeMillis();
+            long startMillis = millisTime();
             try {
                 s2.getInputStream().read();
                 assertTrue(false);
             } catch (SocketTimeoutException expected) {
                 int timeout = s2.getSoTimeout();
-                checkDuration(start, timeout-100, timeout+2000);
+                checkDuration(startMillis, timeout-100, timeout+2000);
             }
         });
     }
@@ -316,14 +316,14 @@
     public void testTimedAccept3() throws IOException {
         try (ServerSocket ss = new ServerSocket(0)) {
             ss.setSoTimeout(2000);
-            long start = System.currentTimeMillis();
+            long startMillis = millisTime();
             try {
                 Socket s = ss.accept();
                 s.close();
                 assertTrue(false);
             } catch (SocketTimeoutException expected) {
                 int timeout = ss.getSoTimeout();
-                checkDuration(start, timeout-100, timeout+2000);
+                checkDuration(startMillis, timeout-100, timeout+2000);
             }
         }
     }
@@ -396,12 +396,12 @@
             ss.setSoTimeout(30*1000);
             long delay = 2000;
             scheduleClose(ss, delay);
-            long start = System.currentTimeMillis();
+            long startMillis = millisTime();
             try {
                 ss.accept().close();
                 assertTrue(false);
             } catch (SocketException expected) {
-                checkDuration(start, delay-100, delay+2000);
+                checkDuration(startMillis, delay-100, delay+2000);
             }
         }
     }
@@ -413,7 +413,7 @@
         try (ServerSocket ss = new ServerSocket(0)) {
             ss.setSoTimeout(2000);
             Thread.currentThread().interrupt();
-            long start = System.currentTimeMillis();
+            long startMillis = millisTime();
             try {
                 Socket s = ss.accept();
                 s.close();
@@ -421,7 +421,7 @@
             } catch (SocketTimeoutException expected) {
                 // accept should have blocked for 2 seconds
                 int timeout = ss.getSoTimeout();
-                checkDuration(start, timeout-100, timeout+2000);
+                checkDuration(startMillis, timeout-100, timeout+2000);
                 assertTrue(Thread.currentThread().isInterrupted());
             } finally {
                 Thread.interrupted(); // clear interrupt status
@@ -437,7 +437,7 @@
             ss.setSoTimeout(4000);
             // interrupt thread after 1 second
             Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 1000);
-            long start = System.currentTimeMillis();
+            long startMillis = millisTime();
             try {
                 Socket s = ss.accept();   // should block for 4 seconds
                 s.close();
@@ -445,7 +445,7 @@
             } catch (SocketTimeoutException expected) {
                 // accept should have blocked for 4 seconds
                 int timeout = ss.getSoTimeout();
-                checkDuration(start, timeout-100, timeout+2000);
+                checkDuration(startMillis, timeout-100, timeout+2000);
                 assertTrue(Thread.currentThread().isInterrupted());
             } finally {
                 interrupter.cancel(true);
@@ -462,7 +462,7 @@
         try (ServerSocket ss = new ServerSocket(0)) {
             ss.setSoTimeout(4000);
 
-            long start = System.currentTimeMillis();
+            long startMillis = millisTime();
 
             Future<Socket> result1 = pool.submit(ss::accept);
             Future<Socket> result2 = pool.submit(ss::accept);
@@ -475,7 +475,7 @@
 
             // should get here in 4 seconds, not 8 seconds
             int timeout = ss.getSoTimeout();
-            checkDuration(start, timeout-100, timeout+2000);
+            checkDuration(startMillis, timeout-100, timeout+2000);
         } finally {
             pool.shutdown();
         }
@@ -489,7 +489,7 @@
         try (ServerSocket ss = new ServerSocket(0)) {
             ss.setSoTimeout(4000);
 
-            long start = System.currentTimeMillis();
+            long startMillis = millisTime();
 
             Future<Socket> result1 = pool.submit(ss::accept);
             Future<Socket> result2 = pool.submit(ss::accept);
@@ -517,7 +517,7 @@
 
             // should get here in 4 seconds, not 8 seconds
             int timeout = ss.getSoTimeout();
-            checkDuration(start, timeout-100, timeout+2000);
+            checkDuration(startMillis, timeout-100, timeout+2000);
         } finally {
             pool.shutdown();
         }
@@ -631,6 +631,14 @@
     }
 
     /**
+     * Returns the current time in milliseconds.
+     */
+    private static long millisTime() {
+        long now = System.nanoTime();
+        return TimeUnit.MILLISECONDS.convert(now, TimeUnit.NANOSECONDS);
+    }
+
+    /**
      * Check the duration of a task
      * @param start start time, in milliseconds
      * @param min minimum expected duration, in milliseconds
@@ -638,8 +646,11 @@
      * @return the duration (now - start), in milliseconds
      */
     private static long checkDuration(long start, long min, long max) {
-        long duration = System.currentTimeMillis() - start;
-        assertTrue(duration >= min && duration <= max);
+        long duration = millisTime() - start;
+        assertTrue(duration >= min,
+                "Duration " + duration + "ms, expected >= " + min + "ms");
+        assertTrue(duration <= max,
+                "Duration " + duration + "ms, expected <= " + max + "ms");
         return duration;
     }
 }
--- a/test/jdk/java/net/Socket/UdpSocket.java	Tue Apr 09 11:48:54 2019 +0100
+++ b/test/jdk/java/net/Socket/UdpSocket.java	Tue Apr 09 12:02:23 2019 +0100
@@ -137,8 +137,9 @@
     }
 
     private static void closeAll(Deque<Socket> sockets) throws IOException {
-        sockets.forEach(s -> {
-            try { s.close(); } catch (IOException ignore) { }
-        });
+        Socket s;
+        while ((s = sockets.poll()) != null) {
+            s.close();
+        }
     }
 }