1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4607272
* @summary Unit test for AsynchronousChannelGroup
* @key randomness
*/
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.io.IOException;
public class Basic {
static final Random rand = new Random();
static final ThreadFactory threadFactory = (Runnable r) -> {
return new Thread(r);
};
public static void main(String[] args) throws Exception {
shutdownTests();
shutdownNowTests();
afterShutdownTests();
miscTests();
}
static void awaitTermination(AsynchronousChannelGroup group) throws InterruptedException {
boolean terminated = group.awaitTermination(20, TimeUnit.SECONDS);
if (!terminated)
throw new RuntimeException("Group should have terminated");
}
static void testShutdownWithNoChannels(ExecutorService pool,
AsynchronousChannelGroup group)
throws Exception
{
group.shutdown();
if (!group.isShutdown())
throw new RuntimeException("Group should be shutdown");
// group should terminate quickly
awaitTermination(group);
if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated");
}
static void testShutdownWithChannels(ExecutorService pool,
AsynchronousChannelGroup group)
throws Exception
{
// create channel that is bound to group
AsynchronousChannel ch;
switch (rand.nextInt(2)) {
case 0 : ch = AsynchronousSocketChannel.open(group); break;
case 1 : ch = AsynchronousServerSocketChannel.open(group); break;
default : throw new AssertionError();
}
group.shutdown();
if (!group.isShutdown())
throw new RuntimeException("Group should be shutdown");
// last channel so should terminate after this channel is closed
ch.close();
// group should terminate quickly
awaitTermination(group);
if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated");
}
static void shutdownTests() throws Exception {
System.out.println("-- test shutdown --");
// test shutdown with no channels in groups
for (int i = 0; i < 100; i++) {
ExecutorService pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withCachedThreadPool(pool, rand.nextInt(5));
testShutdownWithNoChannels(pool, group);
}
for (int i = 0; i < 100; i++) {
int nThreads = 1 + rand.nextInt(8);
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withFixedThreadPool(nThreads, threadFactory);
testShutdownWithNoChannels(null, group);
}
for (int i = 0; i < 100; i++) {
ExecutorService pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withThreadPool(pool);
testShutdownWithNoChannels(pool, group);
}
// test shutdown with channel in group
for (int i = 0; i < 100; i++) {
ExecutorService pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withCachedThreadPool(pool, rand.nextInt(10));
try {
testShutdownWithChannels(pool, group);
} finally {
group.shutdown();
}
}
for (int i = 0; i < 100; i++) {
int nThreads = 1 + rand.nextInt(8);
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withFixedThreadPool(nThreads, threadFactory);
try {
testShutdownWithChannels(null, group);
} finally {
group.shutdown();
}
}
for (int i = 0; i < 100; i++) {
ExecutorService pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withThreadPool(pool);
try {
testShutdownWithChannels(pool, group);
} finally {
group.shutdown();
}
}
}
static void testShutdownNow(ExecutorService pool,
AsynchronousChannelGroup group)
throws Exception
{
// I/O in progress
AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel
.open(group).bind(new InetSocketAddress(0));
ch.accept();
// forceful shutdown
group.shutdownNow();
// shutdownNow is required to close all channels
if (ch.isOpen())
throw new RuntimeException("Channel should be closed");
awaitTermination(group);
if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated");
}
static void shutdownNowTests() throws Exception {
System.out.println("-- test shutdownNow --");
for (int i = 0; i < 10; i++) {
ExecutorService pool = pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withCachedThreadPool(pool, rand.nextInt(5));
try {
testShutdownNow(pool, group);
} finally {
group.shutdown();
}
}
for (int i = 0; i < 10; i++) {
int nThreads = 1 + rand.nextInt(8);
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withFixedThreadPool(nThreads, threadFactory);
try {
testShutdownNow(null, group);
} finally {
group.shutdown();
}
}
for (int i = 0; i < 10; i++) {
ExecutorService pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withThreadPool(pool);
try {
testShutdownNow(pool, group);
} finally {
group.shutdown();
}
}
}
// test creating channels in group after group is shutdown
static void afterShutdownTests() throws Exception {
System.out.println("-- test operations after group is shutdown --");
AsynchronousChannelGroup group =
AsynchronousChannelGroup.withFixedThreadPool(1, threadFactory);
try (AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);
AsynchronousServerSocketChannel listener =
AsynchronousServerSocketChannel.open(group)) {
// initiate accept
listener.bind(new InetSocketAddress(0));
Future<AsynchronousSocketChannel> result = listener.accept();
// shutdown group
group.shutdown();
if (!group.isShutdown())
throw new RuntimeException("Group should be shutdown");
// attempt to create another channel
try {
AsynchronousSocketChannel.open(group);
throw new RuntimeException("ShutdownChannelGroupException expected");
} catch (ShutdownChannelGroupException x) {
}
try {
AsynchronousServerSocketChannel.open(group);
throw new RuntimeException("ShutdownChannelGroupException expected");
} catch (ShutdownChannelGroupException x) {
}
// attempt to create another channel by connecting. This should cause
// the accept operation to fail.
InetAddress lh = InetAddress.getLocalHost();
int port = ((InetSocketAddress)listener.getLocalAddress()).getPort();
InetSocketAddress isa = new InetSocketAddress(lh, port);
ch.connect(isa).get();
try {
result.get();
throw new RuntimeException("Connection was accepted");
} catch (ExecutionException x) {
Throwable cause = x.getCause();
if (!(cause instanceof IOException))
throw new RuntimeException("Cause should be IOException");
cause = cause.getCause();
if (!(cause instanceof ShutdownChannelGroupException))
throw new RuntimeException("IOException cause should be ShutdownChannelGroupException");
}
// initiate another accept even though channel group is shutdown.
Future<AsynchronousSocketChannel> res = listener.accept();
try {
res.get(3, TimeUnit.SECONDS);
throw new RuntimeException("TimeoutException expected");
} catch (TimeoutException x) {
}
// connect to the listener which should cause the accept to complete
AsynchronousSocketChannel.open().connect(isa);
try {
res.get();
throw new RuntimeException("Connection was accepted");
} catch (ExecutionException x) {
Throwable cause = x.getCause();
if (!(cause instanceof IOException))
throw new RuntimeException("Cause should be IOException");
cause = cause.getCause();
if (!(cause instanceof ShutdownChannelGroupException))
throw new RuntimeException("IOException cause should be ShutdownChannelGroupException");
}
// group should *not* terminate as channels are open
boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
if (terminated) {
throw new RuntimeException("Group should not have terminated");
}
} finally {
group.shutdown();
}
}
static void miscTests() throws Exception {
System.out.println("-- miscellenous tests --");
try {
AsynchronousChannelGroup.withFixedThreadPool(1, null);
throw new RuntimeException("NPE expected");
} catch (NullPointerException x) {
}
try {
AsynchronousChannelGroup.withFixedThreadPool(0, threadFactory);
throw new RuntimeException("IAE expected");
} catch (IllegalArgumentException e) {
}
try {
AsynchronousChannelGroup.withCachedThreadPool(null, 0);
throw new RuntimeException("NPE expected");
} catch (NullPointerException x) {
}
try {
AsynchronousChannelGroup.withThreadPool(null);
throw new RuntimeException("NPE expected");
} catch (NullPointerException e) {
}
}
}
|