Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/WatchService/SensitivityModifier.java
41153 views
1
/*
2
* Copyright (c) 2008, 2021, 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 4313887
26
* @summary Sanity test for JDK-specific sensitivity level watch event modifier
27
* @modules jdk.unsupported
28
* @library .. /test/lib
29
* @build jdk.test.lib.Platform
30
* @build jdk.test.lib.RandomFactory
31
* @run main/timeout=240 SensitivityModifier
32
* @key randomness
33
*/
34
35
import java.nio.file.Files;
36
import java.nio.file.FileSystem;
37
import java.nio.file.Path;
38
import java.nio.file.WatchEvent;
39
import java.nio.file.WatchKey;
40
import java.nio.file.WatchService;
41
import static java.nio.file.StandardWatchEventKinds.*;
42
import java.io.OutputStream;
43
import java.io.IOException;
44
import java.util.HashMap;
45
import java.util.Map;
46
import java.util.Random;
47
import java.util.concurrent.TimeUnit;
48
import com.sun.nio.file.SensitivityWatchEventModifier;
49
import jdk.test.lib.Platform;
50
import jdk.test.lib.RandomFactory;
51
52
public class SensitivityModifier {
53
// on macOS and other platforms, watch services might be based on polling
54
// requiring a longer timeout to detect events before returning
55
static final long POLL_TIMEOUT_SECONDS =
56
Platform.isLinux() || Platform.isWindows() ? 1 : 2;
57
58
static final Random RAND = RandomFactory.getRandom();
59
60
static final Map<Path,Integer> pathToTime = new HashMap<>();
61
62
static void register(Path[] dirs, WatchService watcher) throws IOException {
63
pathToTime.clear();
64
SensitivityWatchEventModifier[] sensitivities =
65
SensitivityWatchEventModifier.values();
66
for (int i=0; i<dirs.length; i++) {
67
SensitivityWatchEventModifier sensitivity =
68
sensitivities[RAND.nextInt(sensitivities.length)];
69
Path dir = dirs[i];
70
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY },
71
sensitivity);
72
pathToTime.put(dir, sensitivity.sensitivityValueInSeconds());
73
}
74
}
75
76
@SuppressWarnings("unchecked")
77
static void doTest(Path top) throws Exception {
78
FileSystem fs = top.getFileSystem();
79
try (WatchService watcher = fs.newWatchService()) {
80
81
// create directories and files
82
int nDirs = 5 + RAND.nextInt(20);
83
int nFiles = 50 + RAND.nextInt(50);
84
Path[] dirs = new Path[nDirs];
85
Path[] files = new Path[nFiles];
86
for (int i=0; i<nDirs; i++) {
87
dirs[i] = Files.createDirectory(top.resolve("dir" + i));
88
}
89
for (int i=0; i<nFiles; i++) {
90
Path dir = dirs[RAND.nextInt(nDirs)];
91
files[i] = Files.createFile(dir.resolve("file" + i));
92
}
93
94
// register the directories (random sensitivity)
95
register(dirs, watcher);
96
97
// sleep a bit here to ensure that modification to the first file
98
// can be detected by polling implementations (ie: last modified time
99
// may not change otherwise).
100
try { Thread.sleep(1000); } catch (InterruptedException e) { }
101
102
// modify files and check that events are received
103
for (int i=0; i<10; i++) {
104
Path file = files[RAND.nextInt(nFiles)];
105
System.out.println("Modify: " + file);
106
try (OutputStream out = Files.newOutputStream(file)) {
107
out.write(new byte[100]);
108
}
109
110
System.out.println("Waiting for event(s)...");
111
boolean eventReceived = false;
112
WatchKey key = watcher.take();
113
do {
114
for (WatchEvent<?> event: key.pollEvents()) {
115
if (event.kind() != ENTRY_MODIFY)
116
throw new RuntimeException("Unexpected event: " + event);
117
Path name = ((WatchEvent<Path>)event).context();
118
if (name.equals(file.getFileName())) {
119
eventReceived = true;
120
break;
121
}
122
}
123
key.reset();
124
key = watcher.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS);
125
} while (key != null);
126
127
// we should have received at least one ENTRY_MODIFY event
128
if (eventReceived) {
129
System.out.println("Event OK");
130
} else {
131
Path parent = file.getParent();
132
String msg = String.format("No ENTRY_MODIFY event received for %s (dir: %s, sensitivity: %d)",
133
file, parent, pathToTime.get(parent));
134
throw new RuntimeException(msg);
135
}
136
137
// re-register the directories to force changing their sensitivity
138
// level
139
register(dirs, watcher);
140
}
141
}
142
}
143
144
public static void main(String[] args) throws Exception {
145
Path dir = TestUtil.createTemporaryDirectory();
146
try {
147
doTest(dir);
148
} finally {
149
TestUtil.removeAll(dir);
150
}
151
}
152
}
153
154