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() { |
44 |
+ |
class Implementation implements CollectionImplementation { |
45 |
+ |
public Class<?> klazz() { return LinkedBlockingQueue.class; } |
46 |
+ |
public Collection emptyCollection() { return new LinkedBlockingQueue(); } |
47 |
+ |
public Object makeElement(int i) { return i; } |
48 |
+ |
public boolean isConcurrent() { return true; } |
49 |
+ |
public boolean permitsNulls() { return false; } |
50 |
+ |
} |
51 |
|
return newTestSuite(LinkedBlockingQueueTest.class, |
52 |
|
new Unbounded().testSuite(), |
53 |
< |
new Bounded().testSuite()); |
53 |
> |
new Bounded().testSuite(), |
54 |
> |
CollectionTest.testSuite(new Implementation())); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
< |
* Create a queue of given size containing consecutive |
59 |
< |
* Integers 0 ... n. |
58 |
> |
* Returns a new queue of given size containing consecutive |
59 |
> |
* Integers 0 ... n - 1. |
60 |
|
*/ |
61 |
< |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
61 |
> |
private static LinkedBlockingQueue<Integer> populatedQueue(int n) { |
62 |
|
LinkedBlockingQueue<Integer> q = |
63 |
|
new LinkedBlockingQueue<Integer>(n); |
64 |
|
assertTrue(q.isEmpty()); |
67 |
|
assertFalse(q.isEmpty()); |
68 |
|
assertEquals(0, q.remainingCapacity()); |
69 |
|
assertEquals(n, q.size()); |
70 |
+ |
assertEquals((Integer) 0, q.peek()); |
71 |
|
return q; |
72 |
|
} |
73 |
|
|
117 |
|
*/ |
118 |
|
public void testConstructor5() { |
119 |
|
Integer[] ints = new Integer[SIZE]; |
120 |
< |
for (int i = 0; i < SIZE-1; ++i) |
120 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
121 |
|
ints[i] = new Integer(i); |
122 |
|
Collection<Integer> elements = Arrays.asList(ints); |
123 |
|
try { |
157 |
|
* remainingCapacity decreases on add, increases on remove |
158 |
|
*/ |
159 |
|
public void testRemainingCapacity() { |
160 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
160 |
> |
BlockingQueue q = populatedQueue(SIZE); |
161 |
|
for (int i = 0; i < SIZE; ++i) { |
162 |
|
assertEquals(i, q.remainingCapacity()); |
163 |
< |
assertEquals(SIZE-i, q.size()); |
164 |
< |
q.remove(); |
163 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
164 |
> |
assertEquals(i, q.remove()); |
165 |
|
} |
166 |
|
for (int i = 0; i < SIZE; ++i) { |
167 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
168 |
< |
assertEquals(i, q.size()); |
169 |
< |
q.add(new Integer(i)); |
167 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
168 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
169 |
> |
assertTrue(q.add(i)); |
170 |
|
} |
171 |
|
} |
172 |
|
|
211 |
|
public void testAddAll3() { |
212 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
213 |
|
Integer[] ints = new Integer[SIZE]; |
214 |
< |
for (int i = 0; i < SIZE-1; ++i) |
214 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
215 |
|
ints[i] = new Integer(i); |
216 |
|
Collection<Integer> elements = Arrays.asList(ints); |
217 |
|
try { |
256 |
|
public void testPut() throws InterruptedException { |
257 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
258 |
|
for (int i = 0; i < SIZE; ++i) { |
259 |
< |
Integer I = new Integer(i); |
260 |
< |
q.put(I); |
261 |
< |
assertTrue(q.contains(I)); |
259 |
> |
Integer x = new Integer(i); |
260 |
> |
q.put(x); |
261 |
> |
assertTrue(q.contains(x)); |
262 |
|
} |
263 |
|
assertEquals(0, q.remainingCapacity()); |
264 |
|
} |
292 |
|
}}); |
293 |
|
|
294 |
|
await(pleaseInterrupt); |
295 |
< |
assertThreadStaysAlive(t); |
295 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
296 |
|
t.interrupt(); |
297 |
|
awaitTermination(t); |
298 |
|
assertEquals(SIZE, q.size()); |
327 |
|
assertEquals(0, q.take()); |
328 |
|
|
329 |
|
await(pleaseInterrupt); |
330 |
< |
assertThreadStaysAlive(t); |
330 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
331 |
|
t.interrupt(); |
332 |
|
awaitTermination(t); |
333 |
|
assertEquals(0, q.remainingCapacity()); |
351 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
352 |
|
shouldThrow(); |
353 |
|
} catch (InterruptedException success) {} |
354 |
+ |
assertFalse(Thread.interrupted()); |
355 |
|
}}); |
356 |
|
|
357 |
|
await(pleaseInterrupt); |
358 |
< |
assertThreadStaysAlive(t); |
358 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
359 |
|
t.interrupt(); |
360 |
|
awaitTermination(t); |
361 |
|
} |
378 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
379 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
380 |
|
public void realRun() throws InterruptedException { |
381 |
< |
for (int i = 0; i < SIZE; ++i) { |
370 |
< |
assertEquals(i, q.take()); |
371 |
< |
} |
381 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
382 |
|
|
383 |
|
Thread.currentThread().interrupt(); |
384 |
|
try { |
396 |
|
}}); |
397 |
|
|
398 |
|
await(pleaseInterrupt); |
399 |
< |
assertThreadStaysAlive(t); |
399 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
400 |
|
t.interrupt(); |
401 |
|
awaitTermination(t); |
402 |
|
} |
445 |
|
*/ |
446 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
447 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
448 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
448 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
449 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
450 |
|
public void realRun() throws InterruptedException { |
451 |
< |
for (int i = 0; i < SIZE; ++i) { |
452 |
< |
long t0 = System.nanoTime(); |
451 |
> |
long startTime = System.nanoTime(); |
452 |
> |
for (int i = 0; i < SIZE; i++) |
453 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
454 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
455 |
< |
} |
456 |
< |
long t0 = System.nanoTime(); |
457 |
< |
aboutToWait.countDown(); |
454 |
> |
|
455 |
> |
Thread.currentThread().interrupt(); |
456 |
> |
try { |
457 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
458 |
> |
shouldThrow(); |
459 |
> |
} catch (InterruptedException success) {} |
460 |
> |
assertFalse(Thread.interrupted()); |
461 |
> |
|
462 |
> |
pleaseInterrupt.countDown(); |
463 |
|
try { |
464 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
464 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
465 |
|
shouldThrow(); |
466 |
< |
} catch (InterruptedException success) { |
467 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
468 |
< |
} |
466 |
> |
} catch (InterruptedException success) {} |
467 |
> |
assertFalse(Thread.interrupted()); |
468 |
> |
|
469 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
470 |
|
}}); |
471 |
|
|
472 |
< |
aboutToWait.await(); |
473 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
472 |
> |
await(pleaseInterrupt); |
473 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
474 |
|
t.interrupt(); |
475 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
475 |
> |
awaitTermination(t); |
476 |
|
checkEmpty(q); |
477 |
|
} |
478 |
|
|
529 |
|
assertTrue(q.remove(new Integer(1))); |
530 |
|
assertTrue(q.remove(new Integer(2))); |
531 |
|
assertTrue(q.add(new Integer(3))); |
532 |
< |
assertTrue(q.take() != null); |
532 |
> |
assertNotNull(q.take()); |
533 |
|
} |
534 |
|
|
535 |
|
/** |
588 |
|
assertTrue(changed); |
589 |
|
|
590 |
|
assertTrue(q.containsAll(p)); |
591 |
< |
assertEquals(SIZE-i, q.size()); |
591 |
> |
assertEquals(SIZE - i, q.size()); |
592 |
|
p.remove(); |
593 |
|
} |
594 |
|
} |
601 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
602 |
|
LinkedBlockingQueue p = populatedQueue(i); |
603 |
|
assertTrue(q.removeAll(p)); |
604 |
< |
assertEquals(SIZE-i, q.size()); |
604 |
> |
assertEquals(SIZE - i, q.size()); |
605 |
|
for (int j = 0; j < i; ++j) { |
606 |
< |
Integer I = (Integer)(p.remove()); |
607 |
< |
assertFalse(q.contains(I)); |
606 |
> |
Integer x = (Integer)(p.remove()); |
607 |
> |
assertFalse(q.contains(x)); |
608 |
|
} |
609 |
|
} |
610 |
|
} |
648 |
|
public void testIterator() throws InterruptedException { |
649 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
650 |
|
Iterator it = q.iterator(); |
651 |
< |
while (it.hasNext()) { |
651 |
> |
int i; |
652 |
> |
for (i = 0; it.hasNext(); i++) |
653 |
> |
assertTrue(q.contains(it.next())); |
654 |
> |
assertEquals(i, SIZE); |
655 |
> |
assertIteratorExhausted(it); |
656 |
> |
|
657 |
> |
it = q.iterator(); |
658 |
> |
for (i = 0; it.hasNext(); i++) |
659 |
|
assertEquals(it.next(), q.take()); |
660 |
< |
} |
660 |
> |
assertEquals(i, SIZE); |
661 |
> |
assertIteratorExhausted(it); |
662 |
> |
} |
663 |
> |
|
664 |
> |
/** |
665 |
> |
* iterator of empty collection has no elements |
666 |
> |
*/ |
667 |
> |
public void testEmptyIterator() { |
668 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
669 |
|
} |
670 |
|
|
671 |
|
/** |
736 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
737 |
|
q.add(one); |
738 |
|
q.add(two); |
708 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
739 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
740 |
< |
executor.execute(new CheckedRunnable() { |
741 |
< |
public void realRun() throws InterruptedException { |
742 |
< |
assertFalse(q.offer(three)); |
743 |
< |
threadsStarted.await(); |
744 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
745 |
< |
assertEquals(0, q.remainingCapacity()); |
746 |
< |
}}); |
747 |
< |
|
748 |
< |
executor.execute(new CheckedRunnable() { |
749 |
< |
public void realRun() throws InterruptedException { |
750 |
< |
threadsStarted.await(); |
751 |
< |
assertSame(one, q.take()); |
752 |
< |
}}); |
753 |
< |
|
754 |
< |
joinPool(executor); |
740 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
741 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
742 |
> |
executor.execute(new CheckedRunnable() { |
743 |
> |
public void realRun() throws InterruptedException { |
744 |
> |
assertFalse(q.offer(three)); |
745 |
> |
threadsStarted.await(); |
746 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
747 |
> |
assertEquals(0, q.remainingCapacity()); |
748 |
> |
}}); |
749 |
> |
|
750 |
> |
executor.execute(new CheckedRunnable() { |
751 |
> |
public void realRun() throws InterruptedException { |
752 |
> |
threadsStarted.await(); |
753 |
> |
assertSame(one, q.take()); |
754 |
> |
}}); |
755 |
> |
} |
756 |
|
} |
757 |
|
|
758 |
|
/** |
761 |
|
public void testPollInExecutor() { |
762 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
763 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
764 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
765 |
< |
executor.execute(new CheckedRunnable() { |
766 |
< |
public void realRun() throws InterruptedException { |
767 |
< |
assertNull(q.poll()); |
768 |
< |
threadsStarted.await(); |
769 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
770 |
< |
checkEmpty(q); |
771 |
< |
}}); |
772 |
< |
|
773 |
< |
executor.execute(new CheckedRunnable() { |
774 |
< |
public void realRun() throws InterruptedException { |
775 |
< |
threadsStarted.await(); |
776 |
< |
q.put(one); |
777 |
< |
}}); |
778 |
< |
|
779 |
< |
joinPool(executor); |
764 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
765 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
766 |
> |
executor.execute(new CheckedRunnable() { |
767 |
> |
public void realRun() throws InterruptedException { |
768 |
> |
assertNull(q.poll()); |
769 |
> |
threadsStarted.await(); |
770 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
771 |
> |
checkEmpty(q); |
772 |
> |
}}); |
773 |
> |
|
774 |
> |
executor.execute(new CheckedRunnable() { |
775 |
> |
public void realRun() throws InterruptedException { |
776 |
> |
threadsStarted.await(); |
777 |
> |
q.put(one); |
778 |
> |
}}); |
779 |
> |
} |
780 |
|
} |
781 |
|
|
782 |
|
/** |
786 |
|
Queue x = populatedQueue(SIZE); |
787 |
|
Queue y = serialClone(x); |
788 |
|
|
789 |
< |
assertTrue(x != y); |
789 |
> |
assertNotSame(x, y); |
790 |
|
assertEquals(x.size(), y.size()); |
791 |
|
assertEquals(x.toString(), y.toString()); |
792 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
828 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
829 |
|
Thread t = new Thread(new CheckedRunnable() { |
830 |
|
public void realRun() throws InterruptedException { |
831 |
< |
q.put(new Integer(SIZE+1)); |
831 |
> |
q.put(new Integer(SIZE + 1)); |
832 |
|
}}); |
833 |
|
|
834 |
|
t.start(); |
853 |
|
q.drainTo(l, i); |
854 |
|
int k = (i < SIZE) ? i : SIZE; |
855 |
|
assertEquals(k, l.size()); |
856 |
< |
assertEquals(SIZE-k, q.size()); |
856 |
> |
assertEquals(SIZE - k, q.size()); |
857 |
|
for (int j = 0; j < k; ++j) |
858 |
|
assertEquals(l.get(j), new Integer(j)); |
859 |
< |
while (q.poll() != null) ; |
859 |
> |
do {} while (q.poll() != null); |
860 |
> |
} |
861 |
> |
} |
862 |
> |
|
863 |
> |
/** |
864 |
> |
* remove(null), contains(null) always return false |
865 |
> |
*/ |
866 |
> |
public void testNeverContainsNull() { |
867 |
> |
Collection<?>[] qs = { |
868 |
> |
new LinkedBlockingQueue<Object>(), |
869 |
> |
populatedQueue(2), |
870 |
> |
}; |
871 |
> |
|
872 |
> |
for (Collection<?> q : qs) { |
873 |
> |
assertFalse(q.contains(null)); |
874 |
> |
assertFalse(q.remove(null)); |
875 |
|
} |
876 |
|
} |
877 |
|
|