author | alanb |
Fri, 10 Sep 2010 18:48:49 +0100 | |
changeset 6538 | cb0da65ba680 |
parent 5793 | b8ff20734cae |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4755720 |
|
26 |
* @summary Test if OP_READ is detected with OP_WRITE in interestOps |
|
27 |
*/ |
|
28 |
||
29 |
import java.net.*; |
|
30 |
import java.nio.*; |
|
31 |
import java.nio.channels.*; |
|
32 |
import java.util.*; |
|
33 |
||
34 |
public class OpRead { |
|
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 | 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 | 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 | 51 |
|
5793
b8ff20734cae
6932744: TEST_BUG: java/nio/channels/Selector/OpRead.java failing
alanb
parents:
5506
diff
changeset
|
52 |
sc.configureBlocking(false); |
2 | 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 | 76 |
} |
77 |
} |
|
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 | 83 |
} |
84 |
} |
|
85 |
||
86 |
public static void main(String[] args) throws Exception { |
|
87 |
test(); |
|
88 |
} |
|
89 |
||
90 |
} |