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  * @build jdk.test.lib.RandomFactory
  30  * @run main/timeout=180 LotsOfEvents
  31  * @key randomness
  32  */
  33 
  34 import java.io.IOException;
  35 import java.io.OutputStream;
  36 import java.nio.file.*;
  37 import static java.nio.file.StandardWatchEventKinds.*;
  38 import java.util.*;
  39 import java.util.concurrent.TimeUnit;
  40 import jdk.test.lib.RandomFactory;
  41 
  42 public class LotsOfEvents {
  43 
  44     private static final Random RAND = RandomFactory.getRandom();
  45 
  46     public static void main(String[] args) throws Exception {
  47         Path dir = TestUtil.createTemporaryDirectory();
  48         try {
  49             testOverflowEvent(dir);
  50             testModifyEventsQueuing(dir);
  51         } finally {
  52             TestUtil.removeAll(dir);
  53         }
  54     }
  55 
  56     /**
  57      * Tests that OVERFLOW events are not retreived with other events.
  58      */
  59     static void testOverflowEvent(Path dir)
  60         throws IOException, InterruptedException
  61     {
  62         try (WatchService watcher = dir.getFileSystem().newWatchService()) {
  63             dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
  64 
  65             // create a lot of files
  66             int n = 1024;
  67             Path[] files = new Path[n];
  68             for (int i=0; i<n; i++) {
  69                 files[i] = Files.createFile(dir.resolve("foo" + i));
  70             }
  71 
  72             // give time for events to accumulate (improve chance of overflow)
  73             Thread.sleep(1000);
  74 
  75             // check that we see the create events (or overflow)
  76             drainAndCheckOverflowEvents(dir, watcher, ENTRY_CREATE, n);
  77 
  78             // delete the files
  79             for (int i=0; i<n; i++) {
  80                 Files.delete(files[i]);
  81             }
  82 
  83             // give time for events to accumulate (improve chance of overflow)
  84             Thread.sleep(1000);
  85 
  86             // check that we see the delete events (or overflow)
  87             drainAndCheckOverflowEvents(dir, watcher, ENTRY_DELETE, n);
  88         }
  89     }
  90 
  91     static void drainAndCheckOverflowEvents(Path dir,
  92                                             WatchService watcher,
  93                                             WatchEvent.Kind<?> expectedKind,
  94                                             int count)
  95         throws IOException, InterruptedException
  96     {
  97         // wait for key to be signalled - the timeout is long to allow for
  98         // polling implementations
  99         WatchKey key = watcher.poll(15, TimeUnit.SECONDS);
 100         if (key != null && count == 0)
 101             throw new RuntimeException("Key was signalled (unexpected)");
 102         if (key == null && count > 0)
 103             throw new RuntimeException("Key not signalled (unexpected)");
 104 
 105         int nread = 0;
 106         boolean gotOverflow = false;
 107         while (key != null) {
 108             List<WatchEvent<?>> events = key.pollEvents();
 109             for (WatchEvent<?> event: events) {
 110                 WatchEvent.Kind<?> kind = event.kind();
 111                 if (kind == expectedKind) {
 112                     // expected event kind
 113                     if (++nread > count)
 114                         throw new RuntimeException("More events than expected!!");
 115                 } else if (kind == OVERFLOW) {
 116                     // overflow event should not be retrieved with other events
 117                     if (events.size() > 1)
 118                         throw new RuntimeException("Overflow retrieved with other events");
 119                     gotOverflow = true;
 120                 } else {
 121                     throw new RuntimeException("Unexpected event '" + kind + "'");
 122                 }
 123             }
 124             if (!key.reset())
 125                 throw new RuntimeException("Key is no longer valid");
 126             key = watcher.poll(2, TimeUnit.SECONDS);
 127         }
 128 
 129         // check that all expected events were received or there was an overflow
 130         if (nread < count && !gotOverflow) {
 131             System.err.printf("Test directory %s contains %d files%n",
 132                 dir, Files.list(dir).count());
 133 
 134             long timeBeforePoll = System.nanoTime();
 135             key = watcher.poll(15, TimeUnit.SECONDS);
 136             long timeAfterPoll = System.nanoTime();
 137             if (key == null) {
 138                 System.err.println("key still null after extra polling");
 139             } else {
 140                 List<WatchEvent<?>> events = key.pollEvents();
 141                 System.err.printf("Retrieved key with %d events after %d ns%n",
 142                     events.size(), timeAfterPoll - timeBeforePoll);
 143             }
 144 
 145             throw new RuntimeException("Insufficient "
 146                 + expectedKind.name() + "  events: expected "
 147                 + count + ", received " + nread);
 148         }
 149     }
 150 
 151     /**
 152      * Tests that check that ENTRY_MODIFY events are queued efficiently
 153      */
 154     static void testModifyEventsQueuing(Path dir)
 155         throws IOException, InterruptedException
 156     {
 157         // this test uses a random number of files
 158         final int nfiles = 5 + RAND.nextInt(10);
 159         DirectoryEntry[] entries = new DirectoryEntry[nfiles];
 160         for (int i=0; i<nfiles; i++) {
 161             entries[i] = new DirectoryEntry(dir.resolve("foo" + i));
 162 
 163             // "some" of the files exist, some do not.
 164             entries[i].deleteIfExists();
 165             if (RAND.nextBoolean())
 166                 entries[i].create();
 167         }
 168 
 169         try (WatchService watcher = dir.getFileSystem().newWatchService()) {
 170             dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
 171 
 172             // do several rounds of noise and test
 173             for (int round=0; round<10; round++) {
 174 
 175                 // make some noise!!!
 176                 for (int i=0; i<100; i++) {
 177                     DirectoryEntry entry = entries[RAND.nextInt(nfiles)];
 178                     int action = RAND.nextInt(10);
 179                     switch (action) {
 180                         case 0 : entry.create(); break;
 181                         case 1 : entry.deleteIfExists(); break;
 182                         default: entry.modifyIfExists();
 183                     }
 184                 }
 185 
 186                 // process events and ensure that we don't get repeated modify
 187                 // events for the same file.
 188                 WatchKey key = watcher.poll(15, TimeUnit.SECONDS);
 189                 while (key != null) {
 190                     Set<Path> modified = new HashSet<>();
 191                     for (WatchEvent<?> event: key.pollEvents()) {
 192                         WatchEvent.Kind<?> kind = event.kind();
 193                         Path file = (kind == OVERFLOW) ? null : (Path)event.context();
 194                         if (kind == ENTRY_MODIFY) {
 195                             boolean added = modified.add(file);
 196                             if (!added) {
 197                                 throw new RuntimeException(
 198                                     "ENTRY_MODIFY events not queued efficiently");
 199                             }
 200                         } else {
 201                             if (file != null) modified.remove(file);
 202                         }
 203                     }
 204                     if (!key.reset())
 205                         throw new RuntimeException("Key is no longer valid");
 206                     key = watcher.poll(2, TimeUnit.SECONDS);
 207                 }
 208             }
 209         }
 210     }
 211 
 212     static class DirectoryEntry {
 213         private final Path file;
 214         DirectoryEntry(Path file) {
 215             this.file = file;
 216         }
 217         void create() throws IOException {
 218             if (Files.notExists(file))
 219                 Files.createFile(file);
 220 
 221         }
 222         void deleteIfExists() throws IOException {
 223             Files.deleteIfExists(file);
 224         }
 225         void modifyIfExists() throws IOException {
 226             if (Files.exists(file)) {
 227                 try (OutputStream out = Files.newOutputStream(file, StandardOpenOption.APPEND)) {
 228                     out.write("message".getBytes());
 229                 }
 230             }
 231         }
 232     }
 233 
 234 }