📄 abstractexecutorservicetest.java
字号:
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/licenses/publicdomain
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import junit.framework.*;
import java.util.*;
import edu.emory.mathcs.backport.java.util.*;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import java.math.BigInteger;
import java.security.*;
public class AbstractExecutorServiceTest extends JSR166TestCase{
public static void main(String[] args) {
junit.textui.TestRunner.run (suite());
}
public static Test suite() {
return new TestSuite(AbstractExecutorServiceTest.class);
}
/**
* A no-frills implementation of AbstractExecutorService, designed
* to test the submit methods only.
*/
static class DirectExecutorService extends AbstractExecutorService {
public void execute(Runnable r) { r.run(); }
public void shutdown() { shutdown = true; }
public List shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; }
public boolean isShutdown() { return shutdown; }
public boolean isTerminated() { return isShutdown(); }
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); }
private volatile boolean shutdown = false;
}
/**
* execute(runnable) runs it to completion
*/
public void testExecuteRunnable() {
try {
ExecutorService e = new DirectExecutorService();
TrackedShortRunnable task = new TrackedShortRunnable();
assertFalse(task.done);
Future future = e.submit(task);
future.get();
assertTrue(task.done);
}
catch (ExecutionException ex) {
unexpectedException();
}
catch (InterruptedException ex) {
unexpectedException();
}
}
/**
* Completed submit(callable) returns result
*/
public void testSubmitCallable() {
try {
ExecutorService e = new DirectExecutorService();
Future future = e.submit(new StringTask());
String result = (String)future.get();
assertSame(TEST_STRING, result);
}
catch (ExecutionException ex) {
unexpectedException();
}
catch (InterruptedException ex) {
unexpectedException();
}
}
/**
* Completed submit(runnable) returns successfully
*/
public void testSubmitRunnable() {
try {
ExecutorService e = new DirectExecutorService();
Future future = e.submit(new NoOpRunnable());
future.get();
assertTrue(future.isDone());
}
catch (ExecutionException ex) {
unexpectedException();
}
catch (InterruptedException ex) {
unexpectedException();
}
}
/**
* Completed submit(runnable, result) returns result
*/
public void testSubmitRunnable2() {
try {
ExecutorService e = new DirectExecutorService();
Future future = e.submit(new NoOpRunnable(), TEST_STRING);
String result = (String)future.get();
assertSame(TEST_STRING, result);
}
catch (ExecutionException ex) {
unexpectedException();
}
catch (InterruptedException ex) {
unexpectedException();
}
}
/**
* A submitted privileged action to completion
*/
public void testSubmitPrivilegedAction() {
Policy savedPolicy = null;
try {
savedPolicy = Policy.getPolicy();
AdjustablePolicy policy = new AdjustablePolicy();
policy.addPermission(new RuntimePermission("getContextClassLoader"));
policy.addPermission(new RuntimePermission("setContextClassLoader"));
Policy.setPolicy(policy);
} catch(AccessControlException ok) {
return;
}
try {
ExecutorService e = new DirectExecutorService();
Future future = e.submit(Executors.callable(new PrivilegedAction() {
public Object run() {
return TEST_STRING;
}}));
Object result = future.get();
assertSame(TEST_STRING, result);
}
catch (ExecutionException ex) {
unexpectedException();
}
catch (InterruptedException ex) {
unexpectedException();
}
finally {
try {
Policy.setPolicy(savedPolicy);
} catch(AccessControlException ok) {
return;
}
}
}
/**
* A submitted a privileged exception action runs to completion
*/
public void testSubmitPrivilegedExceptionAction() {
Policy savedPolicy = null;
try {
savedPolicy = Policy.getPolicy();
AdjustablePolicy policy = new AdjustablePolicy();
policy.addPermission(new RuntimePermission("getContextClassLoader"));
policy.addPermission(new RuntimePermission("setContextClassLoader"));
Policy.setPolicy(policy);
} catch(AccessControlException ok) {
return;
}
try {
ExecutorService e = new DirectExecutorService();
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
public Object run() {
return TEST_STRING;
}}));
Object result = future.get();
assertSame(TEST_STRING, result);
}
catch (ExecutionException ex) {
unexpectedException();
}
catch (InterruptedException ex) {
unexpectedException();
}
finally {
Policy.setPolicy(savedPolicy);
}
}
/**
* A submitted failed privileged exception action reports exception
*/
public void testSubmitFailedPrivilegedExceptionAction() {
Policy savedPolicy = null;
try {
savedPolicy = Policy.getPolicy();
AdjustablePolicy policy = new AdjustablePolicy();
policy.addPermission(new RuntimePermission("getContextClassLoader"));
policy.addPermission(new RuntimePermission("setContextClassLoader"));
Policy.setPolicy(policy);
} catch(AccessControlException ok) {
return;
}
try {
ExecutorService e = new DirectExecutorService();
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
public Object run() throws Exception {
throw new IndexOutOfBoundsException();
}}));
Object result = future.get();
shouldThrow();
}
catch (ExecutionException success) {
}
catch (InterruptedException ex) {
unexpectedException();
}
finally {
Policy.setPolicy(savedPolicy);
}
}
/**
* execute(null runnable) throws NPE
*/
public void testExecuteNullRunnable() {
try {
ExecutorService e = new DirectExecutorService();
TrackedShortRunnable task = null;
Future future = e.submit(task);
shouldThrow();
}
catch (NullPointerException success) {
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* submit(null callable) throws NPE
*/
public void testSubmitNullCallable() {
try {
ExecutorService e = new DirectExecutorService();
StringTask t = null;
Future future = e.submit(t);
shouldThrow();
}
catch (NullPointerException success) {
}
catch (Exception ex) {
unexpectedException();
}
}
/**
* submit(runnable) throws RejectedExecutionException if
* executor is saturated.
*/
public void testExecute1() {
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(1));
try {
for(int i = 0; i < 5; ++i){
p.submit(new MediumRunnable());
}
shouldThrow();
} catch(RejectedExecutionException success){}
joinPool(p);
}
/**
* submit(callable) throws RejectedExecutionException
* if executor is saturated.
*/
public void testExecute2() {
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(1));
try {
for(int i = 0; i < 5; ++i) {
p.submit(new SmallCallable());
}
shouldThrow();
} catch(RejectedExecutionException e){}
joinPool(p);
}
/**
* Blocking on submit(callable) throws InterruptedException if
* caller interrupted.
*/
public void testInterruptedSubmit() {
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10));
Thread t = new Thread(new Runnable() {
public void run() {
try {
p.submit(new Callable() {
public Object call() {
try {
Thread.sleep(MEDIUM_DELAY_MS);
shouldThrow();
} catch(InterruptedException e){
}
return null;
}
}).get();
} catch(InterruptedException success){
} catch(Exception e) {
unexpectedException();
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
} catch(Exception e){
unexpectedException();
}
joinPool(p);
}
/**
* get of submitted callable throws Exception if callable
* interrupted
*/
public void testSubmitIE() {
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10));
final Callable c = new Callable() {
public Object call() {
try {
p.submit(new SmallCallable()).get();
shouldThrow();
} catch(InterruptedException e){}
catch(RejectedExecutionException e2){}
catch(ExecutionException e3){}
return Boolean.TRUE;
}
};
Thread t = new Thread(new Runnable() {
public void run() {
try {
c.call();
} catch(Exception e){}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch(InterruptedException e){
unexpectedException();
}
joinPool(p);
}
/**
* get of submit(callable) throws ExecutionException if callable
* throws exception
*/
public void testSubmitEE() {
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue(10));
try {
Callable c = new Callable() {
public Object call() {
int i = 5/0;
return Boolean.TRUE;
}
};
for(int i =0; i < 5; i++){
p.submit(c).get();
}
shouldThrow();
}
catch(ExecutionException success){
} catch(Exception e) {
unexpectedException();
}
joinPool(p);
}
/**
* invokeAny(null) throws NPE
*/
public void testInvokeAny1() {
ExecutorService e = new DirectExecutorService();
try {
e.invokeAny(null);
} catch (NullPointerException success) {
} catch(Exception ex) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -