Path: blob/master/test/jdk/java/nio/channels/Selector/SelectTimeout.java
41153 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8165000 817254726* @summary Verify no IOException on OS X for large timeout value in select()27* and that timeout does not occur too early on Windows.28* @requires (os.family == "mac" | os.family == "windows")29*/30import java.io.IOException;31import java.nio.channels.Selector;32import java.util.concurrent.atomic.AtomicBoolean;33import java.util.concurrent.atomic.AtomicReference;3435public class SelectTimeout {36private static final long BIG_TIMEOUT = 100_000_001_000L; // 816500037private static final long BIGGER_TIMEOUT = 850_000_000_000_000L; // 817254738private static final long SLEEP_MILLIS = 10_000;3940public static void main(String[] args)41throws IOException, InterruptedException {42int failures = 0;43long[] timeouts =44new long[] {1, BIG_TIMEOUT/2, BIG_TIMEOUT - 1, BIG_TIMEOUT,45BIGGER_TIMEOUT};46for (long t : timeouts) {47if (!test(t)) {48failures++;49}50}51if (failures > 0) {52throw new RuntimeException("Test failed!");53} else {54System.out.println("Test succeeded");55}56}5758private static boolean test(final long timeout)59throws InterruptedException, IOException {60AtomicReference<Exception> theException =61new AtomicReference<>();62AtomicBoolean isTimedOut = new AtomicBoolean();6364Selector selector = Selector.open();6566Thread t = new Thread(() -> {67try {68selector.select(timeout);69isTimedOut.set(true);70} catch (IOException ioe) {71theException.set(ioe);72}73});74t.start();7576t.join(SLEEP_MILLIS);7778boolean result;79if (theException.get() == null) {80if (timeout > SLEEP_MILLIS && isTimedOut.get()) {81System.err.printf("Test timed out early with timeout %d%n",82timeout);83result = false;84} else {85System.out.printf("Test succeeded with timeout %d%n", timeout);86result = true;87}88} else {89System.err.printf("Test failed with timeout %d%n", timeout);90theException.get().printStackTrace();91result = false;92}9394t.interrupt();95selector.close();9697return result;98}99}100101102