Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java
38821 views
1
/*
2
* Copyright (c) 2008, 2010, 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
*/
28
29
import java.nio.ByteBuffer;
30
import java.nio.channels.*;
31
import java.net.*;
32
import java.util.*;
33
import java.util.concurrent.*;
34
import java.io.IOException;
35
36
/**
37
* This test verifies that a channel or channel group can be closed from a
38
* completion handler when there are no threads available to handle I/O events.
39
*/
40
41
public class GroupOfOne {
42
43
public static void main(String[] args) throws Exception {
44
// create listener to accept connections
45
final AsynchronousServerSocketChannel listener =
46
AsynchronousServerSocketChannel.open()
47
.bind(new InetSocketAddress(0));
48
final List<AsynchronousSocketChannel> accepted = new ArrayList<AsynchronousSocketChannel>();
49
listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
50
public void completed(AsynchronousSocketChannel ch, Void att) {
51
synchronized (accepted) {
52
accepted.add(ch);
53
}
54
listener.accept((Void)null, this);
55
}
56
public void failed(Throwable exc, Void att) {
57
}
58
});
59
60
int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
61
SocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), port);
62
63
test(sa, true, false);
64
test(sa, false, true);
65
test(sa, true, true);
66
67
// clean-up
68
listener.close();
69
synchronized (accepted) {
70
for (AsynchronousSocketChannel ch: accepted) {
71
ch.close();
72
}
73
}
74
}
75
76
static void test(SocketAddress sa,
77
final boolean closeChannel,
78
final boolean shutdownGroup)
79
throws Exception
80
{
81
// group with 1 thread
82
final AsynchronousChannelGroup group = AsynchronousChannelGroup
83
.withFixedThreadPool(1, new ThreadFactory() {
84
@Override
85
public Thread newThread(final Runnable r) {
86
return new Thread(r);
87
}});
88
final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);
89
90
// the latch counts down when:
91
// 1. The read operation fails (expected)
92
// 2. the close/shutdown completes
93
final CountDownLatch latch = new CountDownLatch(2);
94
95
ch.connect(sa, (Void)null, new CompletionHandler<Void,Void>() {
96
public void completed(Void result, Void att) {
97
System.out.println("Connected");
98
99
// initiate I/O operation that does not complete (successfully)
100
ByteBuffer buf = ByteBuffer.allocate(100);
101
ch.read(buf, (Void)null, new CompletionHandler<Integer,Void>() {
102
public void completed(Integer bytesRead, Void att) {
103
throw new RuntimeException();
104
}
105
public void failed(Throwable exc, Void att) {
106
if (!(exc instanceof AsynchronousCloseException))
107
throw new RuntimeException(exc);
108
System.out.println("Read failed (expected)");
109
latch.countDown();
110
}
111
});
112
113
// close channel or shutdown group
114
try {
115
if (closeChannel) {
116
System.out.print("Close channel ...");
117
ch.close();
118
System.out.println(" done.");
119
}
120
if (shutdownGroup) {
121
System.out.print("Shutdown group ...");
122
group.shutdownNow();
123
System.out.println(" done.");
124
}
125
latch.countDown();
126
} catch (IOException e) {
127
throw new RuntimeException();
128
}
129
}
130
public void failed(Throwable exc, Void att) {
131
throw new RuntimeException(exc);
132
}
133
});
134
135
latch.await();
136
137
// clean-up
138
group.shutdown();
139
boolean terminated = group.awaitTermination(20, TimeUnit.SECONDS);
140
if (!terminated)
141
throw new RuntimeException("Group did not terminate");
142
143
System.out.println("TEST OKAY");
144
}
145
}
146
147