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