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

View File

@ -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();
} }
} }

View File

@ -51,98 +51,135 @@ public class Basic {
miscTests(); 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 { static void shutdownTests() throws Exception {
System.out.println("-- test shutdown --"); System.out.println("-- test shutdown --");
// test shutdown with no channels in groups // test shutdown with no channels in groups
for (int i=0; i<500; i++) { for (int i = 0; i < 100; i++) {
ExecutorService pool = null; ExecutorService pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group; AsynchronousChannelGroup group = AsynchronousChannelGroup
if (rand.nextBoolean()) { .withCachedThreadPool(pool, rand.nextInt(5));
pool = Executors.newCachedThreadPool(); testShutdownWithNoChannels(pool, group);
group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5)); }
} else { for (int i = 0; i < 100; i++) {
int nThreads = 1 + rand.nextInt(8); int nThreads = 1 + rand.nextInt(8);
group = AsynchronousChannelGroup.withFixedThreadPool(nThreads, threadFactory); AsynchronousChannelGroup group = AsynchronousChannelGroup
} .withFixedThreadPool(nThreads, threadFactory);
group.shutdown(); testShutdownWithNoChannels(null, group);
if (!group.isShutdown()) }
throw new RuntimeException("Group should be shutdown"); for (int i = 0; i < 100; i++) {
// group should terminate quickly ExecutorService pool = Executors.newCachedThreadPool();
boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS); AsynchronousChannelGroup group = AsynchronousChannelGroup
if (!terminated) .withThreadPool(pool);
throw new RuntimeException("Group should have terminated"); testShutdownWithNoChannels(pool, group);
if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated");
} }
// shutdown with channel in group // test shutdown with channel in group
for (int i=0; i<500; i++) { for (int i = 0; i < 100; i++) {
ExecutorService pool = null; ExecutorService pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group; AsynchronousChannelGroup group = AsynchronousChannelGroup
if (rand.nextBoolean()) { .withCachedThreadPool(pool, rand.nextInt(10));
pool = Executors.newCachedThreadPool(); testShutdownWithChannels(pool, group);
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");
// 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");
} }
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
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");
} }
static void shutdownNowTests() throws Exception { static void shutdownNowTests() throws Exception {
System.out.println("-- test shutdownNow --"); System.out.println("-- test shutdownNow --");
for (int i=0; i< 10; i++) { for (int i = 0; i < 10; i++) {
ExecutorService pool = null; ExecutorService pool = pool = Executors.newCachedThreadPool();
AsynchronousChannelGroup group; AsynchronousChannelGroup group = AsynchronousChannelGroup
if (rand.nextBoolean()) {
pool = Executors.newCachedThreadPool();
group = AsynchronousChannelGroup
.withCachedThreadPool(pool, rand.nextInt(5)); .withCachedThreadPool(pool, rand.nextInt(5));
} else { testShutdownNow(pool, group);
int nThreads = 1 + rand.nextInt(8); }
group = AsynchronousChannelGroup for (int i = 0; i < 10; i++) {
int nThreads = 1 + rand.nextInt(8);
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withFixedThreadPool(nThreads, threadFactory); .withFixedThreadPool(nThreads, threadFactory);
} testShutdownNow(null, group);
}
// I/O in progress for (int i = 0; i < 10; i++) {
AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel ExecutorService pool = Executors.newCachedThreadPool();
.open(group).bind(new InetSocketAddress(0)); AsynchronousChannelGroup group = AsynchronousChannelGroup
ch.accept(); .withThreadPool(pool);
testShutdownNow(pool, group);
// 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");
} }
} }
@ -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) {
}
} }
} }

View File

@ -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)