Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/Selector/ChangingInterests.java
41153 views
1
/*
2
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
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
*
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.
22
*/
23
24
/* @test
25
* @bug 7200742
26
* @key intermittent
27
* @summary Test that Selector doesn't spin when changing interest ops
28
*/
29
30
/* @test
31
* @requires (os.family == "windows")
32
* @run main/othervm -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider ChangingInterests
33
*/
34
35
import java.net.*;
36
import java.nio.ByteBuffer;
37
import java.nio.channels.*;
38
import static java.nio.channels.SelectionKey.*;
39
import java.io.IOException;
40
41
public class ChangingInterests {
42
43
static int OPS[] = { 0, OP_WRITE, OP_READ, (OP_WRITE|OP_READ) };
44
45
static String toOpsString(int ops) {
46
String s = "";
47
if ((ops & OP_READ) > 0)
48
s += "POLLIN";
49
if ((ops & OP_WRITE) > 0) {
50
if (s.length() > 0)
51
s += "|";
52
s += "POLLOUT";
53
}
54
if (s.length() == 0)
55
s = "0";
56
return "(" + s + ")";
57
}
58
59
/**
60
* Writes two bytes to 'out' and reads one byte from 'in' so
61
* as to make 'in' readable.
62
*/
63
static void makeReadable(SocketChannel out, SocketChannel in) throws IOException {
64
out.write(ByteBuffer.wrap(new byte[2]));
65
ByteBuffer oneByte = ByteBuffer.wrap(new byte[1]);
66
do {
67
int n = in.read(oneByte);
68
if (n == 1) {
69
break;
70
} else if (n == 0) {
71
try {
72
Thread.sleep(50);
73
} catch (InterruptedException ignore) { }
74
} else {
75
throw new RuntimeException
76
("Expected to read 0 or 1 byte; actual number was " + n);
77
}
78
} while (true);
79
}
80
81
static void drain(SocketChannel sc) throws IOException {
82
ByteBuffer buf = ByteBuffer.allocate(100);
83
int n;
84
while ((n = sc.read(buf)) > 0) {
85
buf.rewind();
86
}
87
}
88
89
/**
90
* Changes the given key's interest set from one set to another and then
91
* checks the selected key set and the key's channel.
92
*/
93
static void testChange(SelectionKey key, int from, int to) throws IOException {
94
Selector sel = key.selector();
95
assertTrue(sel.keys().size() == 1, "Only one channel should be registered");
96
97
// ensure that channel is registered with the "from" interest set
98
key.interestOps(from);
99
sel.selectNow();
100
sel.selectedKeys().clear();
101
102
// change to the "to" interest set
103
key.interestOps(to);
104
System.out.println("select...");
105
int selected = sel.selectNow();
106
System.out.println("" + selected + " channel(s) selected");
107
108
int expected = (to == 0) ? 0 : 1;
109
assertTrue(selected == expected, "Expected " + expected);
110
111
// check selected keys
112
for (SelectionKey k: sel.selectedKeys()) {
113
assertTrue(k == key, "Unexpected key selected");
114
115
boolean readable = k.isReadable();
116
boolean writable = k.isWritable();
117
118
System.out.println("key readable: " + readable);
119
System.out.println("key writable: " + writable);
120
121
if ((to & OP_READ) == 0) {
122
assertTrue(!readable, "Not expected to be readable");
123
} else {
124
assertTrue(readable, "Expected to be readable");
125
}
126
127
if ((to & OP_WRITE) == 0) {
128
assertTrue(!writable, "Not expected to be writable");
129
} else {
130
assertTrue(writable, "Expected to be writable");
131
}
132
133
sel.selectedKeys().clear();
134
}
135
}
136
137
/**
138
* Tests that given Selector's select method blocks.
139
*/
140
static void testForSpin(Selector sel) throws IOException {
141
System.out.println("Test for spin...");
142
long start = System.currentTimeMillis();
143
int count = 3;
144
while (count-- > 0) {
145
int selected = sel.select(1000);
146
System.out.println("" + selected + " channel(s) selected");
147
assertTrue(selected == 0, "Channel should not be selected");
148
}
149
long dur = System.currentTimeMillis() - start;
150
assertTrue(dur > 1000, "select was too short");
151
}
152
153
public static void main(String[] args) throws IOException {
154
InetAddress lh = InetAddress.getLocalHost();
155
156
// create loopback connection
157
ServerSocketChannel ssc =
158
ServerSocketChannel.open().bind(new InetSocketAddress(0));
159
160
final SocketChannel sc = SocketChannel.open();
161
sc.setOption(StandardSocketOptions.TCP_NODELAY, true);
162
sc.connect(new InetSocketAddress(lh, ssc.socket().getLocalPort()));
163
SocketChannel peer = ssc.accept();
164
peer.setOption(StandardSocketOptions.TCP_NODELAY, true);
165
166
sc.configureBlocking(false);
167
168
// ensure that channel "sc" is readable
169
makeReadable(peer, sc);
170
171
try (Selector sel = Selector.open()) {
172
SelectionKey key = sc.register(sel, 0);
173
sel.selectNow();
174
175
// test all transitions
176
for (int from: OPS) {
177
for (int to: OPS) {
178
179
System.out.println(toOpsString(from) + " -> " + toOpsString(to));
180
181
testChange(key, from, to);
182
183
// if the interst ops is now 0 then Selector should not spin
184
if (to == 0)
185
testForSpin(sel);
186
187
// if interest ops is now OP_READ then make non-readable
188
// and test that Selector does not spin.
189
if (to == OP_READ) {
190
System.out.println("Drain channel...");
191
drain(sc);
192
testForSpin(sel);
193
System.out.println("Make channel readable again");
194
makeReadable(peer, sc);
195
}
196
197
System.out.println();
198
}
199
}
200
201
} finally {
202
sc.close();
203
peer.close();
204
ssc.close();
205
}
206
}
207
208
static void assertTrue(boolean v, String msg) {
209
if (!v) throw new RuntimeException(msg);
210
}
211
212
}
213
214