test/java/nio/file/WatchService/LotsOfEvents.java

Print this page

        

*** 53,71 **** * Tests that OVERFLOW events are not retreived with other events. */ static void testOverflowEvent(Path dir) throws IOException, InterruptedException { ! WatchService watcher = dir.getFileSystem().newWatchService(); ! try { dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); // create a lot of files int n = 1024; Path[] files = new Path[n]; for (int i=0; i<n; i++) { ! files[i] = dir.resolve("foo" + i).createFile(); } // give time for events to accumulate (improve chance of overflow) Thread.sleep(1000); --- 53,70 ---- * Tests that OVERFLOW events are not retreived with other events. */ static void testOverflowEvent(Path dir) throws IOException, InterruptedException { ! try (WatchService watcher = dir.getFileSystem().newWatchService()) { dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE); // create a lot of files int n = 1024; Path[] files = new Path[n]; for (int i=0; i<n; i++) { ! files[i] = Files.createFile(dir.resolve("foo" + i)); } // give time for events to accumulate (improve chance of overflow) Thread.sleep(1000);
*** 72,91 **** // check that we see the create events (or overflow) drainAndCheckOverflowEvents(watcher, ENTRY_CREATE, n); // delete the files for (int i=0; i<n; i++) { ! files[i].delete(); } // give time for events to accumulate (improve chance of overflow) Thread.sleep(1000); // check that we see the delete events (or overflow) drainAndCheckOverflowEvents(watcher, ENTRY_DELETE, n); - } finally { - watcher.close(); } } static void drainAndCheckOverflowEvents(WatchService watcher, WatchEvent.Kind<?> expectedKind, --- 71,88 ---- // check that we see the create events (or overflow) drainAndCheckOverflowEvents(watcher, ENTRY_CREATE, n); // delete the files for (int i=0; i<n; i++) { ! Files.delete(files[i]); } // give time for events to accumulate (improve chance of overflow) Thread.sleep(1000); // check that we see the delete events (or overflow) drainAndCheckOverflowEvents(watcher, ENTRY_DELETE, n); } } static void drainAndCheckOverflowEvents(WatchService watcher, WatchEvent.Kind<?> expectedKind,
*** 145,156 **** entries[i].deleteIfExists(); if (rand.nextBoolean()) entries[i].create(); } ! WatchService watcher = dir.getFileSystem().newWatchService(); ! try { dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); // do several rounds of noise and test for (int round=0; round<10; round++) { --- 142,152 ---- entries[i].deleteIfExists(); if (rand.nextBoolean()) entries[i].create(); } ! try (WatchService watcher = dir.getFileSystem().newWatchService()) { dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); // do several rounds of noise and test for (int round=0; round<10; round++) {
*** 167,177 **** // process events and ensure that we don't get repeated modify // events for the same file. WatchKey key = watcher.poll(15, TimeUnit.SECONDS); while (key != null) { ! Set<Path> modified = new HashSet<Path>(); for (WatchEvent<?> event: key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); Path file = (kind == OVERFLOW) ? null : (Path)event.context(); if (kind == ENTRY_MODIFY) { boolean added = modified.add(file); --- 163,173 ---- // process events and ensure that we don't get repeated modify // events for the same file. WatchKey key = watcher.poll(15, TimeUnit.SECONDS); while (key != null) { ! Set<Path> modified = new HashSet<>(); for (WatchEvent<?> event: key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); Path file = (kind == OVERFLOW) ? null : (Path)event.context(); if (kind == ENTRY_MODIFY) { boolean added = modified.add(file);
*** 186,221 **** if (!key.reset()) throw new RuntimeException("Key is no longer valid"); key = watcher.poll(2, TimeUnit.SECONDS); } } - - } finally { - watcher.close(); } } static class DirectoryEntry { private final Path file; DirectoryEntry(Path file) { this.file = file; } void create() throws IOException { ! if (file.notExists()) ! file.createFile(); } void deleteIfExists() throws IOException { ! file.deleteIfExists(); } void modifyIfExists() throws IOException { ! if (file.exists()) { ! OutputStream out = file.newOutputStream(StandardOpenOption.APPEND); ! try { out.write("message".getBytes()); - } finally { - out.close(); } } } } --- 182,211 ---- if (!key.reset()) throw new RuntimeException("Key is no longer valid"); key = watcher.poll(2, TimeUnit.SECONDS); } } } } static class DirectoryEntry { private final Path file; DirectoryEntry(Path file) { this.file = file; } void create() throws IOException { ! if (Files.notExists(file)) ! Files.createFile(file); } void deleteIfExists() throws IOException { ! Files.deleteIfExists(file); } void modifyIfExists() throws IOException { ! if (Files.exists(file)) { ! try (OutputStream out = Files.newOutputStream(file, StandardOpenOption.APPEND)) { out.write("message".getBytes()); } } } }