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); |
296 |
|
public void testBlockingPut() throws InterruptedException { |
297 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
298 |
|
Thread t = new Thread(new CheckedRunnable() { |
299 |
< |
public void realRun() { |
300 |
< |
int added = 0; |
299 |
> |
public void realRun() throws InterruptedException { |
300 |
> |
for (int i = 0; i < SIZE; ++i) |
301 |
> |
q.put(i); |
302 |
> |
assertEquals(SIZE, q.size()); |
303 |
> |
assertEquals(0, q.remainingCapacity()); |
304 |
|
try { |
305 |
< |
for (int i = 0; i < SIZE; ++i) { |
306 |
< |
q.put(new Integer(i)); |
307 |
< |
++added; |
308 |
< |
} |
306 |
< |
q.put(new Integer(SIZE)); |
307 |
< |
threadShouldThrow(); |
308 |
< |
} catch (InterruptedException success) { |
309 |
< |
threadAssertEquals(added, SIZE); |
310 |
< |
}}}); |
305 |
> |
q.put(99); |
306 |
> |
shouldThrow(); |
307 |
> |
} catch (InterruptedException success) {} |
308 |
> |
}}); |
309 |
|
|
310 |
|
t.start(); |
311 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
311 |
> |
Thread.sleep(SHORT_DELAY_MS); |
312 |
|
t.interrupt(); |
313 |
|
t.join(); |
314 |
+ |
assertEquals(SIZE, q.size()); |
315 |
+ |
assertEquals(0, q.remainingCapacity()); |
316 |
|
} |
317 |
|
|
318 |
|
/** |
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 |
|
/** |
346 |
|
*/ |
347 |
|
public void testTimedOffer() throws InterruptedException { |
348 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
349 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
349 |
> |
Thread t = new Thread(new CheckedRunnable() { |
350 |
|
public void realRun() throws InterruptedException { |
351 |
|
q.put(new Object()); |
352 |
|
q.put(new Object()); |
353 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
354 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
355 |
< |
}}; |
353 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
354 |
> |
try { |
355 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
356 |
> |
shouldThrow(); |
357 |
> |
} catch (InterruptedException success) {} |
358 |
> |
}}); |
359 |
|
|
360 |
|
t.start(); |
361 |
|
Thread.sleep(SHORT_DELAY_MS); |
369 |
|
public void testTake() throws InterruptedException { |
370 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
371 |
|
for (int i = 0; i < SIZE; ++i) { |
372 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
372 |
> |
assertEquals(i, q.take()); |
373 |
|
} |
374 |
|
} |
375 |
|
|
393 |
|
* Take removes existing elements until empty, then blocks interruptibly |
394 |
|
*/ |
395 |
|
public void testBlockingTake() throws InterruptedException { |
396 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
396 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
397 |
> |
Thread t = new Thread(new CheckedRunnable() { |
398 |
|
public void realRun() throws InterruptedException { |
399 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
399 |
|
for (int i = 0; i < SIZE; ++i) { |
400 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
400 |
> |
assertEquals(i, q.take()); |
401 |
|
} |
402 |
< |
q.take(); |
403 |
< |
}}; |
402 |
> |
try { |
403 |
> |
q.take(); |
404 |
> |
shouldThrow(); |
405 |
> |
} catch (InterruptedException success) {} |
406 |
> |
}}); |
407 |
|
|
408 |
|
t.start(); |
409 |
< |
Thread.sleep(SHORT_DELAY_MS); |
410 |
< |
t.interrupt(); |
411 |
< |
t.join(); |
409 |
> |
Thread.sleep(SHORT_DELAY_MS); |
410 |
> |
t.interrupt(); |
411 |
> |
t.join(); |
412 |
|
} |
413 |
|
|
414 |
|
|
418 |
|
public void testPoll() { |
419 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
420 |
|
for (int i = 0; i < SIZE; ++i) { |
421 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
421 |
> |
assertEquals(i, q.poll()); |
422 |
|
} |
423 |
|
assertNull(q.poll()); |
424 |
|
} |
429 |
|
public void testTimedPoll0() throws InterruptedException { |
430 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
431 |
|
for (int i = 0; i < SIZE; ++i) { |
432 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
432 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
433 |
|
} |
434 |
|
assertNull(q.poll(0, MILLISECONDS)); |
435 |
|
} |
440 |
|
public void testTimedPoll() throws InterruptedException { |
441 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
442 |
|
for (int i = 0; i < SIZE; ++i) { |
443 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
443 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
444 |
|
} |
445 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
446 |
|
} |
450 |
|
* returning timeout status |
451 |
|
*/ |
452 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
453 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
453 |
> |
Thread t = new Thread(new CheckedRunnable() { |
454 |
|
public void realRun() throws InterruptedException { |
455 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
456 |
|
for (int i = 0; i < SIZE; ++i) { |
457 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
457 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; |
458 |
|
} |
459 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
460 |
< |
}}; |
459 |
> |
try { |
460 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
461 |
> |
shouldThrow(); |
462 |
> |
} catch (InterruptedException success) {} |
463 |
> |
}}); |
464 |
|
|
465 |
|
t.start(); |
466 |
< |
Thread.sleep(SMALL_DELAY_MS); |
466 |
> |
Thread.sleep(SHORT_DELAY_MS); |
467 |
|
t.interrupt(); |
468 |
|
t.join(); |
469 |
|
} |
474 |
|
*/ |
475 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
476 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
477 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
477 |
> |
Thread t = new Thread(new CheckedRunnable() { |
478 |
|
public void realRun() throws InterruptedException { |
479 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
480 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
481 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
482 |
< |
}}; |
479 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
480 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
481 |
> |
try { |
482 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
483 |
> |
shouldThrow(); |
484 |
> |
} catch (InterruptedException success) {} |
485 |
> |
}}); |
486 |
|
|
487 |
|
t.start(); |
488 |
|
Thread.sleep(SMALL_DELAY_MS); |
498 |
|
public void testPeek() { |
499 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
500 |
|
for (int i = 0; i < SIZE; ++i) { |
501 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
502 |
< |
q.poll(); |
501 |
> |
assertEquals(i, q.peek()); |
502 |
> |
assertEquals(i, q.poll()); |
503 |
|
assertTrue(q.peek() == null || |
504 |
< |
i != ((Integer)q.peek()).intValue()); |
504 |
> |
!q.peek().equals(i)); |
505 |
|
} |
506 |
|
assertNull(q.peek()); |
507 |
|
} |
512 |
|
public void testElement() { |
513 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
514 |
|
for (int i = 0; i < SIZE; ++i) { |
515 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
516 |
< |
q.poll(); |
515 |
> |
assertEquals(i, q.element()); |
516 |
> |
assertEquals(i, q.poll()); |
517 |
|
} |
518 |
|
try { |
519 |
|
q.element(); |
527 |
|
public void testRemove() { |
528 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
529 |
|
for (int i = 0; i < SIZE; ++i) { |
530 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
530 |
> |
assertEquals(i, q.remove()); |
531 |
|
} |
532 |
|
try { |
533 |
|
q.remove(); |
557 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
558 |
|
for (int i = 0; i < SIZE; ++i) { |
559 |
|
assertTrue(q.contains(new Integer(i))); |
560 |
< |
q.poll(); |
560 |
> |
assertEquals(i, q.poll()); |
561 |
|
assertFalse(q.contains(new Integer(i))); |
562 |
|
} |
563 |
|
} |
652 |
|
* toArray(null) throws NPE |
653 |
|
*/ |
654 |
|
public void testToArray_BadArg() { |
655 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
656 |
|
try { |
648 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
657 |
|
Object o[] = q.toArray(null); |
658 |
|
shouldThrow(); |
659 |
|
} catch (NullPointerException success) {} |
663 |
|
* toArray with incompatible array type throws CCE |
664 |
|
*/ |
665 |
|
public void testToArray1_BadArg() { |
666 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
667 |
|
try { |
668 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
660 |
< |
Object o[] = q.toArray(new String[10] ); |
668 |
> |
Object o[] = q.toArray(new String[10]); |
669 |
|
shouldThrow(); |
670 |
|
} catch (ArrayStoreException success) {} |
671 |
|
} |
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 |
|
|
714 |
|
|
715 |
|
int k = 0; |
716 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
717 |
< |
int i = ((Integer)(it.next())).intValue(); |
710 |
< |
assertEquals(++k, i); |
717 |
> |
assertEquals(++k, it.next()); |
718 |
|
} |
719 |
|
assertEquals(3, k); |
720 |
|
} |
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); |
765 |
– |
|
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() { |