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 { |
315 |
|
public void testBlockingPut() throws InterruptedException { |
316 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
317 |
|
Thread t = new Thread(new CheckedRunnable() { |
318 |
< |
public void realRun() { |
319 |
< |
int added = 0; |
318 |
> |
public void realRun() throws InterruptedException { |
319 |
> |
for (int i = 0; i < SIZE; ++i) |
320 |
> |
q.put(i); |
321 |
> |
assertEquals(SIZE, q.size()); |
322 |
> |
assertEquals(0, q.remainingCapacity()); |
323 |
|
try { |
324 |
< |
for (int i = 0; i < SIZE; ++i) { |
325 |
< |
q.put(new Integer(i)); |
326 |
< |
++added; |
327 |
< |
} |
306 |
< |
q.put(new Integer(SIZE)); |
307 |
< |
threadShouldThrow(); |
308 |
< |
} catch (InterruptedException success) { |
309 |
< |
threadAssertEquals(added, SIZE); |
310 |
< |
}}}); |
324 |
> |
q.put(99); |
325 |
> |
shouldThrow(); |
326 |
> |
} catch (InterruptedException success) {} |
327 |
> |
}}); |
328 |
|
|
329 |
|
t.start(); |
330 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
330 |
> |
Thread.sleep(SHORT_DELAY_MS); |
331 |
|
t.interrupt(); |
332 |
|
t.join(); |
333 |
+ |
assertEquals(SIZE, q.size()); |
334 |
+ |
assertEquals(0, q.remainingCapacity()); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
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 |
|
/** |
365 |
|
*/ |
366 |
|
public void testTimedOffer() throws InterruptedException { |
367 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
368 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
368 |
> |
Thread t = new Thread(new CheckedRunnable() { |
369 |
|
public void realRun() throws InterruptedException { |
370 |
|
q.put(new Object()); |
371 |
|
q.put(new Object()); |
372 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
373 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
374 |
< |
}}; |
372 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
373 |
> |
try { |
374 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
375 |
> |
shouldThrow(); |
376 |
> |
} catch (InterruptedException success) {} |
377 |
> |
}}); |
378 |
|
|
379 |
|
t.start(); |
380 |
|
Thread.sleep(SHORT_DELAY_MS); |
388 |
|
public void testTake() throws InterruptedException { |
389 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
390 |
|
for (int i = 0; i < SIZE; ++i) { |
391 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
391 |
> |
assertEquals(i, q.take()); |
392 |
|
} |
393 |
|
} |
394 |
|
|
412 |
|
* Take removes existing elements until empty, then blocks interruptibly |
413 |
|
*/ |
414 |
|
public void testBlockingTake() throws InterruptedException { |
415 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
415 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
416 |
> |
Thread t = new Thread(new CheckedRunnable() { |
417 |
|
public void realRun() throws InterruptedException { |
399 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
418 |
|
for (int i = 0; i < SIZE; ++i) { |
419 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
419 |
> |
assertEquals(i, q.take()); |
420 |
|
} |
421 |
< |
q.take(); |
422 |
< |
}}; |
421 |
> |
try { |
422 |
> |
q.take(); |
423 |
> |
shouldThrow(); |
424 |
> |
} catch (InterruptedException success) {} |
425 |
> |
}}); |
426 |
|
|
427 |
|
t.start(); |
428 |
< |
Thread.sleep(SHORT_DELAY_MS); |
429 |
< |
t.interrupt(); |
430 |
< |
t.join(); |
428 |
> |
Thread.sleep(SHORT_DELAY_MS); |
429 |
> |
t.interrupt(); |
430 |
> |
t.join(); |
431 |
|
} |
432 |
|
|
433 |
|
|
437 |
|
public void testPoll() { |
438 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
439 |
|
for (int i = 0; i < SIZE; ++i) { |
440 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
440 |
> |
assertEquals(i, q.poll()); |
441 |
|
} |
442 |
|
assertNull(q.poll()); |
443 |
|
} |
448 |
|
public void testTimedPoll0() throws InterruptedException { |
449 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
450 |
|
for (int i = 0; i < SIZE; ++i) { |
451 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
451 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
452 |
|
} |
453 |
|
assertNull(q.poll(0, MILLISECONDS)); |
454 |
|
} |
459 |
|
public void testTimedPoll() throws InterruptedException { |
460 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
461 |
|
for (int i = 0; i < SIZE; ++i) { |
462 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
462 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
463 |
|
} |
464 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
465 |
|
} |
473 |
|
public void realRun() throws InterruptedException { |
474 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
475 |
|
for (int i = 0; i < SIZE; ++i) { |
476 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
476 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; |
477 |
|
} |
478 |
|
try { |
479 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
488 |
|
} |
489 |
|
|
490 |
|
/** |
470 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
471 |
– |
* on interruption throws |
472 |
– |
*/ |
473 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
474 |
– |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
475 |
– |
Thread t = new Thread(new CheckedRunnable() { |
476 |
– |
public void realRun() throws InterruptedException { |
477 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
478 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
479 |
– |
try { |
480 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
481 |
– |
shouldThrow(); |
482 |
– |
} catch (InterruptedException success) {} |
483 |
– |
}}); |
484 |
– |
|
485 |
– |
t.start(); |
486 |
– |
Thread.sleep(SMALL_DELAY_MS); |
487 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
488 |
– |
t.interrupt(); |
489 |
– |
t.join(); |
490 |
– |
} |
491 |
– |
|
492 |
– |
|
493 |
– |
/** |
491 |
|
* peek returns next element, or null if empty |
492 |
|
*/ |
493 |
|
public void testPeek() { |
494 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
495 |
|
for (int i = 0; i < SIZE; ++i) { |
496 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
497 |
< |
q.poll(); |
496 |
> |
assertEquals(i, q.peek()); |
497 |
> |
assertEquals(i, q.poll()); |
498 |
|
assertTrue(q.peek() == null || |
499 |
< |
i != ((Integer)q.peek()).intValue()); |
499 |
> |
!q.peek().equals(i)); |
500 |
|
} |
501 |
|
assertNull(q.peek()); |
502 |
|
} |
507 |
|
public void testElement() { |
508 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
509 |
|
for (int i = 0; i < SIZE; ++i) { |
510 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
511 |
< |
q.poll(); |
510 |
> |
assertEquals(i, q.element()); |
511 |
> |
assertEquals(i, q.poll()); |
512 |
|
} |
513 |
|
try { |
514 |
|
q.element(); |
522 |
|
public void testRemove() { |
523 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
524 |
|
for (int i = 0; i < SIZE; ++i) { |
525 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
525 |
> |
assertEquals(i, q.remove()); |
526 |
|
} |
527 |
|
try { |
528 |
|
q.remove(); |
552 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
553 |
|
for (int i = 0; i < SIZE; ++i) { |
554 |
|
assertTrue(q.contains(new Integer(i))); |
555 |
< |
q.poll(); |
555 |
> |
assertEquals(i, q.poll()); |
556 |
|
assertFalse(q.contains(new Integer(i))); |
557 |
|
} |
558 |
|
} |
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); |
647 |
|
* toArray(null) throws NPE |
648 |
|
*/ |
649 |
|
public void testToArray_BadArg() { |
650 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
651 |
|
try { |
654 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
652 |
|
Object o[] = q.toArray(null); |
653 |
|
shouldThrow(); |
654 |
|
} catch (NullPointerException success) {} |
658 |
|
* toArray with incompatible array type throws CCE |
659 |
|
*/ |
660 |
|
public void testToArray1_BadArg() { |
661 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
662 |
|
try { |
663 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
666 |
< |
Object o[] = q.toArray(new String[10] ); |
663 |
> |
Object o[] = q.toArray(new String[10]); |
664 |
|
shouldThrow(); |
665 |
|
} catch (ArrayStoreException success) {} |
666 |
|
} |
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 |
|
|
709 |
|
|
710 |
|
int k = 0; |
711 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
712 |
< |
int i = ((Integer)(it.next())).intValue(); |
716 |
< |
assertEquals(++k, i); |
712 |
> |
assertEquals(++k, it.next()); |
713 |
|
} |
714 |
|
assertEquals(3, k); |
715 |
|
} |
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); |
771 |
– |
|
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() { |
896 |
|
} |
897 |
|
|
898 |
|
/** |
899 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
899 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
900 |
|
*/ |
901 |
|
public void testDrainToN() { |
902 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
905 |
|
assertTrue(q.offer(new Integer(j))); |
906 |
|
ArrayList l = new ArrayList(); |
907 |
|
q.drainTo(l, i); |
908 |
< |
int k = (i < SIZE)? i : SIZE; |
908 |
> |
int k = (i < SIZE) ? i : SIZE; |
909 |
|
assertEquals(l.size(), k); |
910 |
|
assertEquals(q.size(), SIZE-k); |
911 |
|
for (int j = 0; j < k; ++j) |