231 |
|
*/ |
232 |
|
public void testAdd() { |
233 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
234 |
< |
for (int i = 0; i < SIZE; ++i) { |
235 |
< |
assertTrue(q.add(new Integer(i))); |
236 |
< |
} |
234 |
> |
for (int i = 0; i < SIZE; i++) assertTrue(q.add((Integer) i)); |
235 |
|
assertEquals(0, q.remainingCapacity()); |
236 |
|
try { |
237 |
< |
q.add(new Integer(SIZE)); |
237 |
> |
q.add((Integer) SIZE); |
238 |
|
shouldThrow(); |
239 |
|
} catch (IllegalStateException success) {} |
240 |
|
} |
268 |
|
/** |
269 |
|
* addAll throws ISE if not enough room |
270 |
|
*/ |
271 |
< |
public void testAddAll4() { |
272 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
273 |
< |
Integer[] ints = new Integer[SIZE]; |
274 |
< |
for (int i = 0; i < SIZE; ++i) |
275 |
< |
ints[i] = new Integer(i); |
271 |
> |
public void testAddAll_insufficientSpace() { |
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)); |
276 |
> |
assertEquals(0, q.remainingCapacity()); |
277 |
> |
assertEquals(size, q.size()); |
278 |
> |
assertEquals(0, q.peek()); |
279 |
|
try { |
280 |
< |
q.addAll(Arrays.asList(ints)); |
280 |
> |
q = populatedQueue(0, size, size, false); |
281 |
> |
q.addAll(Collections.nCopies(size + 1, 42)); |
282 |
|
shouldThrow(); |
283 |
|
} catch (IllegalStateException success) {} |
284 |
|
} |
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()); |
399 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
400 |
|
shouldThrow(); |
401 |
|
} catch (InterruptedException success) {} |
402 |
+ |
assertFalse(Thread.interrupted()); |
403 |
|
}}); |
404 |
|
|
405 |
|
await(pleaseInterrupt); |
406 |
< |
assertThreadStaysAlive(t); |
406 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
407 |
|
t.interrupt(); |
408 |
|
awaitTermination(t); |
409 |
|
} |
426 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
427 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
428 |
|
public void realRun() throws InterruptedException { |
429 |
< |
for (int i = 0; i < SIZE; ++i) { |
427 |
< |
assertEquals(i, q.take()); |
428 |
< |
} |
429 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
430 |
|
|
431 |
|
Thread.currentThread().interrupt(); |
432 |
|
try { |
444 |
|
}}); |
445 |
|
|
446 |
|
await(pleaseInterrupt); |
447 |
< |
assertThreadStaysAlive(t); |
447 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
448 |
|
t.interrupt(); |
449 |
|
awaitTermination(t); |
450 |
|
} |
494 |
|
*/ |
495 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
496 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
497 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
497 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
498 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
499 |
|
public void realRun() throws InterruptedException { |
500 |
|
long startTime = System.nanoTime(); |
501 |
< |
for (int i = 0; i < SIZE; ++i) { |
501 |
> |
for (int i = 0; i < SIZE; i++) |
502 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
503 |
< |
} |
504 |
< |
aboutToWait.countDown(); |
503 |
> |
|
504 |
> |
Thread.currentThread().interrupt(); |
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 |
> |
pleaseInterrupt.countDown(); |
512 |
> |
try { |
513 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
514 |
> |
shouldThrow(); |
515 |
> |
} catch (InterruptedException success) {} |
516 |
> |
assertFalse(Thread.interrupted()); |
517 |
> |
|
518 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
519 |
|
}}); |
520 |
|
|
521 |
< |
await(aboutToWait); |
522 |
< |
waitForThreadToEnterWaitState(t); |
521 |
> |
await(pleaseInterrupt); |
522 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
523 |
|
t.interrupt(); |
524 |
|
awaitTermination(t); |
525 |
|
checkEmpty(q); |
572 |
|
* contains(x) reports true when elements added but not yet removed |
573 |
|
*/ |
574 |
|
public void testContains() { |
575 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
576 |
< |
for (int i = 0; i < SIZE; ++i) { |
575 |
> |
int size = ThreadLocalRandom.current().nextInt(1, SIZE); |
576 |
> |
ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false); |
577 |
> |
assertFalse(q.contains(null)); |
578 |
> |
for (int i = 0; i < size; ++i) { |
579 |
|
assertTrue(q.contains(new Integer(i))); |
580 |
|
assertEquals(i, q.poll()); |
581 |
|
assertFalse(q.contains(new Integer(i))); |
586 |
|
* clear removes all elements |
587 |
|
*/ |
588 |
|
public void testClear() { |
589 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
589 |
> |
int size = ThreadLocalRandom.current().nextInt(1, 5); |
590 |
> |
ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false); |
591 |
> |
int capacity = size + q.remainingCapacity(); |
592 |
|
q.clear(); |
593 |
|
assertTrue(q.isEmpty()); |
594 |
|
assertEquals(0, q.size()); |
595 |
< |
assertEquals(SIZE, q.remainingCapacity()); |
595 |
> |
assertEquals(capacity, q.remainingCapacity()); |
596 |
|
q.add(one); |
597 |
|
assertFalse(q.isEmpty()); |
598 |
|
assertTrue(q.contains(one)); |
946 |
|
*/ |
947 |
|
public void testNeverContainsNull() { |
948 |
|
Collection<?>[] qs = { |
949 |
< |
new ArrayBlockingQueue<Object>(10), |
950 |
< |
populatedQueue(2), |
949 |
> |
populatedQueue(0, 1, 10, false), |
950 |
> |
populatedQueue(2, 2, 10, true), |
951 |
|
}; |
952 |
|
|
953 |
|
for (Collection<?> q : qs) { |