14 |
|
import java.io.*; |
15 |
|
|
16 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
17 |
+ |
|
18 |
+ |
public static class Fair extends BlockingQueueTest { |
19 |
+ |
protected BlockingQueue emptyCollection() { |
20 |
+ |
return new ArrayBlockingQueue(20, true); |
21 |
+ |
} |
22 |
+ |
} |
23 |
+ |
|
24 |
+ |
public static class NonFair extends BlockingQueueTest { |
25 |
+ |
protected BlockingQueue emptyCollection() { |
26 |
+ |
return new ArrayBlockingQueue(20, false); |
27 |
+ |
} |
28 |
+ |
} |
29 |
+ |
|
30 |
|
public static void main(String[] args) { |
31 |
< |
junit.textui.TestRunner.run (suite()); |
31 |
> |
junit.textui.TestRunner.run(suite()); |
32 |
|
} |
33 |
+ |
|
34 |
|
public static Test suite() { |
35 |
< |
return new TestSuite(ArrayBlockingQueueTest.class); |
35 |
> |
return newTestSuite(ArrayBlockingQueueTest.class, |
36 |
> |
new Fair().testSuite(), |
37 |
> |
new NonFair().testSuite()); |
38 |
|
} |
39 |
|
|
40 |
|
/** |
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 { |
240 |
|
shouldThrow(); |
241 |
|
} catch (NullPointerException success) {} |
242 |
|
} |
243 |
+ |
|
244 |
|
/** |
245 |
|
* addAll of a collection with any null elements throws NPE after |
246 |
|
* possibly adding some elements |
255 |
|
shouldThrow(); |
256 |
|
} catch (NullPointerException success) {} |
257 |
|
} |
258 |
+ |
|
259 |
|
/** |
260 |
|
* addAll throws ISE if not enough room |
261 |
|
*/ |
269 |
|
shouldThrow(); |
270 |
|
} catch (IllegalStateException success) {} |
271 |
|
} |
272 |
+ |
|
273 |
|
/** |
274 |
|
* Queue contains all elements, in traversal order, of successful addAll |
275 |
|
*/ |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* put(null) throws NPE |
289 |
> |
* put(null) throws NPE |
290 |
|
*/ |
291 |
|
public void testPutNull() throws InterruptedException { |
292 |
|
try { |
338 |
|
* put blocks waiting for take when full |
339 |
|
*/ |
340 |
|
public void testPutWithTake() throws InterruptedException { |
341 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
341 |
> |
final int capacity = 2; |
342 |
> |
final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); |
343 |
|
Thread t = new Thread(new CheckedRunnable() { |
344 |
< |
public void realRun() { |
345 |
< |
int added = 0; |
344 |
> |
public void realRun() throws InterruptedException { |
345 |
> |
for (int i = 0; i < capacity + 1; i++) |
346 |
> |
q.put(i); |
347 |
|
try { |
348 |
< |
q.put(new Object()); |
349 |
< |
++added; |
350 |
< |
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 |
< |
} |
348 |
> |
q.put(99); |
349 |
> |
shouldThrow(); |
350 |
> |
} catch (InterruptedException success) {} |
351 |
|
}}); |
352 |
|
|
353 |
|
t.start(); |
354 |
|
Thread.sleep(SHORT_DELAY_MS); |
355 |
< |
q.take(); |
355 |
> |
assertEquals(q.remainingCapacity(), 0); |
356 |
> |
assertEquals(0, q.take()); |
357 |
> |
Thread.sleep(SHORT_DELAY_MS); |
358 |
|
t.interrupt(); |
359 |
|
t.join(); |
360 |
+ |
assertEquals(q.remainingCapacity(), 0); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
488 |
|
} |
489 |
|
|
490 |
|
/** |
476 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
477 |
– |
* on interruption throws |
478 |
– |
*/ |
479 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
480 |
– |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
481 |
– |
Thread t = new Thread(new CheckedRunnable() { |
482 |
– |
public void realRun() throws InterruptedException { |
483 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
484 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
485 |
– |
try { |
486 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
487 |
– |
shouldThrow(); |
488 |
– |
} catch (InterruptedException success) {} |
489 |
– |
}}); |
490 |
– |
|
491 |
– |
t.start(); |
492 |
– |
Thread.sleep(SMALL_DELAY_MS); |
493 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
494 |
– |
t.interrupt(); |
495 |
– |
t.join(); |
496 |
– |
} |
497 |
– |
|
498 |
– |
|
499 |
– |
/** |
491 |
|
* peek returns next element, or null if empty |
492 |
|
*/ |
493 |
|
public void testPeek() { |
623 |
|
} |
624 |
|
|
625 |
|
/** |
626 |
< |
* toArray contains all elements |
626 |
> |
* toArray contains all elements |
627 |
|
*/ |
628 |
|
public void testToArray() throws InterruptedException { |
629 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
680 |
|
/** |
681 |
|
* iterator.remove removes current element |
682 |
|
*/ |
683 |
< |
public void testIteratorRemove () { |
683 |
> |
public void testIteratorRemove() { |
684 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
685 |
|
q.add(two); |
686 |
|
q.add(one); |
691 |
|
it.remove(); |
692 |
|
|
693 |
|
it = q.iterator(); |
694 |
< |
assertEquals(it.next(), one); |
695 |
< |
assertEquals(it.next(), three); |
694 |
> |
assertSame(it.next(), one); |
695 |
> |
assertSame(it.next(), three); |
696 |
|
assertFalse(it.hasNext()); |
697 |
|
} |
698 |
|
|
717 |
|
/** |
718 |
|
* Modifications do not cause iterators to fail |
719 |
|
*/ |
720 |
< |
public void testWeaklyConsistentIteration () { |
720 |
> |
public void testWeaklyConsistentIteration() { |
721 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
722 |
|
q.add(one); |
723 |
|
q.add(two); |
752 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
753 |
|
executor.execute(new CheckedRunnable() { |
754 |
|
public void realRun() throws InterruptedException { |
755 |
< |
threadAssertFalse(q.offer(three)); |
756 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
757 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
755 |
> |
assertFalse(q.offer(three)); |
756 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
757 |
> |
assertEquals(0, q.remainingCapacity()); |
758 |
|
}}); |
759 |
|
|
760 |
|
executor.execute(new CheckedRunnable() { |
761 |
|
public void realRun() throws InterruptedException { |
762 |
|
Thread.sleep(SMALL_DELAY_MS); |
763 |
< |
threadAssertEquals(one, q.take()); |
763 |
> |
assertSame(one, q.take()); |
764 |
|
}}); |
765 |
|
|
766 |
|
joinPool(executor); |
776 |
– |
|
767 |
|
} |
768 |
|
|
769 |
|
/** |
774 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
775 |
|
executor.execute(new CheckedRunnable() { |
776 |
|
public void realRun() throws InterruptedException { |
777 |
< |
threadAssertNull(q.poll()); |
778 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
779 |
< |
threadAssertTrue(q.isEmpty()); |
777 |
> |
assertNull(q.poll()); |
778 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
779 |
> |
assertTrue(q.isEmpty()); |
780 |
|
}}); |
781 |
|
|
782 |
|
executor.execute(new CheckedRunnable() { |