23 |
|
*/ |
24 |
|
public void testExecute() { |
25 |
|
try { |
26 |
< |
TrackedRunnable runnable =new TrackedRunnable(); |
26 |
> |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
27 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
28 |
|
p1.execute(runnable); |
29 |
|
assertFalse(runnable.done); |
70 |
|
*/ |
71 |
|
public void testSchedule3() { |
72 |
|
try { |
73 |
< |
TrackedRunnable runnable = new TrackedRunnable(); |
73 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
74 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
75 |
|
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
76 |
|
Thread.sleep(SHORT_DELAY_MS); |
89 |
|
*/ |
90 |
|
public void testSchedule4() { |
91 |
|
try { |
92 |
< |
TrackedRunnable runnable = new TrackedRunnable(); |
92 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
93 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
94 |
|
ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
95 |
|
assertFalse(runnable.done); |
108 |
|
*/ |
109 |
|
public void testSchedule5() { |
110 |
|
try { |
111 |
< |
TrackedRunnable runnable = new TrackedRunnable(); |
111 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
112 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
113 |
|
ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
114 |
|
assertFalse(runnable.done); |
321 |
|
assertEquals(5, p1.getTaskCount()); |
322 |
|
joinPool(p1); |
323 |
|
} |
324 |
+ |
|
325 |
+ |
/** |
326 |
+ |
* getThreadFactory returns factory in constructor if not set |
327 |
+ |
*/ |
328 |
+ |
public void testGetThreadFactory() { |
329 |
+ |
ThreadFactory tf = new SimpleThreadFactory(); |
330 |
+ |
ScheduledExecutor p = new ScheduledExecutor(1, tf); |
331 |
+ |
assertSame(tf, p.getThreadFactory()); |
332 |
+ |
p.shutdown(); |
333 |
+ |
joinPool(p); |
334 |
+ |
} |
335 |
+ |
|
336 |
+ |
/** |
337 |
+ |
* setThreadFactory sets the thread factory returned by getThreadFactory |
338 |
+ |
*/ |
339 |
+ |
public void testSetThreadFactory() { |
340 |
+ |
ThreadFactory tf = new SimpleThreadFactory(); |
341 |
+ |
ScheduledExecutor p = new ScheduledExecutor(1); |
342 |
+ |
p.setThreadFactory(tf); |
343 |
+ |
assertSame(tf, p.getThreadFactory()); |
344 |
+ |
p.shutdown(); |
345 |
+ |
joinPool(p); |
346 |
+ |
} |
347 |
+ |
|
348 |
+ |
/** |
349 |
+ |
* setThreadFactory(null) throws NPE |
350 |
+ |
*/ |
351 |
+ |
public void testSetThreadFactoryNull() { |
352 |
+ |
ScheduledExecutor p = new ScheduledExecutor(1); |
353 |
+ |
try { |
354 |
+ |
p.setThreadFactory(null); |
355 |
+ |
shouldThrow(); |
356 |
+ |
} catch (NullPointerException success) { |
357 |
+ |
} finally { |
358 |
+ |
joinPool(p); |
359 |
+ |
} |
360 |
+ |
} |
361 |
|
|
362 |
|
/** |
363 |
|
* is isShutDown is false before shutdown, true after |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* purge removes cancelled tasks from the queue |
418 |
> |
* getQueue returns the work queue, which contains queued tasks |
419 |
> |
*/ |
420 |
> |
public void testGetQueue() { |
421 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
422 |
> |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
423 |
> |
for(int i = 0; i < 5; i++){ |
424 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
425 |
> |
} |
426 |
> |
try { |
427 |
> |
Thread.sleep(SHORT_DELAY_MS); |
428 |
> |
BlockingQueue<Runnable> q = p1.getQueue(); |
429 |
> |
assertTrue(q.contains(tasks[4])); |
430 |
> |
assertFalse(q.contains(tasks[0])); |
431 |
> |
p1.shutdownNow(); |
432 |
> |
} catch(Exception e) { |
433 |
> |
unexpectedException(); |
434 |
> |
} finally { |
435 |
> |
joinPool(p1); |
436 |
> |
} |
437 |
> |
} |
438 |
> |
|
439 |
> |
/** |
440 |
> |
* remove(task) removes queued task, and fails to remove active task |
441 |
> |
*/ |
442 |
> |
public void testRemove() { |
443 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
444 |
> |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
445 |
> |
for(int i = 0; i < 5; i++){ |
446 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
447 |
> |
} |
448 |
> |
try { |
449 |
> |
Thread.sleep(SHORT_DELAY_MS); |
450 |
> |
BlockingQueue<Runnable> q = p1.getQueue(); |
451 |
> |
assertFalse(p1.remove((Runnable)tasks[0])); |
452 |
> |
assertTrue(q.contains((Runnable)tasks[4])); |
453 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
454 |
> |
assertTrue(p1.remove((Runnable)tasks[4])); |
455 |
> |
assertFalse(p1.remove((Runnable)tasks[4])); |
456 |
> |
assertFalse(q.contains((Runnable)tasks[4])); |
457 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
458 |
> |
assertTrue(p1.remove((Runnable)tasks[3])); |
459 |
> |
assertFalse(q.contains((Runnable)tasks[3])); |
460 |
> |
p1.shutdownNow(); |
461 |
> |
} catch(Exception e) { |
462 |
> |
unexpectedException(); |
463 |
> |
} finally { |
464 |
> |
joinPool(p1); |
465 |
> |
} |
466 |
> |
} |
467 |
> |
|
468 |
> |
/** |
469 |
> |
* purge removes cancelled tasks from the queue |
470 |
|
*/ |
471 |
|
public void testPurge() { |
472 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
473 |
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
474 |
|
for(int i = 0; i < 5; i++){ |
475 |
< |
tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); |
475 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
476 |
|
} |
477 |
|
int max = 5; |
478 |
|
if (tasks[4].cancel(true)) --max; |
489 |
|
public void testShutDownNow() { |
490 |
|
ScheduledExecutor p1 = new ScheduledExecutor(1); |
491 |
|
for(int i = 0; i < 5; i++) |
492 |
< |
p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
492 |
> |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
493 |
|
List l = p1.shutdownNow(); |
494 |
|
assertTrue(p1.isShutdown()); |
495 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |