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