8175115: Improve instrumentation of java/nio/file/WatchService/LotsOfEvents.java
authorbpb
Fri, 24 Feb 2017 11:28:00 -0800
changeset 44001 47c9904f9d02
parent 44000 1c98f4d91595
child 44002 553ebf3c9503
8175115: Improve instrumentation of java/nio/file/WatchService/LotsOfEvents.java Summary: Attempt an additional long poll to check for missed events and print more information Reviewed-by: rriggs
jdk/test/java/nio/file/WatchService/LotsOfEvents.java
--- a/jdk/test/java/nio/file/WatchService/LotsOfEvents.java	Fri Feb 24 09:26:59 2017 -0800
+++ b/jdk/test/java/nio/file/WatchService/LotsOfEvents.java	Fri Feb 24 11:28:00 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2017, 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
@@ -23,22 +23,25 @@
 
 /* @test
  * @bug 6907760 6929532
- * @summary Tests WatchService behavior when lots of events are pending
+ * @summary Tests WatchService behavior when lots of events are pending (use -Dseed=X to set PRNG seed)
  * @library ..
+ * @library /lib/testlibrary/
+ * @build jdk.testlibrary.*
  * @run main/timeout=180 LotsOfEvents
  * @key randomness
  */
 
+import java.io.IOException;
+import java.io.OutputStream;
 import java.nio.file.*;
 import static java.nio.file.StandardWatchEventKinds.*;
-import java.io.IOException;
-import java.io.OutputStream;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
+import jdk.testlibrary.RandomFactory;
 
 public class LotsOfEvents {
 
-    static final Random rand = new Random();
+    private static final Random RAND = RandomFactory.getRandom();
 
     public static void main(String[] args) throws Exception {
         Path dir = TestUtil.createTemporaryDirectory();
@@ -70,7 +73,7 @@
             Thread.sleep(1000);
 
             // check that we see the create events (or overflow)
-            drainAndCheckOverflowEvents(watcher, ENTRY_CREATE, n);
+            drainAndCheckOverflowEvents(dir, watcher, ENTRY_CREATE, n);
 
             // delete the files
             for (int i=0; i<n; i++) {
@@ -81,11 +84,12 @@
             Thread.sleep(1000);
 
             // check that we see the delete events (or overflow)
-            drainAndCheckOverflowEvents(watcher, ENTRY_DELETE, n);
+            drainAndCheckOverflowEvents(dir, watcher, ENTRY_DELETE, n);
         }
     }
 
-    static void drainAndCheckOverflowEvents(WatchService watcher,
+    static void drainAndCheckOverflowEvents(Path dir,
+                                            WatchService watcher,
                                             WatchEvent.Kind<?> expectedKind,
                                             int count)
         throws IOException, InterruptedException
@@ -123,8 +127,25 @@
         }
 
         // check that all expected events were received or there was an overflow
-        if (nread < count && !gotOverflow)
-            throw new RuntimeException("Insufficient events");
+        if (nread < count && !gotOverflow) {
+            System.err.printf("Test directory %s contains %d files%n",
+                dir, Files.list(dir).count());
+
+            long timeBeforePoll = System.nanoTime();
+            key = watcher.poll(15, TimeUnit.SECONDS);
+            long timeAfterPoll = System.nanoTime();
+            if (key == null) {
+                System.err.println("key still null after extra polling");
+            } else {
+                List<WatchEvent<?>> events = key.pollEvents();
+                System.err.printf("Retrieved key with %d events after %d ns%n",
+                    events.size(), timeAfterPoll - timeBeforePoll);
+            }
+
+            throw new RuntimeException("Insufficient "
+                + expectedKind.name() + "  events: expected "
+                + count + ", received " + nread);
+        }
     }
 
     /**
@@ -134,14 +155,14 @@
         throws IOException, InterruptedException
     {
         // this test uses a random number of files
-        final int nfiles = 5 + rand.nextInt(10);
+        final int nfiles = 5 + RAND.nextInt(10);
         DirectoryEntry[] entries = new DirectoryEntry[nfiles];
         for (int i=0; i<nfiles; i++) {
             entries[i] = new DirectoryEntry(dir.resolve("foo" + i));
 
             // "some" of the files exist, some do not.
             entries[i].deleteIfExists();
-            if (rand.nextBoolean())
+            if (RAND.nextBoolean())
                 entries[i].create();
         }
 
@@ -153,8 +174,8 @@
 
                 // make some noise!!!
                 for (int i=0; i<100; i++) {
-                    DirectoryEntry entry = entries[rand.nextInt(nfiles)];
-                    int action = rand.nextInt(10);
+                    DirectoryEntry entry = entries[RAND.nextInt(nfiles)];
+                    int action = RAND.nextInt(10);
                     switch (action) {
                         case 0 : entry.create(); break;
                         case 1 : entry.deleteIfExists(); break;