15 |
|
|
16 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(ArrayBlockingQueueTest.class); |
224 |
|
shouldThrow(); |
225 |
|
} catch (NullPointerException success) {} |
226 |
|
} |
227 |
+ |
|
228 |
|
/** |
229 |
|
* addAll of a collection with any null elements throws NPE after |
230 |
|
* possibly adding some elements |
239 |
|
shouldThrow(); |
240 |
|
} catch (NullPointerException success) {} |
241 |
|
} |
242 |
+ |
|
243 |
|
/** |
244 |
|
* addAll throws ISE if not enough room |
245 |
|
*/ |
253 |
|
shouldThrow(); |
254 |
|
} catch (IllegalStateException success) {} |
255 |
|
} |
256 |
+ |
|
257 |
|
/** |
258 |
|
* Queue contains all elements, in traversal order, of successful addAll |
259 |
|
*/ |
322 |
|
* put blocks waiting for take when full |
323 |
|
*/ |
324 |
|
public void testPutWithTake() throws InterruptedException { |
325 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
325 |
> |
final int capacity = 2; |
326 |
> |
final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); |
327 |
|
Thread t = new Thread(new CheckedRunnable() { |
328 |
< |
public void realRun() { |
329 |
< |
int added = 0; |
328 |
> |
public void realRun() throws InterruptedException { |
329 |
> |
for (int i = 0; i < capacity + 1; i++) |
330 |
> |
q.put(i); |
331 |
|
try { |
332 |
< |
q.put(new Object()); |
333 |
< |
++added; |
334 |
< |
q.put(new Object()); |
330 |
< |
++added; |
331 |
< |
q.put(new Object()); |
332 |
< |
++added; |
333 |
< |
q.put(new Object()); |
334 |
< |
++added; |
335 |
< |
threadShouldThrow(); |
336 |
< |
} catch (InterruptedException success) { |
337 |
< |
threadAssertTrue(added >= 2); |
338 |
< |
} |
332 |
> |
q.put(99); |
333 |
> |
shouldThrow(); |
334 |
> |
} catch (InterruptedException success) {} |
335 |
|
}}); |
336 |
|
|
337 |
|
t.start(); |
338 |
|
Thread.sleep(SHORT_DELAY_MS); |
339 |
< |
q.take(); |
339 |
> |
assertEquals(q.remainingCapacity(), 0); |
340 |
> |
assertEquals(0, q.take()); |
341 |
> |
Thread.sleep(SHORT_DELAY_MS); |
342 |
|
t.interrupt(); |
343 |
|
t.join(); |
344 |
+ |
assertEquals(q.remainingCapacity(), 0); |
345 |
|
} |
346 |
|
|
347 |
|
/** |
479 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
480 |
|
Thread t = new Thread(new CheckedRunnable() { |
481 |
|
public void realRun() throws InterruptedException { |
483 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
484 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
482 |
|
try { |
483 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
484 |
+ |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
485 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
486 |
|
shouldThrow(); |
487 |
|
} catch (InterruptedException success) {} |
688 |
|
/** |
689 |
|
* iterator.remove removes current element |
690 |
|
*/ |
691 |
< |
public void testIteratorRemove () { |
691 |
> |
public void testIteratorRemove() { |
692 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
693 |
|
q.add(two); |
694 |
|
q.add(one); |
699 |
|
it.remove(); |
700 |
|
|
701 |
|
it = q.iterator(); |
702 |
< |
assertEquals(it.next(), one); |
703 |
< |
assertEquals(it.next(), three); |
702 |
> |
assertSame(it.next(), one); |
703 |
> |
assertSame(it.next(), three); |
704 |
|
assertFalse(it.hasNext()); |
705 |
|
} |
706 |
|
|
725 |
|
/** |
726 |
|
* Modifications do not cause iterators to fail |
727 |
|
*/ |
728 |
< |
public void testWeaklyConsistentIteration () { |
728 |
> |
public void testWeaklyConsistentIteration() { |
729 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
730 |
|
q.add(one); |
731 |
|
q.add(two); |
760 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
761 |
|
executor.execute(new CheckedRunnable() { |
762 |
|
public void realRun() throws InterruptedException { |
763 |
< |
threadAssertFalse(q.offer(three)); |
764 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
765 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
763 |
> |
assertFalse(q.offer(three)); |
764 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
765 |
> |
assertEquals(0, q.remainingCapacity()); |
766 |
|
}}); |
767 |
|
|
768 |
|
executor.execute(new CheckedRunnable() { |
769 |
|
public void realRun() throws InterruptedException { |
770 |
|
Thread.sleep(SMALL_DELAY_MS); |
771 |
< |
threadAssertEquals(one, q.take()); |
771 |
> |
assertSame(one, q.take()); |
772 |
|
}}); |
773 |
|
|
774 |
|
joinPool(executor); |
776 |
– |
|
775 |
|
} |
776 |
|
|
777 |
|
/** |
782 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
|
executor.execute(new CheckedRunnable() { |
784 |
|
public void realRun() throws InterruptedException { |
785 |
< |
threadAssertNull(q.poll()); |
786 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
< |
threadAssertTrue(q.isEmpty()); |
785 |
> |
assertNull(q.poll()); |
786 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
> |
assertTrue(q.isEmpty()); |
788 |
|
}}); |
789 |
|
|
790 |
|
executor.execute(new CheckedRunnable() { |