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 |
|
/** |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* Constructor throws IAE if capacity argument nonpositive |
63 |
> |
* Constructor throws IAE if capacity argument nonpositive |
64 |
|
*/ |
65 |
|
public void testConstructor2() { |
66 |
|
try { |
120 |
|
* Queue contains all elements of collection used to initialize |
121 |
|
*/ |
122 |
|
public void testConstructor7() { |
123 |
< |
try { |
124 |
< |
Integer[] ints = new Integer[SIZE]; |
125 |
< |
for (int i = 0; i < SIZE; ++i) |
126 |
< |
ints[i] = new Integer(i); |
127 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
128 |
< |
for (int i = 0; i < SIZE; ++i) |
113 |
< |
assertEquals(ints[i], q.poll()); |
114 |
< |
} |
115 |
< |
finally {} |
123 |
> |
Integer[] ints = new Integer[SIZE]; |
124 |
> |
for (int i = 0; i < SIZE; ++i) |
125 |
> |
ints[i] = new Integer(i); |
126 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
127 |
> |
for (int i = 0; i < SIZE; ++i) |
128 |
> |
assertEquals(ints[i], q.poll()); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
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 |
|
*/ |
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 |
< |
} |
309 |
< |
q.put(new Integer(SIZE)); |
310 |
< |
threadShouldThrow(); |
311 |
< |
} catch (InterruptedException ie) { |
312 |
< |
threadAssertEquals(added, SIZE); |
313 |
< |
}}}); |
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()); |
333 |
< |
++added; |
334 |
< |
q.put(new Object()); |
335 |
< |
++added; |
336 |
< |
q.put(new Object()); |
337 |
< |
++added; |
338 |
< |
threadShouldThrow(); |
339 |
< |
} catch (InterruptedException e) { |
340 |
< |
threadAssertTrue(added >= 2); |
341 |
< |
} |
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 { |
402 |
– |
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 |
|
} |
469 |
|
* returning timeout status |
470 |
|
*/ |
471 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
472 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
472 |
> |
Thread t = new Thread(new CheckedRunnable() { |
473 |
|
public void realRun() throws InterruptedException { |
474 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
475 |
|
for (int i = 0; i < SIZE; ++i) { |
476 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
476 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; |
477 |
|
} |
478 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
479 |
< |
}}; |
480 |
< |
|
481 |
< |
t.start(); |
482 |
< |
Thread.sleep(SMALL_DELAY_MS); |
465 |
< |
t.interrupt(); |
466 |
< |
t.join(); |
467 |
< |
} |
468 |
< |
|
469 |
< |
/** |
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 ThreadShouldThrow(InterruptedException.class) { |
476 |
< |
public void realRun() throws InterruptedException { |
477 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
478 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
479 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
480 |
< |
}}; |
478 |
> |
try { |
479 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
480 |
> |
shouldThrow(); |
481 |
> |
} catch (InterruptedException success) {} |
482 |
> |
}}); |
483 |
|
|
484 |
|
t.start(); |
485 |
< |
Thread.sleep(SMALL_DELAY_MS); |
484 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
485 |
> |
Thread.sleep(SHORT_DELAY_MS); |
486 |
|
t.interrupt(); |
487 |
|
t.join(); |
488 |
|
} |
489 |
|
|
489 |
– |
|
490 |
|
/** |
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 |
|
} |
647 |
|
* toArray(null) throws NPE |
648 |
|
*/ |
649 |
|
public void testToArray_BadArg() { |
650 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
651 |
|
try { |
651 |
– |
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); |
663 |
< |
Object o[] = q.toArray(new String[10] ); |
663 |
> |
Object o[] = q.toArray(new String[10]); |
664 |
|
shouldThrow(); |
665 |
< |
} catch (ArrayStoreException success) {} |
665 |
> |
} catch (ArrayStoreException success) {} |
666 |
|
} |
667 |
|
|
668 |
|
|
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(); |
713 |
< |
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); |
768 |
– |
|
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() { |