diff --git a/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java b/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java index ce76e6ef65a..91b5471145c 100644 --- a/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java +++ b/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -62,8 +62,8 @@ import static java.nio.file.LinkOption.NOFOLLOW_LINKS; class PollingWatchService extends AbstractWatchService { - // default polling interval in seconds - private static final int DEFAULT_POLLING_INTERVAL = 2; + // polling interval in seconds + private static final int POLLING_INTERVAL = 2; // map of registrations private final Map map = new HashMap<>(); @@ -118,19 +118,13 @@ class PollingWatchService if (eventSet.isEmpty()) throw new IllegalArgumentException("No events to register"); - // Extended modifiers may be used to specify the sensitivity level - int sensitivity = DEFAULT_POLLING_INTERVAL; + // no modifiers supported at this time for (WatchEvent.Modifier modifier : modifiers) { if (modifier == null) throw new NullPointerException(); - - if (ExtendedOptions.SENSITIVITY_HIGH.matches(modifier)) { - sensitivity = ExtendedOptions.SENSITIVITY_HIGH.parameter(); - } else if (ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier)) { - sensitivity = ExtendedOptions.SENSITIVITY_MEDIUM.parameter(); - } else if (ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) { - sensitivity = ExtendedOptions.SENSITIVITY_LOW.parameter(); - } else { + if (!ExtendedOptions.SENSITIVITY_HIGH.matches(modifier) && + !ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier) && + !ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) { throw new UnsupportedOperationException("Modifier not supported"); } } @@ -142,12 +136,11 @@ class PollingWatchService // registration is done in privileged block as it requires the // attributes of the entries in the directory. try { - int value = sensitivity; return AccessController.doPrivileged( new PrivilegedExceptionAction() { @Override public PollingWatchKey run() throws IOException { - return doPrivilegedRegister(path, eventSet, value); + return doPrivilegedRegister(path, eventSet); } }); } catch (PrivilegedActionException pae) { @@ -161,8 +154,7 @@ class PollingWatchService // registers directory returning a new key if not already registered or // existing key if already registered private PollingWatchKey doPrivilegedRegister(Path path, - Set> events, - int sensitivityInSeconds) + Set> events) throws IOException { // check file is a directory and get its file key if possible @@ -191,7 +183,7 @@ class PollingWatchService watchKey.disable(); } } - watchKey.enable(events, sensitivityInSeconds); + watchKey.enable(events); return watchKey; } @@ -300,8 +292,8 @@ class PollingWatchService valid = false; } - // enables periodic polling - void enable(Set> events, long period) { + // enables periodic polling with interval POLLING_INTERVAL + void enable(Set> events) { synchronized (this) { // update the events this.events = events; @@ -309,7 +301,8 @@ class PollingWatchService // create the periodic task to poll directories Runnable thunk = new Runnable() { public void run() { poll(); }}; this.poller = scheduledExecutor - .scheduleAtFixedRate(thunk, period, period, TimeUnit.SECONDS); + .scheduleAtFixedRate(thunk, POLLING_INTERVAL, + POLLING_INTERVAL, TimeUnit.SECONDS); } } diff --git a/test/jdk/java/nio/file/WatchService/SensitivityModifier.java b/test/jdk/java/nio/file/WatchService/SensitivityModifier.java index b43a4b926ce..ac02614c628 100644 --- a/test/jdk/java/nio/file/WatchService/SensitivityModifier.java +++ b/test/jdk/java/nio/file/WatchService/SensitivityModifier.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,122 +22,53 @@ */ /* @test - * @bug 4313887 + * @bug 4313887 8303413 * @summary Sanity test for JDK-specific sensitivity level watch event modifier * @modules jdk.unsupported * @library .. /test/lib - * @build jdk.test.lib.Platform * @build jdk.test.lib.RandomFactory * @run main/timeout=240 SensitivityModifier * @key randomness */ -import java.nio.file.Files; import java.nio.file.FileSystem; import java.nio.file.Path; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import static java.nio.file.StandardWatchEventKinds.*; -import java.io.OutputStream; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; import java.util.Random; -import java.util.concurrent.TimeUnit; import com.sun.nio.file.SensitivityWatchEventModifier; -import jdk.test.lib.Platform; import jdk.test.lib.RandomFactory; public class SensitivityModifier { - // on macOS and other platforms, watch services might be based on polling - // requiring a longer timeout to detect events before returning - static final long POLL_TIMEOUT_SECONDS = - Platform.isLinux() || Platform.isWindows() ? 1 : 2; static final Random RAND = RandomFactory.getRandom(); - static final Map pathToTime = new HashMap<>(); - - static void register(Path[] dirs, WatchService watcher) throws IOException { - pathToTime.clear(); + static WatchKey register(Path dir, WatchService watcher) + throws IOException { SensitivityWatchEventModifier[] sensitivities = SensitivityWatchEventModifier.values(); - for (int i=0; i[]{ ENTRY_MODIFY }, - sensitivity); - pathToTime.put(dir, sensitivity.sensitivityValueInSeconds()); - } + SensitivityWatchEventModifier sensitivity = + sensitivities[RAND.nextInt(sensitivities.length)]; + return dir.register(watcher, new WatchEvent.Kind[]{ ENTRY_MODIFY }, + sensitivity); } @SuppressWarnings("unchecked") - static void doTest(Path top) throws Exception { - FileSystem fs = top.getFileSystem(); + static void doTest(Path dir) throws Exception { + FileSystem fs = dir.getFileSystem(); try (WatchService watcher = fs.newWatchService()) { + // register the directory (random sensitivity) + WatchKey key = register(dir, watcher); - // create directories and files - int nDirs = 5 + RAND.nextInt(20); - int nFiles = 50 + RAND.nextInt(50); - Path[] dirs = new Path[nDirs]; - Path[] files = new Path[nFiles]; - for (int i=0; i event: key.pollEvents()) { - if (event.kind() != ENTRY_MODIFY) - throw new RuntimeException("Unexpected event: " + event); - Path name = ((WatchEvent)event).context(); - if (name.equals(file.getFileName())) { - eventReceived = true; - break; - } - } - key.reset(); - key = watcher.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS); - } while (key != null); - - // we should have received at least one ENTRY_MODIFY event - if (eventReceived) { - System.out.println("Event OK"); - } else { - Path parent = file.getParent(); - String msg = String.format("No ENTRY_MODIFY event received for %s (dir: %s, sensitivity: %d)", - file, parent, pathToTime.get(parent)); - throw new RuntimeException(msg); - } - - // re-register the directories to force changing their sensitivity - // level - register(dirs, watcher); - } + // cancel the registration + key.cancel(); } }