Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8264539: Improve failure message of java/nio/file/WatchService/ #3290

wants to merge 2 commits into from
Changes from all commits
File filter

Filter by extension

Filter by extension

Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,5 @@
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* This code is free software; you can redistribute it and/or modify it
@@ -25,31 +25,51 @@
* @bug 4313887
* @summary Sanity test for JDK-specific sensitivity level watch event modifier
* @modules jdk.unsupported
* @library ..
* @library .. /test/lib
* @build jdk.test.lib.Platform
* @build jdk.test.lib.RandomFactory
* @run main/timeout=240 SensitivityModifier
* @key randomness

import java.nio.file.*;
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.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 = new Random();
static final Random RAND = RandomFactory.getRandom();

static final Map<Path,Integer> pathToTime = new HashMap<>();

static void register(Path[] dirs, WatchService watcher) throws IOException {
SensitivityWatchEventModifier[] sensitivtives =
SensitivityWatchEventModifier[] sensitivities =
for (int i=0; i<dirs.length; i++) {
SensitivityWatchEventModifier sensivity =
sensitivtives[ rand.nextInt(sensitivtives.length) ];
SensitivityWatchEventModifier sensitivity =
Path dir = dirs[i];
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY },
pathToTime.put(dir, sensitivity.sensitivityValueInSeconds());

@@ -59,15 +79,15 @@ static void doTest(Path top) throws Exception {
try (WatchService watcher = fs.newWatchService()) {

// create directories and files
int nDirs = 5 + rand.nextInt(20);
int nFiles = 50 + rand.nextInt(50);
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<nDirs; i++) {
dirs[i] = Files.createDirectory(top.resolve("dir" + i));
for (int i=0; i<nFiles; i++) {
Path dir = dirs[rand.nextInt(nDirs)];
Path dir = dirs[RAND.nextInt(nDirs)];
files[i] = Files.createFile(dir.resolve("file" + i));

@@ -81,7 +101,7 @@ static void doTest(Path top) throws Exception {

// modify files and check that events are received
for (int i=0; i<10; i++) {
Path file = files[rand.nextInt(nFiles)];
Path file = files[RAND.nextInt(nFiles)];
System.out.println("Modify: " + file);
try (OutputStream out = Files.newOutputStream(file)) {
out.write(new byte[100]);
@@ -101,14 +121,17 @@ static void doTest(Path top) throws Exception {
key = watcher.poll(1, TimeUnit.SECONDS);
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 {
throw new RuntimeException("No ENTRY_MODIFY event received for " + file);
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);
Copy link

@AlanBateman AlanBateman Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear to me that this helps. Have you looked at changing the 1s at L104. I assume the issue is that the test needs the idle (no events) time increased to deal with cases where the watcher is slow to queue events.

Copy link
Member Author

@bplb bplb Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a better idea but I think this change should go in also. I have not in any case been able to reproduce it. There is no record of on which specific machines it was sighted.

Copy link

@AlanBateman AlanBateman Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



// re-register the directories to force changing their sensitivity