author | dfuchs |
Wed, 07 Nov 2012 13:24:39 +0100 | |
changeset 14415 | 7a31b0e0cfaf |
parent 14005 | b2531e8d8b8b |
child 23576 | ec3f230ed099 |
permissions | -rw-r--r-- |
14005
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
1 |
/* |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
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 |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
26 |
* @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
|
27 |
*/ |
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 |
import java.net.*; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
30 |
import java.nio.ByteBuffer; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
31 |
import java.nio.channels.*; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
32 |
import static java.nio.channels.SelectionKey.*; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
33 |
import java.io.IOException; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
34 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
35 |
public class ChangingInterests { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
36 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
37 |
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
|
38 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
39 |
static String toOpsString(int ops) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
40 |
String s = ""; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
41 |
if ((ops & OP_READ) > 0) |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
42 |
s += "POLLIN"; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
43 |
if ((ops & OP_WRITE) > 0) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
44 |
if (s.length() > 0) |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
45 |
s += "|"; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
46 |
s += "POLLOUT"; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
47 |
} |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
48 |
if (s.length() == 0) |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
49 |
s = "0"; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
50 |
return "(" + s + ")"; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
51 |
} |
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 |
static void write1(SocketChannel peer) throws IOException { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
54 |
peer.write(ByteBuffer.wrap(new byte[1])); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
55 |
// give time for other end to be readable |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
56 |
try { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
57 |
Thread.sleep(50); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
58 |
} catch (InterruptedException ignore) { } |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
59 |
} |
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 |
static void drain(SocketChannel sc) throws IOException { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
62 |
ByteBuffer buf = ByteBuffer.allocate(100); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
63 |
int n; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
64 |
while ((n = sc.read(buf)) > 0) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
65 |
buf.rewind(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
66 |
} |
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 |
* 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
|
71 |
* 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
|
72 |
*/ |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
73 |
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
|
74 |
Selector sel = key.selector(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
75 |
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
|
76 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
77 |
// 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
|
78 |
key.interestOps(from); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
79 |
sel.selectNow(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
80 |
sel.selectedKeys().clear(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
81 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
82 |
// change to the "to" interest set |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
83 |
key.interestOps(to); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
84 |
System.out.println("select..."); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
85 |
int selected = sel.selectNow(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
86 |
System.out.println("" + selected + " channel(s) selected"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
87 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
88 |
int expected = (to == 0) ? 0 : 1; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
89 |
assertTrue(selected == expected, "Expected " + expected); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
90 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
91 |
// check selected keys |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
92 |
for (SelectionKey k: sel.selectedKeys()) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
93 |
assertTrue(k == key, "Unexpected key selected"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
94 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
95 |
boolean readable = k.isReadable(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
96 |
boolean writable = k.isWritable(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
97 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
98 |
System.out.println("key readable: " + readable); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
99 |
System.out.println("key writable: " + writable); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
100 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
101 |
if ((to & OP_READ) == 0) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
102 |
assertTrue(!readable, "Not expected to be readable"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
103 |
} else { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
104 |
assertTrue(readable, "Expected to be readable"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
105 |
} |
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 |
if ((to & OP_WRITE) == 0) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
108 |
assertTrue(!writable, "Not expected to be writable"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
109 |
} else { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
110 |
assertTrue(writable, "Expected to be writable"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
111 |
} |
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 |
sel.selectedKeys().clear(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
114 |
} |
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 |
* Tests that given Selector's select method blocks. |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
119 |
*/ |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
120 |
static void testForSpin(Selector sel) throws IOException { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
121 |
System.out.println("Test for spin..."); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
122 |
long start = System.currentTimeMillis(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
123 |
int count = 3; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
124 |
while (count-- > 0) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
125 |
int selected = sel.select(1000); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
126 |
System.out.println("" + selected + " channel(s) selected"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
127 |
assertTrue(selected == 0, "Channel should not be selected"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
128 |
} |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
129 |
long dur = System.currentTimeMillis() - start; |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
130 |
assertTrue(dur > 1000, "select was too short"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
131 |
} |
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 |
public static void main(String[] args) throws IOException { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
134 |
InetAddress lh = InetAddress.getLocalHost(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
135 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
136 |
// create loopback connection |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
137 |
ServerSocketChannel ssc = |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
138 |
ServerSocketChannel.open().bind(new InetSocketAddress(0)); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
139 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
140 |
final SocketChannel sc = SocketChannel.open(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
141 |
sc.connect(new InetSocketAddress(lh, ssc.socket().getLocalPort())); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
142 |
SocketChannel peer = ssc.accept(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
143 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
144 |
sc.configureBlocking(false); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
145 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
146 |
// ensure that channel "sc" is readable |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
147 |
write1(peer); |
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 |
try (Selector sel = Selector.open()) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
150 |
SelectionKey key = sc.register(sel, 0); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
151 |
sel.selectNow(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
152 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
153 |
// test all transitions |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
154 |
for (int from: OPS) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
155 |
for (int to: OPS) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
156 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
157 |
System.out.println(toOpsString(from) + " -> " + toOpsString(to)); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
158 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
159 |
testChange(key, from, to); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
160 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
161 |
// 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
|
162 |
if (to == 0) |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
163 |
testForSpin(sel); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
164 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
165 |
// 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
|
166 |
// and test that Selector does not spin. |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
167 |
if (to == OP_READ) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
168 |
System.out.println("Drain channel..."); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
169 |
drain(sc); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
170 |
testForSpin(sel); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
171 |
System.out.println("Make channel readable again"); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
172 |
write1(peer); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
173 |
} |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
174 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
175 |
System.out.println(); |
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 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
179 |
} finally { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
180 |
sc.close(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
181 |
peer.close(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
182 |
ssc.close(); |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
183 |
} |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
184 |
} |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
185 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
186 |
static void assertTrue(boolean v, String msg) { |
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
187 |
if (!v) throw new RuntimeException(msg); |
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 |
|
b2531e8d8b8b
7200742: (se) Selector.select does not block when starting Coherence (sol)
alanb
parents:
diff
changeset
|
190 |
} |