8264539: Improve failure message of java/nio/file/WatchService/SensitivityModifier.java

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2021-04-02 19:53:18 +00:00
parent 4133dedeef
commit 3991b329b1

View File

@ -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.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.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 = 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 =
pathToTime.clear();
SensitivityWatchEventModifier[] sensitivities =
SensitivityWatchEventModifier.values();
for (int i=0; i<dirs.length; i++) {
SensitivityWatchEventModifier sensivity =
sensitivtives[ rand.nextInt(sensitivtives.length) ];
SensitivityWatchEventModifier sensitivity =
sensitivities[RAND.nextInt(sensitivities.length)];
Path dir = dirs[i];
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY },
sensitivity);
pathToTime.put(dir, sensitivity.sensitivityValueInSeconds());
}
}
@ -59,15 +79,15 @@ public class SensitivityModifier {
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 @@ public class SensitivityModifier {
// 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 @@ public class SensitivityModifier {
}
}
key.reset();
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);
}
// re-register the directories to force changing their sensitivity