391 |
|
/** |
392 |
|
* take blocks interruptibly when empty |
393 |
|
*/ |
394 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
395 |
< |
final DelayQueue q = new DelayQueue(); |
396 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
397 |
< |
public void realRun() throws InterruptedException { |
398 |
< |
q.take(); |
399 |
< |
}}; |
400 |
< |
|
401 |
< |
t.start(); |
394 |
> |
public void testTakeFromEmptyBlocksInterruptibly() |
395 |
> |
throws InterruptedException { |
396 |
> |
final BlockingQueue q = new DelayQueue(); |
397 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
398 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
399 |
> |
public void realRun() { |
400 |
> |
long t0 = System.nanoTime(); |
401 |
> |
threadStarted.countDown(); |
402 |
> |
try { |
403 |
> |
q.take(); |
404 |
> |
shouldThrow(); |
405 |
> |
} catch (InterruptedException expected) {} |
406 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
407 |
> |
}}); |
408 |
> |
threadStarted.await(); |
409 |
|
Thread.sleep(SHORT_DELAY_MS); |
410 |
+ |
assertTrue(t.isAlive()); |
411 |
|
t.interrupt(); |
412 |
< |
t.join(); |
412 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
413 |
> |
assertFalse(t.isAlive()); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
448 |
|
} |
449 |
|
|
450 |
|
/** |
451 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
451 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
452 |
|
*/ |
453 |
|
public void testTimedPoll0() throws InterruptedException { |
454 |
|
DelayQueue q = populatedQueue(SIZE); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
462 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
463 |
|
*/ |
464 |
|
public void testTimedPoll() throws InterruptedException { |
465 |
|
DelayQueue q = populatedQueue(SIZE); |
493 |
|
} |
494 |
|
|
495 |
|
/** |
496 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
497 |
< |
* on interruption throws |
496 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
497 |
> |
* on interruption throws |
498 |
|
*/ |
499 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
500 |
|
final DelayQueue q = new DelayQueue(); |
501 |
|
final PDelay pdelay = new PDelay(0); |
502 |
+ |
final CheckedBarrier barrier = new CheckedBarrier(2); |
503 |
|
Thread t = new Thread(new CheckedRunnable() { |
504 |
|
public void realRun() throws InterruptedException { |
505 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
506 |
< |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
506 |
> |
|
507 |
> |
barrier.await(); |
508 |
> |
assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
509 |
> |
|
510 |
> |
Thread.currentThread().interrupt(); |
511 |
|
try { |
512 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
512 |
> |
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
513 |
> |
shouldThrow(); |
514 |
> |
} catch (InterruptedException success) {} |
515 |
> |
|
516 |
> |
barrier.await(); |
517 |
> |
try { |
518 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
519 |
|
shouldThrow(); |
520 |
|
} catch (InterruptedException success) {} |
521 |
|
}}); |
522 |
|
|
523 |
|
t.start(); |
524 |
< |
Thread.sleep(SMALL_DELAY_MS); |
524 |
> |
barrier.await(); |
525 |
|
assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); |
526 |
+ |
barrier.await(); |
527 |
+ |
sleep(SHORT_DELAY_MS); |
528 |
|
t.interrupt(); |
529 |
|
t.join(); |
530 |
|
} |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
< |
* toArray with incompatible array type throws CCE |
707 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
708 |
|
*/ |
709 |
|
public void testToArray1_BadArg() { |
710 |
|
DelayQueue q = populatedQueue(SIZE); |
711 |
|
try { |
712 |
< |
Object o[] = q.toArray(new String[10]); |
712 |
> |
q.toArray(new String[10]); |
713 |
|
shouldThrow(); |
714 |
|
} catch (ArrayStoreException success) {} |
715 |
|
} |
784 |
|
* Delayed actions do not occur until their delay elapses |
785 |
|
*/ |
786 |
|
public void testDelay() throws InterruptedException { |
787 |
< |
DelayQueue q = new DelayQueue(); |
788 |
< |
NanoDelay[] elements = new NanoDelay[SIZE]; |
789 |
< |
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 |
< |
} |
787 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
788 |
> |
for (int i = 0; i < SIZE; ++i) |
789 |
> |
q.add(new NanoDelay(1000000L * (SIZE - i))); |
790 |
|
|
791 |
|
long last = 0; |
792 |
|
for (int i = 0; i < SIZE; ++i) { |
793 |
< |
NanoDelay e = (NanoDelay)(q.take()); |
793 |
> |
NanoDelay e = q.take(); |
794 |
|
long tt = e.getTriggerTime(); |
795 |
< |
assertTrue(tt <= System.nanoTime()); |
795 |
> |
assertTrue(System.nanoTime() - tt >= 0); |
796 |
|
if (i != 0) |
797 |
|
assertTrue(tt >= last); |
798 |
|
last = tt; |
799 |
|
} |
800 |
+ |
assertTrue(q.isEmpty()); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
806 |
|
public void testPeekDelayed() { |
807 |
|
DelayQueue q = new DelayQueue(); |
808 |
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
809 |
< |
assert(q.peek() != null); |
809 |
> |
assertNotNull(q.peek()); |
810 |
|
} |
811 |
|
|
812 |
|
|
919 |
|
} |
920 |
|
|
921 |
|
/** |
922 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
922 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
923 |
|
*/ |
924 |
|
public void testDrainToN() { |
925 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
926 |
|
DelayQueue q = populatedQueue(SIZE); |
927 |
|
ArrayList l = new ArrayList(); |
928 |
|
q.drainTo(l, i); |
929 |
< |
int k = (i < SIZE)? i : SIZE; |
929 |
> |
int k = (i < SIZE) ? i : SIZE; |
930 |
|
assertEquals(q.size(), SIZE-k); |
931 |
|
assertEquals(l.size(), k); |
932 |
|
} |