13 |
|
|
14 |
|
public class DelayQueueTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
|
19 |
|
public static Test suite() { |
191 |
|
} |
192 |
|
|
193 |
|
/** |
194 |
< |
* remainingCapacity does not change when elementa added or removed, |
194 |
> |
* remainingCapacity does not change when elements added or removed, |
195 |
|
* but size does |
196 |
|
*/ |
197 |
|
public void testRemainingCapacity() { |
284 |
|
shouldThrow(); |
285 |
|
} catch (NullPointerException success) {} |
286 |
|
} |
287 |
+ |
|
288 |
|
/** |
289 |
|
* addAll of a collection with any null elements throws NPE after |
290 |
|
* possibly adding some elements |
484 |
|
} |
485 |
|
|
486 |
|
/** |
487 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
488 |
< |
* on interruption throws |
487 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
488 |
> |
* on interruption throws |
489 |
|
*/ |
490 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
491 |
|
final DelayQueue q = new DelayQueue(); |
492 |
|
final PDelay pdelay = new PDelay(0); |
493 |
+ |
final CheckedBarrier barrier = new CheckedBarrier(2); |
494 |
|
Thread t = new Thread(new CheckedRunnable() { |
495 |
|
public void realRun() throws InterruptedException { |
496 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
497 |
< |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
497 |
> |
|
498 |
> |
barrier.await(); |
499 |
> |
assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
500 |
> |
|
501 |
> |
Thread.currentThread().interrupt(); |
502 |
> |
try { |
503 |
> |
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
504 |
> |
shouldThrow(); |
505 |
> |
} catch (InterruptedException success) {} |
506 |
> |
|
507 |
> |
barrier.await(); |
508 |
|
try { |
509 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
509 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
510 |
|
shouldThrow(); |
511 |
|
} catch (InterruptedException success) {} |
512 |
|
}}); |
513 |
|
|
514 |
|
t.start(); |
515 |
< |
Thread.sleep(SMALL_DELAY_MS); |
515 |
> |
barrier.await(); |
516 |
|
assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); |
517 |
+ |
barrier.await(); |
518 |
+ |
sleep(SHORT_DELAY_MS); |
519 |
|
t.interrupt(); |
520 |
|
t.join(); |
521 |
|
} |
722 |
|
/** |
723 |
|
* iterator.remove removes current element |
724 |
|
*/ |
725 |
< |
public void testIteratorRemove () { |
725 |
> |
public void testIteratorRemove() { |
726 |
|
final DelayQueue q = new DelayQueue(); |
727 |
|
q.add(new PDelay(2)); |
728 |
|
q.add(new PDelay(1)); |
768 |
|
}}); |
769 |
|
|
770 |
|
joinPool(executor); |
757 |
– |
|
771 |
|
} |
772 |
|
|
773 |
|
|
775 |
|
* Delayed actions do not occur until their delay elapses |
776 |
|
*/ |
777 |
|
public void testDelay() throws InterruptedException { |
778 |
< |
DelayQueue q = new DelayQueue(); |
779 |
< |
NanoDelay[] elements = new NanoDelay[SIZE]; |
780 |
< |
for (int i = 0; i < SIZE; ++i) { |
768 |
< |
elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i)); |
769 |
< |
} |
770 |
< |
for (int i = 0; i < SIZE; ++i) { |
771 |
< |
q.add(elements[i]); |
772 |
< |
} |
778 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
779 |
> |
for (int i = 0; i < SIZE; ++i) |
780 |
> |
q.add(new NanoDelay(1000000L * (SIZE - i))); |
781 |
|
|
782 |
|
long last = 0; |
783 |
|
for (int i = 0; i < SIZE; ++i) { |
784 |
< |
NanoDelay e = (NanoDelay)(q.take()); |
784 |
> |
NanoDelay e = q.take(); |
785 |
|
long tt = e.getTriggerTime(); |
786 |
< |
assertTrue(tt <= System.nanoTime()); |
786 |
> |
assertTrue(System.nanoTime() - tt >= 0); |
787 |
|
if (i != 0) |
788 |
|
assertTrue(tt >= last); |
789 |
|
last = tt; |
790 |
|
} |
791 |
+ |
assertTrue(q.isEmpty()); |
792 |
|
} |
793 |
|
|
794 |
|
/** |
797 |
|
public void testPeekDelayed() { |
798 |
|
DelayQueue q = new DelayQueue(); |
799 |
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
800 |
< |
assert(q.peek() != null); |
800 |
> |
assertNotNull(q.peek()); |
801 |
|
} |
802 |
|
|
803 |
|
|