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