1 |
dl |
1.1 |
/* |
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 |
jsr166 |
1.19 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
import junit.framework.*; |
8 |
|
|
import java.util.*; |
9 |
|
|
import java.util.concurrent.*; |
10 |
jsr166 |
1.6 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
jsr166 |
1.28 |
import java.util.concurrent.atomic.AtomicInteger; |
12 |
dl |
1.1 |
|
13 |
|
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
14 |
|
|
public static void main(String[] args) { |
15 |
jsr166 |
1.12 |
junit.textui.TestRunner.run(suite()); |
16 |
dl |
1.1 |
} |
17 |
|
|
public static Test suite() { |
18 |
jsr166 |
1.7 |
return new TestSuite(ScheduledExecutorSubclassTest.class); |
19 |
dl |
1.1 |
} |
20 |
|
|
|
21 |
jsr166 |
1.2 |
static class CustomTask<V> implements RunnableScheduledFuture<V> { |
22 |
dl |
1.1 |
RunnableScheduledFuture<V> task; |
23 |
|
|
volatile boolean ran; |
24 |
|
|
CustomTask(RunnableScheduledFuture<V> t) { task = t; } |
25 |
|
|
public boolean isPeriodic() { return task.isPeriodic(); } |
26 |
jsr166 |
1.2 |
public void run() { |
27 |
dl |
1.1 |
ran = true; |
28 |
jsr166 |
1.2 |
task.run(); |
29 |
dl |
1.1 |
} |
30 |
|
|
public long getDelay(TimeUnit unit) { return task.getDelay(unit); } |
31 |
|
|
public int compareTo(Delayed t) { |
32 |
jsr166 |
1.2 |
return task.compareTo(((CustomTask)t).task); |
33 |
dl |
1.1 |
} |
34 |
|
|
public boolean cancel(boolean mayInterruptIfRunning) { |
35 |
|
|
return task.cancel(mayInterruptIfRunning); |
36 |
|
|
} |
37 |
|
|
public boolean isCancelled() { return task.isCancelled(); } |
38 |
|
|
public boolean isDone() { return task.isDone(); } |
39 |
jsr166 |
1.30 |
public V get() throws InterruptedException, ExecutionException { |
40 |
dl |
1.1 |
V v = task.get(); |
41 |
|
|
assertTrue(ran); |
42 |
|
|
return v; |
43 |
|
|
} |
44 |
jsr166 |
1.30 |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
45 |
dl |
1.1 |
V v = task.get(time, unit); |
46 |
|
|
assertTrue(ran); |
47 |
|
|
return v; |
48 |
|
|
} |
49 |
|
|
} |
50 |
jsr166 |
1.2 |
|
51 |
dl |
1.1 |
public class CustomExecutor extends ScheduledThreadPoolExecutor { |
52 |
|
|
|
53 |
|
|
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) { |
54 |
|
|
return new CustomTask<V>(task); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) { |
58 |
|
|
return new CustomTask<V>(task); |
59 |
|
|
} |
60 |
jsr166 |
1.29 |
CustomExecutor(int corePoolSize) { super(corePoolSize); } |
61 |
dl |
1.1 |
CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) { |
62 |
|
|
super(corePoolSize, handler); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
CustomExecutor(int corePoolSize, ThreadFactory threadFactory) { |
66 |
|
|
super(corePoolSize, threadFactory); |
67 |
|
|
} |
68 |
jsr166 |
1.2 |
CustomExecutor(int corePoolSize, ThreadFactory threadFactory, |
69 |
dl |
1.1 |
RejectedExecutionHandler handler) { |
70 |
|
|
super(corePoolSize, threadFactory, handler); |
71 |
|
|
} |
72 |
jsr166 |
1.2 |
|
73 |
dl |
1.1 |
} |
74 |
jsr166 |
1.2 |
|
75 |
dl |
1.1 |
/** |
76 |
|
|
* execute successfully executes a runnable |
77 |
|
|
*/ |
78 |
jsr166 |
1.6 |
public void testExecute() throws InterruptedException { |
79 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
80 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
81 |
|
|
final Runnable task = new CheckedRunnable() { |
82 |
|
|
public void realRun() { |
83 |
|
|
done.countDown(); |
84 |
|
|
}}; |
85 |
|
|
try { |
86 |
|
|
p.execute(task); |
87 |
|
|
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
|
|
} finally { |
89 |
|
|
joinPool(p); |
90 |
|
|
} |
91 |
dl |
1.1 |
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* delayed schedule of callable successfully executes after delay |
95 |
|
|
*/ |
96 |
jsr166 |
1.6 |
public void testSchedule1() throws Exception { |
97 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
98 |
jsr166 |
1.26 |
final long startTime = System.nanoTime(); |
99 |
jsr166 |
1.15 |
final CountDownLatch done = new CountDownLatch(1); |
100 |
|
|
try { |
101 |
|
|
Callable task = new CheckedCallable<Boolean>() { |
102 |
|
|
public Boolean realCall() { |
103 |
|
|
done.countDown(); |
104 |
jsr166 |
1.26 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
105 |
jsr166 |
1.15 |
return Boolean.TRUE; |
106 |
|
|
}}; |
107 |
jsr166 |
1.26 |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
108 |
|
|
assertSame(Boolean.TRUE, f.get()); |
109 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
110 |
jsr166 |
1.15 |
assertTrue(done.await(0L, MILLISECONDS)); |
111 |
|
|
} finally { |
112 |
|
|
joinPool(p); |
113 |
|
|
} |
114 |
dl |
1.1 |
} |
115 |
|
|
|
116 |
|
|
/** |
117 |
jsr166 |
1.14 |
* delayed schedule of runnable successfully executes after delay |
118 |
dl |
1.1 |
*/ |
119 |
jsr166 |
1.15 |
public void testSchedule3() throws Exception { |
120 |
|
|
CustomExecutor p = new CustomExecutor(1); |
121 |
jsr166 |
1.26 |
final long startTime = System.nanoTime(); |
122 |
jsr166 |
1.15 |
final CountDownLatch done = new CountDownLatch(1); |
123 |
|
|
try { |
124 |
|
|
Runnable task = new CheckedRunnable() { |
125 |
|
|
public void realRun() { |
126 |
|
|
done.countDown(); |
127 |
jsr166 |
1.26 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
128 |
jsr166 |
1.15 |
}}; |
129 |
jsr166 |
1.26 |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
130 |
|
|
await(done); |
131 |
|
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
132 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
133 |
jsr166 |
1.15 |
} finally { |
134 |
|
|
joinPool(p); |
135 |
|
|
} |
136 |
dl |
1.1 |
} |
137 |
jsr166 |
1.2 |
|
138 |
dl |
1.1 |
/** |
139 |
|
|
* scheduleAtFixedRate executes runnable after given initial delay |
140 |
|
|
*/ |
141 |
jsr166 |
1.6 |
public void testSchedule4() throws InterruptedException { |
142 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
143 |
jsr166 |
1.26 |
final long startTime = System.nanoTime(); |
144 |
jsr166 |
1.15 |
final CountDownLatch done = new CountDownLatch(1); |
145 |
|
|
try { |
146 |
|
|
Runnable task = new CheckedRunnable() { |
147 |
|
|
public void realRun() { |
148 |
|
|
done.countDown(); |
149 |
jsr166 |
1.26 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
150 |
jsr166 |
1.15 |
}}; |
151 |
|
|
ScheduledFuture f = |
152 |
jsr166 |
1.26 |
p.scheduleAtFixedRate(task, timeoutMillis(), |
153 |
|
|
LONG_DELAY_MS, MILLISECONDS); |
154 |
|
|
await(done); |
155 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
156 |
jsr166 |
1.15 |
f.cancel(true); |
157 |
|
|
} finally { |
158 |
|
|
joinPool(p); |
159 |
|
|
} |
160 |
dl |
1.1 |
} |
161 |
|
|
|
162 |
|
|
/** |
163 |
|
|
* scheduleWithFixedDelay executes runnable after given initial delay |
164 |
|
|
*/ |
165 |
jsr166 |
1.6 |
public void testSchedule5() throws InterruptedException { |
166 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
167 |
jsr166 |
1.26 |
final long startTime = System.nanoTime(); |
168 |
jsr166 |
1.15 |
final CountDownLatch done = new CountDownLatch(1); |
169 |
|
|
try { |
170 |
|
|
Runnable task = new CheckedRunnable() { |
171 |
|
|
public void realRun() { |
172 |
|
|
done.countDown(); |
173 |
jsr166 |
1.26 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
174 |
jsr166 |
1.15 |
}}; |
175 |
|
|
ScheduledFuture f = |
176 |
jsr166 |
1.26 |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
177 |
|
|
LONG_DELAY_MS, MILLISECONDS); |
178 |
|
|
await(done); |
179 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
180 |
jsr166 |
1.15 |
f.cancel(true); |
181 |
|
|
} finally { |
182 |
|
|
joinPool(p); |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
static class RunnableCounter implements Runnable { |
187 |
|
|
AtomicInteger count = new AtomicInteger(0); |
188 |
|
|
public void run() { count.getAndIncrement(); } |
189 |
dl |
1.1 |
} |
190 |
jsr166 |
1.2 |
|
191 |
dl |
1.1 |
/** |
192 |
|
|
* scheduleAtFixedRate executes series of tasks at given rate |
193 |
|
|
*/ |
194 |
jsr166 |
1.6 |
public void testFixedRateSequence() throws InterruptedException { |
195 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
196 |
jsr166 |
1.6 |
RunnableCounter counter = new RunnableCounter(); |
197 |
|
|
ScheduledFuture h = |
198 |
jsr166 |
1.15 |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
199 |
dl |
1.21 |
delay(SMALL_DELAY_MS); |
200 |
jsr166 |
1.6 |
h.cancel(true); |
201 |
|
|
int c = counter.count.get(); |
202 |
|
|
// By time scaling conventions, we must have at least |
203 |
|
|
// an execution per SHORT delay, but no more than one SHORT more |
204 |
|
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
205 |
|
|
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
206 |
jsr166 |
1.15 |
joinPool(p); |
207 |
dl |
1.1 |
} |
208 |
|
|
|
209 |
|
|
/** |
210 |
|
|
* scheduleWithFixedDelay executes series of tasks with given period |
211 |
|
|
*/ |
212 |
jsr166 |
1.6 |
public void testFixedDelaySequence() throws InterruptedException { |
213 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
214 |
jsr166 |
1.6 |
RunnableCounter counter = new RunnableCounter(); |
215 |
|
|
ScheduledFuture h = |
216 |
jsr166 |
1.15 |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
217 |
dl |
1.21 |
delay(SMALL_DELAY_MS); |
218 |
jsr166 |
1.6 |
h.cancel(true); |
219 |
|
|
int c = counter.count.get(); |
220 |
|
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
221 |
|
|
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
222 |
jsr166 |
1.15 |
joinPool(p); |
223 |
dl |
1.1 |
} |
224 |
|
|
|
225 |
|
|
/** |
226 |
jsr166 |
1.12 |
* execute(null) throws NPE |
227 |
dl |
1.1 |
*/ |
228 |
jsr166 |
1.6 |
public void testExecuteNull() throws InterruptedException { |
229 |
|
|
CustomExecutor se = new CustomExecutor(1); |
230 |
dl |
1.1 |
try { |
231 |
jsr166 |
1.7 |
se.execute(null); |
232 |
dl |
1.1 |
shouldThrow(); |
233 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
234 |
|
|
joinPool(se); |
235 |
dl |
1.1 |
} |
236 |
|
|
|
237 |
|
|
/** |
238 |
jsr166 |
1.12 |
* schedule(null) throws NPE |
239 |
dl |
1.1 |
*/ |
240 |
jsr166 |
1.6 |
public void testScheduleNull() throws InterruptedException { |
241 |
dl |
1.1 |
CustomExecutor se = new CustomExecutor(1); |
242 |
jsr166 |
1.7 |
try { |
243 |
dl |
1.1 |
TrackedCallable callable = null; |
244 |
jsr166 |
1.7 |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
245 |
dl |
1.1 |
shouldThrow(); |
246 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
247 |
|
|
joinPool(se); |
248 |
dl |
1.1 |
} |
249 |
jsr166 |
1.2 |
|
250 |
dl |
1.1 |
/** |
251 |
|
|
* execute throws RejectedExecutionException if shutdown |
252 |
|
|
*/ |
253 |
|
|
public void testSchedule1_RejectedExecutionException() { |
254 |
|
|
CustomExecutor se = new CustomExecutor(1); |
255 |
|
|
try { |
256 |
|
|
se.shutdown(); |
257 |
|
|
se.schedule(new NoOpRunnable(), |
258 |
jsr166 |
1.6 |
MEDIUM_DELAY_MS, MILLISECONDS); |
259 |
dl |
1.1 |
shouldThrow(); |
260 |
jsr166 |
1.4 |
} catch (RejectedExecutionException success) { |
261 |
dl |
1.1 |
} catch (SecurityException ok) { |
262 |
|
|
} |
263 |
jsr166 |
1.2 |
|
264 |
dl |
1.1 |
joinPool(se); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
/** |
268 |
|
|
* schedule throws RejectedExecutionException if shutdown |
269 |
|
|
*/ |
270 |
|
|
public void testSchedule2_RejectedExecutionException() { |
271 |
|
|
CustomExecutor se = new CustomExecutor(1); |
272 |
|
|
try { |
273 |
|
|
se.shutdown(); |
274 |
|
|
se.schedule(new NoOpCallable(), |
275 |
jsr166 |
1.6 |
MEDIUM_DELAY_MS, MILLISECONDS); |
276 |
dl |
1.1 |
shouldThrow(); |
277 |
jsr166 |
1.4 |
} catch (RejectedExecutionException success) { |
278 |
dl |
1.1 |
} catch (SecurityException ok) { |
279 |
|
|
} |
280 |
|
|
joinPool(se); |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
/** |
284 |
|
|
* schedule callable throws RejectedExecutionException if shutdown |
285 |
|
|
*/ |
286 |
jsr166 |
1.20 |
public void testSchedule3_RejectedExecutionException() { |
287 |
|
|
CustomExecutor se = new CustomExecutor(1); |
288 |
|
|
try { |
289 |
|
|
se.shutdown(); |
290 |
|
|
se.schedule(new NoOpCallable(), |
291 |
|
|
MEDIUM_DELAY_MS, MILLISECONDS); |
292 |
|
|
shouldThrow(); |
293 |
|
|
} catch (RejectedExecutionException success) { |
294 |
|
|
} catch (SecurityException ok) { |
295 |
|
|
} |
296 |
|
|
joinPool(se); |
297 |
dl |
1.1 |
} |
298 |
|
|
|
299 |
|
|
/** |
300 |
jsr166 |
1.14 |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
301 |
dl |
1.1 |
*/ |
302 |
|
|
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
303 |
|
|
CustomExecutor se = new CustomExecutor(1); |
304 |
|
|
try { |
305 |
|
|
se.shutdown(); |
306 |
|
|
se.scheduleAtFixedRate(new NoOpRunnable(), |
307 |
jsr166 |
1.6 |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
308 |
dl |
1.1 |
shouldThrow(); |
309 |
jsr166 |
1.4 |
} catch (RejectedExecutionException success) { |
310 |
dl |
1.1 |
} catch (SecurityException ok) { |
311 |
jsr166 |
1.2 |
} |
312 |
dl |
1.1 |
joinPool(se); |
313 |
|
|
} |
314 |
jsr166 |
1.2 |
|
315 |
dl |
1.1 |
/** |
316 |
|
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
317 |
|
|
*/ |
318 |
|
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
319 |
|
|
CustomExecutor se = new CustomExecutor(1); |
320 |
|
|
try { |
321 |
|
|
se.shutdown(); |
322 |
|
|
se.scheduleWithFixedDelay(new NoOpRunnable(), |
323 |
jsr166 |
1.6 |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
324 |
dl |
1.1 |
shouldThrow(); |
325 |
jsr166 |
1.4 |
} catch (RejectedExecutionException success) { |
326 |
dl |
1.1 |
} catch (SecurityException ok) { |
327 |
jsr166 |
1.2 |
} |
328 |
dl |
1.1 |
joinPool(se); |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
/** |
332 |
jsr166 |
1.14 |
* getActiveCount increases but doesn't overestimate, when a |
333 |
|
|
* thread becomes active |
334 |
dl |
1.1 |
*/ |
335 |
jsr166 |
1.6 |
public void testGetActiveCount() throws InterruptedException { |
336 |
jsr166 |
1.15 |
final ThreadPoolExecutor p = new CustomExecutor(2); |
337 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
338 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
339 |
|
|
try { |
340 |
|
|
assertEquals(0, p.getActiveCount()); |
341 |
|
|
p.execute(new CheckedRunnable() { |
342 |
|
|
public void realRun() throws InterruptedException { |
343 |
|
|
threadStarted.countDown(); |
344 |
|
|
assertEquals(1, p.getActiveCount()); |
345 |
|
|
done.await(); |
346 |
|
|
}}); |
347 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
348 |
|
|
assertEquals(1, p.getActiveCount()); |
349 |
|
|
} finally { |
350 |
|
|
done.countDown(); |
351 |
|
|
joinPool(p); |
352 |
|
|
} |
353 |
dl |
1.1 |
} |
354 |
jsr166 |
1.2 |
|
355 |
dl |
1.1 |
/** |
356 |
jsr166 |
1.14 |
* getCompletedTaskCount increases, but doesn't overestimate, |
357 |
|
|
* when tasks complete |
358 |
dl |
1.1 |
*/ |
359 |
jsr166 |
1.9 |
public void testGetCompletedTaskCount() throws InterruptedException { |
360 |
jsr166 |
1.15 |
final ThreadPoolExecutor p = new CustomExecutor(2); |
361 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
362 |
|
|
final CountDownLatch threadProceed = new CountDownLatch(1); |
363 |
|
|
final CountDownLatch threadDone = new CountDownLatch(1); |
364 |
|
|
try { |
365 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
366 |
|
|
p.execute(new CheckedRunnable() { |
367 |
|
|
public void realRun() throws InterruptedException { |
368 |
|
|
threadStarted.countDown(); |
369 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
370 |
|
|
threadProceed.await(); |
371 |
|
|
threadDone.countDown(); |
372 |
|
|
}}); |
373 |
jsr166 |
1.27 |
await(threadStarted); |
374 |
jsr166 |
1.15 |
assertEquals(0, p.getCompletedTaskCount()); |
375 |
|
|
threadProceed.countDown(); |
376 |
|
|
threadDone.await(); |
377 |
jsr166 |
1.26 |
long startTime = System.nanoTime(); |
378 |
|
|
while (p.getCompletedTaskCount() != 1) { |
379 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
380 |
|
|
fail("timed out"); |
381 |
|
|
Thread.yield(); |
382 |
|
|
} |
383 |
jsr166 |
1.15 |
} finally { |
384 |
|
|
joinPool(p); |
385 |
|
|
} |
386 |
dl |
1.1 |
} |
387 |
jsr166 |
1.2 |
|
388 |
dl |
1.1 |
/** |
389 |
jsr166 |
1.14 |
* getCorePoolSize returns size given in constructor if not otherwise set |
390 |
dl |
1.1 |
*/ |
391 |
|
|
public void testGetCorePoolSize() { |
392 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
393 |
|
|
assertEquals(1, p.getCorePoolSize()); |
394 |
|
|
joinPool(p); |
395 |
dl |
1.1 |
} |
396 |
jsr166 |
1.2 |
|
397 |
dl |
1.1 |
/** |
398 |
jsr166 |
1.14 |
* getLargestPoolSize increases, but doesn't overestimate, when |
399 |
|
|
* multiple threads active |
400 |
dl |
1.1 |
*/ |
401 |
jsr166 |
1.6 |
public void testGetLargestPoolSize() throws InterruptedException { |
402 |
jsr166 |
1.15 |
final int THREADS = 3; |
403 |
|
|
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
404 |
|
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
405 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
406 |
|
|
try { |
407 |
|
|
assertEquals(0, p.getLargestPoolSize()); |
408 |
|
|
for (int i = 0; i < THREADS; i++) |
409 |
|
|
p.execute(new CheckedRunnable() { |
410 |
|
|
public void realRun() throws InterruptedException { |
411 |
|
|
threadsStarted.countDown(); |
412 |
|
|
done.await(); |
413 |
|
|
assertEquals(THREADS, p.getLargestPoolSize()); |
414 |
|
|
}}); |
415 |
|
|
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
416 |
|
|
assertEquals(THREADS, p.getLargestPoolSize()); |
417 |
|
|
} finally { |
418 |
|
|
done.countDown(); |
419 |
|
|
joinPool(p); |
420 |
|
|
assertEquals(THREADS, p.getLargestPoolSize()); |
421 |
|
|
} |
422 |
dl |
1.1 |
} |
423 |
jsr166 |
1.2 |
|
424 |
dl |
1.1 |
/** |
425 |
jsr166 |
1.14 |
* getPoolSize increases, but doesn't overestimate, when threads |
426 |
|
|
* become active |
427 |
dl |
1.1 |
*/ |
428 |
jsr166 |
1.15 |
public void testGetPoolSize() throws InterruptedException { |
429 |
|
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
430 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
431 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
432 |
|
|
try { |
433 |
|
|
assertEquals(0, p.getPoolSize()); |
434 |
|
|
p.execute(new CheckedRunnable() { |
435 |
|
|
public void realRun() throws InterruptedException { |
436 |
|
|
threadStarted.countDown(); |
437 |
|
|
assertEquals(1, p.getPoolSize()); |
438 |
|
|
done.await(); |
439 |
|
|
}}); |
440 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
441 |
|
|
assertEquals(1, p.getPoolSize()); |
442 |
|
|
} finally { |
443 |
|
|
done.countDown(); |
444 |
|
|
joinPool(p); |
445 |
|
|
} |
446 |
dl |
1.1 |
} |
447 |
jsr166 |
1.2 |
|
448 |
dl |
1.1 |
/** |
449 |
jsr166 |
1.14 |
* getTaskCount increases, but doesn't overestimate, when tasks |
450 |
|
|
* submitted |
451 |
dl |
1.1 |
*/ |
452 |
jsr166 |
1.6 |
public void testGetTaskCount() throws InterruptedException { |
453 |
jsr166 |
1.15 |
final ThreadPoolExecutor p = new CustomExecutor(1); |
454 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
455 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
456 |
|
|
final int TASKS = 5; |
457 |
|
|
try { |
458 |
|
|
assertEquals(0, p.getTaskCount()); |
459 |
|
|
for (int i = 0; i < TASKS; i++) |
460 |
|
|
p.execute(new CheckedRunnable() { |
461 |
|
|
public void realRun() throws InterruptedException { |
462 |
|
|
threadStarted.countDown(); |
463 |
|
|
done.await(); |
464 |
|
|
}}); |
465 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
466 |
|
|
assertEquals(TASKS, p.getTaskCount()); |
467 |
|
|
} finally { |
468 |
|
|
done.countDown(); |
469 |
|
|
joinPool(p); |
470 |
|
|
} |
471 |
dl |
1.1 |
} |
472 |
|
|
|
473 |
jsr166 |
1.2 |
/** |
474 |
dl |
1.1 |
* getThreadFactory returns factory in constructor if not set |
475 |
|
|
*/ |
476 |
|
|
public void testGetThreadFactory() { |
477 |
|
|
ThreadFactory tf = new SimpleThreadFactory(); |
478 |
jsr166 |
1.7 |
CustomExecutor p = new CustomExecutor(1, tf); |
479 |
dl |
1.1 |
assertSame(tf, p.getThreadFactory()); |
480 |
|
|
joinPool(p); |
481 |
|
|
} |
482 |
|
|
|
483 |
jsr166 |
1.2 |
/** |
484 |
dl |
1.1 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
485 |
|
|
*/ |
486 |
|
|
public void testSetThreadFactory() { |
487 |
|
|
ThreadFactory tf = new SimpleThreadFactory(); |
488 |
jsr166 |
1.7 |
CustomExecutor p = new CustomExecutor(1); |
489 |
dl |
1.1 |
p.setThreadFactory(tf); |
490 |
|
|
assertSame(tf, p.getThreadFactory()); |
491 |
|
|
joinPool(p); |
492 |
|
|
} |
493 |
|
|
|
494 |
jsr166 |
1.2 |
/** |
495 |
dl |
1.1 |
* setThreadFactory(null) throws NPE |
496 |
|
|
*/ |
497 |
|
|
public void testSetThreadFactoryNull() { |
498 |
jsr166 |
1.7 |
CustomExecutor p = new CustomExecutor(1); |
499 |
dl |
1.1 |
try { |
500 |
|
|
p.setThreadFactory(null); |
501 |
|
|
shouldThrow(); |
502 |
|
|
} catch (NullPointerException success) { |
503 |
|
|
} finally { |
504 |
|
|
joinPool(p); |
505 |
|
|
} |
506 |
|
|
} |
507 |
jsr166 |
1.2 |
|
508 |
dl |
1.1 |
/** |
509 |
jsr166 |
1.23 |
* isShutdown is false before shutdown, true after |
510 |
dl |
1.1 |
*/ |
511 |
|
|
public void testIsShutdown() { |
512 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
513 |
dl |
1.1 |
try { |
514 |
jsr166 |
1.15 |
assertFalse(p.isShutdown()); |
515 |
dl |
1.1 |
} |
516 |
|
|
finally { |
517 |
jsr166 |
1.15 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
518 |
dl |
1.1 |
} |
519 |
jsr166 |
1.15 |
assertTrue(p.isShutdown()); |
520 |
dl |
1.1 |
} |
521 |
|
|
|
522 |
|
|
/** |
523 |
jsr166 |
1.14 |
* isTerminated is false before termination, true after |
524 |
dl |
1.1 |
*/ |
525 |
jsr166 |
1.6 |
public void testIsTerminated() throws InterruptedException { |
526 |
jsr166 |
1.15 |
final ThreadPoolExecutor p = new CustomExecutor(1); |
527 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
528 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
529 |
|
|
assertFalse(p.isTerminated()); |
530 |
|
|
try { |
531 |
|
|
p.execute(new CheckedRunnable() { |
532 |
|
|
public void realRun() throws InterruptedException { |
533 |
jsr166 |
1.18 |
assertFalse(p.isTerminated()); |
534 |
jsr166 |
1.15 |
threadStarted.countDown(); |
535 |
|
|
done.await(); |
536 |
|
|
}}); |
537 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
538 |
jsr166 |
1.18 |
assertFalse(p.isTerminating()); |
539 |
jsr166 |
1.15 |
done.countDown(); |
540 |
dl |
1.1 |
} finally { |
541 |
jsr166 |
1.15 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
542 |
dl |
1.1 |
} |
543 |
jsr166 |
1.15 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
544 |
|
|
assertTrue(p.isTerminated()); |
545 |
dl |
1.1 |
} |
546 |
|
|
|
547 |
|
|
/** |
548 |
jsr166 |
1.14 |
* isTerminating is not true when running or when terminated |
549 |
dl |
1.1 |
*/ |
550 |
jsr166 |
1.6 |
public void testIsTerminating() throws InterruptedException { |
551 |
jsr166 |
1.15 |
final ThreadPoolExecutor p = new CustomExecutor(1); |
552 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
553 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
554 |
|
|
try { |
555 |
|
|
assertFalse(p.isTerminating()); |
556 |
|
|
p.execute(new CheckedRunnable() { |
557 |
|
|
public void realRun() throws InterruptedException { |
558 |
jsr166 |
1.17 |
assertFalse(p.isTerminating()); |
559 |
jsr166 |
1.15 |
threadStarted.countDown(); |
560 |
|
|
done.await(); |
561 |
|
|
}}); |
562 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
563 |
|
|
assertFalse(p.isTerminating()); |
564 |
|
|
done.countDown(); |
565 |
|
|
} finally { |
566 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
567 |
|
|
} |
568 |
|
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
569 |
|
|
assertTrue(p.isTerminated()); |
570 |
|
|
assertFalse(p.isTerminating()); |
571 |
dl |
1.1 |
} |
572 |
|
|
|
573 |
|
|
/** |
574 |
|
|
* getQueue returns the work queue, which contains queued tasks |
575 |
|
|
*/ |
576 |
jsr166 |
1.6 |
public void testGetQueue() throws InterruptedException { |
577 |
jsr166 |
1.15 |
ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
578 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
579 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
580 |
|
|
try { |
581 |
|
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
582 |
|
|
for (int i = 0; i < tasks.length; i++) { |
583 |
|
|
Runnable r = new CheckedRunnable() { |
584 |
|
|
public void realRun() throws InterruptedException { |
585 |
|
|
threadStarted.countDown(); |
586 |
|
|
done.await(); |
587 |
|
|
}}; |
588 |
|
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
589 |
|
|
} |
590 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
591 |
|
|
BlockingQueue<Runnable> q = p.getQueue(); |
592 |
|
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
593 |
dl |
1.1 |
assertFalse(q.contains(tasks[0])); |
594 |
|
|
} finally { |
595 |
jsr166 |
1.15 |
done.countDown(); |
596 |
|
|
joinPool(p); |
597 |
dl |
1.1 |
} |
598 |
|
|
} |
599 |
|
|
|
600 |
|
|
/** |
601 |
|
|
* remove(task) removes queued task, and fails to remove active task |
602 |
|
|
*/ |
603 |
jsr166 |
1.6 |
public void testRemove() throws InterruptedException { |
604 |
jsr166 |
1.15 |
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
605 |
dl |
1.1 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
606 |
jsr166 |
1.15 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
607 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
608 |
dl |
1.1 |
try { |
609 |
jsr166 |
1.15 |
for (int i = 0; i < tasks.length; i++) { |
610 |
|
|
Runnable r = new CheckedRunnable() { |
611 |
|
|
public void realRun() throws InterruptedException { |
612 |
|
|
threadStarted.countDown(); |
613 |
|
|
done.await(); |
614 |
|
|
}}; |
615 |
|
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
616 |
|
|
} |
617 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
618 |
|
|
BlockingQueue<Runnable> q = p.getQueue(); |
619 |
|
|
assertFalse(p.remove((Runnable)tasks[0])); |
620 |
dl |
1.1 |
assertTrue(q.contains((Runnable)tasks[4])); |
621 |
|
|
assertTrue(q.contains((Runnable)tasks[3])); |
622 |
jsr166 |
1.15 |
assertTrue(p.remove((Runnable)tasks[4])); |
623 |
|
|
assertFalse(p.remove((Runnable)tasks[4])); |
624 |
dl |
1.1 |
assertFalse(q.contains((Runnable)tasks[4])); |
625 |
|
|
assertTrue(q.contains((Runnable)tasks[3])); |
626 |
jsr166 |
1.15 |
assertTrue(p.remove((Runnable)tasks[3])); |
627 |
dl |
1.1 |
assertFalse(q.contains((Runnable)tasks[3])); |
628 |
|
|
} finally { |
629 |
jsr166 |
1.15 |
done.countDown(); |
630 |
|
|
joinPool(p); |
631 |
dl |
1.1 |
} |
632 |
|
|
} |
633 |
|
|
|
634 |
|
|
/** |
635 |
jsr166 |
1.14 |
* purge removes cancelled tasks from the queue |
636 |
dl |
1.1 |
*/ |
637 |
jsr166 |
1.6 |
public void testPurge() throws InterruptedException { |
638 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
639 |
dl |
1.1 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
640 |
jsr166 |
1.22 |
for (int i = 0; i < tasks.length; i++) |
641 |
|
|
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
642 |
|
|
LONG_DELAY_MS, MILLISECONDS); |
643 |
dl |
1.1 |
try { |
644 |
jsr166 |
1.15 |
int max = tasks.length; |
645 |
dl |
1.1 |
if (tasks[4].cancel(true)) --max; |
646 |
|
|
if (tasks[3].cancel(true)) --max; |
647 |
|
|
// There must eventually be an interference-free point at |
648 |
|
|
// which purge will not fail. (At worst, when queue is empty.) |
649 |
jsr166 |
1.22 |
long startTime = System.nanoTime(); |
650 |
|
|
do { |
651 |
jsr166 |
1.15 |
p.purge(); |
652 |
|
|
long count = p.getTaskCount(); |
653 |
jsr166 |
1.22 |
if (count == max) |
654 |
|
|
return; |
655 |
|
|
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
656 |
|
|
fail("Purge failed to remove cancelled tasks"); |
657 |
dl |
1.1 |
} finally { |
658 |
jsr166 |
1.15 |
for (ScheduledFuture task : tasks) |
659 |
|
|
task.cancel(true); |
660 |
|
|
joinPool(p); |
661 |
dl |
1.1 |
} |
662 |
|
|
} |
663 |
|
|
|
664 |
|
|
/** |
665 |
jsr166 |
1.23 |
* shutdownNow returns a list containing tasks that were not run |
666 |
dl |
1.1 |
*/ |
667 |
jsr166 |
1.23 |
public void testShutdownNow() { |
668 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
669 |
jsr166 |
1.3 |
for (int i = 0; i < 5; i++) |
670 |
jsr166 |
1.22 |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
671 |
|
|
LONG_DELAY_MS, MILLISECONDS); |
672 |
dl |
1.1 |
try { |
673 |
jsr166 |
1.22 |
List<Runnable> l = p.shutdownNow(); |
674 |
|
|
assertTrue(p.isShutdown()); |
675 |
|
|
assertEquals(5, l.size()); |
676 |
jsr166 |
1.2 |
} catch (SecurityException ok) { |
677 |
jsr166 |
1.22 |
// Allowed in case test doesn't have privs |
678 |
|
|
} finally { |
679 |
|
|
joinPool(p); |
680 |
dl |
1.1 |
} |
681 |
|
|
} |
682 |
|
|
|
683 |
|
|
/** |
684 |
|
|
* In default setting, shutdown cancels periodic but not delayed |
685 |
|
|
* tasks at shutdown |
686 |
|
|
*/ |
687 |
jsr166 |
1.23 |
public void testShutdown1() throws InterruptedException { |
688 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
689 |
|
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
690 |
|
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
691 |
jsr166 |
1.2 |
|
692 |
jsr166 |
1.6 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
693 |
jsr166 |
1.15 |
for (int i = 0; i < tasks.length; i++) |
694 |
|
|
tasks[i] = p.schedule(new NoOpRunnable(), |
695 |
|
|
SHORT_DELAY_MS, MILLISECONDS); |
696 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
697 |
|
|
BlockingQueue<Runnable> q = p.getQueue(); |
698 |
|
|
for (ScheduledFuture task : tasks) { |
699 |
|
|
assertFalse(task.isDone()); |
700 |
|
|
assertFalse(task.isCancelled()); |
701 |
|
|
assertTrue(q.contains(task)); |
702 |
jsr166 |
1.6 |
} |
703 |
jsr166 |
1.15 |
assertTrue(p.isShutdown()); |
704 |
|
|
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
705 |
|
|
assertTrue(p.isTerminated()); |
706 |
|
|
for (ScheduledFuture task : tasks) { |
707 |
|
|
assertTrue(task.isDone()); |
708 |
|
|
assertFalse(task.isCancelled()); |
709 |
dl |
1.1 |
} |
710 |
|
|
} |
711 |
|
|
|
712 |
|
|
/** |
713 |
|
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
714 |
|
|
* delayed tasks are cancelled at shutdown |
715 |
|
|
*/ |
716 |
jsr166 |
1.23 |
public void testShutdown2() throws InterruptedException { |
717 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
718 |
|
|
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
719 |
jsr166 |
1.16 |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
720 |
|
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
721 |
jsr166 |
1.6 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
722 |
jsr166 |
1.15 |
for (int i = 0; i < tasks.length; i++) |
723 |
|
|
tasks[i] = p.schedule(new NoOpRunnable(), |
724 |
|
|
SHORT_DELAY_MS, MILLISECONDS); |
725 |
|
|
BlockingQueue q = p.getQueue(); |
726 |
|
|
assertEquals(tasks.length, q.size()); |
727 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
728 |
|
|
assertTrue(p.isShutdown()); |
729 |
jsr166 |
1.6 |
assertTrue(q.isEmpty()); |
730 |
jsr166 |
1.15 |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
731 |
|
|
assertTrue(p.isTerminated()); |
732 |
|
|
for (ScheduledFuture task : tasks) { |
733 |
|
|
assertTrue(task.isDone()); |
734 |
|
|
assertTrue(task.isCancelled()); |
735 |
|
|
} |
736 |
dl |
1.1 |
} |
737 |
|
|
|
738 |
|
|
/** |
739 |
|
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
740 |
jsr166 |
1.15 |
* periodic tasks are cancelled at shutdown |
741 |
dl |
1.1 |
*/ |
742 |
jsr166 |
1.23 |
public void testShutdown3() throws InterruptedException { |
743 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
744 |
jsr166 |
1.16 |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
745 |
|
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
746 |
jsr166 |
1.15 |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
747 |
jsr166 |
1.16 |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
748 |
|
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
749 |
jsr166 |
1.24 |
long initialDelay = LONG_DELAY_MS; |
750 |
jsr166 |
1.6 |
ScheduledFuture task = |
751 |
jsr166 |
1.24 |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
752 |
|
|
5, MILLISECONDS); |
753 |
jsr166 |
1.15 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
754 |
|
|
assertTrue(p.isShutdown()); |
755 |
|
|
assertTrue(p.getQueue().isEmpty()); |
756 |
|
|
assertTrue(task.isDone()); |
757 |
|
|
assertTrue(task.isCancelled()); |
758 |
jsr166 |
1.24 |
joinPool(p); |
759 |
dl |
1.1 |
} |
760 |
|
|
|
761 |
|
|
/** |
762 |
|
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
763 |
jsr166 |
1.15 |
* periodic tasks are not cancelled at shutdown |
764 |
dl |
1.1 |
*/ |
765 |
jsr166 |
1.23 |
public void testShutdown4() throws InterruptedException { |
766 |
jsr166 |
1.15 |
CustomExecutor p = new CustomExecutor(1); |
767 |
|
|
final CountDownLatch counter = new CountDownLatch(2); |
768 |
dl |
1.1 |
try { |
769 |
jsr166 |
1.16 |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
770 |
|
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
771 |
|
|
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
772 |
jsr166 |
1.15 |
final Runnable r = new CheckedRunnable() { |
773 |
|
|
public void realRun() { |
774 |
|
|
counter.countDown(); |
775 |
|
|
}}; |
776 |
dl |
1.1 |
ScheduledFuture task = |
777 |
jsr166 |
1.15 |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
778 |
|
|
assertFalse(task.isDone()); |
779 |
dl |
1.1 |
assertFalse(task.isCancelled()); |
780 |
jsr166 |
1.15 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
781 |
dl |
1.1 |
assertFalse(task.isCancelled()); |
782 |
jsr166 |
1.15 |
assertFalse(p.isTerminated()); |
783 |
|
|
assertTrue(p.isShutdown()); |
784 |
|
|
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
785 |
dl |
1.1 |
assertFalse(task.isCancelled()); |
786 |
jsr166 |
1.15 |
assertTrue(task.cancel(false)); |
787 |
dl |
1.1 |
assertTrue(task.isDone()); |
788 |
jsr166 |
1.15 |
assertTrue(task.isCancelled()); |
789 |
|
|
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
790 |
|
|
assertTrue(p.isTerminated()); |
791 |
dl |
1.1 |
} |
792 |
jsr166 |
1.2 |
finally { |
793 |
jsr166 |
1.15 |
joinPool(p); |
794 |
dl |
1.1 |
} |
795 |
|
|
} |
796 |
|
|
|
797 |
|
|
/** |
798 |
|
|
* completed submit of callable returns result |
799 |
|
|
*/ |
800 |
jsr166 |
1.6 |
public void testSubmitCallable() throws Exception { |
801 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
802 |
|
|
try { |
803 |
|
|
Future<String> future = e.submit(new StringTask()); |
804 |
|
|
String result = future.get(); |
805 |
|
|
assertSame(TEST_STRING, result); |
806 |
|
|
} finally { |
807 |
|
|
joinPool(e); |
808 |
|
|
} |
809 |
|
|
} |
810 |
|
|
|
811 |
|
|
/** |
812 |
|
|
* completed submit of runnable returns successfully |
813 |
|
|
*/ |
814 |
jsr166 |
1.6 |
public void testSubmitRunnable() throws Exception { |
815 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
816 |
|
|
try { |
817 |
|
|
Future<?> future = e.submit(new NoOpRunnable()); |
818 |
|
|
future.get(); |
819 |
|
|
assertTrue(future.isDone()); |
820 |
|
|
} finally { |
821 |
|
|
joinPool(e); |
822 |
|
|
} |
823 |
|
|
} |
824 |
|
|
|
825 |
|
|
/** |
826 |
|
|
* completed submit of (runnable, result) returns result |
827 |
|
|
*/ |
828 |
jsr166 |
1.6 |
public void testSubmitRunnable2() throws Exception { |
829 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
830 |
|
|
try { |
831 |
|
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
832 |
|
|
String result = future.get(); |
833 |
|
|
assertSame(TEST_STRING, result); |
834 |
|
|
} finally { |
835 |
|
|
joinPool(e); |
836 |
|
|
} |
837 |
|
|
} |
838 |
|
|
|
839 |
|
|
/** |
840 |
|
|
* invokeAny(null) throws NPE |
841 |
|
|
*/ |
842 |
jsr166 |
1.6 |
public void testInvokeAny1() throws Exception { |
843 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
844 |
|
|
try { |
845 |
|
|
e.invokeAny(null); |
846 |
jsr166 |
1.6 |
shouldThrow(); |
847 |
dl |
1.1 |
} catch (NullPointerException success) { |
848 |
|
|
} finally { |
849 |
|
|
joinPool(e); |
850 |
|
|
} |
851 |
|
|
} |
852 |
|
|
|
853 |
|
|
/** |
854 |
|
|
* invokeAny(empty collection) throws IAE |
855 |
|
|
*/ |
856 |
jsr166 |
1.6 |
public void testInvokeAny2() throws Exception { |
857 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
858 |
|
|
try { |
859 |
|
|
e.invokeAny(new ArrayList<Callable<String>>()); |
860 |
jsr166 |
1.6 |
shouldThrow(); |
861 |
dl |
1.1 |
} catch (IllegalArgumentException success) { |
862 |
|
|
} finally { |
863 |
|
|
joinPool(e); |
864 |
|
|
} |
865 |
|
|
} |
866 |
|
|
|
867 |
|
|
/** |
868 |
|
|
* invokeAny(c) throws NPE if c has null elements |
869 |
|
|
*/ |
870 |
jsr166 |
1.6 |
public void testInvokeAny3() throws Exception { |
871 |
jsr166 |
1.11 |
CountDownLatch latch = new CountDownLatch(1); |
872 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
873 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
874 |
|
|
l.add(latchAwaitingStringTask(latch)); |
875 |
|
|
l.add(null); |
876 |
dl |
1.1 |
try { |
877 |
|
|
e.invokeAny(l); |
878 |
jsr166 |
1.6 |
shouldThrow(); |
879 |
dl |
1.1 |
} catch (NullPointerException success) { |
880 |
|
|
} finally { |
881 |
jsr166 |
1.6 |
latch.countDown(); |
882 |
dl |
1.1 |
joinPool(e); |
883 |
|
|
} |
884 |
|
|
} |
885 |
|
|
|
886 |
|
|
/** |
887 |
|
|
* invokeAny(c) throws ExecutionException if no task completes |
888 |
|
|
*/ |
889 |
jsr166 |
1.6 |
public void testInvokeAny4() throws Exception { |
890 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
891 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
892 |
|
|
l.add(new NPETask()); |
893 |
dl |
1.1 |
try { |
894 |
|
|
e.invokeAny(l); |
895 |
jsr166 |
1.6 |
shouldThrow(); |
896 |
dl |
1.1 |
} catch (ExecutionException success) { |
897 |
jsr166 |
1.6 |
assertTrue(success.getCause() instanceof NullPointerException); |
898 |
dl |
1.1 |
} finally { |
899 |
|
|
joinPool(e); |
900 |
|
|
} |
901 |
|
|
} |
902 |
|
|
|
903 |
|
|
/** |
904 |
|
|
* invokeAny(c) returns result of some task |
905 |
|
|
*/ |
906 |
jsr166 |
1.6 |
public void testInvokeAny5() throws Exception { |
907 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
908 |
|
|
try { |
909 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
910 |
dl |
1.1 |
l.add(new StringTask()); |
911 |
|
|
l.add(new StringTask()); |
912 |
|
|
String result = e.invokeAny(l); |
913 |
|
|
assertSame(TEST_STRING, result); |
914 |
|
|
} finally { |
915 |
|
|
joinPool(e); |
916 |
|
|
} |
917 |
|
|
} |
918 |
|
|
|
919 |
|
|
/** |
920 |
|
|
* invokeAll(null) throws NPE |
921 |
|
|
*/ |
922 |
jsr166 |
1.6 |
public void testInvokeAll1() throws Exception { |
923 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
924 |
|
|
try { |
925 |
|
|
e.invokeAll(null); |
926 |
jsr166 |
1.6 |
shouldThrow(); |
927 |
dl |
1.1 |
} catch (NullPointerException success) { |
928 |
|
|
} finally { |
929 |
|
|
joinPool(e); |
930 |
|
|
} |
931 |
|
|
} |
932 |
|
|
|
933 |
|
|
/** |
934 |
|
|
* invokeAll(empty collection) returns empty collection |
935 |
|
|
*/ |
936 |
jsr166 |
1.6 |
public void testInvokeAll2() throws Exception { |
937 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
938 |
|
|
try { |
939 |
|
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
940 |
|
|
assertTrue(r.isEmpty()); |
941 |
|
|
} finally { |
942 |
|
|
joinPool(e); |
943 |
|
|
} |
944 |
|
|
} |
945 |
|
|
|
946 |
|
|
/** |
947 |
|
|
* invokeAll(c) throws NPE if c has null elements |
948 |
|
|
*/ |
949 |
jsr166 |
1.6 |
public void testInvokeAll3() throws Exception { |
950 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
951 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
952 |
|
|
l.add(new StringTask()); |
953 |
|
|
l.add(null); |
954 |
dl |
1.1 |
try { |
955 |
|
|
e.invokeAll(l); |
956 |
jsr166 |
1.6 |
shouldThrow(); |
957 |
dl |
1.1 |
} catch (NullPointerException success) { |
958 |
|
|
} finally { |
959 |
|
|
joinPool(e); |
960 |
|
|
} |
961 |
|
|
} |
962 |
|
|
|
963 |
|
|
/** |
964 |
|
|
* get of invokeAll(c) throws exception on failed task |
965 |
|
|
*/ |
966 |
jsr166 |
1.6 |
public void testInvokeAll4() throws Exception { |
967 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
968 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
969 |
|
|
l.add(new NPETask()); |
970 |
|
|
List<Future<String>> futures = e.invokeAll(l); |
971 |
|
|
assertEquals(1, futures.size()); |
972 |
dl |
1.1 |
try { |
973 |
jsr166 |
1.11 |
futures.get(0).get(); |
974 |
jsr166 |
1.6 |
shouldThrow(); |
975 |
jsr166 |
1.3 |
} catch (ExecutionException success) { |
976 |
jsr166 |
1.6 |
assertTrue(success.getCause() instanceof NullPointerException); |
977 |
dl |
1.1 |
} finally { |
978 |
|
|
joinPool(e); |
979 |
|
|
} |
980 |
|
|
} |
981 |
|
|
|
982 |
|
|
/** |
983 |
|
|
* invokeAll(c) returns results of all completed tasks |
984 |
|
|
*/ |
985 |
jsr166 |
1.6 |
public void testInvokeAll5() throws Exception { |
986 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
987 |
|
|
try { |
988 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
989 |
dl |
1.1 |
l.add(new StringTask()); |
990 |
|
|
l.add(new StringTask()); |
991 |
jsr166 |
1.11 |
List<Future<String>> futures = e.invokeAll(l); |
992 |
|
|
assertEquals(2, futures.size()); |
993 |
|
|
for (Future<String> future : futures) |
994 |
jsr166 |
1.6 |
assertSame(TEST_STRING, future.get()); |
995 |
dl |
1.1 |
} finally { |
996 |
|
|
joinPool(e); |
997 |
|
|
} |
998 |
|
|
} |
999 |
|
|
|
1000 |
|
|
/** |
1001 |
|
|
* timed invokeAny(null) throws NPE |
1002 |
|
|
*/ |
1003 |
jsr166 |
1.6 |
public void testTimedInvokeAny1() throws Exception { |
1004 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1005 |
|
|
try { |
1006 |
jsr166 |
1.6 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1007 |
|
|
shouldThrow(); |
1008 |
dl |
1.1 |
} catch (NullPointerException success) { |
1009 |
|
|
} finally { |
1010 |
|
|
joinPool(e); |
1011 |
|
|
} |
1012 |
|
|
} |
1013 |
|
|
|
1014 |
|
|
/** |
1015 |
|
|
* timed invokeAny(,,null) throws NPE |
1016 |
|
|
*/ |
1017 |
jsr166 |
1.6 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1018 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1019 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1020 |
|
|
l.add(new StringTask()); |
1021 |
dl |
1.1 |
try { |
1022 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1023 |
jsr166 |
1.6 |
shouldThrow(); |
1024 |
dl |
1.1 |
} catch (NullPointerException success) { |
1025 |
|
|
} finally { |
1026 |
|
|
joinPool(e); |
1027 |
|
|
} |
1028 |
|
|
} |
1029 |
|
|
|
1030 |
|
|
/** |
1031 |
|
|
* timed invokeAny(empty collection) throws IAE |
1032 |
|
|
*/ |
1033 |
jsr166 |
1.6 |
public void testTimedInvokeAny2() throws Exception { |
1034 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1035 |
|
|
try { |
1036 |
jsr166 |
1.6 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1037 |
|
|
shouldThrow(); |
1038 |
dl |
1.1 |
} catch (IllegalArgumentException success) { |
1039 |
|
|
} finally { |
1040 |
|
|
joinPool(e); |
1041 |
|
|
} |
1042 |
|
|
} |
1043 |
|
|
|
1044 |
|
|
/** |
1045 |
|
|
* timed invokeAny(c) throws NPE if c has null elements |
1046 |
|
|
*/ |
1047 |
jsr166 |
1.6 |
public void testTimedInvokeAny3() throws Exception { |
1048 |
jsr166 |
1.11 |
CountDownLatch latch = new CountDownLatch(1); |
1049 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1050 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1051 |
|
|
l.add(latchAwaitingStringTask(latch)); |
1052 |
|
|
l.add(null); |
1053 |
dl |
1.1 |
try { |
1054 |
jsr166 |
1.6 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1055 |
|
|
shouldThrow(); |
1056 |
dl |
1.1 |
} catch (NullPointerException success) { |
1057 |
|
|
} finally { |
1058 |
jsr166 |
1.6 |
latch.countDown(); |
1059 |
dl |
1.1 |
joinPool(e); |
1060 |
|
|
} |
1061 |
|
|
} |
1062 |
|
|
|
1063 |
|
|
/** |
1064 |
|
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1065 |
|
|
*/ |
1066 |
jsr166 |
1.6 |
public void testTimedInvokeAny4() throws Exception { |
1067 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1068 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1069 |
|
|
l.add(new NPETask()); |
1070 |
dl |
1.1 |
try { |
1071 |
jsr166 |
1.6 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1072 |
|
|
shouldThrow(); |
1073 |
jsr166 |
1.3 |
} catch (ExecutionException success) { |
1074 |
jsr166 |
1.6 |
assertTrue(success.getCause() instanceof NullPointerException); |
1075 |
dl |
1.1 |
} finally { |
1076 |
|
|
joinPool(e); |
1077 |
|
|
} |
1078 |
|
|
} |
1079 |
|
|
|
1080 |
|
|
/** |
1081 |
|
|
* timed invokeAny(c) returns result of some task |
1082 |
|
|
*/ |
1083 |
jsr166 |
1.6 |
public void testTimedInvokeAny5() throws Exception { |
1084 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1085 |
|
|
try { |
1086 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1087 |
dl |
1.1 |
l.add(new StringTask()); |
1088 |
|
|
l.add(new StringTask()); |
1089 |
jsr166 |
1.6 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1090 |
dl |
1.1 |
assertSame(TEST_STRING, result); |
1091 |
|
|
} finally { |
1092 |
|
|
joinPool(e); |
1093 |
|
|
} |
1094 |
|
|
} |
1095 |
|
|
|
1096 |
|
|
/** |
1097 |
|
|
* timed invokeAll(null) throws NPE |
1098 |
|
|
*/ |
1099 |
jsr166 |
1.6 |
public void testTimedInvokeAll1() throws Exception { |
1100 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1101 |
|
|
try { |
1102 |
jsr166 |
1.6 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1103 |
|
|
shouldThrow(); |
1104 |
dl |
1.1 |
} catch (NullPointerException success) { |
1105 |
|
|
} finally { |
1106 |
|
|
joinPool(e); |
1107 |
|
|
} |
1108 |
|
|
} |
1109 |
|
|
|
1110 |
|
|
/** |
1111 |
|
|
* timed invokeAll(,,null) throws NPE |
1112 |
|
|
*/ |
1113 |
jsr166 |
1.6 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1114 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1115 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1116 |
|
|
l.add(new StringTask()); |
1117 |
dl |
1.1 |
try { |
1118 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1119 |
jsr166 |
1.6 |
shouldThrow(); |
1120 |
dl |
1.1 |
} catch (NullPointerException success) { |
1121 |
|
|
} finally { |
1122 |
|
|
joinPool(e); |
1123 |
|
|
} |
1124 |
|
|
} |
1125 |
|
|
|
1126 |
|
|
/** |
1127 |
|
|
* timed invokeAll(empty collection) returns empty collection |
1128 |
|
|
*/ |
1129 |
jsr166 |
1.6 |
public void testTimedInvokeAll2() throws Exception { |
1130 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1131 |
|
|
try { |
1132 |
jsr166 |
1.6 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1133 |
dl |
1.1 |
assertTrue(r.isEmpty()); |
1134 |
|
|
} finally { |
1135 |
|
|
joinPool(e); |
1136 |
|
|
} |
1137 |
|
|
} |
1138 |
|
|
|
1139 |
|
|
/** |
1140 |
|
|
* timed invokeAll(c) throws NPE if c has null elements |
1141 |
|
|
*/ |
1142 |
jsr166 |
1.6 |
public void testTimedInvokeAll3() throws Exception { |
1143 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1144 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1145 |
|
|
l.add(new StringTask()); |
1146 |
|
|
l.add(null); |
1147 |
dl |
1.1 |
try { |
1148 |
jsr166 |
1.6 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1149 |
|
|
shouldThrow(); |
1150 |
dl |
1.1 |
} catch (NullPointerException success) { |
1151 |
|
|
} finally { |
1152 |
|
|
joinPool(e); |
1153 |
|
|
} |
1154 |
|
|
} |
1155 |
|
|
|
1156 |
|
|
/** |
1157 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1158 |
|
|
*/ |
1159 |
jsr166 |
1.6 |
public void testTimedInvokeAll4() throws Exception { |
1160 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1161 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1162 |
|
|
l.add(new NPETask()); |
1163 |
|
|
List<Future<String>> futures = |
1164 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1165 |
|
|
assertEquals(1, futures.size()); |
1166 |
dl |
1.1 |
try { |
1167 |
jsr166 |
1.11 |
futures.get(0).get(); |
1168 |
jsr166 |
1.6 |
shouldThrow(); |
1169 |
jsr166 |
1.3 |
} catch (ExecutionException success) { |
1170 |
jsr166 |
1.6 |
assertTrue(success.getCause() instanceof NullPointerException); |
1171 |
dl |
1.1 |
} finally { |
1172 |
|
|
joinPool(e); |
1173 |
|
|
} |
1174 |
|
|
} |
1175 |
|
|
|
1176 |
|
|
/** |
1177 |
|
|
* timed invokeAll(c) returns results of all completed tasks |
1178 |
|
|
*/ |
1179 |
jsr166 |
1.6 |
public void testTimedInvokeAll5() throws Exception { |
1180 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1181 |
|
|
try { |
1182 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1183 |
dl |
1.1 |
l.add(new StringTask()); |
1184 |
|
|
l.add(new StringTask()); |
1185 |
jsr166 |
1.11 |
List<Future<String>> futures = |
1186 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1187 |
|
|
assertEquals(2, futures.size()); |
1188 |
|
|
for (Future<String> future : futures) |
1189 |
jsr166 |
1.6 |
assertSame(TEST_STRING, future.get()); |
1190 |
dl |
1.1 |
} finally { |
1191 |
|
|
joinPool(e); |
1192 |
|
|
} |
1193 |
|
|
} |
1194 |
|
|
|
1195 |
|
|
/** |
1196 |
|
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1197 |
|
|
*/ |
1198 |
jsr166 |
1.6 |
public void testTimedInvokeAll6() throws Exception { |
1199 |
dl |
1.1 |
ExecutorService e = new CustomExecutor(2); |
1200 |
|
|
try { |
1201 |
jsr166 |
1.11 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1202 |
dl |
1.1 |
l.add(new StringTask()); |
1203 |
|
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1204 |
|
|
l.add(new StringTask()); |
1205 |
jsr166 |
1.11 |
List<Future<String>> futures = |
1206 |
|
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1207 |
jsr166 |
1.27 |
assertEquals(l.size(), futures.size()); |
1208 |
|
|
for (Future future : futures) |
1209 |
|
|
assertTrue(future.isDone()); |
1210 |
|
|
assertFalse(futures.get(0).isCancelled()); |
1211 |
|
|
assertTrue(futures.get(1).isCancelled()); |
1212 |
dl |
1.1 |
} finally { |
1213 |
|
|
joinPool(e); |
1214 |
|
|
} |
1215 |
|
|
} |
1216 |
|
|
|
1217 |
|
|
} |