Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/WatchService/LotsOfCancels.java
38828 views
/*1* Copyright (c) 2014, 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*/2930import java.nio.file.ClosedWatchServiceException;31import java.nio.file.FileSystems;32import java.nio.file.Files;33import java.nio.file.Path;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 top = Files.createTempDirectory("work");53top.toFile().deleteOnExit();54for (int i=1; i<=16; i++) {55Path dir = Files.createDirectory(top.resolve("dir-" + i));56WatchService watcher = FileSystems.getDefault().newWatchService();57pool.submit(() -> handle(dir, watcher));58pool.submit(() -> poll(watcher));59}60} finally {61pool.shutdown();62}6364// give thread pool lots of time to terminate65if (!pool.awaitTermination(5L, TimeUnit.MINUTES))66throw new RuntimeException("Thread pool did not terminate");6768if (failed)69throw new RuntimeException("Test failed, see log for details");70}7172/**73* Stress the given WatchService, specifically the cancel method, in74* the given directory. Closes the WatchService when done.75*/76static void handle(Path dir, WatchService watcher) {77try {78try {79Path file = dir.resolve("anyfile");80for (int i=0; i<2000; i++) {81WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);82Files.createFile(file);83Files.delete(file);84key.cancel();85}86} finally {87watcher.close();88}89} catch (Exception e) {90e.printStackTrace();91failed = true;92}93}9495/**96* Polls the given WatchService in a tight loop. This keeps the event97* queue drained, it also hogs a CPU core which seems necessary to98* tickle the original bug.99*/100static void poll(WatchService watcher) {101try {102for (;;) {103WatchKey key = watcher.take();104if (key != null) {105key.pollEvents();106key.reset();107}108}109} catch (ClosedWatchServiceException expected) {110// nothing to do111} catch (Exception e) {112e.printStackTrace();113failed = true;114}115}116117}118119120121