jdk/test/java/nio/file/WatchService/OverflowEventIsLoner.java
changeset 5134 aaee94191629
parent 5133 0a1777c5b601
parent 5020 c94d9cc81f49
child 5135 99420aad4834
equal deleted inserted replaced
5133:0a1777c5b601 5134:aaee94191629
     1 /*
       
     2  * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 6907760
       
    26  * @summary Check that the OVERFLOW event is not retrieved with other events
       
    27  * @library ..
       
    28  */
       
    29 
       
    30 import java.nio.file.*;
       
    31 import static java.nio.file.StandardWatchEventKind.*;
       
    32 import java.io.IOException;
       
    33 import java.util.List;
       
    34 import java.util.concurrent.TimeUnit;
       
    35 
       
    36 public class OverflowEventIsLoner {
       
    37 
       
    38     static void drainEvents(WatchService watcher,
       
    39                             WatchEvent.Kind<?> expectedKind,
       
    40                             int count)
       
    41         throws IOException, InterruptedException
       
    42     {
       
    43         // wait for key to be signalled - the timeout is long to allow for
       
    44         // polling implementations
       
    45         WatchKey key = watcher.poll(15, TimeUnit.SECONDS);
       
    46         if (key != null && count == 0)
       
    47             throw new RuntimeException("Key was signalled (unexpected)");
       
    48         if (key == null && count > 0)
       
    49             throw new RuntimeException("Key not signalled (unexpected)");
       
    50 
       
    51         int nread = 0;
       
    52         boolean gotOverflow = false;
       
    53         do {
       
    54             List<WatchEvent<?>> events = key.pollEvents();
       
    55             for (WatchEvent<?> event: events) {
       
    56                 WatchEvent.Kind<?> kind = event.kind();
       
    57                 if (kind == expectedKind) {
       
    58                     // expected event kind
       
    59                     if (++nread > count)
       
    60                         throw new RuntimeException("More events than expected!!");
       
    61                 } else if (kind == OVERFLOW) {
       
    62                     // overflow event should not be retrieved with other events
       
    63                     if (events.size() > 1)
       
    64                         throw new RuntimeException("Overflow retrieved with other events");
       
    65                     gotOverflow = true;
       
    66                 } else {
       
    67                     throw new RuntimeException("Unexpected event '" + kind + "'");
       
    68                 }
       
    69             }
       
    70             if (!key.reset())
       
    71                 throw new RuntimeException("Key is no longer valid");
       
    72             key = watcher.poll(2, TimeUnit.SECONDS);
       
    73         } while (key != null);
       
    74 
       
    75         // check that all expected events were received or there was an overflow
       
    76         if (nread < count && !gotOverflow)
       
    77             throw new RuntimeException("Insufficient events");
       
    78     }
       
    79 
       
    80 
       
    81     static void test(Path dir) throws IOException, InterruptedException {
       
    82         WatchService watcher = dir.getFileSystem().newWatchService();
       
    83         try {
       
    84             WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
       
    85 
       
    86             // create a lot of files
       
    87             int n = 1024;
       
    88             Path[] files = new Path[n];
       
    89             for (int i=0; i<n; i++) {
       
    90                 files[i] = dir.resolve("foo" + i).createFile();
       
    91             }
       
    92 
       
    93             // give time for events to accumulate (improve chance of overflow)
       
    94             Thread.sleep(1000);
       
    95 
       
    96             // check that we see the create events (or overflow)
       
    97             drainEvents(watcher, ENTRY_CREATE, n);
       
    98 
       
    99             // delete the files
       
   100             for (int i=0; i<n; i++) {
       
   101                 files[i].delete();
       
   102             }
       
   103 
       
   104             // give time for events to accumulate (improve chance of overflow)
       
   105             Thread.sleep(1000);
       
   106 
       
   107             // check that we see the delete events (or overflow)
       
   108             drainEvents(watcher, ENTRY_DELETE, n);
       
   109         } finally {
       
   110             watcher.close();
       
   111         }
       
   112     }
       
   113 
       
   114     public static void main(String[] args) throws Exception {
       
   115         Path dir = TestUtil.createTemporaryDirectory();
       
   116         try {
       
   117             test(dir);
       
   118         } finally {
       
   119             TestUtil.removeAll(dir);
       
   120         }
       
   121     }
       
   122 }