1 /*
   2 * Copyright (c) 2016, 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
  27  * @summary Verify no IOException on OS X for large timeout value in select().
  28  * @requires (os.family == "mac")
  29  */
  30 import java.io.IOException;
  31 import java.nio.channels.Selector;
  32 
  33 public class SelectTimeout {
  34     private static final long HUGE_TIMEOUT = 100000001000L;
  35     private static final long SLEEP_MILLIS = 10000;
  36 
  37     private static Exception theException;
  38 
  39     public static void main(String[] args)
  40         throws IOException, InterruptedException {
  41         int failures = 0;
  42         long[] timeouts =
  43             new long[] {0, HUGE_TIMEOUT/2, HUGE_TIMEOUT - 1, HUGE_TIMEOUT};
  44         for (long t : timeouts) {
  45             if (!test(t)) {
  46                 failures++;
  47             }
  48         }
  49         if (failures > 0) {
  50             throw new RuntimeException("Test failed!");
  51         } else {
  52             System.out.println("Test succeeded");
  53         }
  54     }
  55 
  56     private static boolean test(final long timeout)
  57         throws InterruptedException, IOException {
  58         theException = null;
  59 
  60         Selector selector = Selector.open();
  61 
  62         Thread t = new Thread(() -> {
  63             try {
  64                 selector.select(timeout);
  65             } catch (IOException ioe) {
  66                 theException = ioe;
  67             }
  68         });
  69         t.start();
  70 
  71         Thread.currentThread().sleep(SLEEP_MILLIS);
  72         t.interrupt();
  73 
  74         if (theException == null) {
  75             System.out.printf("Test succeeded with timeout %d%n", timeout);
  76             return true;
  77         } else {
  78             System.err.printf("Test failed with timeout %d%n", timeout);
  79             theException.printStackTrace();
  80             return false;
  81         }
  82     }
  83 }