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); |
319 |
|
* put blocks waiting for take when full |
320 |
|
*/ |
321 |
|
public void testPutWithTake() throws InterruptedException { |
322 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
322 |
> |
final int capacity = 2; |
323 |
> |
final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); |
324 |
|
Thread t = new Thread(new CheckedRunnable() { |
325 |
< |
public void realRun() { |
326 |
< |
int added = 0; |
325 |
> |
public void realRun() throws InterruptedException { |
326 |
> |
for (int i = 0; i < capacity + 1; i++) |
327 |
> |
q.put(i); |
328 |
|
try { |
329 |
< |
q.put(new Object()); |
330 |
< |
++added; |
331 |
< |
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 |
< |
} |
329 |
> |
q.put(99); |
330 |
> |
shouldThrow(); |
331 |
> |
} catch (InterruptedException success) {} |
332 |
|
}}); |
333 |
|
|
334 |
|
t.start(); |
335 |
|
Thread.sleep(SHORT_DELAY_MS); |
336 |
< |
q.take(); |
336 |
> |
assertEquals(q.remainingCapacity(), 0); |
337 |
> |
assertEquals(0, q.take()); |
338 |
> |
Thread.sleep(SHORT_DELAY_MS); |
339 |
|
t.interrupt(); |
340 |
|
t.join(); |
341 |
+ |
assertEquals(q.remainingCapacity(), 0); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
685 |
|
/** |
686 |
|
* iterator.remove removes current element |
687 |
|
*/ |
688 |
< |
public void testIteratorRemove () { |
688 |
> |
public void testIteratorRemove() { |
689 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
690 |
|
q.add(two); |
691 |
|
q.add(one); |
696 |
|
it.remove(); |
697 |
|
|
698 |
|
it = q.iterator(); |
699 |
< |
assertEquals(it.next(), one); |
700 |
< |
assertEquals(it.next(), three); |
699 |
> |
assertSame(it.next(), one); |
700 |
> |
assertSame(it.next(), three); |
701 |
|
assertFalse(it.hasNext()); |
702 |
|
} |
703 |
|
|
722 |
|
/** |
723 |
|
* Modifications do not cause iterators to fail |
724 |
|
*/ |
725 |
< |
public void testWeaklyConsistentIteration () { |
725 |
> |
public void testWeaklyConsistentIteration() { |
726 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
727 |
|
q.add(one); |
728 |
|
q.add(two); |
757 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
758 |
|
executor.execute(new CheckedRunnable() { |
759 |
|
public void realRun() throws InterruptedException { |
760 |
< |
threadAssertFalse(q.offer(three)); |
761 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
762 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
760 |
> |
assertFalse(q.offer(three)); |
761 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
762 |
> |
assertEquals(0, q.remainingCapacity()); |
763 |
|
}}); |
764 |
|
|
765 |
|
executor.execute(new CheckedRunnable() { |
766 |
|
public void realRun() throws InterruptedException { |
767 |
|
Thread.sleep(SMALL_DELAY_MS); |
768 |
< |
threadAssertEquals(one, q.take()); |
768 |
> |
assertSame(one, q.take()); |
769 |
|
}}); |
770 |
|
|
771 |
|
joinPool(executor); |
776 |
– |
|
772 |
|
} |
773 |
|
|
774 |
|
/** |
779 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
780 |
|
executor.execute(new CheckedRunnable() { |
781 |
|
public void realRun() throws InterruptedException { |
782 |
< |
threadAssertNull(q.poll()); |
783 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
784 |
< |
threadAssertTrue(q.isEmpty()); |
782 |
> |
assertNull(q.poll()); |
783 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
784 |
> |
assertTrue(q.isEmpty()); |
785 |
|
}}); |
786 |
|
|
787 |
|
executor.execute(new CheckedRunnable() { |