test/jdk/java/nio/channels/Selector/WakeupNow.java
changeset 47216 71c04702a3d5
parent 7668 d4a77089c587
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4940372
       
    26  * @summary Ensure that the wakeup state is cleared by selectNow()
       
    27  */
       
    28 
       
    29 import java.nio.channels.*;
       
    30 
       
    31 public class WakeupNow {
       
    32 
       
    33     public static void main(String[] args) throws Exception {
       
    34         test1();
       
    35         test2();
       
    36     }
       
    37 
       
    38     // Test if selectNow clears wakeup with the wakeup fd and
       
    39     // another fd in the selector.
       
    40     // This fails before the fix on Linux
       
    41     private static void test1() throws Exception {
       
    42         Selector sel = Selector.open();
       
    43         Pipe p = Pipe.open();
       
    44         p.source().configureBlocking(false);
       
    45         p.source().register(sel, SelectionKey.OP_READ);
       
    46         sel.wakeup();
       
    47         // ensure wakeup is consumed by selectNow
       
    48         Thread.sleep(2000);
       
    49         sel.selectNow();
       
    50         long startTime = System.currentTimeMillis();
       
    51         int n = sel.select(2000);
       
    52         long endTime = System.currentTimeMillis();
       
    53         p.source().close();
       
    54         p.sink().close();
       
    55         sel.close();
       
    56         if (endTime - startTime < 1000)
       
    57             throw new RuntimeException("test failed");
       
    58     }
       
    59 
       
    60     // Test if selectNow clears wakeup with only the wakeup fd
       
    61     // in the selector.
       
    62     // This fails before the fix on Solaris
       
    63     private static void test2() throws Exception {
       
    64         Selector sel = Selector.open();
       
    65         Pipe p = Pipe.open();
       
    66         p.source().configureBlocking(false);
       
    67         sel.wakeup();
       
    68         // ensure wakeup is consumed by selectNow
       
    69         Thread.sleep(2000);
       
    70         sel.selectNow();
       
    71         long startTime = System.currentTimeMillis();
       
    72         int n = sel.select(2000);
       
    73         long endTime = System.currentTimeMillis();
       
    74         sel.close();
       
    75         if (endTime - startTime < 1000)
       
    76             throw new RuntimeException("test failed");
       
    77     }
       
    78 
       
    79 }