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