269 |
|
* addAll throws ISE if not enough room |
270 |
|
*/ |
271 |
|
public void testAddAll_insufficientSpace() { |
272 |
< |
int size = ThreadLocalRandom.current().nextInt(SIZE); |
272 |
> |
int size = ThreadLocalRandom.current().nextInt(1, SIZE); |
273 |
|
ArrayBlockingQueue q = populatedQueue(0, size, size, false); |
274 |
|
// Just fits: |
275 |
|
q.addAll(populatedQueue(size, size, 2 * size, false)); |
340 |
|
}}); |
341 |
|
|
342 |
|
await(pleaseInterrupt); |
343 |
< |
assertThreadStaysAlive(t); |
343 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
344 |
|
t.interrupt(); |
345 |
|
awaitTermination(t); |
346 |
|
assertEquals(SIZE, q.size()); |
375 |
|
assertEquals(0, q.take()); |
376 |
|
|
377 |
|
await(pleaseInterrupt); |
378 |
< |
assertThreadStaysAlive(t); |
378 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
379 |
|
t.interrupt(); |
380 |
|
awaitTermination(t); |
381 |
|
assertEquals(0, q.remainingCapacity()); |
402 |
|
}}); |
403 |
|
|
404 |
|
await(pleaseInterrupt); |
405 |
< |
assertThreadStaysAlive(t); |
405 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
406 |
|
t.interrupt(); |
407 |
|
awaitTermination(t); |
408 |
|
} |
425 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
426 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
427 |
|
public void realRun() throws InterruptedException { |
428 |
< |
for (int i = 0; i < SIZE; ++i) { |
429 |
< |
assertEquals(i, q.take()); |
430 |
< |
} |
428 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
429 |
|
|
430 |
|
Thread.currentThread().interrupt(); |
431 |
|
try { |
443 |
|
}}); |
444 |
|
|
445 |
|
await(pleaseInterrupt); |
446 |
< |
assertThreadStaysAlive(t); |
446 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
447 |
|
t.interrupt(); |
448 |
|
awaitTermination(t); |
449 |
|
} |
493 |
|
*/ |
494 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
495 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
496 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
496 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
497 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
498 |
|
public void realRun() throws InterruptedException { |
499 |
|
long startTime = System.nanoTime(); |
500 |
|
for (int i = 0; i < SIZE; ++i) { |
501 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
502 |
|
} |
503 |
< |
aboutToWait.countDown(); |
503 |
> |
|
504 |
> |
pleaseInterrupt.countDown(); |
505 |
|
try { |
506 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
507 |
|
shouldThrow(); |
508 |
< |
} catch (InterruptedException success) { |
509 |
< |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
510 |
< |
} |
508 |
> |
} catch (InterruptedException success) {} |
509 |
> |
assertFalse(Thread.interrupted()); |
510 |
> |
|
511 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
512 |
|
}}); |
513 |
|
|
514 |
< |
await(aboutToWait); |
515 |
< |
waitForThreadToEnterWaitState(t); |
514 |
> |
await(pleaseInterrupt); |
515 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
516 |
|
t.interrupt(); |
517 |
|
awaitTermination(t); |
518 |
|
checkEmpty(q); |
565 |
|
* contains(x) reports true when elements added but not yet removed |
566 |
|
*/ |
567 |
|
public void testContains() { |
568 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
569 |
< |
for (int i = 0; i < SIZE; ++i) { |
568 |
> |
int size = ThreadLocalRandom.current().nextInt(1, SIZE); |
569 |
> |
ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false); |
570 |
> |
assertFalse(q.contains(null)); |
571 |
> |
for (int i = 0; i < size; ++i) { |
572 |
|
assertTrue(q.contains(new Integer(i))); |
573 |
|
assertEquals(i, q.poll()); |
574 |
|
assertFalse(q.contains(new Integer(i))); |
579 |
|
* clear removes all elements |
580 |
|
*/ |
581 |
|
public void testClear() { |
582 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
582 |
> |
int size = ThreadLocalRandom.current().nextInt(1, 5); |
583 |
> |
ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false); |
584 |
> |
int capacity = size + q.remainingCapacity(); |
585 |
|
q.clear(); |
586 |
|
assertTrue(q.isEmpty()); |
587 |
|
assertEquals(0, q.size()); |
588 |
< |
assertEquals(SIZE, q.remainingCapacity()); |
588 |
> |
assertEquals(capacity, q.remainingCapacity()); |
589 |
|
q.add(one); |
590 |
|
assertFalse(q.isEmpty()); |
591 |
|
assertTrue(q.contains(one)); |
939 |
|
*/ |
940 |
|
public void testNeverContainsNull() { |
941 |
|
Collection<?>[] qs = { |
942 |
< |
new ArrayBlockingQueue<Object>(10), |
943 |
< |
populatedQueue(2), |
942 |
> |
populatedQueue(0, 1, 10, false), |
943 |
> |
populatedQueue(2, 2, 10, true), |
944 |
|
}; |
945 |
|
|
946 |
|
for (Collection<?> q : qs) { |