Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/Selector/UpdateReadyOps.java
41153 views
1
/*
2
* Copyright (c) 2018, 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
* @run testng UpdateReadyOps
26
* @summary Test that the ready set from a selection operation is bitwise-disjoined
27
* into a key's ready set when the key is already in the selected-key set
28
*/
29
30
import java.io.Closeable;
31
import java.io.IOException;
32
import java.net.InetAddress;
33
import java.net.InetSocketAddress;
34
import java.nio.ByteBuffer;
35
import java.nio.channels.SelectionKey;
36
import java.nio.channels.Selector;
37
import java.nio.channels.ServerSocketChannel;
38
import java.nio.channels.SocketChannel;
39
40
import org.testng.annotations.Test;
41
import static org.testng.Assert.*;
42
43
@Test
44
public class UpdateReadyOps {
45
46
/**
47
* Test that OP_WRITE is preserved when updating the ready set of a key in
48
* the selected-key set to add OP_READ.
49
*/
50
public void testOpWritePreserved() throws Exception {
51
try (ConnectionPair pair = new ConnectionPair();
52
Selector sel = Selector.open()) {
53
54
SocketChannel sc1 = pair.channel1();
55
SocketChannel sc2 = pair.channel2();
56
57
sc1.configureBlocking(false);
58
SelectionKey key = sc1.register(sel, SelectionKey.OP_WRITE);
59
60
int updated = sel.select();
61
assertTrue(updated == 1);
62
assertTrue(sel.selectedKeys().contains(key));
63
assertFalse(key.isReadable());
64
assertTrue(key.isWritable());
65
66
// select again, should be no updates
67
updated = sel.select();
68
assertTrue(updated == 0);
69
assertTrue(sel.selectedKeys().contains(key));
70
assertFalse(key.isReadable());
71
assertTrue(key.isWritable());
72
73
// write some bytes
74
sc2.write(helloMessage());
75
76
// change interest ops to OP_READ, do a selection operation, and
77
// check that the ready set becomes OP_READ|OP_WRITE.
78
79
key.interestOps(SelectionKey.OP_READ);
80
updated = sel.select();
81
assertTrue(updated == 1);
82
assertTrue(sel.selectedKeys().size() == 1);
83
assertTrue(key.isReadable());
84
assertTrue(key.isWritable());
85
assertTrue(key.readyOps() == (SelectionKey.OP_READ|SelectionKey.OP_WRITE));
86
87
// select again, should be no updates
88
updated = sel.select();
89
assertTrue(updated == 0);
90
assertTrue(sel.selectedKeys().size() == 1);
91
assertTrue(key.isReadable());
92
assertTrue(key.isWritable());
93
}
94
}
95
96
/**
97
* Test that OP_READ is preserved when updating the ready set of a key in
98
* the selected-key set to add OP_WRITE.
99
*/
100
public void testOpReadPreserved() throws Exception {
101
try (ConnectionPair pair = new ConnectionPair();
102
Selector sel = Selector.open()) {
103
104
SocketChannel sc1 = pair.channel1();
105
SocketChannel sc2 = pair.channel2();
106
107
sc1.configureBlocking(false);
108
SelectionKey key = sc1.register(sel, SelectionKey.OP_READ);
109
110
// write some bytes
111
sc2.write(helloMessage());
112
113
int updated = sel.select();
114
assertTrue(updated == 1);
115
assertTrue(sel.selectedKeys().size() == 1);
116
assertTrue(sel.selectedKeys().contains(key));
117
assertTrue(key.isReadable());
118
assertFalse(key.isWritable());
119
120
// select again, should be no updates
121
updated = sel.select();
122
assertTrue(updated == 0);
123
assertTrue(sel.selectedKeys().contains(key));
124
assertTrue(key.isReadable());
125
assertFalse(key.isWritable());
126
127
key.interestOps(SelectionKey.OP_WRITE);
128
updated = sel.select();
129
assertTrue(updated == 1);
130
assertTrue(sel.selectedKeys().size() == 1);
131
assertTrue(sel.selectedKeys().contains(key));
132
assertTrue(key.isReadable());
133
assertTrue(key.isWritable());
134
assertTrue(key.readyOps() == (SelectionKey.OP_READ|SelectionKey.OP_WRITE));
135
136
// select again, should be no updates
137
updated = sel.select();
138
assertTrue(updated == 0);
139
assertTrue(sel.selectedKeys().size() == 1);
140
assertTrue(key.isReadable());
141
assertTrue(key.isWritable());
142
}
143
}
144
145
static class ConnectionPair implements Closeable {
146
147
private final SocketChannel sc1;
148
private final SocketChannel sc2;
149
150
ConnectionPair() throws IOException {
151
InetAddress lb = InetAddress.getLoopbackAddress();
152
try (ServerSocketChannel ssc = ServerSocketChannel.open()) {
153
ssc.bind(new InetSocketAddress(lb, 0));
154
this.sc1 = SocketChannel.open(ssc.getLocalAddress());
155
this.sc2 = ssc.accept();
156
}
157
}
158
159
SocketChannel channel1() {
160
return sc1;
161
}
162
163
SocketChannel channel2() {
164
return sc2;
165
}
166
167
public void close() throws IOException {
168
sc1.close();
169
sc2.close();
170
}
171
}
172
173
static ByteBuffer helloMessage() throws Exception {
174
return ByteBuffer.wrap("hello".getBytes("UTF-8"));
175
}
176
}
177
178