mirror of
https://github.com/corda/corda.git
synced 2024-12-30 17:57:02 +00:00
9bb3d6b972
git-subtree-dir: sgx-jvm/avian git-subtree-mainline:f978eab8d1
git-subtree-split:09e4fe60d0
55 lines
1.7 KiB
Java
55 lines
1.7 KiB
Java
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.ExecutorCompletionService;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
public class CompletionServiceTest {
|
|
public static void main(String args[]) throws InterruptedException, ExecutionException {
|
|
Executor dumbExecutor = new Executor() {
|
|
@Override
|
|
public void execute(Runnable task) {
|
|
new Thread(task).start();
|
|
}
|
|
};
|
|
|
|
pollNoResultTest(dumbExecutor);
|
|
pollTimeoutNoResultTest(dumbExecutor);
|
|
takeTest(dumbExecutor);
|
|
}
|
|
|
|
private static void verify(boolean val) {
|
|
if (! val) {
|
|
throw new RuntimeException();
|
|
}
|
|
}
|
|
|
|
private static void pollNoResultTest(Executor executor) {
|
|
ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(executor);
|
|
|
|
verify(ecs.poll() == null);
|
|
}
|
|
|
|
private static void pollTimeoutNoResultTest(Executor executor) throws InterruptedException {
|
|
long delayTime = 0;
|
|
ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(executor);
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
verify(ecs.poll(delayTime, TimeUnit.MILLISECONDS) == null);
|
|
verify(System.currentTimeMillis() - startTime >= delayTime);
|
|
}
|
|
|
|
private static void takeTest(Executor executor) throws InterruptedException, ExecutionException {
|
|
ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(executor);
|
|
final Object result = new Object();
|
|
ecs.submit(new Callable<Object>() {
|
|
@Override
|
|
public Object call() throws Exception {
|
|
return result;
|
|
}
|
|
});
|
|
|
|
verify(ecs.take().get() == result);
|
|
}
|
|
}
|