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 |
|
|
395 |
|
/** |
378 |
– |
* take blocks interruptibly when empty |
379 |
– |
*/ |
380 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
381 |
– |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
382 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
383 |
– |
public void realRun() throws InterruptedException { |
384 |
– |
q.take(); |
385 |
– |
}}; |
386 |
– |
|
387 |
– |
t.start(); |
388 |
– |
Thread.sleep(SHORT_DELAY_MS); |
389 |
– |
t.interrupt(); |
390 |
– |
t.join(); |
391 |
– |
} |
392 |
– |
|
393 |
– |
/** |
396 |
|
* Take removes existing elements until empty, then blocks interruptibly |
397 |
|
*/ |
398 |
|
public void testBlockingTake() throws InterruptedException { |
399 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
399 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
400 |
> |
Thread t = new Thread(new CheckedRunnable() { |
401 |
|
public void realRun() throws InterruptedException { |
399 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
402 |
|
for (int i = 0; i < SIZE; ++i) { |
403 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
403 |
> |
assertEquals(i, q.take()); |
404 |
|
} |
405 |
< |
q.take(); |
406 |
< |
}}; |
405 |
> |
try { |
406 |
> |
q.take(); |
407 |
> |
shouldThrow(); |
408 |
> |
} catch (InterruptedException success) {} |
409 |
> |
}}); |
410 |
|
|
411 |
|
t.start(); |
412 |
< |
Thread.sleep(SHORT_DELAY_MS); |
413 |
< |
t.interrupt(); |
414 |
< |
t.join(); |
412 |
> |
Thread.sleep(SHORT_DELAY_MS); |
413 |
> |
t.interrupt(); |
414 |
> |
t.join(); |
415 |
|
} |
416 |
|
|
417 |
|
|
421 |
|
public void testPoll() { |
422 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
424 |
> |
assertEquals(i, q.poll()); |
425 |
|
} |
426 |
|
assertNull(q.poll()); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
430 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
431 |
|
*/ |
432 |
|
public void testTimedPoll0() throws InterruptedException { |
433 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
434 |
|
for (int i = 0; i < SIZE; ++i) { |
435 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
435 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
436 |
|
} |
437 |
|
assertNull(q.poll(0, MILLISECONDS)); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
441 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
442 |
|
*/ |
443 |
|
public void testTimedPoll() throws InterruptedException { |
444 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
445 |
|
for (int i = 0; i < SIZE; ++i) { |
446 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
446 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
447 |
|
} |
448 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
449 |
|
} |
457 |
|
public void realRun() throws InterruptedException { |
458 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
459 |
|
for (int i = 0; i < SIZE; ++i) { |
460 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
460 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; |
461 |
|
} |
462 |
|
try { |
463 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
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 |
– |
/** |
475 |
|
* peek returns next element, or null if empty |
476 |
|
*/ |
477 |
|
public void testPeek() { |
478 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
479 |
|
for (int i = 0; i < SIZE; ++i) { |
480 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
481 |
< |
q.poll(); |
480 |
> |
assertEquals(i, q.peek()); |
481 |
> |
assertEquals(i, q.poll()); |
482 |
|
assertTrue(q.peek() == null || |
483 |
< |
i != ((Integer)q.peek()).intValue()); |
483 |
> |
!q.peek().equals(i)); |
484 |
|
} |
485 |
|
assertNull(q.peek()); |
486 |
|
} |
491 |
|
public void testElement() { |
492 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
493 |
|
for (int i = 0; i < SIZE; ++i) { |
494 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
495 |
< |
q.poll(); |
494 |
> |
assertEquals(i, q.element()); |
495 |
> |
assertEquals(i, q.poll()); |
496 |
|
} |
497 |
|
try { |
498 |
|
q.element(); |
506 |
|
public void testRemove() { |
507 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
508 |
|
for (int i = 0; i < SIZE; ++i) { |
509 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
509 |
> |
assertEquals(i, q.remove()); |
510 |
|
} |
511 |
|
try { |
512 |
|
q.remove(); |
536 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
537 |
|
for (int i = 0; i < SIZE; ++i) { |
538 |
|
assertTrue(q.contains(new Integer(i))); |
539 |
< |
q.poll(); |
539 |
> |
assertEquals(i, q.poll()); |
540 |
|
assertFalse(q.contains(new Integer(i))); |
541 |
|
} |
542 |
|
} |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
< |
* toArray contains all elements |
610 |
> |
* toArray contains all elements in FIFO order |
611 |
|
*/ |
612 |
< |
public void testToArray() throws InterruptedException { |
612 |
> |
public void testToArray() { |
613 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
614 |
|
Object[] o = q.toArray(); |
615 |
|
for (int i = 0; i < o.length; i++) |
616 |
< |
assertEquals(o[i], q.take()); |
616 |
> |
assertSame(o[i], q.poll()); |
617 |
|
} |
618 |
|
|
619 |
|
/** |
620 |
< |
* toArray(a) contains all elements |
620 |
> |
* toArray(a) contains all elements in FIFO order |
621 |
|
*/ |
622 |
< |
public void testToArray2() throws InterruptedException { |
622 |
> |
public void testToArray2() { |
623 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
624 |
|
Integer[] ints = new Integer[SIZE]; |
625 |
< |
ints = (Integer[])q.toArray(ints); |
625 |
> |
assertSame(ints, q.toArray(ints)); |
626 |
|
for (int i = 0; i < ints.length; i++) |
627 |
< |
assertEquals(ints[i], q.take()); |
627 |
> |
assertSame(ints[i], q.poll()); |
628 |
|
} |
629 |
|
|
630 |
|
/** |
631 |
< |
* toArray(null) throws NPE |
631 |
> |
* toArray(null) throws NullPointerException |
632 |
|
*/ |
633 |
< |
public void testToArray_BadArg() { |
633 |
> |
public void testToArray_NullArg() { |
634 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
635 |
|
try { |
636 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
655 |
< |
Object o[] = q.toArray(null); |
636 |
> |
q.toArray(null); |
637 |
|
shouldThrow(); |
638 |
|
} catch (NullPointerException success) {} |
639 |
|
} |
640 |
|
|
641 |
|
/** |
642 |
< |
* toArray with incompatible array type throws CCE |
642 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
643 |
|
*/ |
644 |
|
public void testToArray1_BadArg() { |
645 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
646 |
|
try { |
647 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
666 |
< |
Object o[] = q.toArray(new String[10] ); |
647 |
> |
q.toArray(new String[10]); |
648 |
|
shouldThrow(); |
649 |
|
} catch (ArrayStoreException success) {} |
650 |
|
} |
664 |
|
/** |
665 |
|
* iterator.remove removes current element |
666 |
|
*/ |
667 |
< |
public void testIteratorRemove () { |
667 |
> |
public void testIteratorRemove() { |
668 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
669 |
|
q.add(two); |
670 |
|
q.add(one); |
675 |
|
it.remove(); |
676 |
|
|
677 |
|
it = q.iterator(); |
678 |
< |
assertEquals(it.next(), one); |
679 |
< |
assertEquals(it.next(), three); |
678 |
> |
assertSame(it.next(), one); |
679 |
> |
assertSame(it.next(), three); |
680 |
|
assertFalse(it.hasNext()); |
681 |
|
} |
682 |
|
|
693 |
|
|
694 |
|
int k = 0; |
695 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
696 |
< |
int i = ((Integer)(it.next())).intValue(); |
716 |
< |
assertEquals(++k, i); |
696 |
> |
assertEquals(++k, it.next()); |
697 |
|
} |
698 |
|
assertEquals(3, k); |
699 |
|
} |
701 |
|
/** |
702 |
|
* Modifications do not cause iterators to fail |
703 |
|
*/ |
704 |
< |
public void testWeaklyConsistentIteration () { |
704 |
> |
public void testWeaklyConsistentIteration() { |
705 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
706 |
|
q.add(one); |
707 |
|
q.add(two); |
736 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
737 |
|
executor.execute(new CheckedRunnable() { |
738 |
|
public void realRun() throws InterruptedException { |
739 |
< |
threadAssertFalse(q.offer(three)); |
740 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
741 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
739 |
> |
assertFalse(q.offer(three)); |
740 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
741 |
> |
assertEquals(0, q.remainingCapacity()); |
742 |
|
}}); |
743 |
|
|
744 |
|
executor.execute(new CheckedRunnable() { |
745 |
|
public void realRun() throws InterruptedException { |
746 |
|
Thread.sleep(SMALL_DELAY_MS); |
747 |
< |
threadAssertEquals(one, q.take()); |
747 |
> |
assertSame(one, q.take()); |
748 |
|
}}); |
749 |
|
|
750 |
|
joinPool(executor); |
771 |
– |
|
751 |
|
} |
752 |
|
|
753 |
|
/** |
758 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
759 |
|
executor.execute(new CheckedRunnable() { |
760 |
|
public void realRun() throws InterruptedException { |
761 |
< |
threadAssertNull(q.poll()); |
762 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
763 |
< |
threadAssertTrue(q.isEmpty()); |
761 |
> |
assertNull(q.poll()); |
762 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
763 |
> |
assertTrue(q.isEmpty()); |
764 |
|
}}); |
765 |
|
|
766 |
|
executor.execute(new CheckedRunnable() { |
880 |
|
} |
881 |
|
|
882 |
|
/** |
883 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
883 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
884 |
|
*/ |
885 |
|
public void testDrainToN() { |
886 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
889 |
|
assertTrue(q.offer(new Integer(j))); |
890 |
|
ArrayList l = new ArrayList(); |
891 |
|
q.drainTo(l, i); |
892 |
< |
int k = (i < SIZE)? i : SIZE; |
892 |
> |
int k = (i < SIZE) ? i : SIZE; |
893 |
|
assertEquals(l.size(), k); |
894 |
|
assertEquals(q.size(), SIZE-k); |
895 |
|
for (int j = 0; j < k; ++j) |