7189886: (aio) Add test coverage for AsynchronousChannelGroup.withThreadPool
Reviewed-by: alanb
Contributed-by: amy.lu@oracle.com
--- a/jdk/test/java/nio/channels/AsynchronousChannelGroup/AsExecutor.java Wed Aug 08 12:37:02 2012 +0100
+++ b/jdk/test/java/nio/channels/AsynchronousChannelGroup/AsExecutor.java Wed Aug 08 15:31:22 2012 +0100
@@ -37,24 +37,30 @@
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
+ AsynchronousChannelGroup group3 = AsynchronousChannelGroup
+ .withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
+ testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
+ testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
+ testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
+ group3.shutdown();
}
}
--- a/jdk/test/java/nio/channels/AsynchronousChannelGroup/Basic.java Wed Aug 08 12:37:02 2012 +0100
+++ b/jdk/test/java/nio/channels/AsynchronousChannelGroup/Basic.java Wed Aug 08 15:31:22 2012 +0100
@@ -51,98 +51,135 @@
miscTests();
}
+ 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
+ boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
+ if (!terminated)
+ throw new RuntimeException("Group should have terminated");
+ 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
+ boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
+ if (!terminated)
+ throw new RuntimeException("Group should have terminated");
+ 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<500; i++) {
- ExecutorService pool = null;
- AsynchronousChannelGroup group;
- if (rand.nextBoolean()) {
- pool = Executors.newCachedThreadPool();
- group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5));
- } else {
- int nThreads = 1 + rand.nextInt(8);
- group = AsynchronousChannelGroup.withFixedThreadPool(nThreads, threadFactory);
- }
- group.shutdown();
- if (!group.isShutdown())
- throw new RuntimeException("Group should be shutdown");
- // group should terminate quickly
- boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
- if (!terminated)
- throw new RuntimeException("Group should have terminated");
- if (pool != null && !pool.isTerminated())
- throw new RuntimeException("Executor should have terminated");
+ 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);
}
- // shutdown with channel in group
- for (int i=0; i<500; i++) {
- ExecutorService pool = null;
- AsynchronousChannelGroup group;
- if (rand.nextBoolean()) {
- pool = Executors.newCachedThreadPool();
- group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(10));
- } else {
- int nThreads = 1 + rand.nextInt(8);
- group = AsynchronousChannelGroup.withFixedThreadPool(nThreads, threadFactory);
- }
- // 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");
+ // 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));
+ testShutdownWithChannels(pool, group);
+ }
+ for (int i = 0; i < 100; i++) {
+ int nThreads = 1 + rand.nextInt(8);
+ AsynchronousChannelGroup group = AsynchronousChannelGroup
+ .withFixedThreadPool(nThreads, threadFactory);
+ testShutdownWithChannels(null, group);
+ }
+ for (int i = 0; i < 100; i++) {
+ ExecutorService pool = Executors.newCachedThreadPool();
+ AsynchronousChannelGroup group = AsynchronousChannelGroup
+ .withThreadPool(pool);
+ testShutdownWithChannels(pool, group);
+ }
+ }
- // last channel so should terminate after this channel is closed
- ch.close();
+ static void testShutdownNow(ExecutorService pool,
+ AsynchronousChannelGroup group)
+ throws Exception
+ {
+ // I/O in progress
+ AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel
+ .open(group).bind(new InetSocketAddress(0));
+ ch.accept();
- // group should terminate quickly
- boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
- if (!terminated)
- throw new RuntimeException("Group should have terminated");
- if (pool != null && !pool.isTerminated())
- throw new RuntimeException("Executor should have terminated");
- }
+ // forceful shutdown
+ group.shutdownNow();
+
+ // shutdownNow is required to close all channels
+ if (ch.isOpen())
+ throw new RuntimeException("Channel should be closed");
+
+ boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
+ if (!terminated)
+ throw new RuntimeException("Group should have terminated");
+ 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 = null;
- AsynchronousChannelGroup group;
- if (rand.nextBoolean()) {
- pool = Executors.newCachedThreadPool();
- group = AsynchronousChannelGroup
+ for (int i = 0; i < 10; i++) {
+ ExecutorService pool = pool = Executors.newCachedThreadPool();
+ AsynchronousChannelGroup group = AsynchronousChannelGroup
.withCachedThreadPool(pool, rand.nextInt(5));
- } else {
- int nThreads = 1 + rand.nextInt(8);
- group = AsynchronousChannelGroup
+ testShutdownNow(pool, group);
+ }
+ for (int i = 0; i < 10; i++) {
+ int nThreads = 1 + rand.nextInt(8);
+ AsynchronousChannelGroup group = AsynchronousChannelGroup
.withFixedThreadPool(nThreads, threadFactory);
- }
-
- // 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");
-
- boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
- if (!terminated)
- throw new RuntimeException("Group should have terminated");
- if (pool != null && !pool.isTerminated())
- throw new RuntimeException("Executor should have terminated");
+ testShutdownNow(null, group);
+ }
+ for (int i = 0; i < 10; i++) {
+ ExecutorService pool = Executors.newCachedThreadPool();
+ AsynchronousChannelGroup group = AsynchronousChannelGroup
+ .withThreadPool(pool);
+ testShutdownNow(pool, group);
}
}
@@ -245,5 +282,10 @@
throw new RuntimeException("NPE expected");
} catch (NullPointerException x) {
}
+ try {
+ AsynchronousChannelGroup.withThreadPool(null);
+ throw new RuntimeException("NPE expected");
+ } catch (NullPointerException e) {
+ }
}
}
--- a/jdk/test/java/nio/channels/AsynchronousChannelGroup/Restart.java Wed Aug 08 12:37:02 2012 +0100
+++ b/jdk/test/java/nio/channels/AsynchronousChannelGroup/Restart.java Wed Aug 08 15:31:22 2012 +0100
@@ -71,17 +71,23 @@
testRestart(group, 100);
group.shutdown();
- // group with custom thread pool
+ // group with cached thread pool
ExecutorService pool = Executors.newCachedThreadPool(factory);
group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5));
testRestart(group, 100);
group.shutdown();
+ // group with custom thread pool
+ group = AsynchronousChannelGroup
+ .withThreadPool(Executors.newFixedThreadPool(1+rand.nextInt(5), factory));
+ testRestart(group, 100);
+ group.shutdown();
+
// give time for threads to terminate
Thread.sleep(3000);
int actual = exceptionCount.get();
- if (actual != 200)
- throw new RuntimeException(actual + " exceptions, expected: " + 200);
+ if (actual != 300)
+ throw new RuntimeException(actual + " exceptions, expected: " + 300);
}
static void testRestart(AsynchronousChannelGroup group, int count)