Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/WatchService/UpdateInterference.java
41153 views
1
/*
2
* Copyright (c) 2015, 2016, 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 8145981
26
* @summary LinuxWatchService sometimes reports inotify events against wrong directory
27
* @run main UpdateInterference
28
*/
29
import java.io.IOException;
30
import java.nio.file.*;
31
import java.util.List;
32
import java.util.concurrent.TimeUnit;
33
import static java.nio.file.StandardWatchEventKinds.*;
34
35
public class UpdateInterference {
36
37
private static volatile boolean stop;
38
39
public static void main(String[] args) throws IOException, InterruptedException {
40
final Path root = Files.createTempDirectory("test");
41
final Path foo = root.resolve("foo");
42
final Path bar = root.resolve("bar");
43
final Path baz = root.resolve("baz");
44
45
Files.createDirectory(foo);
46
Files.createDirectory(bar);
47
Files.createDirectory(baz);
48
49
try (final WatchService watcher = root.getFileSystem().newWatchService()) {
50
final WatchKey fooKey = foo.register(watcher, ENTRY_CREATE);
51
final WatchKey barKey = bar.register(watcher, ENTRY_CREATE);
52
53
Thread t1 = null;
54
Thread t2 = null;
55
try {
56
t1 = new Thread() {
57
58
@Override
59
public void run() {
60
while (!stop) {
61
try {
62
final Path temp = Files.createTempFile(foo, "temp", ".tmp");
63
Files.delete(temp);
64
Thread.sleep(10);
65
} catch (IOException | InterruptedException e) {
66
throw new RuntimeException(e);
67
}
68
}
69
}
70
};
71
72
t2 = new Thread() {
73
74
@Override
75
public void run() {
76
WatchKey bazKeys[] = new WatchKey[32];
77
while (!stop) {
78
try {
79
for( int i = 0; i < bazKeys.length; i++) {
80
bazKeys[i] = baz.register(watcher, ENTRY_CREATE);
81
}
82
for( int i = 0; i < bazKeys.length; i++) {
83
bazKeys[i].cancel();
84
}
85
Thread.sleep(1);
86
} catch (IOException | InterruptedException e) {
87
throw new RuntimeException(e);
88
}
89
}
90
}
91
};
92
93
t1.start();
94
t2.start();
95
96
long time = System.currentTimeMillis();
97
while ((System.currentTimeMillis() - time) < 15000) {
98
final WatchKey key = watcher.poll(60, TimeUnit.SECONDS);
99
if (key == null) continue;
100
101
if (key != fooKey) {
102
List<WatchEvent<?>> pollEvents = key.pollEvents();
103
for (WatchEvent<?> watchEvent : pollEvents) {
104
System.out.println(watchEvent.count() + " " +
105
watchEvent.kind() + " " +
106
watchEvent.context());
107
}
108
throw new RuntimeException("Event received for unexpected key");
109
}
110
key.reset();
111
}
112
} finally {
113
// the threads should stop before WatchService is closed
114
// to avoid ClosedWatchServiceException
115
stop = true;
116
117
// wait for threads to finish
118
if (t1 != null) {
119
t1.join();
120
}
121
122
if (t2 != null) {
123
t2.join();
124
}
125
}
126
} finally {
127
// clean up
128
Files.delete(foo);
129
Files.delete(bar);
130
Files.delete(baz);
131
Files.delete(root);
132
}
133
}
134
}
135
136
137