test/jdk/java/nio/channels/Selector/SelectTimeout.java
changeset 47216 71c04702a3d5
parent 43705 e34cf4c6b854
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2016, 2017, 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 /*
       
    25  * @test
       
    26  * @bug 8165000 8172547
       
    27  * @summary Verify no IOException on OS X for large timeout value in select()
       
    28  * and that timeout does not occur too early on Windows.
       
    29  * @requires (os.family == "mac" | os.family == "windows")
       
    30  */
       
    31 import java.io.IOException;
       
    32 import java.nio.channels.Selector;
       
    33 import java.util.concurrent.atomic.AtomicBoolean;
       
    34 import java.util.concurrent.atomic.AtomicReference;
       
    35 
       
    36 public class SelectTimeout {
       
    37     private static final long BIG_TIMEOUT    = 100_000_001_000L; // 8165000
       
    38     private static final long BIGGER_TIMEOUT = 850_000_000_000_000L; // 8172547
       
    39     private static final long SLEEP_MILLIS   = 10_000;
       
    40 
       
    41     public static void main(String[] args)
       
    42         throws IOException, InterruptedException {
       
    43         int failures = 0;
       
    44         long[] timeouts =
       
    45             new long[] {1, BIG_TIMEOUT/2, BIG_TIMEOUT - 1, BIG_TIMEOUT,
       
    46                 BIGGER_TIMEOUT};
       
    47         for (long t : timeouts) {
       
    48             if (!test(t)) {
       
    49                 failures++;
       
    50             }
       
    51         }
       
    52         if (failures > 0) {
       
    53             throw new RuntimeException("Test failed!");
       
    54         } else {
       
    55             System.out.println("Test succeeded");
       
    56         }
       
    57     }
       
    58 
       
    59     private static boolean test(final long timeout)
       
    60         throws InterruptedException, IOException {
       
    61         AtomicReference<Exception> theException =
       
    62             new AtomicReference<>();
       
    63         AtomicBoolean isTimedOut = new AtomicBoolean();
       
    64 
       
    65         Selector selector = Selector.open();
       
    66 
       
    67         Thread t = new Thread(() -> {
       
    68             try {
       
    69                 selector.select(timeout);
       
    70                 isTimedOut.set(true);
       
    71             } catch (IOException ioe) {
       
    72                 theException.set(ioe);
       
    73             }
       
    74         });
       
    75         t.start();
       
    76 
       
    77         t.join(SLEEP_MILLIS);
       
    78 
       
    79         boolean result;
       
    80         if (theException.get() == null) {
       
    81             if (timeout > SLEEP_MILLIS && isTimedOut.get()) {
       
    82                 System.err.printf("Test timed out early with timeout %d%n",
       
    83                     timeout);
       
    84                 result = false;
       
    85             } else {
       
    86                 System.out.printf("Test succeeded with timeout %d%n", timeout);
       
    87                 result = true;
       
    88             }
       
    89         } else {
       
    90             System.err.printf("Test failed with timeout %d%n", timeout);
       
    91             theException.get().printStackTrace();
       
    92             result = false;
       
    93         }
       
    94 
       
    95         t.interrupt();
       
    96         selector.close();
       
    97 
       
    98         return result;
       
    99     }
       
   100 }