jdk/test/java/nio/channels/Selector/OpRead.java
author alanb
Fri, 10 Sep 2010 18:48:49 +0100
changeset 6538 cb0da65ba680
parent 5793 b8ff20734cae
child 7668 d4a77089c587
permissions -rw-r--r--
6983794: TEST_BUG: test/java/nio/channels/Selector/ConnectWrite.java failing Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4755720
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test if OP_READ is detected with OP_WRITE in interestOps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class OpRead {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
5793
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    36
    static void test() throws Exception {
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    37
        ServerSocketChannel ssc = null;
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    38
        SocketChannel sc = null;
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    39
        SocketChannel peer = null;
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    40
        try {
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    41
            ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
5793
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    43
            // loopback connection
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    44
            InetAddress lh = InetAddress.getLocalHost();
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    45
            sc = SocketChannel.open(new InetSocketAddress(lh, ssc.socket().getLocalPort()));
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    46
            peer = ssc.accept();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
5793
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    48
            // peer sends message so that "sc" will be readable
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    49
            int n = peer.write(ByteBuffer.wrap("Hello".getBytes()));
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    50
            assert n > 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
5793
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    52
            sc.configureBlocking(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
5793
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    54
            Selector selector = Selector.open();
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    55
            SelectionKey key = sc.register(selector, SelectionKey.OP_READ |
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    56
                SelectionKey.OP_WRITE);
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    57
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    58
            boolean done = false;
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    59
            int failCount = 0;
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    60
            while (!done) {
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    61
                if (selector.select() > 0) {
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    62
                    Set<SelectionKey> keys = selector.selectedKeys();
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    63
                    Iterator<SelectionKey> iterator = keys.iterator();
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    64
                    while (iterator.hasNext()) {
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    65
                        key = iterator.next();
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    66
                        iterator.remove();
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    67
                        if (key.isWritable()) {
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    68
                            failCount++;
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    69
                            if (failCount > 10)
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    70
                                throw new RuntimeException("Test failed");
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    71
                            Thread.sleep(250);
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    72
                        }
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    73
                        if (key.isReadable()) {
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    74
                            done = true;
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    75
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
5793
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    79
        } finally {
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    80
            if (peer != null) peer.close();
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    81
            if (sc != null) sc.close();
b8ff20734cae 6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents: 5506
diff changeset
    82
            if (ssc != null) ssc.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}