Path: blob/master/test/jdk/java/nio/file/WatchService/UpdateInterference.java
41153 views
/*1* Copyright (c) 2015, 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 814598125* @summary LinuxWatchService sometimes reports inotify events against wrong directory26* @run main UpdateInterference27*/28import java.io.IOException;29import java.nio.file.*;30import java.util.List;31import java.util.concurrent.TimeUnit;32import static java.nio.file.StandardWatchEventKinds.*;3334public class UpdateInterference {3536private static volatile boolean stop;3738public static void main(String[] args) throws IOException, InterruptedException {39final Path root = Files.createTempDirectory("test");40final Path foo = root.resolve("foo");41final Path bar = root.resolve("bar");42final Path baz = root.resolve("baz");4344Files.createDirectory(foo);45Files.createDirectory(bar);46Files.createDirectory(baz);4748try (final WatchService watcher = root.getFileSystem().newWatchService()) {49final WatchKey fooKey = foo.register(watcher, ENTRY_CREATE);50final WatchKey barKey = bar.register(watcher, ENTRY_CREATE);5152Thread t1 = null;53Thread t2 = null;54try {55t1 = new Thread() {5657@Override58public void run() {59while (!stop) {60try {61final Path temp = Files.createTempFile(foo, "temp", ".tmp");62Files.delete(temp);63Thread.sleep(10);64} catch (IOException | InterruptedException e) {65throw new RuntimeException(e);66}67}68}69};7071t2 = new Thread() {7273@Override74public void run() {75WatchKey bazKeys[] = new WatchKey[32];76while (!stop) {77try {78for( int i = 0; i < bazKeys.length; i++) {79bazKeys[i] = baz.register(watcher, ENTRY_CREATE);80}81for( int i = 0; i < bazKeys.length; i++) {82bazKeys[i].cancel();83}84Thread.sleep(1);85} catch (IOException | InterruptedException e) {86throw new RuntimeException(e);87}88}89}90};9192t1.start();93t2.start();9495long time = System.currentTimeMillis();96while ((System.currentTimeMillis() - time) < 15000) {97final WatchKey key = watcher.poll(60, TimeUnit.SECONDS);98if (key == null) continue;99100if (key != fooKey) {101List<WatchEvent<?>> pollEvents = key.pollEvents();102for (WatchEvent<?> watchEvent : pollEvents) {103System.out.println(watchEvent.count() + " " +104watchEvent.kind() + " " +105watchEvent.context());106}107throw new RuntimeException("Event received for unexpected key");108}109key.reset();110}111} finally {112// the threads should stop before WatchService is closed113// to avoid ClosedWatchServiceException114stop = true;115116// wait for threads to finish117if (t1 != null) {118t1.join();119}120121if (t2 != null) {122t2.join();123}124}125} finally {126// clean up127Files.delete(foo);128Files.delete(bar);129Files.delete(baz);130Files.delete(root);131}132}133}134135136137