test/jdk/java/nio/channels/Selector/SelectTimeout.java
author alanb
Fri, 14 Sep 2018 16:56:09 +0100
changeset 51745 90c1dcdebc64
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208780: (se) test SelectWithConsumer.testReadableAndWriteable(): failure Reviewed-by: bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
     1
/*
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
     4
 *
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
     7
 * published by the Free Software Foundation.
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
     8
 *
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    13
 * accompanied this code).
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    14
 *
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    18
 *
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    21
 * questions.
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    22
 */
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    23
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    24
/*
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    25
 * @test
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    26
 * @bug 8165000 8172547
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    27
 * @summary Verify no IOException on OS X for large timeout value in select()
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    28
 * and that timeout does not occur too early on Windows.
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    29
 * @requires (os.family == "mac" | os.family == "windows")
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    30
 */
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    31
import java.io.IOException;
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    32
import java.nio.channels.Selector;
43705
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    33
import java.util.concurrent.atomic.AtomicBoolean;
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    34
import java.util.concurrent.atomic.AtomicReference;
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    35
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    36
public class SelectTimeout {
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    37
    private static final long BIG_TIMEOUT    = 100_000_001_000L; // 8165000
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    38
    private static final long BIGGER_TIMEOUT = 850_000_000_000_000L; // 8172547
43705
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    39
    private static final long SLEEP_MILLIS   = 10_000;
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    40
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    41
    public static void main(String[] args)
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    42
        throws IOException, InterruptedException {
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    43
        int failures = 0;
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    44
        long[] timeouts =
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    45
            new long[] {1, BIG_TIMEOUT/2, BIG_TIMEOUT - 1, BIG_TIMEOUT,
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    46
                BIGGER_TIMEOUT};
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    47
        for (long t : timeouts) {
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    48
            if (!test(t)) {
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    49
                failures++;
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    50
            }
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    51
        }
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    52
        if (failures > 0) {
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    53
            throw new RuntimeException("Test failed!");
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    54
        } else {
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    55
            System.out.println("Test succeeded");
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    56
        }
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    57
    }
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    58
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    59
    private static boolean test(final long timeout)
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    60
        throws InterruptedException, IOException {
43705
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    61
        AtomicReference<Exception> theException =
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    62
            new AtomicReference<>();
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    63
        AtomicBoolean isTimedOut = new AtomicBoolean();
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    64
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    65
        Selector selector = Selector.open();
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    66
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    67
        Thread t = new Thread(() -> {
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    68
            try {
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    69
                selector.select(timeout);
43705
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    70
                isTimedOut.set(true);
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    71
            } catch (IOException ioe) {
43705
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    72
                theException.set(ioe);
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    73
            }
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    74
        });
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    75
        t.start();
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    76
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    77
        t.join(SLEEP_MILLIS);
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    78
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    79
        boolean result;
43705
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    80
        if (theException.get() == null) {
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    81
            if (timeout > SLEEP_MILLIS && isTimedOut.get()) {
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    82
                System.err.printf("Test timed out early with timeout %d%n",
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    83
                    timeout);
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    84
                result = false;
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    85
            } else {
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    86
                System.out.printf("Test succeeded with timeout %d%n", timeout);
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    87
                result = true;
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    88
            }
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    89
        } else {
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    90
            System.err.printf("Test failed with timeout %d%n", timeout);
43705
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    91
            theException.get().printStackTrace();
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    92
            result = false;
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    93
        }
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    94
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    95
        t.interrupt();
43705
e34cf4c6b854 8173387: java/nio/channels/Selector/SelectTimeout.java failed with "Test timed out early with timeout 100000000999"
bpb
parents: 43222
diff changeset
    96
        selector.close();
43222
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    97
9664871bbd35 8172547: (se) Selector.select(Long.MAX_VALUE) fires repeatedly
bpb
parents: 40739
diff changeset
    98
        return result;
40739
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
    99
    }
bdff9be6c98f 8165000: Selector.select(timeout) throws IOException when timeout is a large long
bpb
parents:
diff changeset
   100
}