Path: blob/master/test/jdk/java/nio/file/WatchService/DeleteInterference.java
41153 views
/*1* Copyright (c) 2016, Red Hat, Inc. and/or its affiliates.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/**24* @test25* @bug 815392526* @summary Tests potential interference between a thread creating and closing27* a WatchService with another thread that is deleting and re-creating the28* directory at around the same time. This scenario tickled a timing bug29* in the Windows implementation.30*/3132import java.io.IOException;33import java.nio.file.DirectoryStream;34import java.nio.file.FileSystem;35import java.nio.file.FileSystems;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.nio.file.WatchService;40import java.util.concurrent.ExecutorService;41import java.util.concurrent.Executors;42import java.util.concurrent.Future;4344import static java.lang.System.out;45import static java.nio.file.StandardWatchEventKinds.*;4647public class DeleteInterference {4849private static final int ITERATIONS_COUNT = 1024;5051/**52* Execute two tasks in a thread pool. One task loops on creating and53* closing a WatchService, the other task deletes and re-creates the54* directory.55*/56public static void main(String[] args) throws Exception {57Path testDir = Paths.get(System.getProperty("test.dir", "."));58Path dir = Files.createTempDirectory(testDir, "DeleteInterference");59ExecutorService pool = Executors.newCachedThreadPool();60try {61Future<?> task1 = pool.submit(() -> openAndCloseWatcher(dir));62Future<?> task2 = pool.submit(() -> deleteAndRecreateDirectory(dir));63task1.get();64task2.get();65} finally {66pool.shutdown();67}68}6970private static void openAndCloseWatcher(Path dir) {71FileSystem fs = FileSystems.getDefault();72for (int i = 0; i < ITERATIONS_COUNT; i++) {73out.printf("open %d begin%n", i);74try (WatchService watcher = fs.newWatchService()) {75dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);76} catch (IOException ioe) {77// ignore78} finally {79out.printf("open %d end%n", i);80}81}82}8384private static void deleteAndRecreateDirectory(Path dir) {85for (int i = 0; i < ITERATIONS_COUNT; i++) {86out.printf("del %d begin%n", i);87try {88deleteFileTree(dir);89Path subdir = Files.createDirectories(dir.resolve("subdir"));90Files.createFile(subdir.resolve("test"));91} catch (IOException ioe) {92// ignore93} finally {94out.printf("del %d end%n", i);95}96}97}9899private static void deleteFileTree(Path file) {100try {101if (Files.isDirectory(file)) {102try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) {103for (Path pa : stream) {104deleteFileTree(pa);105}106}107}108Files.delete(file);109} catch (IOException ioe) {110// ignore111}112}113}114115116