Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/WatchService/UpdateInterference.java
38828 views
/*1* Copyright (c) 2015, 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 {35public static void main(String[] args) throws IOException, InterruptedException {36final Path root = Files.createTempDirectory("test");37final Path foo = root.resolve("foo");38final Path bar = root.resolve("bar");39final Path baz = root.resolve("baz");4041Files.createDirectory(foo);42Files.createDirectory(bar);43Files.createDirectory(baz);4445final WatchService watcher = root.getFileSystem().newWatchService();46final WatchKey fooKey = foo.register(watcher, ENTRY_CREATE);47final WatchKey barKey = bar.register(watcher, ENTRY_CREATE);4849new Thread() {50{ setDaemon(true); }5152@Override53public void run() {54while (true) {55try {56final Path temp = Files.createTempFile(foo, "temp", ".tmp");57Files.delete(temp);58Thread.sleep(10);59} catch (IOException | InterruptedException e) {60throw new RuntimeException(e);61}62}63}64}.start();6566new Thread() {67{ setDaemon(true); }6869@Override70public void run() {71WatchKey bazKeys[] = new WatchKey[32];72while (true) {73try {74for( int i = 0; i < bazKeys.length; i++) {75bazKeys[i] = baz.register(watcher, ENTRY_CREATE);76}77for( int i = 0; i < bazKeys.length; i++) {78bazKeys[i].cancel();79}80Thread.sleep(1);81} catch (IOException | InterruptedException e) {82throw new RuntimeException(e);83}84}85}86}.start();8788long time = System.currentTimeMillis();89while ((System.currentTimeMillis() - time) < 15000) {90final WatchKey key = watcher.poll(60, TimeUnit.SECONDS);91if (key == null) continue;9293if (key != fooKey) {94List<WatchEvent<?>> pollEvents = key.pollEvents();95for (WatchEvent<?> watchEvent : pollEvents) {96System.out.println(watchEvent.count() + " " +97watchEvent.kind() + " " +98watchEvent.context());99}100throw new RuntimeException("Event received for unexpected key");101}102key.reset();103}104}105}106107108109