7189886: (aio) Add test coverage for AsynchronousChannelGroup.withThreadPool

Reviewed-by: alanb
This commit is contained in:
Amy Lu 2012-08-08 15:31:22 +01:00 committed by Alan Bateman
parent b3ebff94ab
commit 56779e429b
3 changed files with 135 additions and 81 deletions
jdk/test/java/nio/channels/AsynchronousChannelGroup

@ -37,24 +37,30 @@ public class AsExecutor {
.withFixedThreadPool(5, factory); .withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0); .withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try { try {
// execute simple tasks // execute simple tasks
testSimpleTask(group1); testSimpleTask(group1);
testSimpleTask(group2); testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again // install security manager and test again
System.setSecurityManager( new SecurityManager() ); System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1); testSimpleTask(group1);
testSimpleTask(group2); testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot // attempt to execute tasks that run with only frames from boot
// class loader on the stack. // class loader on the stack.
testAttackingTask(group1); testAttackingTask(group1);
testAttackingTask(group2); testAttackingTask(group2);
testAttackingTask(group3);
} finally { } finally {
group1.shutdown(); group1.shutdown();
group2.shutdown(); group2.shutdown();
group3.shutdown();
} }
} }

@ -51,20 +51,10 @@ public class Basic {
miscTests(); miscTests();
} }
static void shutdownTests() throws Exception { static void testShutdownWithNoChannels(ExecutorService pool,
System.out.println("-- test shutdown --"); AsynchronousChannelGroup group)
throws Exception
// 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(); group.shutdown();
if (!group.isShutdown()) if (!group.isShutdown())
throw new RuntimeException("Group should be shutdown"); throw new RuntimeException("Group should be shutdown");
@ -76,17 +66,11 @@ public class Basic {
throw new RuntimeException("Executor should have terminated"); throw new RuntimeException("Executor should have terminated");
} }
// shutdown with channel in group static void testShutdownWithChannels(ExecutorService pool,
for (int i=0; i<500; i++) { AsynchronousChannelGroup group)
ExecutorService pool = null; throws Exception
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 // create channel that is bound to group
AsynchronousChannel ch; AsynchronousChannel ch;
switch (rand.nextInt(2)) { switch (rand.nextInt(2)) {
@ -108,24 +92,55 @@ public class Basic {
if (pool != null && !pool.isTerminated()) if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated"); throw new RuntimeException("Executor should have terminated");
} }
}
static void shutdownNowTests() throws Exception { static void shutdownTests() throws Exception {
System.out.println("-- test shutdownNow --"); System.out.println("-- test shutdown --");
for (int i=0; i< 10; i++) { // test shutdown with no channels in groups
ExecutorService pool = null; for (int i = 0; i < 100; i++) {
AsynchronousChannelGroup group; ExecutorService pool = Executors.newCachedThreadPool();
if (rand.nextBoolean()) { AsynchronousChannelGroup group = AsynchronousChannelGroup
pool = Executors.newCachedThreadPool();
group = AsynchronousChannelGroup
.withCachedThreadPool(pool, rand.nextInt(5)); .withCachedThreadPool(pool, rand.nextInt(5));
} else { testShutdownWithNoChannels(pool, group);
}
for (int i = 0; i < 100; i++) {
int nThreads = 1 + rand.nextInt(8); int nThreads = 1 + rand.nextInt(8);
group = AsynchronousChannelGroup AsynchronousChannelGroup group = AsynchronousChannelGroup
.withFixedThreadPool(nThreads, threadFactory); .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));
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);
}
}
static void testShutdownNow(ExecutorService pool,
AsynchronousChannelGroup group)
throws Exception
{
// I/O in progress // I/O in progress
AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel
.open(group).bind(new InetSocketAddress(0)); .open(group).bind(new InetSocketAddress(0));
@ -144,6 +159,28 @@ public class Basic {
if (pool != null && !pool.isTerminated()) if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated"); 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));
testShutdownNow(pool, group);
}
for (int i = 0; i < 10; i++) {
int nThreads = 1 + rand.nextInt(8);
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withFixedThreadPool(nThreads, threadFactory);
testShutdownNow(null, group);
}
for (int i = 0; i < 10; i++) {
ExecutorService pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withThreadPool(pool);
testShutdownNow(pool, group);
}
} }
// test creating channels in group after group is shutdown // test creating channels in group after group is shutdown
@ -245,5 +282,10 @@ public class Basic {
throw new RuntimeException("NPE expected"); throw new RuntimeException("NPE expected");
} catch (NullPointerException x) { } catch (NullPointerException x) {
} }
try {
AsynchronousChannelGroup.withThreadPool(null);
throw new RuntimeException("NPE expected");
} catch (NullPointerException e) {
}
} }
} }

@ -71,17 +71,23 @@ public class Restart {
testRestart(group, 100); testRestart(group, 100);
group.shutdown(); group.shutdown();
// group with custom thread pool // group with cached thread pool
ExecutorService pool = Executors.newCachedThreadPool(factory); ExecutorService pool = Executors.newCachedThreadPool(factory);
group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5)); group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5));
testRestart(group, 100); testRestart(group, 100);
group.shutdown(); 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 // give time for threads to terminate
Thread.sleep(3000); Thread.sleep(3000);
int actual = exceptionCount.get(); int actual = exceptionCount.get();
if (actual != 200) if (actual != 300)
throw new RuntimeException(actual + " exceptions, expected: " + 200); throw new RuntimeException(actual + " exceptions, expected: " + 300);
} }
static void testRestart(AsynchronousChannelGroup group, int count) static void testRestart(AsynchronousChannelGroup group, int count)