You need to sign in or sign up before continuing.
提交 3dd6a453 编写于 作者: A alanb

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

Reviewed-by: alanb
Contributed-by: amy.lu@oracle.com
上级 eeb618e0
......@@ -37,24 +37,30 @@ public class AsExecutor {
.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();
}
}
......
......@@ -51,98 +51,135 @@ public class Basic {
miscTests();
}
static void shutdownTests() throws Exception {
System.out.println("-- test shutdown --");
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");
}
// 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");
}
// 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");
// 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 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 shutdownNowTests() throws Exception {
System.out.println("-- test shutdownNow --");
static void shutdownTests() throws Exception {
System.out.println("-- test shutdown --");
for (int i=0; i< 10; i++) {
ExecutorService pool = null;
AsynchronousChannelGroup group;
if (rand.nextBoolean()) {
pool = Executors.newCachedThreadPool();
group = AsynchronousChannelGroup
// 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));
} else {
int nThreads = 1 + rand.nextInt(8);
group = AsynchronousChannelGroup
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));
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);
}
}
// I/O in progress
AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel
static void testShutdownNow(ExecutorService pool,
AsynchronousChannelGroup group)
throws Exception
{
// I/O in progress
AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel
.open(group).bind(new InetSocketAddress(0));
ch.accept();
ch.accept();
// forceful shutdown
group.shutdownNow();
// 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");
}
// shutdownNow is required to close all channels
if (ch.isOpen())
throw new RuntimeException("Channel should be closed");
static void shutdownNowTests() throws Exception {
System.out.println("-- test shutdownNow --");
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 < 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);
}
}
......@@ -245,5 +282,10 @@ public class Basic {
throw new RuntimeException("NPE expected");
} 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);
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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册