37 |
|
} |
38 |
|
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run(suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
108 |
|
*/ |
109 |
|
public void testConstructor5() { |
110 |
|
Integer[] ints = new Integer[SIZE]; |
111 |
< |
for (int i = 0; i < SIZE-1; ++i) |
111 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
112 |
|
ints[i] = new Integer(i); |
113 |
|
Collection<Integer> elements = Arrays.asList(ints); |
114 |
|
try { |
148 |
|
* remainingCapacity decreases on add, increases on remove |
149 |
|
*/ |
150 |
|
public void testRemainingCapacity() { |
151 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
151 |
> |
BlockingQueue q = populatedQueue(SIZE); |
152 |
|
for (int i = 0; i < SIZE; ++i) { |
153 |
|
assertEquals(i, q.remainingCapacity()); |
154 |
< |
assertEquals(SIZE-i, q.size()); |
155 |
< |
q.remove(); |
154 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
155 |
> |
assertEquals(i, q.remove()); |
156 |
|
} |
157 |
|
for (int i = 0; i < SIZE; ++i) { |
158 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
159 |
< |
assertEquals(i, q.size()); |
160 |
< |
q.add(new Integer(i)); |
158 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
159 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
160 |
> |
assertTrue(q.add(i)); |
161 |
|
} |
162 |
|
} |
163 |
|
|
202 |
|
public void testAddAll3() { |
203 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
204 |
|
Integer[] ints = new Integer[SIZE]; |
205 |
< |
for (int i = 0; i < SIZE-1; ++i) |
205 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
206 |
|
ints[i] = new Integer(i); |
207 |
|
Collection<Integer> elements = Arrays.asList(ints); |
208 |
|
try { |
440 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
441 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
442 |
|
public void realRun() throws InterruptedException { |
443 |
+ |
long startTime = System.nanoTime(); |
444 |
|
for (int i = 0; i < SIZE; ++i) { |
444 |
– |
long t0 = System.nanoTime(); |
445 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
446 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
446 |
|
} |
448 |
– |
long t0 = System.nanoTime(); |
447 |
|
aboutToWait.countDown(); |
448 |
|
try { |
449 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
449 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
450 |
|
shouldThrow(); |
451 |
|
} catch (InterruptedException success) { |
452 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
452 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
453 |
|
} |
454 |
|
}}); |
455 |
|
|
456 |
< |
aboutToWait.await(); |
457 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
456 |
> |
await(aboutToWait); |
457 |
> |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
458 |
|
t.interrupt(); |
459 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
459 |
> |
awaitTermination(t); |
460 |
|
checkEmpty(q); |
461 |
|
} |
462 |
|
|
572 |
|
assertTrue(changed); |
573 |
|
|
574 |
|
assertTrue(q.containsAll(p)); |
575 |
< |
assertEquals(SIZE-i, q.size()); |
575 |
> |
assertEquals(SIZE - i, q.size()); |
576 |
|
p.remove(); |
577 |
|
} |
578 |
|
} |
585 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
586 |
|
LinkedBlockingQueue p = populatedQueue(i); |
587 |
|
assertTrue(q.removeAll(p)); |
588 |
< |
assertEquals(SIZE-i, q.size()); |
588 |
> |
assertEquals(SIZE - i, q.size()); |
589 |
|
for (int j = 0; j < i; ++j) { |
590 |
|
Integer x = (Integer)(p.remove()); |
591 |
|
assertFalse(q.contains(x)); |
632 |
|
public void testIterator() throws InterruptedException { |
633 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
634 |
|
Iterator it = q.iterator(); |
635 |
< |
while (it.hasNext()) { |
635 |
> |
int i; |
636 |
> |
for (i = 0; it.hasNext(); i++) |
637 |
> |
assertTrue(q.contains(it.next())); |
638 |
> |
assertEquals(i, SIZE); |
639 |
> |
assertIteratorExhausted(it); |
640 |
> |
|
641 |
> |
it = q.iterator(); |
642 |
> |
for (i = 0; it.hasNext(); i++) |
643 |
|
assertEquals(it.next(), q.take()); |
644 |
< |
} |
644 |
> |
assertEquals(i, SIZE); |
645 |
> |
assertIteratorExhausted(it); |
646 |
> |
} |
647 |
> |
|
648 |
> |
/** |
649 |
> |
* iterator of empty collection has no elements |
650 |
> |
*/ |
651 |
> |
public void testEmptyIterator() { |
652 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
653 |
|
} |
654 |
|
|
655 |
|
/** |
720 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
721 |
|
q.add(one); |
722 |
|
q.add(two); |
710 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
723 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
724 |
< |
executor.execute(new CheckedRunnable() { |
725 |
< |
public void realRun() throws InterruptedException { |
726 |
< |
assertFalse(q.offer(three)); |
727 |
< |
threadsStarted.await(); |
728 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
729 |
< |
assertEquals(0, q.remainingCapacity()); |
730 |
< |
}}); |
731 |
< |
|
732 |
< |
executor.execute(new CheckedRunnable() { |
733 |
< |
public void realRun() throws InterruptedException { |
734 |
< |
threadsStarted.await(); |
735 |
< |
assertSame(one, q.take()); |
736 |
< |
}}); |
737 |
< |
|
738 |
< |
joinPool(executor); |
724 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
725 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
726 |
> |
executor.execute(new CheckedRunnable() { |
727 |
> |
public void realRun() throws InterruptedException { |
728 |
> |
assertFalse(q.offer(three)); |
729 |
> |
threadsStarted.await(); |
730 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
731 |
> |
assertEquals(0, q.remainingCapacity()); |
732 |
> |
}}); |
733 |
> |
|
734 |
> |
executor.execute(new CheckedRunnable() { |
735 |
> |
public void realRun() throws InterruptedException { |
736 |
> |
threadsStarted.await(); |
737 |
> |
assertSame(one, q.take()); |
738 |
> |
}}); |
739 |
> |
} |
740 |
|
} |
741 |
|
|
742 |
|
/** |
745 |
|
public void testPollInExecutor() { |
746 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
747 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
748 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
749 |
< |
executor.execute(new CheckedRunnable() { |
750 |
< |
public void realRun() throws InterruptedException { |
751 |
< |
assertNull(q.poll()); |
752 |
< |
threadsStarted.await(); |
753 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
754 |
< |
checkEmpty(q); |
755 |
< |
}}); |
756 |
< |
|
757 |
< |
executor.execute(new CheckedRunnable() { |
758 |
< |
public void realRun() throws InterruptedException { |
759 |
< |
threadsStarted.await(); |
760 |
< |
q.put(one); |
761 |
< |
}}); |
762 |
< |
|
763 |
< |
joinPool(executor); |
748 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
749 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
750 |
> |
executor.execute(new CheckedRunnable() { |
751 |
> |
public void realRun() throws InterruptedException { |
752 |
> |
assertNull(q.poll()); |
753 |
> |
threadsStarted.await(); |
754 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
755 |
> |
checkEmpty(q); |
756 |
> |
}}); |
757 |
> |
|
758 |
> |
executor.execute(new CheckedRunnable() { |
759 |
> |
public void realRun() throws InterruptedException { |
760 |
> |
threadsStarted.await(); |
761 |
> |
q.put(one); |
762 |
> |
}}); |
763 |
> |
} |
764 |
|
} |
765 |
|
|
766 |
|
/** |
812 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
813 |
|
Thread t = new Thread(new CheckedRunnable() { |
814 |
|
public void realRun() throws InterruptedException { |
815 |
< |
q.put(new Integer(SIZE+1)); |
815 |
> |
q.put(new Integer(SIZE + 1)); |
816 |
|
}}); |
817 |
|
|
818 |
|
t.start(); |
837 |
|
q.drainTo(l, i); |
838 |
|
int k = (i < SIZE) ? i : SIZE; |
839 |
|
assertEquals(k, l.size()); |
840 |
< |
assertEquals(SIZE-k, q.size()); |
840 |
> |
assertEquals(SIZE - k, q.size()); |
841 |
|
for (int j = 0; j < k; ++j) |
842 |
|
assertEquals(l.get(j), new Integer(j)); |
843 |
|
do {} while (q.poll() != null); |