Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/AsynchronousChannelGroup/Identity.java
41152 views
1
/*
2
* Copyright (c) 2008, 2016, 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 4607272 6842687
26
* @summary Unit test for AsynchronousChannelGroup
27
* @key randomness
28
*/
29
30
import java.nio.ByteBuffer;
31
import java.nio.channels.*;
32
import java.net.*;
33
import java.util.*;
34
import java.util.concurrent.*;
35
import java.util.concurrent.atomic.*;
36
import java.io.IOException;
37
38
/**
39
* Tests that the completion handler is invoked by a thread with
40
* the expected identity.
41
*/
42
43
public class Identity {
44
static final Random rand = new Random();
45
static final CountDownLatch done = new CountDownLatch(1);
46
static final AtomicBoolean failed = new AtomicBoolean(false);
47
48
static void fail(String msg) {
49
failed.set(true);
50
done.countDown();
51
throw new RuntimeException(msg);
52
}
53
54
// thread-local identifies the thread
55
private static final ThreadLocal<Integer> myGroup =
56
new ThreadLocal<Integer>() {
57
@Override protected Integer initialValue() {
58
return Integer.valueOf(-1);
59
}
60
};
61
62
// creates a ThreadFactory that constructs groups with the given identity
63
static final ThreadFactory createThreadFactory(final int groupId) {
64
return new ThreadFactory() {
65
@Override
66
public Thread newThread(final Runnable r) {
67
Thread t = new Thread(new Runnable() {
68
public void run() {
69
myGroup.set(groupId);
70
r.run();
71
}});
72
t.setDaemon(true);
73
return t;
74
}
75
};
76
}
77
78
public static void main(String[] args) throws Exception {
79
// create 3-10 channels, each in its own group
80
final int groupCount = 3 + rand.nextInt(8);
81
final AsynchronousChannelGroup[] groups = new AsynchronousChannelGroup[groupCount];
82
final AsynchronousSocketChannel[] channels = new AsynchronousSocketChannel[groupCount];
83
84
// create listener to accept connections
85
try (final AsynchronousServerSocketChannel listener =
86
AsynchronousServerSocketChannel.open()) {
87
88
listener.bind(new InetSocketAddress(0));
89
listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
90
public void completed(final AsynchronousSocketChannel ch, Void att) {
91
listener.accept((Void)null, this);
92
final ByteBuffer buf = ByteBuffer.allocate(100);
93
ch.read(buf, ch, new CompletionHandler<Integer,AsynchronousSocketChannel>() {
94
public void completed(Integer bytesRead, AsynchronousSocketChannel ch) {
95
if (bytesRead < 0) {
96
try { ch.close(); } catch (IOException ignore) { }
97
} else {
98
buf.clear();
99
ch.read(buf, ch, this);
100
}
101
}
102
public void failed(Throwable exc, AsynchronousSocketChannel ch) {
103
try { ch.close(); } catch (IOException ignore) { }
104
}
105
});
106
}
107
public void failed(Throwable exc, Void att) {
108
}
109
});
110
int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
111
SocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), port);
112
113
for (int i=0; i<groupCount; i++) {
114
ThreadFactory factory = createThreadFactory(i);
115
AsynchronousChannelGroup group;
116
if (rand.nextBoolean()) {
117
int nThreads = 1 + rand.nextInt(10);
118
group = AsynchronousChannelGroup.withFixedThreadPool(nThreads, factory);
119
} else {
120
ExecutorService pool = Executors.newCachedThreadPool(factory);
121
group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5));
122
}
123
groups[i] = group;
124
125
// create channel in group and connect it to the server
126
AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);
127
ch.connect(sa).get();
128
channels[i] = ch;
129
}
130
131
// randomly write to each channel, ensuring that the completion handler
132
// is always invoked by a thread with the right identity.
133
final AtomicInteger writeCount = new AtomicInteger(100);
134
channels[0].write(getBuffer(), 0, new CompletionHandler<Integer,Integer>() {
135
public void completed(Integer bytesWritten, Integer groupId) {
136
if (bytesWritten != 1)
137
fail("Expected 1 byte to be written");
138
if (!myGroup.get().equals(groupId))
139
fail("Handler invoked by thread with the wrong identity");
140
if (writeCount.decrementAndGet() > 0) {
141
int id = rand.nextInt(groupCount);
142
channels[id].write(getBuffer(), id, this);
143
} else {
144
done.countDown();
145
}
146
}
147
public void failed(Throwable exc, Integer groupId) {
148
fail(exc.getMessage());
149
}
150
});
151
152
// wait until done
153
done.await();
154
} finally {
155
// clean-up
156
for (AsynchronousSocketChannel ch: channels)
157
ch.close();
158
for (AsynchronousChannelGroup group: groups)
159
group.shutdownNow();
160
161
if (failed.get())
162
throw new RuntimeException("Test failed - see log for details");
163
}
164
}
165
166
static ByteBuffer getBuffer() {
167
ByteBuffer buf;
168
if (rand.nextBoolean()) {
169
buf = ByteBuffer.allocateDirect(1);
170
} else {
171
buf = ByteBuffer.allocate(1);
172
}
173
buf.put((byte)0);
174
buf.flip();
175
return buf;
176
}
177
}
178
179