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; |
17 |
– |
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; |
22 |
< |
import java.io.*; |
21 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
26 |
|
|
32 |
|
|
33 |
|
public static class Bounded extends BlockingQueueTest { |
34 |
|
protected BlockingQueue emptyCollection() { |
35 |
< |
return new LinkedBlockingQueue(20); |
35 |
> |
return new LinkedBlockingQueue(SIZE); |
36 |
|
} |
37 |
|
} |
38 |
|
|
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* Create a queue of given size containing consecutive |
50 |
> |
* Returns a new queue of given size containing consecutive |
51 |
|
* Integers 0 ... n. |
52 |
|
*/ |
53 |
|
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
247 |
|
public void testPut() throws InterruptedException { |
248 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
249 |
|
for (int i = 0; i < SIZE; ++i) { |
250 |
< |
Integer I = new Integer(i); |
251 |
< |
q.put(I); |
252 |
< |
assertTrue(q.contains(I)); |
250 |
> |
Integer x = new Integer(i); |
251 |
> |
q.put(x); |
252 |
> |
assertTrue(q.contains(x)); |
253 |
|
} |
254 |
|
assertEquals(0, q.remainingCapacity()); |
255 |
|
} |
314 |
|
}}); |
315 |
|
|
316 |
|
await(pleaseTake); |
317 |
< |
assertEquals(q.remainingCapacity(), 0); |
317 |
> |
assertEquals(0, q.remainingCapacity()); |
318 |
|
assertEquals(0, q.take()); |
319 |
|
|
320 |
|
await(pleaseInterrupt); |
321 |
|
assertThreadStaysAlive(t); |
322 |
|
t.interrupt(); |
323 |
|
awaitTermination(t); |
324 |
< |
assertEquals(q.remainingCapacity(), 0); |
324 |
> |
assertEquals(0, q.remainingCapacity()); |
325 |
|
} |
326 |
|
|
327 |
|
/** |
506 |
|
} |
507 |
|
|
508 |
|
/** |
507 |
– |
* remove(x) removes x and returns true if present |
508 |
– |
*/ |
509 |
– |
public void testRemoveElement() { |
510 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
511 |
– |
for (int i = 1; i < SIZE; i+=2) { |
512 |
– |
assertTrue(q.contains(i)); |
513 |
– |
assertTrue(q.remove(i)); |
514 |
– |
assertFalse(q.contains(i)); |
515 |
– |
assertTrue(q.contains(i-1)); |
516 |
– |
} |
517 |
– |
for (int i = 0; i < SIZE; i+=2) { |
518 |
– |
assertTrue(q.contains(i)); |
519 |
– |
assertTrue(q.remove(i)); |
520 |
– |
assertFalse(q.contains(i)); |
521 |
– |
assertFalse(q.remove(i+1)); |
522 |
– |
assertFalse(q.contains(i+1)); |
523 |
– |
} |
524 |
– |
assertTrue(q.isEmpty()); |
525 |
– |
} |
526 |
– |
|
527 |
– |
/** |
509 |
|
* An add following remove(x) succeeds |
510 |
|
*/ |
511 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
515 |
|
assertTrue(q.remove(new Integer(1))); |
516 |
|
assertTrue(q.remove(new Integer(2))); |
517 |
|
assertTrue(q.add(new Integer(3))); |
518 |
< |
assertTrue(q.take() != null); |
518 |
> |
assertNotNull(q.take()); |
519 |
|
} |
520 |
|
|
521 |
|
/** |
589 |
|
assertTrue(q.removeAll(p)); |
590 |
|
assertEquals(SIZE-i, q.size()); |
591 |
|
for (int j = 0; j < i; ++j) { |
592 |
< |
Integer I = (Integer)(p.remove()); |
593 |
< |
assertFalse(q.contains(I)); |
592 |
> |
Integer x = (Integer)(p.remove()); |
593 |
> |
assertFalse(q.contains(x)); |
594 |
|
} |
595 |
|
} |
596 |
|
} |
634 |
|
public void testIterator() throws InterruptedException { |
635 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
636 |
|
Iterator it = q.iterator(); |
637 |
< |
while (it.hasNext()) { |
637 |
> |
int i; |
638 |
> |
for (i = 0; it.hasNext(); i++) |
639 |
> |
assertTrue(q.contains(it.next())); |
640 |
> |
assertEquals(i, SIZE); |
641 |
> |
assertIteratorExhausted(it); |
642 |
> |
|
643 |
> |
it = q.iterator(); |
644 |
> |
for (i = 0; it.hasNext(); i++) |
645 |
|
assertEquals(it.next(), q.take()); |
646 |
< |
} |
646 |
> |
assertEquals(i, SIZE); |
647 |
> |
assertIteratorExhausted(it); |
648 |
> |
} |
649 |
> |
|
650 |
> |
/** |
651 |
> |
* iterator of empty collection has no elements |
652 |
> |
*/ |
653 |
> |
public void testEmptyIterator() { |
654 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
769 |
|
* A deserialized serialized queue has same elements in same order |
770 |
|
*/ |
771 |
|
public void testSerialization() throws Exception { |
772 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
772 |
> |
Queue x = populatedQueue(SIZE); |
773 |
> |
Queue y = serialClone(x); |
774 |
|
|
775 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
776 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
777 |
< |
out.writeObject(q); |
778 |
< |
out.close(); |
779 |
< |
|
780 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
781 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
782 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
783 |
< |
assertEquals(q.size(), r.size()); |
787 |
< |
while (!q.isEmpty()) |
788 |
< |
assertEquals(q.remove(), r.remove()); |
775 |
> |
assertNotSame(x, y); |
776 |
> |
assertEquals(x.size(), y.size()); |
777 |
> |
assertEquals(x.toString(), y.toString()); |
778 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
779 |
> |
while (!x.isEmpty()) { |
780 |
> |
assertFalse(y.isEmpty()); |
781 |
> |
assertEquals(x.remove(), y.remove()); |
782 |
> |
} |
783 |
> |
assertTrue(y.isEmpty()); |
784 |
|
} |
785 |
|
|
786 |
|
/** |
790 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
791 |
|
ArrayList l = new ArrayList(); |
792 |
|
q.drainTo(l); |
793 |
< |
assertEquals(q.size(), 0); |
794 |
< |
assertEquals(l.size(), SIZE); |
793 |
> |
assertEquals(0, q.size()); |
794 |
> |
assertEquals(SIZE, l.size()); |
795 |
|
for (int i = 0; i < SIZE; ++i) |
796 |
|
assertEquals(l.get(i), new Integer(i)); |
797 |
|
q.add(zero); |
801 |
|
assertTrue(q.contains(one)); |
802 |
|
l.clear(); |
803 |
|
q.drainTo(l); |
804 |
< |
assertEquals(q.size(), 0); |
805 |
< |
assertEquals(l.size(), 2); |
804 |
> |
assertEquals(0, q.size()); |
805 |
> |
assertEquals(2, l.size()); |
806 |
|
for (int i = 0; i < 2; ++i) |
807 |
|
assertEquals(l.get(i), new Integer(i)); |
808 |
|
} |
838 |
|
ArrayList l = new ArrayList(); |
839 |
|
q.drainTo(l, i); |
840 |
|
int k = (i < SIZE) ? i : SIZE; |
841 |
< |
assertEquals(l.size(), k); |
842 |
< |
assertEquals(q.size(), SIZE-k); |
841 |
> |
assertEquals(k, l.size()); |
842 |
> |
assertEquals(SIZE-k, q.size()); |
843 |
|
for (int j = 0; j < k; ++j) |
844 |
|
assertEquals(l.get(j), new Integer(j)); |
845 |
< |
while (q.poll() != null) ; |
845 |
> |
do {} while (q.poll() != null); |
846 |
> |
} |
847 |
> |
} |
848 |
> |
|
849 |
> |
/** |
850 |
> |
* remove(null), contains(null) always return false |
851 |
> |
*/ |
852 |
> |
public void testNeverContainsNull() { |
853 |
> |
Collection<?>[] qs = { |
854 |
> |
new LinkedBlockingQueue<Object>(), |
855 |
> |
populatedQueue(2), |
856 |
> |
}; |
857 |
> |
|
858 |
> |
for (Collection<?> q : qs) { |
859 |
> |
assertFalse(q.contains(null)); |
860 |
> |
assertFalse(q.remove(null)); |
861 |
|
} |
862 |
|
} |
863 |
|
|