jdk/test/java/nio/file/WatchService/LotsOfCancels.java
author alanb
Fri, 05 Sep 2014 12:18:31 +0100
changeset 26449 df1302c8e4bd
child 39335 8dcd06ba5682
permissions -rw-r--r--
8029516: (fs) WatchKey cancel unreliable on Windows Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26449
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     1
/*
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     4
 *
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     8
 *
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    13
 * accompanied this code).
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    14
 *
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    18
 *
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    21
 * questions.
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    22
 */
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    23
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    24
/* @test
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    25
 * @bug 8029516
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    26
 * @summary Bash on WatchKey.cancel with a view to causing a crash when
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    27
 *    an outstanding I/O operation on directory completes after the
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    28
 *    directory has been closed
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    29
 */
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    30
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    31
import java.nio.file.ClosedWatchServiceException;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    32
import java.nio.file.FileSystems;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    33
import java.nio.file.Files;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    34
import java.nio.file.Path;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    35
import java.nio.file.WatchKey;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    36
import java.nio.file.WatchService;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    37
import static java.nio.file.StandardWatchEventKinds.*;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    38
import java.util.concurrent.ExecutorService;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    39
import java.util.concurrent.Executors;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    40
import java.util.concurrent.TimeUnit;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    41
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    42
public class LotsOfCancels {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    43
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    44
    // set to true for any exceptions
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    45
    static volatile boolean failed;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    46
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    48
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    49
        // create a bunch of directories. Create two tasks for each directory,
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    50
        // one to bash on cancel, the other to poll the events
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    51
        ExecutorService pool = Executors.newCachedThreadPool();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    52
        try {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    53
            Path top = Files.createTempDirectory("work");
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    54
            top.toFile().deleteOnExit();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    55
            for (int i=1; i<=16; i++) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    56
                Path dir = Files.createDirectory(top.resolve("dir-" + i));
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    57
                WatchService watcher = FileSystems.getDefault().newWatchService();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    58
                pool.submit(() -> handle(dir, watcher));
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    59
                pool.submit(() -> poll(watcher));
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    60
            }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    61
        } finally {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    62
            pool.shutdown();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    63
        }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    64
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    65
        // give thread pool lots of time to terminate
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    66
        if (!pool.awaitTermination(5L, TimeUnit.MINUTES))
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    67
            throw new RuntimeException("Thread pool did not terminate");
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    68
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    69
        if (failed)
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    70
            throw new RuntimeException("Test failed, see log for details");
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    71
    }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    72
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    73
    /**
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    74
     * Stress the given WatchService, specifically the cancel method, in
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    75
     * the given directory. Closes the WatchService when done.
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    76
     */
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    77
    static void handle(Path dir, WatchService watcher) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    78
        try {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    79
            try {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    80
                Path file = dir.resolve("anyfile");
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    81
                for (int i=0; i<2000; i++) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    82
                    WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    83
                    Files.createFile(file);
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    84
                    Files.delete(file);
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    85
                    key.cancel();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    86
                }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    87
            } finally {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    88
                watcher.close();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    89
            }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    90
        } catch (Exception e) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    91
            e.printStackTrace();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    92
            failed = true;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    93
        }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    94
    }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    95
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    96
    /**
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    97
     * Polls the given WatchService in a tight loop. This keeps the event
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    98
     * queue drained, it also hogs a CPU core which seems necessary to
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
    99
     * tickle the original bug.
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   100
     */
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   101
    static void poll(WatchService watcher) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   102
        try {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   103
            for (;;) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   104
                WatchKey key = watcher.take();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   105
                if (key != null) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   106
                    key.pollEvents();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   107
                    key.reset();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   108
                }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   109
            }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   110
        } catch (ClosedWatchServiceException expected) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   111
            // nothing to do
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   112
        } catch (Exception e) {
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   113
            e.printStackTrace();
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   114
            failed = true;
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   115
        }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   116
    }
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   117
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   118
}
df1302c8e4bd 8029516: (fs) WatchKey cancel unreliable on Windows
alanb
parents:
diff changeset
   119