Path: blob/master/test/jdk/java/nio/file/WatchService/LotsOfCancels.java
41153 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.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/* @test24* @bug 802951625* @summary Bash on WatchKey.cancel with a view to causing a crash when26* an outstanding I/O operation on directory completes after the27* directory has been closed28*/29import java.nio.file.ClosedWatchServiceException;30import java.nio.file.FileSystems;31import java.nio.file.Files;32import java.nio.file.Path;33import java.nio.file.Paths;34import java.nio.file.WatchKey;35import java.nio.file.WatchService;36import static java.nio.file.StandardWatchEventKinds.*;37import java.util.concurrent.ExecutorService;38import java.util.concurrent.Executors;39import java.util.concurrent.TimeUnit;4041public class LotsOfCancels {4243// set to true for any exceptions44static volatile boolean failed;4546public static void main(String[] args) throws Exception {4748// create a bunch of directories. Create two tasks for each directory,49// one to bash on cancel, the other to poll the events50ExecutorService pool = Executors.newCachedThreadPool();51try {52Path testDir = Paths.get(System.getProperty("test.dir", "."));53Path top = Files.createTempDirectory(testDir, "LotsOfCancels");54for (int i=1; i<=16; i++) {55int id = i;56Path dir = Files.createDirectory(top.resolve("dir-" + i));57WatchService watcher = FileSystems.getDefault().newWatchService();58pool.submit(() -> handle(id, dir, watcher));59pool.submit(() -> poll(id, watcher));60}61} finally {62pool.shutdown();63}6465// give thread pool lots of time to terminate66if (!pool.awaitTermination(5L, TimeUnit.MINUTES))67throw new RuntimeException("Thread pool did not terminate");6869if (failed)70throw new RuntimeException("Test failed, see log for details");71}7273/**74* Stress the given WatchService, specifically the cancel method, in75* the given directory. Closes the WatchService when done.76*/77static void handle(int id, Path dir, WatchService watcher) {78System.out.printf("begin handle %d%n", id);79try {80try {81Path file = dir.resolve("anyfile");82for (int i=0; i<2000; i++) {83WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);84Files.createFile(file);85Files.delete(file);86key.cancel();87}88} finally {89System.out.printf("WatchService %d closing ...%n", id);90watcher.close();91System.out.printf("WatchService %d closed %n", id);92}93} catch (Exception e) {94e.printStackTrace();95failed = true;96}97System.out.printf("end handle %d%n", id);98}99100/**101* Polls the given WatchService in a tight loop. This keeps the event102* queue drained, it also hogs a CPU core which seems necessary to103* tickle the original bug.104*/105static void poll(int id, WatchService watcher) {106System.out.printf("begin poll %d%n", id);107try {108for (;;) {109WatchKey key = watcher.take();110if (key != null) {111key.pollEvents();112key.reset();113}114}115} catch (ClosedWatchServiceException expected) {116// nothing to do but print117System.out.printf("poll %d expected exception %s%n", id, expected);118} catch (Exception e) {119e.printStackTrace();120failed = true;121}122System.out.printf("end poll %d%n", id);123}124}125126127