Path: blob/master/test/jdk/java/nio/file/WatchService/SensitivityModifier.java
41153 views
/*1* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 431388725* @summary Sanity test for JDK-specific sensitivity level watch event modifier26* @modules jdk.unsupported27* @library .. /test/lib28* @build jdk.test.lib.Platform29* @build jdk.test.lib.RandomFactory30* @run main/timeout=240 SensitivityModifier31* @key randomness32*/3334import java.nio.file.Files;35import java.nio.file.FileSystem;36import java.nio.file.Path;37import java.nio.file.WatchEvent;38import java.nio.file.WatchKey;39import java.nio.file.WatchService;40import static java.nio.file.StandardWatchEventKinds.*;41import java.io.OutputStream;42import java.io.IOException;43import java.util.HashMap;44import java.util.Map;45import java.util.Random;46import java.util.concurrent.TimeUnit;47import com.sun.nio.file.SensitivityWatchEventModifier;48import jdk.test.lib.Platform;49import jdk.test.lib.RandomFactory;5051public class SensitivityModifier {52// on macOS and other platforms, watch services might be based on polling53// requiring a longer timeout to detect events before returning54static final long POLL_TIMEOUT_SECONDS =55Platform.isLinux() || Platform.isWindows() ? 1 : 2;5657static final Random RAND = RandomFactory.getRandom();5859static final Map<Path,Integer> pathToTime = new HashMap<>();6061static void register(Path[] dirs, WatchService watcher) throws IOException {62pathToTime.clear();63SensitivityWatchEventModifier[] sensitivities =64SensitivityWatchEventModifier.values();65for (int i=0; i<dirs.length; i++) {66SensitivityWatchEventModifier sensitivity =67sensitivities[RAND.nextInt(sensitivities.length)];68Path dir = dirs[i];69dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY },70sensitivity);71pathToTime.put(dir, sensitivity.sensitivityValueInSeconds());72}73}7475@SuppressWarnings("unchecked")76static void doTest(Path top) throws Exception {77FileSystem fs = top.getFileSystem();78try (WatchService watcher = fs.newWatchService()) {7980// create directories and files81int nDirs = 5 + RAND.nextInt(20);82int nFiles = 50 + RAND.nextInt(50);83Path[] dirs = new Path[nDirs];84Path[] files = new Path[nFiles];85for (int i=0; i<nDirs; i++) {86dirs[i] = Files.createDirectory(top.resolve("dir" + i));87}88for (int i=0; i<nFiles; i++) {89Path dir = dirs[RAND.nextInt(nDirs)];90files[i] = Files.createFile(dir.resolve("file" + i));91}9293// register the directories (random sensitivity)94register(dirs, watcher);9596// sleep a bit here to ensure that modification to the first file97// can be detected by polling implementations (ie: last modified time98// may not change otherwise).99try { Thread.sleep(1000); } catch (InterruptedException e) { }100101// modify files and check that events are received102for (int i=0; i<10; i++) {103Path file = files[RAND.nextInt(nFiles)];104System.out.println("Modify: " + file);105try (OutputStream out = Files.newOutputStream(file)) {106out.write(new byte[100]);107}108109System.out.println("Waiting for event(s)...");110boolean eventReceived = false;111WatchKey key = watcher.take();112do {113for (WatchEvent<?> event: key.pollEvents()) {114if (event.kind() != ENTRY_MODIFY)115throw new RuntimeException("Unexpected event: " + event);116Path name = ((WatchEvent<Path>)event).context();117if (name.equals(file.getFileName())) {118eventReceived = true;119break;120}121}122key.reset();123key = watcher.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS);124} while (key != null);125126// we should have received at least one ENTRY_MODIFY event127if (eventReceived) {128System.out.println("Event OK");129} else {130Path parent = file.getParent();131String msg = String.format("No ENTRY_MODIFY event received for %s (dir: %s, sensitivity: %d)",132file, parent, pathToTime.get(parent));133throw new RuntimeException(msg);134}135136// re-register the directories to force changing their sensitivity137// level138register(dirs, watcher);139}140}141}142143public static void main(String[] args) throws Exception {144Path dir = TestUtil.createTemporaryDirectory();145try {146doTest(dir);147} finally {148TestUtil.removeAll(dir);149}150}151}152153154