2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
13 |
|
|
14 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(ScheduledExecutorTest.class); |
43 |
|
catch(Exception e){ |
44 |
|
unexpectedException(); |
45 |
|
} |
46 |
< |
|
46 |
> |
|
47 |
|
} |
48 |
|
|
49 |
|
|
86 |
|
unexpectedException(); |
87 |
|
} |
88 |
|
} |
89 |
< |
|
89 |
> |
|
90 |
|
/** |
91 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
92 |
|
*/ |
127 |
|
unexpectedException(); |
128 |
|
} |
129 |
|
} |
130 |
< |
|
130 |
> |
|
131 |
|
/** |
132 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
133 |
|
*/ |
135 |
|
try { |
136 |
|
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
137 |
|
RunnableCounter counter = new RunnableCounter(); |
138 |
< |
ScheduledFuture h = |
138 |
> |
ScheduledFuture h = |
139 |
|
p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS); |
140 |
|
Thread.sleep(SMALL_DELAY_MS); |
141 |
|
h.cancel(true); |
157 |
|
try { |
158 |
|
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
159 |
|
RunnableCounter counter = new RunnableCounter(); |
160 |
< |
ScheduledFuture h = |
160 |
> |
ScheduledFuture h = |
161 |
|
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); |
162 |
|
Thread.sleep(SMALL_DELAY_MS); |
163 |
|
h.cancel(true); |
184 |
|
catch(Exception e){ |
185 |
|
unexpectedException(); |
186 |
|
} |
187 |
< |
|
187 |
> |
|
188 |
|
joinPool(se); |
189 |
|
} |
190 |
|
|
203 |
|
} |
204 |
|
joinPool(se); |
205 |
|
} |
206 |
< |
|
206 |
> |
|
207 |
|
/** |
208 |
|
* execute throws RejectedExecutionException if shutdown |
209 |
|
*/ |
217 |
|
} catch(RejectedExecutionException success){ |
218 |
|
} catch (SecurityException ok) { |
219 |
|
} |
220 |
< |
|
220 |
> |
|
221 |
|
joinPool(se); |
222 |
|
|
223 |
|
} |
266 |
|
shouldThrow(); |
267 |
|
} catch(RejectedExecutionException success){ |
268 |
|
} catch (SecurityException ok) { |
269 |
< |
} |
269 |
> |
} |
270 |
|
joinPool(se); |
271 |
|
} |
272 |
< |
|
272 |
> |
|
273 |
|
/** |
274 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
275 |
|
*/ |
282 |
|
shouldThrow(); |
283 |
|
} catch(RejectedExecutionException success){ |
284 |
|
} catch (SecurityException ok) { |
285 |
< |
} |
285 |
> |
} |
286 |
|
joinPool(se); |
287 |
|
} |
288 |
|
|
302 |
|
assertEquals(1, p2.getActiveCount()); |
303 |
|
joinPool(p2); |
304 |
|
} |
305 |
< |
|
305 |
> |
|
306 |
|
/** |
307 |
|
* getCompletedTaskCount increases, but doesn't overestimate, |
308 |
|
* when tasks complete |
319 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
320 |
|
joinPool(p2); |
321 |
|
} |
322 |
< |
|
322 |
> |
|
323 |
|
/** |
324 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
324 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
325 |
|
*/ |
326 |
|
public void testGetCorePoolSize() { |
327 |
|
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
328 |
|
assertEquals(1, p1.getCorePoolSize()); |
329 |
|
joinPool(p1); |
330 |
|
} |
331 |
< |
|
331 |
> |
|
332 |
|
/** |
333 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
334 |
|
* multiple threads active |
346 |
|
assertEquals(2, p2.getLargestPoolSize()); |
347 |
|
joinPool(p2); |
348 |
|
} |
349 |
< |
|
349 |
> |
|
350 |
|
/** |
351 |
|
* getPoolSize increases, but doesn't overestimate, when threads |
352 |
|
* become active |
358 |
|
assertEquals(1, p1.getPoolSize()); |
359 |
|
joinPool(p1); |
360 |
|
} |
361 |
< |
|
361 |
> |
|
362 |
|
/** |
363 |
|
* getTaskCount increases, but doesn't overestimate, when tasks |
364 |
|
* submitted |
377 |
|
joinPool(p1); |
378 |
|
} |
379 |
|
|
380 |
< |
/** |
380 |
> |
/** |
381 |
|
* getThreadFactory returns factory in constructor if not set |
382 |
|
*/ |
383 |
|
public void testGetThreadFactory() { |
387 |
|
joinPool(p); |
388 |
|
} |
389 |
|
|
390 |
< |
/** |
390 |
> |
/** |
391 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
392 |
|
*/ |
393 |
|
public void testSetThreadFactory() { |
398 |
|
joinPool(p); |
399 |
|
} |
400 |
|
|
401 |
< |
/** |
401 |
> |
/** |
402 |
|
* setThreadFactory(null) throws NPE |
403 |
|
*/ |
404 |
|
public void testSetThreadFactoryNull() { |
411 |
|
joinPool(p); |
412 |
|
} |
413 |
|
} |
414 |
< |
|
414 |
> |
|
415 |
|
/** |
416 |
|
* is isShutDown is false before shutdown, true after |
417 |
|
*/ |
418 |
|
public void testIsShutdown() { |
419 |
< |
|
419 |
> |
|
420 |
|
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
421 |
|
try { |
422 |
|
assertFalse(p1.isShutdown()); |
427 |
|
assertTrue(p1.isShutdown()); |
428 |
|
} |
429 |
|
|
430 |
< |
|
430 |
> |
|
431 |
|
/** |
432 |
|
* isTerminated is false before termination, true after |
433 |
|
*/ |
443 |
|
assertTrue(p1.isTerminated()); |
444 |
|
} catch(Exception e){ |
445 |
|
unexpectedException(); |
446 |
< |
} |
446 |
> |
} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
464 |
|
assertFalse(p1.isTerminating()); |
465 |
|
} catch(Exception e){ |
466 |
|
unexpectedException(); |
467 |
< |
} |
467 |
> |
} |
468 |
|
} |
469 |
|
|
470 |
|
/** |
557 |
|
List l; |
558 |
|
try { |
559 |
|
l = p1.shutdownNow(); |
560 |
< |
} catch (SecurityException ok) { |
560 |
> |
} catch (SecurityException ok) { |
561 |
|
return; |
562 |
|
} |
563 |
|
assertTrue(p1.isShutdown()); |
590 |
|
assertTrue(tasks[i].isDone()); |
591 |
|
assertFalse(tasks[i].isCancelled()); |
592 |
|
} |
593 |
< |
|
593 |
> |
|
594 |
|
} |
595 |
|
catch(Exception ex) { |
596 |
|
unexpectedException(); |
669 |
|
catch(Exception ex) { |
670 |
|
unexpectedException(); |
671 |
|
} |
672 |
< |
finally { |
672 |
> |
finally { |
673 |
|
joinPool(p1); |
674 |
|
} |
675 |
|
} |
876 |
|
l.add(new NPETask()); |
877 |
|
List<Future<String>> result = e.invokeAll(l); |
878 |
|
assertEquals(1, result.size()); |
879 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
879 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
880 |
|
it.next().get(); |
881 |
|
} catch(ExecutionException success) { |
882 |
|
} catch(Exception ex) { |
897 |
|
l.add(new StringTask()); |
898 |
|
List<Future<String>> result = e.invokeAll(l); |
899 |
|
assertEquals(2, result.size()); |
900 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
900 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
901 |
|
assertSame(TEST_STRING, it.next().get()); |
902 |
|
} catch (ExecutionException success) { |
903 |
|
} catch(Exception ex) { |
1084 |
|
l.add(new NPETask()); |
1085 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1086 |
|
assertEquals(1, result.size()); |
1087 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1087 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1088 |
|
it.next().get(); |
1089 |
|
} catch(ExecutionException success) { |
1090 |
|
} catch(Exception ex) { |
1105 |
|
l.add(new StringTask()); |
1106 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1107 |
|
assertEquals(2, result.size()); |
1108 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1108 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1109 |
|
assertSame(TEST_STRING, it.next().get()); |
1110 |
|
} catch (ExecutionException success) { |
1111 |
|
} catch(Exception ex) { |
1127 |
|
l.add(new StringTask()); |
1128 |
|
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1129 |
|
assertEquals(3, result.size()); |
1130 |
< |
Iterator<Future<String>> it = result.iterator(); |
1130 |
> |
Iterator<Future<String>> it = result.iterator(); |
1131 |
|
Future<String> f1 = it.next(); |
1132 |
|
Future<String> f2 = it.next(); |
1133 |
|
Future<String> f3 = it.next(); |