2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.util.concurrent.atomic.*; |
15 |
|
import java.math.BigInteger; |
16 |
|
import java.security.*; |
17 |
|
|
18 |
< |
public class ExecutorCompletionServiceTest extends JSR166TestCase{ |
18 |
> |
public class ExecutorCompletionServiceTest extends JSR166TestCase { |
19 |
|
public static void main(String[] args) { |
20 |
< |
junit.textui.TestRunner.run (suite()); |
20 |
> |
junit.textui.TestRunner.run (suite()); |
21 |
|
} |
22 |
|
public static Test suite() { |
23 |
|
return new TestSuite(ExecutorCompletionServiceTest.class); |
26 |
|
|
27 |
|
/** |
28 |
|
* Creating a new ECS with null Executor throw NPE |
29 |
< |
*/ |
29 |
> |
*/ |
30 |
|
public void testConstructorNPE() { |
31 |
|
try { |
32 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(null); |
37 |
|
|
38 |
|
/** |
39 |
|
* Creating a new ECS with null queue throw NPE |
40 |
< |
*/ |
40 |
> |
*/ |
41 |
|
public void testConstructorNPE2() { |
42 |
|
try { |
43 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
49 |
|
|
50 |
|
/** |
51 |
|
* Submitting a null callable throws NPE |
52 |
< |
*/ |
52 |
> |
*/ |
53 |
|
public void testSubmitNPE() { |
54 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
55 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
65 |
|
|
66 |
|
/** |
67 |
|
* Submitting a null runnable throws NPE |
68 |
< |
*/ |
68 |
> |
*/ |
69 |
|
public void testSubmitNPE2() { |
70 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
71 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
81 |
|
|
82 |
|
/** |
83 |
|
* A taken submitted task is completed |
84 |
< |
*/ |
84 |
> |
*/ |
85 |
|
public void testTake() { |
86 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
87 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
99 |
|
|
100 |
|
/** |
101 |
|
* Take returns the same future object returned by submit |
102 |
< |
*/ |
102 |
> |
*/ |
103 |
|
public void testTake2() { |
104 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
105 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
117 |
|
|
118 |
|
/** |
119 |
|
* If poll returns non-null, the returned task is completed |
120 |
< |
*/ |
120 |
> |
*/ |
121 |
|
public void testPoll1() { |
122 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
123 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
142 |
|
|
143 |
|
/** |
144 |
|
* If timed poll returns non-null, the returned task is completed |
145 |
< |
*/ |
145 |
> |
*/ |
146 |
|
public void testPoll2() { |
147 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
148 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
150 |
|
assertNull(ecs.poll()); |
151 |
|
Callable c = new StringTask(); |
152 |
|
ecs.submit(c); |
153 |
< |
Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
154 |
< |
if (f != null) |
153 |
> |
Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS); |
154 |
> |
if (f != null) |
155 |
|
assertTrue(f.isDone()); |
156 |
|
} catch (Exception ex) { |
157 |
|
unexpectedException(); |