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); |
33 |
|
shouldThrow(); |
34 |
< |
} catch (NullPointerException success) { |
34 |
< |
} |
34 |
> |
} catch (NullPointerException success) {} |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
|
* Creating a new ECS with null queue throw NPE |
39 |
< |
*/ |
39 |
> |
*/ |
40 |
|
public void testConstructorNPE2() { |
41 |
|
try { |
42 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
43 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e, null); |
44 |
|
shouldThrow(); |
45 |
< |
} catch (NullPointerException success) { |
46 |
< |
} |
45 |
> |
} catch (NullPointerException success) {} |
46 |
|
} |
47 |
|
|
48 |
|
/** |
49 |
|
* Submitting a null callable throws NPE |
50 |
< |
*/ |
50 |
> |
*/ |
51 |
|
public void testSubmitNPE() { |
52 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
53 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
63 |
|
|
64 |
|
/** |
65 |
|
* Submitting a null runnable throws NPE |
66 |
< |
*/ |
66 |
> |
*/ |
67 |
|
public void testSubmitNPE2() { |
68 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
69 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
79 |
|
|
80 |
|
/** |
81 |
|
* A taken submitted task is completed |
82 |
< |
*/ |
83 |
< |
public void testTake() { |
82 |
> |
*/ |
83 |
> |
public void testTake() throws InterruptedException { |
84 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
85 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
86 |
|
try { |
88 |
|
ecs.submit(c); |
89 |
|
Future f = ecs.take(); |
90 |
|
assertTrue(f.isDone()); |
92 |
– |
} catch (Exception ex) { |
93 |
– |
unexpectedException(); |
91 |
|
} finally { |
92 |
|
joinPool(e); |
93 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
|
* Take returns the same future object returned by submit |
98 |
< |
*/ |
99 |
< |
public void testTake2() { |
98 |
> |
*/ |
99 |
> |
public void testTake2() throws InterruptedException { |
100 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
101 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
102 |
|
try { |
104 |
|
Future f1 = ecs.submit(c); |
105 |
|
Future f2 = ecs.take(); |
106 |
|
assertSame(f1, f2); |
110 |
– |
} catch (Exception ex) { |
111 |
– |
unexpectedException(); |
107 |
|
} finally { |
108 |
|
joinPool(e); |
109 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
|
* If poll returns non-null, the returned task is completed |
114 |
< |
*/ |
115 |
< |
public void testPoll1() { |
114 |
> |
*/ |
115 |
> |
public void testPoll1() throws InterruptedException { |
116 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
117 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
118 |
|
try { |
127 |
|
break; |
128 |
|
} |
129 |
|
} |
135 |
– |
} catch (Exception ex) { |
136 |
– |
unexpectedException(); |
130 |
|
} finally { |
131 |
|
joinPool(e); |
132 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
|
* If timed poll returns non-null, the returned task is completed |
137 |
< |
*/ |
138 |
< |
public void testPoll2() { |
137 |
> |
*/ |
138 |
> |
public void testPoll2() throws InterruptedException { |
139 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
140 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
141 |
|
try { |
142 |
|
assertNull(ecs.poll()); |
143 |
|
Callable c = new StringTask(); |
144 |
|
ecs.submit(c); |
145 |
< |
Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
146 |
< |
if (f != null) |
145 |
> |
Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS); |
146 |
> |
if (f != null) |
147 |
|
assertTrue(f.isDone()); |
155 |
– |
} catch (Exception ex) { |
156 |
– |
unexpectedException(); |
148 |
|
} finally { |
149 |
|
joinPool(e); |
150 |
|
} |
153 |
|
* Submitting to underlying AES that overrides newTaskFor(Callable) |
154 |
|
* returns and eventually runs Future returned by newTaskFor. |
155 |
|
*/ |
156 |
< |
public void testNewTaskForCallable() { |
156 |
> |
public void testNewTaskForCallable() throws InterruptedException { |
157 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
158 |
|
class MyCallableFuture<V> extends FutureTask<V> { |
159 |
|
MyCallableFuture(Callable<V> c) { super(c); } |
177 |
|
Future f2 = ecs.take(); |
178 |
|
assertSame("submit and take must return same objects", f1, f2); |
179 |
|
assertTrue("completed task must have set done", done.get()); |
189 |
– |
} catch (Exception ex) { |
190 |
– |
unexpectedException(); |
180 |
|
} finally { |
181 |
|
joinPool(e); |
182 |
|
} |
186 |
|
* Submitting to underlying AES that overrides newTaskFor(Runnable,T) |
187 |
|
* returns and eventually runs Future returned by newTaskFor. |
188 |
|
*/ |
189 |
< |
public void testNewTaskForRunnable() { |
189 |
> |
public void testNewTaskForRunnable() throws InterruptedException { |
190 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
191 |
|
class MyRunnableFuture<V> extends FutureTask<V> { |
192 |
|
MyRunnableFuture(Runnable t, V r) { super(t, r); } |
210 |
|
Future f2 = ecs.take(); |
211 |
|
assertSame("submit and take must return same objects", f1, f2); |
212 |
|
assertTrue("completed task must have set done", done.get()); |
224 |
– |
} catch (Exception ex) { |
225 |
– |
unexpectedException(); |
213 |
|
} finally { |
214 |
|
joinPool(e); |
215 |
|
} |
216 |
|
} |
217 |
|
|
231 |
– |
|
232 |
– |
|
218 |
|
} |