jdk/test/java/nio/channels/Selector/ChangingInterests.java
author darcy
Fri, 11 Mar 2016 15:30:20 -0800
changeset 36621 12b058031872
parent 23576 ec3f230ed099
child 44120 f569cf365ae3
permissions -rw-r--r--
8151750: Mark ChangingInterests.java as intermittently failing Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
     1
/*
36621
12b058031872 8151750: Mark ChangingInterests.java as intermittently failing
darcy
parents: 23576
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
     4
 *
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
     8
 *
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    13
 * accompanied this code).
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    14
 *
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    18
 *
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    21
 * questions.
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    22
 */
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    23
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    24
/* @test
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    25
 * @bug 7200742
36621
12b058031872 8151750: Mark ChangingInterests.java as intermittently failing
darcy
parents: 23576
diff changeset
    26
 * @key intermittent
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    27
 * @summary Test that Selector doesn't spin when changing interest ops
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    28
 */
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    29
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    30
import java.net.*;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    31
import java.nio.ByteBuffer;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    32
import java.nio.channels.*;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    33
import static java.nio.channels.SelectionKey.*;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    34
import java.io.IOException;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    35
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    36
public class ChangingInterests {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    37
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    38
    static int OPS[] = { 0, OP_WRITE, OP_READ, (OP_WRITE|OP_READ) };
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    39
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    40
    static String toOpsString(int ops) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    41
        String s = "";
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    42
        if ((ops & OP_READ) > 0)
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    43
            s += "POLLIN";
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    44
        if ((ops & OP_WRITE) > 0) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    45
            if (s.length() > 0)
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    46
                s += "|";
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    47
            s += "POLLOUT";
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    48
        }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    49
        if (s.length() == 0)
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    50
            s = "0";
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    51
        return "(" + s + ")";
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    52
    }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    53
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    54
    static void write1(SocketChannel peer) throws IOException {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    55
        peer.write(ByteBuffer.wrap(new byte[1]));
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    56
        // give time for other end to be readable
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    57
        try {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    58
            Thread.sleep(50);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    59
        } catch (InterruptedException ignore) { }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    60
    }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    61
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    62
    static void drain(SocketChannel sc) throws IOException {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    63
        ByteBuffer buf = ByteBuffer.allocate(100);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    64
        int n;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    65
        while ((n = sc.read(buf)) > 0) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    66
            buf.rewind();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    67
        }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    68
    }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    69
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    70
    /**
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    71
     * Changes the given key's interest set from one set to another and then
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    72
     * checks the selected key set and the key's channel.
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    73
     */
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    74
    static void testChange(SelectionKey key, int from, int to) throws IOException {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    75
        Selector sel = key.selector();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    76
        assertTrue(sel.keys().size() == 1, "Only one channel should be registered");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    77
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    78
        // ensure that channel is registered with the "from" interest set
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    79
        key.interestOps(from);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    80
        sel.selectNow();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    81
        sel.selectedKeys().clear();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    82
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    83
        // change to the "to" interest set
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    84
        key.interestOps(to);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    85
        System.out.println("select...");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    86
        int selected = sel.selectNow();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    87
        System.out.println("" + selected + " channel(s) selected");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    88
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    89
        int expected = (to == 0) ? 0 : 1;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    90
        assertTrue(selected == expected, "Expected " + expected);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    91
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    92
        // check selected keys
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    93
        for (SelectionKey k: sel.selectedKeys()) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    94
            assertTrue(k == key, "Unexpected key selected");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    95
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    96
            boolean readable = k.isReadable();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    97
            boolean writable = k.isWritable();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    98
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
    99
            System.out.println("key readable: " + readable);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   100
            System.out.println("key writable: " + writable);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   101
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   102
            if ((to & OP_READ) == 0) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   103
                assertTrue(!readable, "Not expected to be readable");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   104
            } else {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   105
                assertTrue(readable, "Expected to be readable");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   106
            }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   107
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   108
            if ((to & OP_WRITE) == 0) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   109
                assertTrue(!writable, "Not expected to be writable");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   110
            } else {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   111
                assertTrue(writable, "Expected to be writable");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   112
            }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   113
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   114
            sel.selectedKeys().clear();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   115
        }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   116
    }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   117
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   118
    /**
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   119
     * Tests that given Selector's select method blocks.
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   120
     */
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   121
    static void testForSpin(Selector sel) throws IOException {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   122
        System.out.println("Test for spin...");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   123
        long start = System.currentTimeMillis();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   124
        int count = 3;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   125
        while (count-- > 0) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   126
            int selected = sel.select(1000);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   127
            System.out.println("" + selected + " channel(s) selected");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   128
            assertTrue(selected == 0, "Channel should not be selected");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   129
        }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   130
        long dur = System.currentTimeMillis() - start;
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   131
        assertTrue(dur > 1000, "select was too short");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   132
    }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   133
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   134
    public static void main(String[] args) throws IOException {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   135
        InetAddress lh = InetAddress.getLocalHost();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   136
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   137
        // create loopback connection
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   138
        ServerSocketChannel ssc =
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   139
            ServerSocketChannel.open().bind(new InetSocketAddress(0));
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   140
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   141
        final SocketChannel sc = SocketChannel.open();
23576
ec3f230ed099 8031563: TEST_BUG: java/nio/channels/Selector/ChangingInterests.java failed once
alanb
parents: 14005
diff changeset
   142
        sc.setOption(StandardSocketOptions.TCP_NODELAY, true);
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   143
        sc.connect(new InetSocketAddress(lh, ssc.socket().getLocalPort()));
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   144
        SocketChannel peer = ssc.accept();
23576
ec3f230ed099 8031563: TEST_BUG: java/nio/channels/Selector/ChangingInterests.java failed once
alanb
parents: 14005
diff changeset
   145
        peer.setOption(StandardSocketOptions.TCP_NODELAY, true);
14005
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   146
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   147
        sc.configureBlocking(false);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   148
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   149
        // ensure that channel "sc" is readable
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   150
        write1(peer);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   151
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   152
        try (Selector sel = Selector.open()) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   153
            SelectionKey key = sc.register(sel, 0);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   154
            sel.selectNow();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   155
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   156
            // test all transitions
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   157
            for (int from: OPS) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   158
                for (int to: OPS) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   159
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   160
                    System.out.println(toOpsString(from) + " -> " + toOpsString(to));
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   161
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   162
                    testChange(key, from, to);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   163
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   164
                    // if the interst ops is now 0 then Selector should not spin
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   165
                    if (to == 0)
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   166
                        testForSpin(sel);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   167
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   168
                    // if interest ops is now OP_READ then make non-readable
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   169
                    // and test that Selector does not spin.
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   170
                    if (to == OP_READ) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   171
                        System.out.println("Drain channel...");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   172
                        drain(sc);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   173
                        testForSpin(sel);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   174
                        System.out.println("Make channel readable again");
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   175
                        write1(peer);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   176
                    }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   177
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   178
                    System.out.println();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   179
                }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   180
            }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   181
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   182
        } finally {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   183
            sc.close();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   184
            peer.close();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   185
            ssc.close();
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   186
        }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   187
    }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   188
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   189
    static void assertTrue(boolean v, String msg) {
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   190
        if (!v) throw new RuntimeException(msg);
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   191
    }
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   192
b2531e8d8b8b 7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff changeset
   193
}