| 5 | import java.util.concurrent.TimeUnit; |
| 6 | |
| 7 | public class CompletionServiceTest { |
| 8 | public static void main(String args[]) throws InterruptedException, ExecutionException { |
| 9 | Executor dumbExecutor = new Executor() { |
| 10 | @Override |
| 11 | public void execute(Runnable task) { |
| 12 | new Thread(task).start(); |
| 13 | } |
| 14 | }; |
| 15 | |
| 16 | pollNoResultTest(dumbExecutor); |
| 17 | pollTimeoutNoResultTest(dumbExecutor); |
| 18 | takeTest(dumbExecutor); |
| 19 | } |
| 20 | |
| 21 | private static void verify(boolean val) { |
| 22 | if (! val) { |
| 23 | throw new RuntimeException(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | private static void pollNoResultTest(Executor executor) { |
| 28 | ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(executor); |
| 29 | |
| 30 | verify(ecs.poll() == null); |
| 31 | } |
| 32 | |
| 33 | private static void pollTimeoutNoResultTest(Executor executor) throws InterruptedException { |
| 34 | long delayTime = 0; |
| 35 | ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(executor); |
| 36 | |
| 37 | long startTime = System.currentTimeMillis(); |
| 38 | verify(ecs.poll(delayTime, TimeUnit.MILLISECONDS) == null); |
| 39 | verify(System.currentTimeMillis() - startTime >= delayTime); |
| 40 | } |
| 41 | |
| 42 | private static void takeTest(Executor executor) throws InterruptedException, ExecutionException { |
| 43 | ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(executor); |
| 44 | final Object result = new Object(); |
| 45 | ecs.submit(new Callable<Object>() { |
| 46 | @Override |
| 47 | public Object call() throws Exception { |
| 48 | return result; |
| 49 | } |
| 50 | }); |
| 51 | |
| 52 | verify(ecs.take().get() == result); |
| 53 | } |
| 54 | } |
nothing calls this directly
no outgoing calls
no test coverage detected