mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8303413: (fs) Ignore polling interval sensitivity modifiers in PollingWatchService
Reviewed-by: alanb
This commit is contained in:
parent
99443142cc
commit
a04b1049ff
2 changed files with 31 additions and 107 deletions
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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
|
class PollingWatchService
|
||||||
extends AbstractWatchService
|
extends AbstractWatchService
|
||||||
{
|
{
|
||||||
// default polling interval in seconds
|
// polling interval in seconds
|
||||||
private static final int DEFAULT_POLLING_INTERVAL = 2;
|
private static final int POLLING_INTERVAL = 2;
|
||||||
|
|
||||||
// map of registrations
|
// map of registrations
|
||||||
private final Map<Object, PollingWatchKey> map = new HashMap<>();
|
private final Map<Object, PollingWatchKey> map = new HashMap<>();
|
||||||
|
@ -118,19 +118,13 @@ class PollingWatchService
|
||||||
if (eventSet.isEmpty())
|
if (eventSet.isEmpty())
|
||||||
throw new IllegalArgumentException("No events to register");
|
throw new IllegalArgumentException("No events to register");
|
||||||
|
|
||||||
// Extended modifiers may be used to specify the sensitivity level
|
// no modifiers supported at this time
|
||||||
int sensitivity = DEFAULT_POLLING_INTERVAL;
|
|
||||||
for (WatchEvent.Modifier modifier : modifiers) {
|
for (WatchEvent.Modifier modifier : modifiers) {
|
||||||
if (modifier == null)
|
if (modifier == null)
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
|
if (!ExtendedOptions.SENSITIVITY_HIGH.matches(modifier) &&
|
||||||
if (ExtendedOptions.SENSITIVITY_HIGH.matches(modifier)) {
|
!ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier) &&
|
||||||
sensitivity = ExtendedOptions.SENSITIVITY_HIGH.parameter();
|
!ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) {
|
||||||
} 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 {
|
|
||||||
throw new UnsupportedOperationException("Modifier not supported");
|
throw new UnsupportedOperationException("Modifier not supported");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,12 +136,11 @@ class PollingWatchService
|
||||||
// registration is done in privileged block as it requires the
|
// registration is done in privileged block as it requires the
|
||||||
// attributes of the entries in the directory.
|
// attributes of the entries in the directory.
|
||||||
try {
|
try {
|
||||||
int value = sensitivity;
|
|
||||||
return AccessController.doPrivileged(
|
return AccessController.doPrivileged(
|
||||||
new PrivilegedExceptionAction<PollingWatchKey>() {
|
new PrivilegedExceptionAction<PollingWatchKey>() {
|
||||||
@Override
|
@Override
|
||||||
public PollingWatchKey run() throws IOException {
|
public PollingWatchKey run() throws IOException {
|
||||||
return doPrivilegedRegister(path, eventSet, value);
|
return doPrivilegedRegister(path, eventSet);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (PrivilegedActionException pae) {
|
} catch (PrivilegedActionException pae) {
|
||||||
|
@ -161,8 +154,7 @@ class PollingWatchService
|
||||||
// registers directory returning a new key if not already registered or
|
// registers directory returning a new key if not already registered or
|
||||||
// existing key if already registered
|
// existing key if already registered
|
||||||
private PollingWatchKey doPrivilegedRegister(Path path,
|
private PollingWatchKey doPrivilegedRegister(Path path,
|
||||||
Set<? extends WatchEvent.Kind<?>> events,
|
Set<? extends WatchEvent.Kind<?>> events)
|
||||||
int sensitivityInSeconds)
|
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// check file is a directory and get its file key if possible
|
// check file is a directory and get its file key if possible
|
||||||
|
@ -191,7 +183,7 @@ class PollingWatchService
|
||||||
watchKey.disable();
|
watchKey.disable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
watchKey.enable(events, sensitivityInSeconds);
|
watchKey.enable(events);
|
||||||
return watchKey;
|
return watchKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,8 +292,8 @@ class PollingWatchService
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// enables periodic polling
|
// enables periodic polling with interval POLLING_INTERVAL
|
||||||
void enable(Set<? extends WatchEvent.Kind<?>> events, long period) {
|
void enable(Set<? extends WatchEvent.Kind<?>> events) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// update the events
|
// update the events
|
||||||
this.events = events;
|
this.events = events;
|
||||||
|
@ -309,7 +301,8 @@ class PollingWatchService
|
||||||
// create the periodic task to poll directories
|
// create the periodic task to poll directories
|
||||||
Runnable thunk = new Runnable() { public void run() { poll(); }};
|
Runnable thunk = new Runnable() { public void run() { poll(); }};
|
||||||
this.poller = scheduledExecutor
|
this.poller = scheduledExecutor
|
||||||
.scheduleAtFixedRate(thunk, period, period, TimeUnit.SECONDS);
|
.scheduleAtFixedRate(thunk, POLLING_INTERVAL,
|
||||||
|
POLLING_INTERVAL, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -22,122 +22,53 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* @test
|
/* @test
|
||||||
* @bug 4313887
|
* @bug 4313887 8303413
|
||||||
* @summary Sanity test for JDK-specific sensitivity level watch event modifier
|
* @summary Sanity test for JDK-specific sensitivity level watch event modifier
|
||||||
* @modules jdk.unsupported
|
* @modules jdk.unsupported
|
||||||
* @library .. /test/lib
|
* @library .. /test/lib
|
||||||
* @build jdk.test.lib.Platform
|
|
||||||
* @build jdk.test.lib.RandomFactory
|
* @build jdk.test.lib.RandomFactory
|
||||||
* @run main/timeout=240 SensitivityModifier
|
* @run main/timeout=240 SensitivityModifier
|
||||||
* @key randomness
|
* @key randomness
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.FileSystem;
|
import java.nio.file.FileSystem;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.WatchEvent;
|
import java.nio.file.WatchEvent;
|
||||||
import java.nio.file.WatchKey;
|
import java.nio.file.WatchKey;
|
||||||
import java.nio.file.WatchService;
|
import java.nio.file.WatchService;
|
||||||
import static java.nio.file.StandardWatchEventKinds.*;
|
import static java.nio.file.StandardWatchEventKinds.*;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import com.sun.nio.file.SensitivityWatchEventModifier;
|
import com.sun.nio.file.SensitivityWatchEventModifier;
|
||||||
import jdk.test.lib.Platform;
|
|
||||||
import jdk.test.lib.RandomFactory;
|
import jdk.test.lib.RandomFactory;
|
||||||
|
|
||||||
public class SensitivityModifier {
|
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 Random RAND = RandomFactory.getRandom();
|
||||||
|
|
||||||
static final Map<Path,Integer> pathToTime = new HashMap<>();
|
static WatchKey register(Path dir, WatchService watcher)
|
||||||
|
throws IOException {
|
||||||
static void register(Path[] dirs, WatchService watcher) throws IOException {
|
|
||||||
pathToTime.clear();
|
|
||||||
SensitivityWatchEventModifier[] sensitivities =
|
SensitivityWatchEventModifier[] sensitivities =
|
||||||
SensitivityWatchEventModifier.values();
|
SensitivityWatchEventModifier.values();
|
||||||
for (int i=0; i<dirs.length; i++) {
|
|
||||||
SensitivityWatchEventModifier sensitivity =
|
SensitivityWatchEventModifier sensitivity =
|
||||||
sensitivities[RAND.nextInt(sensitivities.length)];
|
sensitivities[RAND.nextInt(sensitivities.length)];
|
||||||
Path dir = dirs[i];
|
return dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY },
|
||||||
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY },
|
|
||||||
sensitivity);
|
sensitivity);
|
||||||
pathToTime.put(dir, sensitivity.sensitivityValueInSeconds());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
static void doTest(Path top) throws Exception {
|
static void doTest(Path dir) throws Exception {
|
||||||
FileSystem fs = top.getFileSystem();
|
FileSystem fs = dir.getFileSystem();
|
||||||
try (WatchService watcher = fs.newWatchService()) {
|
try (WatchService watcher = fs.newWatchService()) {
|
||||||
|
// register the directory (random sensitivity)
|
||||||
|
WatchKey key = register(dir, watcher);
|
||||||
|
|
||||||
// create directories and files
|
// check validity
|
||||||
int nDirs = 5 + RAND.nextInt(20);
|
if (!key.isValid())
|
||||||
int nFiles = 50 + RAND.nextInt(50);
|
throw new RuntimeException("Registration is invalid");
|
||||||
Path[] dirs = new Path[nDirs];
|
|
||||||
Path[] files = new Path[nFiles];
|
|
||||||
for (int i=0; i<nDirs; i++) {
|
|
||||||
dirs[i] = Files.createDirectory(top.resolve("dir" + i));
|
|
||||||
}
|
|
||||||
for (int i=0; i<nFiles; i++) {
|
|
||||||
Path dir = dirs[RAND.nextInt(nDirs)];
|
|
||||||
files[i] = Files.createFile(dir.resolve("file" + i));
|
|
||||||
}
|
|
||||||
|
|
||||||
// register the directories (random sensitivity)
|
// cancel the registration
|
||||||
register(dirs, watcher);
|
key.cancel();
|
||||||
|
|
||||||
// sleep a bit here to ensure that modification to the first file
|
|
||||||
// can be detected by polling implementations (ie: last modified time
|
|
||||||
// may not change otherwise).
|
|
||||||
try { Thread.sleep(1000); } catch (InterruptedException e) { }
|
|
||||||
|
|
||||||
// modify files and check that events are received
|
|
||||||
for (int i=0; i<10; i++) {
|
|
||||||
Path file = files[RAND.nextInt(nFiles)];
|
|
||||||
System.out.println("Modify: " + file);
|
|
||||||
try (OutputStream out = Files.newOutputStream(file)) {
|
|
||||||
out.write(new byte[100]);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Waiting for event(s)...");
|
|
||||||
boolean eventReceived = false;
|
|
||||||
WatchKey key = watcher.take();
|
|
||||||
do {
|
|
||||||
for (WatchEvent<?> event: key.pollEvents()) {
|
|
||||||
if (event.kind() != ENTRY_MODIFY)
|
|
||||||
throw new RuntimeException("Unexpected event: " + event);
|
|
||||||
Path name = ((WatchEvent<Path>)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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue