11 |
|
|
12 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
|
return new TestSuite(ScheduledExecutorTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
static class CustomTask<V> implements RunnableScheduledFuture<V> { |
20 |
> |
static class CustomTask<V> implements RunnableScheduledFuture<V> { |
21 |
|
RunnableScheduledFuture<V> task; |
22 |
|
volatile boolean ran; |
23 |
|
CustomTask(RunnableScheduledFuture<V> t) { task = t; } |
24 |
|
public boolean isPeriodic() { return task.isPeriodic(); } |
25 |
< |
public void run() { |
25 |
> |
public void run() { |
26 |
|
ran = true; |
27 |
< |
task.run(); |
27 |
> |
task.run(); |
28 |
|
} |
29 |
|
public long getDelay(TimeUnit unit) { return task.getDelay(unit); } |
30 |
|
public int compareTo(Delayed t) { |
31 |
< |
return task.compareTo(((CustomTask)t).task); |
31 |
> |
return task.compareTo(((CustomTask)t).task); |
32 |
|
} |
33 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
34 |
|
return task.cancel(mayInterruptIfRunning); |
46 |
|
return v; |
47 |
|
} |
48 |
|
} |
49 |
< |
|
49 |
> |
|
50 |
|
|
51 |
|
public class CustomExecutor extends ScheduledThreadPoolExecutor { |
52 |
|
|
65 |
|
CustomExecutor(int corePoolSize, ThreadFactory threadFactory) { |
66 |
|
super(corePoolSize, threadFactory); |
67 |
|
} |
68 |
< |
CustomExecutor(int corePoolSize, ThreadFactory threadFactory, |
68 |
> |
CustomExecutor(int corePoolSize, ThreadFactory threadFactory, |
69 |
|
RejectedExecutionHandler handler) { |
70 |
|
super(corePoolSize, threadFactory, handler); |
71 |
|
} |
72 |
< |
|
72 |
> |
|
73 |
|
} |
74 |
< |
|
74 |
> |
|
75 |
|
|
76 |
|
|
77 |
|
/** |
97 |
|
catch(Exception e){ |
98 |
|
unexpectedException(); |
99 |
|
} |
100 |
< |
|
100 |
> |
|
101 |
|
} |
102 |
|
|
103 |
|
|
140 |
|
unexpectedException(); |
141 |
|
} |
142 |
|
} |
143 |
< |
|
143 |
> |
|
144 |
|
/** |
145 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
146 |
|
*/ |
181 |
|
unexpectedException(); |
182 |
|
} |
183 |
|
} |
184 |
< |
|
184 |
> |
|
185 |
|
/** |
186 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
187 |
|
*/ |
189 |
|
try { |
190 |
|
CustomExecutor p1 = new CustomExecutor(1); |
191 |
|
RunnableCounter counter = new RunnableCounter(); |
192 |
< |
ScheduledFuture h = |
192 |
> |
ScheduledFuture h = |
193 |
|
p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS); |
194 |
|
Thread.sleep(SMALL_DELAY_MS); |
195 |
|
h.cancel(true); |
211 |
|
try { |
212 |
|
CustomExecutor p1 = new CustomExecutor(1); |
213 |
|
RunnableCounter counter = new RunnableCounter(); |
214 |
< |
ScheduledFuture h = |
214 |
> |
ScheduledFuture h = |
215 |
|
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); |
216 |
|
Thread.sleep(SMALL_DELAY_MS); |
217 |
|
h.cancel(true); |
238 |
|
catch(Exception e){ |
239 |
|
unexpectedException(); |
240 |
|
} |
241 |
< |
|
241 |
> |
|
242 |
|
joinPool(se); |
243 |
|
} |
244 |
|
|
257 |
|
} |
258 |
|
joinPool(se); |
259 |
|
} |
260 |
< |
|
260 |
> |
|
261 |
|
/** |
262 |
|
* execute throws RejectedExecutionException if shutdown |
263 |
|
*/ |
271 |
|
} catch(RejectedExecutionException success){ |
272 |
|
} catch (SecurityException ok) { |
273 |
|
} |
274 |
< |
|
274 |
> |
|
275 |
|
joinPool(se); |
276 |
|
|
277 |
|
} |
320 |
|
shouldThrow(); |
321 |
|
} catch(RejectedExecutionException success){ |
322 |
|
} catch (SecurityException ok) { |
323 |
< |
} |
323 |
> |
} |
324 |
|
joinPool(se); |
325 |
|
} |
326 |
< |
|
326 |
> |
|
327 |
|
/** |
328 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
329 |
|
*/ |
336 |
|
shouldThrow(); |
337 |
|
} catch(RejectedExecutionException success){ |
338 |
|
} catch (SecurityException ok) { |
339 |
< |
} |
339 |
> |
} |
340 |
|
joinPool(se); |
341 |
|
} |
342 |
|
|
356 |
|
assertEquals(1, p2.getActiveCount()); |
357 |
|
joinPool(p2); |
358 |
|
} |
359 |
< |
|
359 |
> |
|
360 |
|
/** |
361 |
|
* getCompletedTaskCount increases, but doesn't overestimate, |
362 |
|
* when tasks complete |
373 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
374 |
|
joinPool(p2); |
375 |
|
} |
376 |
< |
|
376 |
> |
|
377 |
|
/** |
378 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
378 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
379 |
|
*/ |
380 |
|
public void testGetCorePoolSize() { |
381 |
|
CustomExecutor p1 = new CustomExecutor(1); |
382 |
|
assertEquals(1, p1.getCorePoolSize()); |
383 |
|
joinPool(p1); |
384 |
|
} |
385 |
< |
|
385 |
> |
|
386 |
|
/** |
387 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
388 |
|
* multiple threads active |
400 |
|
assertEquals(2, p2.getLargestPoolSize()); |
401 |
|
joinPool(p2); |
402 |
|
} |
403 |
< |
|
403 |
> |
|
404 |
|
/** |
405 |
|
* getPoolSize increases, but doesn't overestimate, when threads |
406 |
|
* become active |
412 |
|
assertEquals(1, p1.getPoolSize()); |
413 |
|
joinPool(p1); |
414 |
|
} |
415 |
< |
|
415 |
> |
|
416 |
|
/** |
417 |
|
* getTaskCount increases, but doesn't overestimate, when tasks |
418 |
|
* submitted |
431 |
|
joinPool(p1); |
432 |
|
} |
433 |
|
|
434 |
< |
/** |
434 |
> |
/** |
435 |
|
* getThreadFactory returns factory in constructor if not set |
436 |
|
*/ |
437 |
|
public void testGetThreadFactory() { |
441 |
|
joinPool(p); |
442 |
|
} |
443 |
|
|
444 |
< |
/** |
444 |
> |
/** |
445 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
446 |
|
*/ |
447 |
|
public void testSetThreadFactory() { |
452 |
|
joinPool(p); |
453 |
|
} |
454 |
|
|
455 |
< |
/** |
455 |
> |
/** |
456 |
|
* setThreadFactory(null) throws NPE |
457 |
|
*/ |
458 |
|
public void testSetThreadFactoryNull() { |
465 |
|
joinPool(p); |
466 |
|
} |
467 |
|
} |
468 |
< |
|
468 |
> |
|
469 |
|
/** |
470 |
|
* is isShutDown is false before shutdown, true after |
471 |
|
*/ |
472 |
|
public void testIsShutdown() { |
473 |
< |
|
473 |
> |
|
474 |
|
CustomExecutor p1 = new CustomExecutor(1); |
475 |
|
try { |
476 |
|
assertFalse(p1.isShutdown()); |
481 |
|
assertTrue(p1.isShutdown()); |
482 |
|
} |
483 |
|
|
484 |
< |
|
484 |
> |
|
485 |
|
/** |
486 |
|
* isTerminated is false before termination, true after |
487 |
|
*/ |
497 |
|
assertTrue(p1.isTerminated()); |
498 |
|
} catch(Exception e){ |
499 |
|
unexpectedException(); |
500 |
< |
} |
500 |
> |
} |
501 |
|
} |
502 |
|
|
503 |
|
/** |
518 |
|
assertFalse(p1.isTerminating()); |
519 |
|
} catch(Exception e){ |
520 |
|
unexpectedException(); |
521 |
< |
} |
521 |
> |
} |
522 |
|
} |
523 |
|
|
524 |
|
/** |
611 |
|
List l; |
612 |
|
try { |
613 |
|
l = p1.shutdownNow(); |
614 |
< |
} catch (SecurityException ok) { |
614 |
> |
} catch (SecurityException ok) { |
615 |
|
return; |
616 |
|
} |
617 |
|
assertTrue(p1.isShutdown()); |
644 |
|
assertTrue(tasks[i].isDone()); |
645 |
|
assertFalse(tasks[i].isCancelled()); |
646 |
|
} |
647 |
< |
|
647 |
> |
|
648 |
|
} |
649 |
|
catch(Exception ex) { |
650 |
|
unexpectedException(); |
723 |
|
catch(Exception ex) { |
724 |
|
unexpectedException(); |
725 |
|
} |
726 |
< |
finally { |
726 |
> |
finally { |
727 |
|
joinPool(p1); |
728 |
|
} |
729 |
|
} |
930 |
|
l.add(new NPETask()); |
931 |
|
List<Future<String>> result = e.invokeAll(l); |
932 |
|
assertEquals(1, result.size()); |
933 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
933 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
934 |
|
it.next().get(); |
935 |
|
} catch(ExecutionException success) { |
936 |
|
} catch(Exception ex) { |
951 |
|
l.add(new StringTask()); |
952 |
|
List<Future<String>> result = e.invokeAll(l); |
953 |
|
assertEquals(2, result.size()); |
954 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
954 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
955 |
|
assertSame(TEST_STRING, it.next().get()); |
956 |
|
} catch (ExecutionException success) { |
957 |
|
} catch(Exception ex) { |
1138 |
|
l.add(new NPETask()); |
1139 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1140 |
|
assertEquals(1, result.size()); |
1141 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1141 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1142 |
|
it.next().get(); |
1143 |
|
} catch(ExecutionException success) { |
1144 |
|
} catch(Exception ex) { |
1159 |
|
l.add(new StringTask()); |
1160 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1161 |
|
assertEquals(2, result.size()); |
1162 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1162 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1163 |
|
assertSame(TEST_STRING, it.next().get()); |
1164 |
|
} catch (ExecutionException success) { |
1165 |
|
} catch(Exception ex) { |
1181 |
|
l.add(new StringTask()); |
1182 |
|
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1183 |
|
assertEquals(3, result.size()); |
1184 |
< |
Iterator<Future<String>> it = result.iterator(); |
1184 |
> |
Iterator<Future<String>> it = result.iterator(); |
1185 |
|
Future<String> f1 = it.next(); |
1186 |
|
Future<String> f2 = it.next(); |
1187 |
|
Future<String> f3 = it.next(); |