jdk/test/java/nio/file/WatchService/DeleteInterference.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 40123 673d89604418
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     1
/*
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2016, Red Hat, Inc. and/or its affiliates.
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     4
 *
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     8
 *
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    13
 * accompanied this code).
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    14
 *
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 40123
diff changeset
    18
 *
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 40123
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 40123
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 40123
diff changeset
    21
 * questions.
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    22
 */
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    23
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    24
/**
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    25
 * @test
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    26
 * @bug 8153925
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    27
 * @summary Tests potential interference between a thread creating and closing
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    28
 *     a WatchService with another thread that is deleting and re-creating the
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    29
 *     directory at around the same time. This scenario tickled a timing bug
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    30
 *     in the Windows implementation.
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    31
 */
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    32
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    33
import java.io.IOException;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    34
import java.nio.file.DirectoryStream;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    35
import java.nio.file.FileSystem;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    36
import java.nio.file.FileSystems;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    37
import java.nio.file.Files;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    38
import java.nio.file.Path;
39726
0dbf4284a3f6 8160220: (fs) Tests in jdk/test/java/nio/file/WatchService leave directory trees behind
bpb
parents: 39335
diff changeset
    39
import java.nio.file.Paths;
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    40
import java.nio.file.WatchService;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    41
import java.util.concurrent.ExecutorService;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    42
import java.util.concurrent.Executors;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    43
import java.util.concurrent.Future;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    44
40123
673d89604418 8162902: Add some debugging output to test/java/nio/file/WatchService/DeleteInterference
bpb
parents: 39726
diff changeset
    45
import static java.lang.System.out;
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    46
import static java.nio.file.StandardWatchEventKinds.*;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    47
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    48
public class DeleteInterference {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    49
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    50
    private static final int ITERATIONS_COUNT = 1024;
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    51
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    52
    /**
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    53
     * Execute two tasks in a thread pool. One task loops on creating and
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    54
     * closing a WatchService, the other task deletes and re-creates the
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    55
     * directory.
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    56
     */
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    57
    public static void main(String[] args) throws Exception {
39726
0dbf4284a3f6 8160220: (fs) Tests in jdk/test/java/nio/file/WatchService leave directory trees behind
bpb
parents: 39335
diff changeset
    58
        Path testDir = Paths.get(System.getProperty("test.dir", "."));
0dbf4284a3f6 8160220: (fs) Tests in jdk/test/java/nio/file/WatchService leave directory trees behind
bpb
parents: 39335
diff changeset
    59
        Path dir = Files.createTempDirectory(testDir, "DeleteInterference");
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    60
        ExecutorService pool = Executors.newCachedThreadPool();
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    61
        try {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    62
            Future<?> task1 = pool.submit(() -> openAndCloseWatcher(dir));
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    63
            Future<?> task2 = pool.submit(() -> deleteAndRecreateDirectory(dir));
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    64
            task1.get();
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    65
            task2.get();
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    66
        } finally {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    67
            pool.shutdown();
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    68
        }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    69
    }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    70
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    71
    private static void openAndCloseWatcher(Path dir) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    72
        FileSystem fs = FileSystems.getDefault();
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    73
        for (int i = 0; i < ITERATIONS_COUNT; i++) {
40123
673d89604418 8162902: Add some debugging output to test/java/nio/file/WatchService/DeleteInterference
bpb
parents: 39726
diff changeset
    74
            out.printf("open %d begin%n", i);
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    75
            try (WatchService watcher = fs.newWatchService()) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    76
                dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    77
            } catch (IOException ioe) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    78
                // ignore
40123
673d89604418 8162902: Add some debugging output to test/java/nio/file/WatchService/DeleteInterference
bpb
parents: 39726
diff changeset
    79
            } finally {
673d89604418 8162902: Add some debugging output to test/java/nio/file/WatchService/DeleteInterference
bpb
parents: 39726
diff changeset
    80
                out.printf("open %d end%n", i);
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    81
            }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    82
        }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    83
    }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    84
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    85
    private static void deleteAndRecreateDirectory(Path dir) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    86
        for (int i = 0; i < ITERATIONS_COUNT; i++) {
40123
673d89604418 8162902: Add some debugging output to test/java/nio/file/WatchService/DeleteInterference
bpb
parents: 39726
diff changeset
    87
            out.printf("del %d begin%n", i);
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    88
            try {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    89
                deleteFileTree(dir);
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    90
                Path subdir = Files.createDirectories(dir.resolve("subdir"));
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    91
                Files.createFile(subdir.resolve("test"));
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    92
            } catch (IOException ioe) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    93
                // ignore
40123
673d89604418 8162902: Add some debugging output to test/java/nio/file/WatchService/DeleteInterference
bpb
parents: 39726
diff changeset
    94
            } finally {
673d89604418 8162902: Add some debugging output to test/java/nio/file/WatchService/DeleteInterference
bpb
parents: 39726
diff changeset
    95
                out.printf("del %d end%n", i);
37727
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    96
            }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    97
        }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    98
    }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
    99
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   100
    private static void deleteFileTree(Path file) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   101
        try {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   102
            if (Files.isDirectory(file)) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   103
                try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   104
                    for (Path pa : stream) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   105
                        deleteFileTree(pa);
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   106
                    }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   107
                }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   108
            }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   109
            Files.delete(file);
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   110
        } catch (IOException ioe) {
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   111
            // ignore
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   112
        }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   113
    }
f33cc7ee4a9c 8153925: (fs) WatchService hangs on GetOverlappedResult and locks directory (win)
alanb
parents:
diff changeset
   114
}