1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
41 |
|
* Create a queue of given size containing consecutive |
42 |
|
* Integers 0 ... n. |
43 |
|
*/ |
44 |
< |
private LinkedBlockingQueue populatedQueue(int n) { |
45 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
44 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
45 |
> |
LinkedBlockingQueue<Integer> q = |
46 |
> |
new LinkedBlockingQueue<Integer>(n); |
47 |
|
assertTrue(q.isEmpty()); |
48 |
|
for (int i = 0; i < n; i++) |
49 |
|
assertTrue(q.offer(new Integer(i))); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
418 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
419 |
|
*/ |
420 |
|
public void testTimedPoll0() throws InterruptedException { |
421 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
426 |
|
} |
427 |
|
|
428 |
|
/** |
429 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
429 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
430 |
|
*/ |
431 |
|
public void testTimedPoll() throws InterruptedException { |
432 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
441 |
|
* returning timeout status |
442 |
|
*/ |
443 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
444 |
< |
Thread t = new Thread(new CheckedRunnable() { |
444 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
445 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
446 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
447 |
|
public void realRun() throws InterruptedException { |
445 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
448 |
|
for (int i = 0; i < SIZE; ++i) { |
449 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
449 |
> |
long t0 = System.nanoTime(); |
450 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
451 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
452 |
|
} |
453 |
+ |
long t0 = System.nanoTime(); |
454 |
+ |
aboutToWait.countDown(); |
455 |
|
try { |
456 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
456 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
457 |
|
shouldThrow(); |
458 |
< |
} catch (InterruptedException success) {} |
458 |
> |
} catch (InterruptedException success) { |
459 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
460 |
> |
} |
461 |
|
}}); |
462 |
|
|
463 |
< |
t.start(); |
464 |
< |
Thread.sleep(SHORT_DELAY_MS); |
463 |
> |
aboutToWait.await(); |
464 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
465 |
|
t.interrupt(); |
466 |
< |
t.join(); |
466 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
467 |
> |
checkEmpty(q); |
468 |
|
} |
469 |
|
|
470 |
|
/** |
516 |
|
public void testRemoveElement() { |
517 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
518 |
|
for (int i = 1; i < SIZE; i+=2) { |
519 |
< |
assertTrue(q.remove(new Integer(i))); |
519 |
> |
assertTrue(q.contains(i)); |
520 |
> |
assertTrue(q.remove(i)); |
521 |
> |
assertFalse(q.contains(i)); |
522 |
> |
assertTrue(q.contains(i-1)); |
523 |
|
} |
524 |
|
for (int i = 0; i < SIZE; i+=2) { |
525 |
< |
assertTrue(q.remove(new Integer(i))); |
526 |
< |
assertFalse(q.remove(new Integer(i+1))); |
525 |
> |
assertTrue(q.contains(i)); |
526 |
> |
assertTrue(q.remove(i)); |
527 |
> |
assertFalse(q.contains(i)); |
528 |
> |
assertFalse(q.remove(i+1)); |
529 |
> |
assertFalse(q.contains(i+1)); |
530 |
|
} |
531 |
|
assertTrue(q.isEmpty()); |
532 |
|
} |
622 |
|
} |
623 |
|
|
624 |
|
/** |
625 |
< |
* toArray contains all elements |
625 |
> |
* toArray contains all elements in FIFO order |
626 |
|
*/ |
627 |
< |
public void testToArray() throws InterruptedException { |
627 |
> |
public void testToArray() { |
628 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
629 |
|
Object[] o = q.toArray(); |
630 |
|
for (int i = 0; i < o.length; i++) |
631 |
< |
assertEquals(o[i], q.take()); |
631 |
> |
assertSame(o[i], q.poll()); |
632 |
|
} |
633 |
|
|
634 |
|
/** |
635 |
< |
* toArray(a) contains all elements |
635 |
> |
* toArray(a) contains all elements in FIFO order |
636 |
|
*/ |
637 |
|
public void testToArray2() throws InterruptedException { |
638 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
638 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
639 |
|
Integer[] ints = new Integer[SIZE]; |
640 |
< |
ints = (Integer[])q.toArray(ints); |
640 |
> |
Integer[] array = q.toArray(ints); |
641 |
> |
assertSame(ints, array); |
642 |
|
for (int i = 0; i < ints.length; i++) |
643 |
< |
assertEquals(ints[i], q.take()); |
643 |
> |
assertSame(ints[i], q.poll()); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* toArray(null) throws NPE |
647 |
> |
* toArray(null) throws NullPointerException |
648 |
|
*/ |
649 |
< |
public void testToArray_BadArg() { |
649 |
> |
public void testToArray_NullArg() { |
650 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
651 |
|
try { |
652 |
< |
Object o[] = q.toArray(null); |
652 |
> |
q.toArray(null); |
653 |
|
shouldThrow(); |
654 |
|
} catch (NullPointerException success) {} |
655 |
|
} |
656 |
|
|
657 |
|
/** |
658 |
< |
* toArray with incompatible array type throws CCE |
658 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
659 |
|
*/ |
660 |
|
public void testToArray1_BadArg() { |
661 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
662 |
|
try { |
663 |
< |
Object o[] = q.toArray(new String[10]); |
663 |
> |
q.toArray(new String[10]); |
664 |
|
shouldThrow(); |
665 |
|
} catch (ArrayStoreException success) {} |
666 |
|
} |