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 ArrayBlockingQueue populatedQueue(int n) { |
45 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
44 |
> |
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
45 |
> |
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n); |
46 |
|
assertTrue(q.isEmpty()); |
47 |
|
for (int i = 0; i < n; i++) |
48 |
|
assertTrue(q.offer(new Integer(i))); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* offer(null) throws NPE |
164 |
> |
* offer(null) throws NPE |
165 |
|
*/ |
166 |
|
public void testOfferNull() { |
167 |
|
try { |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* add(null) throws NPE |
175 |
> |
* add(null) throws NPE |
176 |
|
*/ |
177 |
|
public void testAddNull() { |
178 |
|
try { |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* addAll(null) throws NPE |
210 |
> |
* addAll(null) throws NPE |
211 |
|
*/ |
212 |
|
public void testAddAll1() { |
213 |
|
try { |
230 |
|
|
231 |
|
|
232 |
|
/** |
233 |
< |
* addAll of a collection with null elements throws NPE |
233 |
> |
* addAll of a collection with null elements throws NPE |
234 |
|
*/ |
235 |
|
public void testAddAll2() { |
236 |
|
try { |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* put(null) throws NPE |
289 |
> |
* put(null) throws NPE |
290 |
|
*/ |
291 |
|
public void testPutNull() throws InterruptedException { |
292 |
|
try { |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
– |
* take blocks interruptibly when empty |
397 |
– |
*/ |
398 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
399 |
– |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
400 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
401 |
– |
public void realRun() throws InterruptedException { |
402 |
– |
q.take(); |
403 |
– |
}}; |
404 |
– |
|
405 |
– |
t.start(); |
406 |
– |
Thread.sleep(SHORT_DELAY_MS); |
407 |
– |
t.interrupt(); |
408 |
– |
t.join(); |
409 |
– |
} |
410 |
– |
|
411 |
– |
/** |
396 |
|
* Take removes existing elements until empty, then blocks interruptibly |
397 |
|
*/ |
398 |
|
public void testBlockingTake() throws InterruptedException { |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
430 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
431 |
|
*/ |
432 |
|
public void testTimedPoll0() throws InterruptedException { |
433 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
441 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
442 |
|
*/ |
443 |
|
public void testTimedPoll() throws InterruptedException { |
444 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
453 |
|
* returning timeout status |
454 |
|
*/ |
455 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
456 |
< |
Thread t = new Thread(new CheckedRunnable() { |
456 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
457 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
458 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
459 |
|
public void realRun() throws InterruptedException { |
474 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
460 |
|
for (int i = 0; i < SIZE; ++i) { |
461 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; |
461 |
> |
long t0 = System.nanoTime(); |
462 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
463 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
464 |
|
} |
465 |
+ |
long t0 = System.nanoTime(); |
466 |
+ |
aboutToWait.countDown(); |
467 |
|
try { |
468 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
468 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
469 |
|
shouldThrow(); |
470 |
< |
} catch (InterruptedException success) {} |
470 |
> |
} catch (InterruptedException success) { |
471 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
472 |
> |
} |
473 |
|
}}); |
474 |
|
|
475 |
< |
t.start(); |
476 |
< |
Thread.sleep(SHORT_DELAY_MS); |
475 |
> |
aboutToWait.await(); |
476 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
477 |
|
t.interrupt(); |
478 |
< |
t.join(); |
478 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
479 |
> |
checkEmpty(q); |
480 |
|
} |
481 |
|
|
482 |
|
/** |
615 |
|
} |
616 |
|
|
617 |
|
/** |
618 |
< |
* toArray contains all elements |
618 |
> |
* toArray contains all elements in FIFO order |
619 |
|
*/ |
620 |
< |
public void testToArray() throws InterruptedException { |
620 |
> |
public void testToArray() { |
621 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
622 |
|
Object[] o = q.toArray(); |
623 |
|
for (int i = 0; i < o.length; i++) |
624 |
< |
assertEquals(o[i], q.take()); |
624 |
> |
assertSame(o[i], q.poll()); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
< |
* toArray(a) contains all elements |
628 |
> |
* toArray(a) contains all elements in FIFO order |
629 |
|
*/ |
630 |
< |
public void testToArray2() throws InterruptedException { |
631 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
630 |
> |
public void testToArray2() { |
631 |
> |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
632 |
|
Integer[] ints = new Integer[SIZE]; |
633 |
< |
ints = (Integer[])q.toArray(ints); |
633 |
> |
Integer[] array = q.toArray(ints); |
634 |
> |
assertSame(ints, array); |
635 |
|
for (int i = 0; i < ints.length; i++) |
636 |
< |
assertEquals(ints[i], q.take()); |
636 |
> |
assertSame(ints[i], q.poll()); |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
< |
* toArray(null) throws NPE |
640 |
> |
* toArray(null) throws NullPointerException |
641 |
|
*/ |
642 |
< |
public void testToArray_BadArg() { |
642 |
> |
public void testToArray_NullArg() { |
643 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
644 |
|
try { |
645 |
< |
Object o[] = q.toArray(null); |
645 |
> |
q.toArray(null); |
646 |
|
shouldThrow(); |
647 |
|
} catch (NullPointerException success) {} |
648 |
|
} |
649 |
|
|
650 |
|
/** |
651 |
< |
* toArray with incompatible array type throws CCE |
651 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
652 |
|
*/ |
653 |
|
public void testToArray1_BadArg() { |
654 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
655 |
|
try { |
656 |
< |
Object o[] = q.toArray(new String[10]); |
656 |
> |
q.toArray(new String[10]); |
657 |
|
shouldThrow(); |
658 |
|
} catch (ArrayStoreException success) {} |
659 |
|
} |
889 |
|
} |
890 |
|
|
891 |
|
/** |
892 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
892 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
893 |
|
*/ |
894 |
|
public void testDrainToN() { |
895 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
898 |
|
assertTrue(q.offer(new Integer(j))); |
899 |
|
ArrayList l = new ArrayList(); |
900 |
|
q.drainTo(l, i); |
901 |
< |
int k = (i < SIZE)? i : SIZE; |
901 |
> |
int k = (i < SIZE) ? i : SIZE; |
902 |
|
assertEquals(l.size(), k); |
903 |
|
assertEquals(q.size(), SIZE-k); |
904 |
|
for (int j = 0; j < k; ++j) |