6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.Arrays; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
|
import java.util.ArrayList; |
12 |
+ |
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.Iterator; |
15 |
|
import java.util.NoSuchElementException; |
16 |
|
import java.util.Queue; |
17 |
|
import java.util.concurrent.BlockingQueue; |
18 |
|
import java.util.concurrent.CountDownLatch; |
18 |
– |
import java.util.concurrent.LinkedBlockingQueue; |
19 |
|
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
26 |
|
|
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() { |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* Creates a queue of given size containing consecutive |
51 |
< |
* Integers 0 ... n. |
50 |
> |
* Returns a new queue of given size containing consecutive |
51 |
> |
* Integers 0 ... n - 1. |
52 |
|
*/ |
53 |
|
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
54 |
|
LinkedBlockingQueue<Integer> q = |
59 |
|
assertFalse(q.isEmpty()); |
60 |
|
assertEquals(0, q.remainingCapacity()); |
61 |
|
assertEquals(n, q.size()); |
62 |
+ |
assertEquals((Integer) 0, q.peek()); |
63 |
|
return q; |
64 |
|
} |
65 |
|
|
109 |
|
*/ |
110 |
|
public void testConstructor5() { |
111 |
|
Integer[] ints = new Integer[SIZE]; |
112 |
< |
for (int i = 0; i < SIZE-1; ++i) |
112 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
113 |
|
ints[i] = new Integer(i); |
114 |
|
Collection<Integer> elements = Arrays.asList(ints); |
115 |
|
try { |
149 |
|
* remainingCapacity decreases on add, increases on remove |
150 |
|
*/ |
151 |
|
public void testRemainingCapacity() { |
152 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
152 |
> |
BlockingQueue q = populatedQueue(SIZE); |
153 |
|
for (int i = 0; i < SIZE; ++i) { |
154 |
|
assertEquals(i, q.remainingCapacity()); |
155 |
< |
assertEquals(SIZE-i, q.size()); |
156 |
< |
q.remove(); |
155 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
156 |
> |
assertEquals(i, q.remove()); |
157 |
|
} |
158 |
|
for (int i = 0; i < SIZE; ++i) { |
159 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
160 |
< |
assertEquals(i, q.size()); |
161 |
< |
q.add(new Integer(i)); |
159 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
160 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
161 |
> |
assertTrue(q.add(i)); |
162 |
|
} |
163 |
|
} |
164 |
|
|
203 |
|
public void testAddAll3() { |
204 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
205 |
|
Integer[] ints = new Integer[SIZE]; |
206 |
< |
for (int i = 0; i < SIZE-1; ++i) |
206 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
207 |
|
ints[i] = new Integer(i); |
208 |
|
Collection<Integer> elements = Arrays.asList(ints); |
209 |
|
try { |
248 |
|
public void testPut() throws InterruptedException { |
249 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
250 |
|
for (int i = 0; i < SIZE; ++i) { |
251 |
< |
Integer I = new Integer(i); |
252 |
< |
q.put(I); |
253 |
< |
assertTrue(q.contains(I)); |
251 |
> |
Integer x = new Integer(i); |
252 |
> |
q.put(x); |
253 |
> |
assertTrue(q.contains(x)); |
254 |
|
} |
255 |
|
assertEquals(0, q.remainingCapacity()); |
256 |
|
} |
441 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
442 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
443 |
|
public void realRun() throws InterruptedException { |
444 |
+ |
long startTime = System.nanoTime(); |
445 |
|
for (int i = 0; i < SIZE; ++i) { |
442 |
– |
long t0 = System.nanoTime(); |
446 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
444 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
447 |
|
} |
446 |
– |
long t0 = System.nanoTime(); |
448 |
|
aboutToWait.countDown(); |
449 |
|
try { |
450 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
450 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
451 |
|
shouldThrow(); |
452 |
|
} catch (InterruptedException success) { |
453 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
453 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
454 |
|
} |
455 |
|
}}); |
456 |
|
|
457 |
< |
aboutToWait.await(); |
458 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
457 |
> |
await(aboutToWait); |
458 |
> |
waitForThreadToEnterWaitState(t); |
459 |
|
t.interrupt(); |
460 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
460 |
> |
awaitTermination(t); |
461 |
|
checkEmpty(q); |
462 |
|
} |
463 |
|
|
514 |
|
assertTrue(q.remove(new Integer(1))); |
515 |
|
assertTrue(q.remove(new Integer(2))); |
516 |
|
assertTrue(q.add(new Integer(3))); |
517 |
< |
assertTrue(q.take() != null); |
517 |
> |
assertNotNull(q.take()); |
518 |
|
} |
519 |
|
|
520 |
|
/** |
573 |
|
assertTrue(changed); |
574 |
|
|
575 |
|
assertTrue(q.containsAll(p)); |
576 |
< |
assertEquals(SIZE-i, q.size()); |
576 |
> |
assertEquals(SIZE - i, q.size()); |
577 |
|
p.remove(); |
578 |
|
} |
579 |
|
} |
586 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
587 |
|
LinkedBlockingQueue p = populatedQueue(i); |
588 |
|
assertTrue(q.removeAll(p)); |
589 |
< |
assertEquals(SIZE-i, q.size()); |
589 |
> |
assertEquals(SIZE - i, q.size()); |
590 |
|
for (int j = 0; j < i; ++j) { |
591 |
< |
Integer I = (Integer)(p.remove()); |
592 |
< |
assertFalse(q.contains(I)); |
591 |
> |
Integer x = (Integer)(p.remove()); |
592 |
> |
assertFalse(q.contains(x)); |
593 |
|
} |
594 |
|
} |
595 |
|
} |
633 |
|
public void testIterator() throws InterruptedException { |
634 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
635 |
|
Iterator it = q.iterator(); |
636 |
< |
while (it.hasNext()) { |
636 |
> |
int i; |
637 |
> |
for (i = 0; it.hasNext(); i++) |
638 |
> |
assertTrue(q.contains(it.next())); |
639 |
> |
assertEquals(i, SIZE); |
640 |
> |
assertIteratorExhausted(it); |
641 |
> |
|
642 |
> |
it = q.iterator(); |
643 |
> |
for (i = 0; it.hasNext(); i++) |
644 |
|
assertEquals(it.next(), q.take()); |
645 |
< |
} |
645 |
> |
assertEquals(i, SIZE); |
646 |
> |
assertIteratorExhausted(it); |
647 |
> |
} |
648 |
> |
|
649 |
> |
/** |
650 |
> |
* iterator of empty collection has no elements |
651 |
> |
*/ |
652 |
> |
public void testEmptyIterator() { |
653 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
654 |
|
} |
655 |
|
|
656 |
|
/** |
721 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
722 |
|
q.add(one); |
723 |
|
q.add(two); |
708 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
724 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
725 |
< |
executor.execute(new CheckedRunnable() { |
726 |
< |
public void realRun() throws InterruptedException { |
727 |
< |
assertFalse(q.offer(three)); |
728 |
< |
threadsStarted.await(); |
729 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
730 |
< |
assertEquals(0, q.remainingCapacity()); |
731 |
< |
}}); |
732 |
< |
|
733 |
< |
executor.execute(new CheckedRunnable() { |
734 |
< |
public void realRun() throws InterruptedException { |
735 |
< |
threadsStarted.await(); |
736 |
< |
assertSame(one, q.take()); |
737 |
< |
}}); |
738 |
< |
|
739 |
< |
joinPool(executor); |
725 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
726 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
727 |
> |
executor.execute(new CheckedRunnable() { |
728 |
> |
public void realRun() throws InterruptedException { |
729 |
> |
assertFalse(q.offer(three)); |
730 |
> |
threadsStarted.await(); |
731 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
732 |
> |
assertEquals(0, q.remainingCapacity()); |
733 |
> |
}}); |
734 |
> |
|
735 |
> |
executor.execute(new CheckedRunnable() { |
736 |
> |
public void realRun() throws InterruptedException { |
737 |
> |
threadsStarted.await(); |
738 |
> |
assertSame(one, q.take()); |
739 |
> |
}}); |
740 |
> |
} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
746 |
|
public void testPollInExecutor() { |
747 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
748 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
749 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
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 |
< |
joinPool(executor); |
749 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
750 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
751 |
> |
executor.execute(new CheckedRunnable() { |
752 |
> |
public void realRun() throws InterruptedException { |
753 |
> |
assertNull(q.poll()); |
754 |
> |
threadsStarted.await(); |
755 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
756 |
> |
checkEmpty(q); |
757 |
> |
}}); |
758 |
> |
|
759 |
> |
executor.execute(new CheckedRunnable() { |
760 |
> |
public void realRun() throws InterruptedException { |
761 |
> |
threadsStarted.await(); |
762 |
> |
q.put(one); |
763 |
> |
}}); |
764 |
> |
} |
765 |
|
} |
766 |
|
|
767 |
|
/** |
771 |
|
Queue x = populatedQueue(SIZE); |
772 |
|
Queue y = serialClone(x); |
773 |
|
|
774 |
< |
assertTrue(x != y); |
774 |
> |
assertNotSame(x, y); |
775 |
|
assertEquals(x.size(), y.size()); |
776 |
|
assertEquals(x.toString(), y.toString()); |
777 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
813 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
814 |
|
Thread t = new Thread(new CheckedRunnable() { |
815 |
|
public void realRun() throws InterruptedException { |
816 |
< |
q.put(new Integer(SIZE+1)); |
816 |
> |
q.put(new Integer(SIZE + 1)); |
817 |
|
}}); |
818 |
|
|
819 |
|
t.start(); |
838 |
|
q.drainTo(l, i); |
839 |
|
int k = (i < SIZE) ? i : SIZE; |
840 |
|
assertEquals(k, l.size()); |
841 |
< |
assertEquals(SIZE-k, q.size()); |
841 |
> |
assertEquals(SIZE - k, q.size()); |
842 |
|
for (int j = 0; j < k; ++j) |
843 |
|
assertEquals(l.get(j), new Integer(j)); |
844 |
< |
while (q.poll() != null) ; |
844 |
> |
do {} while (q.poll() != null); |
845 |
> |
} |
846 |
> |
} |
847 |
> |
|
848 |
> |
/** |
849 |
> |
* remove(null), contains(null) always return false |
850 |
> |
*/ |
851 |
> |
public void testNeverContainsNull() { |
852 |
> |
Collection<?>[] qs = { |
853 |
> |
new LinkedBlockingQueue<Object>(), |
854 |
> |
populatedQueue(2), |
855 |
> |
}; |
856 |
> |
|
857 |
> |
for (Collection<?> q : qs) { |
858 |
> |
assertFalse(q.contains(null)); |
859 |
> |
assertFalse(q.remove(null)); |
860 |
|
} |
861 |
|
} |
862 |
|
|