1 /*
   2  * Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 6907760 6929532
  26  * @summary Tests WatchService behavior when lots of events are pending (use -Dseed=X to set PRNG seed)
  27  * @library ..
  28  * @library /test/lib
  29  * @run main/timeout=180 LotsOfEvents
  30  * @key randomness
  31  */
  32 
  33 import java.io.IOException;
  34 import java.io.OutputStream;
  35 import java.nio.file.*;
  36 import static java.nio.file.StandardWatchEventKinds.*;
  37 import java.util.*;
  38 import java.util.concurrent.TimeUnit;
  39 import jdk.test.lib.RandomFactory;
  40 
  41 public class LotsOfEvents {
  42 
  43     private static final Random RAND = RandomFactory.getRandom();
  44 
  45     public static void main(String[] args) throws Exception {
  46         Path dir = TestUtil.createTemporaryDirectory();
  47         try {
  48             testOverflowEvent(dir);
  49             testModifyEventsQueuing(dir);
  50         } finally {
  51             TestUtil.removeAll(dir);
  52         }
  53     }
  54 
  55     /**
  56      * Tests that OVERFLOW events are not retreived with other events.
  57      */
  58     static void testOverflowEvent(Path dir)
  59         throws IOException, InterruptedException
  60     {
  61         try (WatchService watcher = dir.getFileSystem().newWatchService()) {
  62             dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
  63 
  64             // create a lot of files
  65             int n = 1024;
  66             Path[] files = new Path[n];
  67             for (int i=0; i<n; i++) {
  68                 files[i] = Files.createFile(dir.resolve("foo" + i));
  69             }
  70 
  71             // give time for events to accumulate (improve chance of overflow)
  72             Thread.sleep(1000);
  73 
  74             // check that we see the create events (or overflow)
  75             drainAndCheckOverflowEvents(dir, watcher, ENTRY_CREATE, n);
  76 
  77             // delete the files
  78             for (int i=0; i<n; i++) {
  79                 Files.delete(files[i]);
  80             }
  81 
  82             // give time for events to accumulate (improve chance of overflow)
  83             Thread.sleep(1000);
  84 
  85             // check that we see the delete events (or overflow)
  86             drainAndCheckOverflowEvents(dir, watcher, ENTRY_DELETE, n);
  87         }
  88     }
  89 
  90     static void drainAndCheckOverflowEvents(Path dir,
  91                                             WatchService watcher,
  92                                             WatchEvent.Kind<?> expectedKind,
  93                                             int count)
  94         throws IOException, InterruptedException
  95     {
  96         // wait for key to be signalled - the timeout is long to allow for
  97         // polling implementations
  98         WatchKey key = watcher.poll(15, TimeUnit.SECONDS);
  99         if (key != null && count == 0)
 100             throw new RuntimeException("Key was signalled (unexpected)");
 101         if (key == null && count > 0)
 102             throw new RuntimeException("Key not signalled (unexpected)");
 103 
 104         int nread = 0;
 105         boolean gotOverflow = false;
 106         while (key != null) {
 107             List<WatchEvent<?>> events = key.pollEvents();
 108             for (WatchEvent<?> event: events) {
 109                 WatchEvent.Kind<?> kind = event.kind();
 110                 if (kind == expectedKind) {
 111                     // expected event kind
 112                     if (++nread > count)
 113                         throw new RuntimeException("More events than expected!!");
 114                 } else if (kind == OVERFLOW) {
 115                     // overflow event should not be retrieved with other events
 116                     if (events.size() > 1)
 117                         throw new RuntimeException("Overflow retrieved with other events");
 118                     gotOverflow = true;
 119                 } else {
 120                     throw new RuntimeException("Unexpected event '" + kind + "'");
 121                 }
 122             }
 123             if (!key.reset())
 124                 throw new RuntimeException("Key is no longer valid");
 125             key = watcher.poll(2, TimeUnit.SECONDS);
 126         }
 127 
 128         // check that all expected events were received or there was an overflow
 129         if (nread < count && !gotOverflow) {
 130             System.err.printf("Test directory %s contains %d files%n",
 131                 dir, Files.list(dir).count());
 132 
 133             long timeBeforePoll = System.nanoTime();
 134             key = watcher.poll(15, TimeUnit.SECONDS);
 135             long timeAfterPoll = System.nanoTime();
 136             if (key == null) {
 137                 System.err.println("key still null after extra polling");
 138             } else {
 139                 List<WatchEvent<?>> events = key.pollEvents();
 140                 System.err.printf("Retrieved key with %d events after %d ns%n",
 141                     events.size(), timeAfterPoll - timeBeforePoll);
 142             }
 143 
 144             throw new RuntimeException("Insufficient "
 145                 + expectedKind.name() + "  events: expected "
 146                 + count + ", received " + nread);
 147         }
 148     }
 149 
 150     /**
 151      * Tests that check that ENTRY_MODIFY events are queued efficiently
 152      */
 153     static void testModifyEventsQueuing(Path dir)
 154         throws IOException, InterruptedException
 155     {
 156         // this test uses a random number of files
 157         final int nfiles = 5 + RAND.nextInt(10);
 158         DirectoryEntry[] entries = new DirectoryEntry[nfiles];
 159         for (int i=0; i<nfiles; i++) {
 160             entries[i] = new DirectoryEntry(dir.resolve("foo" + i));
 161 
 162             // "some" of the files exist, some do not.
 163             entries[i].deleteIfExists();
 164             if (RAND.nextBoolean())
 165                 entries[i].create();
 166         }
 167 
 168         try (WatchService watcher = dir.getFileSystem().newWatchService()) {
 169             dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
 170 
 171             // do several rounds of noise and test
 172             for (int round=0; round<10; round++) {
 173 
 174                 // make some noise!!!
 175                 for (int i=0; i<100; i++) {
 176                     DirectoryEntry entry = entries[RAND.nextInt(nfiles)];
 177                     int action = RAND.nextInt(10);
 178                     switch (action) {
 179                         case 0 : entry.create(); break;
 180                         case 1 : entry.deleteIfExists(); break;
 181                         default: entry.modifyIfExists();
 182                     }
 183                 }
 184 
 185                 // process events and ensure that we don't get repeated modify
 186                 // events for the same file.
 187                 WatchKey key = watcher.poll(15, TimeUnit.SECONDS);
 188                 while (key != null) {
 189                     Set<Path> modified = new HashSet<>();
 190                     for (WatchEvent<?> event: key.pollEvents()) {
 191                         WatchEvent.Kind<?> kind = event.kind();
 192                         Path file = (kind == OVERFLOW) ? null : (Path)event.context();
 193                         if (kind == ENTRY_MODIFY) {
 194                             boolean added = modified.add(file);
 195                             if (!added) {
 196                                 throw new RuntimeException(
 197                                     "ENTRY_MODIFY events not queued efficiently");
 198                             }
 199                         } else {
 200                             if (file != null) modified.remove(file);
 201                         }
 202                     }
 203                     if (!key.reset())
 204                         throw new RuntimeException("Key is no longer valid");
 205                     key = watcher.poll(2, TimeUnit.SECONDS);
 206                 }
 207             }
 208         }
 209     }
 210 
 211     static class DirectoryEntry {
 212         private final Path file;
 213         DirectoryEntry(Path file) {
 214             this.file = file;
 215         }
 216         void create() throws IOException {
 217             if (Files.notExists(file))
 218                 Files.createFile(file);
 219 
 220         }
 221         void deleteIfExists() throws IOException {
 222             Files.deleteIfExists(file);
 223         }
 224         void modifyIfExists() throws IOException {
 225             if (Files.exists(file)) {
 226                 try (OutputStream out = Files.newOutputStream(file, StandardOpenOption.APPEND)) {
 227                     out.write("message".getBytes());
 228                 }
 229             }
 230         }
 231     }
 232 
 233 }