Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/WatchService/DeleteInterference.java
38828 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*/1819/**20* @test21* @bug 815392522* @summary Tests potential interference between a thread creating and closing23* a WatchService with another thread that is deleting and re-creating the24* directory at around the same time. This scenario tickled a timing bug25* in the Windows implementation.26*/2728import java.io.IOException;29import java.nio.file.DirectoryStream;30import java.nio.file.FileSystem;31import java.nio.file.FileSystems;32import java.nio.file.Files;33import java.nio.file.Path;34import java.nio.file.WatchService;35import java.util.concurrent.ExecutorService;36import java.util.concurrent.Executors;37import java.util.concurrent.Future;3839import static java.nio.file.StandardWatchEventKinds.*;4041public class DeleteInterference {4243private static final int ITERATIONS_COUNT = 1024;4445/**46* Execute two tasks in a thread pool. One task loops on creating and47* closing a WatchService, the other task deletes and re-creates the48* directory.49*/50public static void main(String[] args) throws Exception {51Path dir = Files.createTempDirectory("work");52ExecutorService pool = Executors.newCachedThreadPool();53try {54Future<?> task1 = pool.submit(() -> openAndCloseWatcher(dir));55Future<?> task2 = pool.submit(() -> deleteAndRecreateDirectory(dir));56task1.get();57task2.get();58} finally {59pool.shutdown();60deleteFileTree(dir);61}62}6364private static void openAndCloseWatcher(Path dir) {65FileSystem fs = FileSystems.getDefault();66for (int i = 0; i < ITERATIONS_COUNT; i++) {67try (WatchService watcher = fs.newWatchService()) {68dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);69} catch (IOException ioe) {70// ignore71}72}73}7475private static void deleteAndRecreateDirectory(Path dir) {76for (int i = 0; i < ITERATIONS_COUNT; i++) {77try {78deleteFileTree(dir);79Path subdir = Files.createDirectories(dir.resolve("subdir"));80Files.createFile(subdir.resolve("test"));81} catch (IOException ioe) {82// ignore83}84}85}8687private static void deleteFileTree(Path file) {88try {89if (Files.isDirectory(file)) {90try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) {91for (Path pa : stream) {92deleteFileTree(pa);93}94}95}96Files.delete(file);97} catch (IOException ioe) {98// ignore99}100}101}102103104