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) { |
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 |
|
/** |
754 |
|
* A deserialized serialized queue has same elements in same order |
755 |
|
*/ |
756 |
|
public void testSerialization() throws Exception { |
757 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
757 |
> |
Queue x = populatedQueue(SIZE); |
758 |
> |
Queue y = serialClone(x); |
759 |
|
|
760 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
761 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
762 |
< |
out.writeObject(q); |
763 |
< |
out.close(); |
764 |
< |
|
765 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
766 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
767 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
768 |
< |
assertEquals(q.size(), r.size()); |
787 |
< |
while (!q.isEmpty()) |
788 |
< |
assertEquals(q.remove(), r.remove()); |
760 |
> |
assertNotSame(x, y); |
761 |
> |
assertEquals(x.size(), y.size()); |
762 |
> |
assertEquals(x.toString(), y.toString()); |
763 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
764 |
> |
while (!x.isEmpty()) { |
765 |
> |
assertFalse(y.isEmpty()); |
766 |
> |
assertEquals(x.remove(), y.remove()); |
767 |
> |
} |
768 |
> |
assertTrue(y.isEmpty()); |
769 |
|
} |
770 |
|
|
771 |
|
/** |
775 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
776 |
|
ArrayList l = new ArrayList(); |
777 |
|
q.drainTo(l); |
778 |
< |
assertEquals(q.size(), 0); |
779 |
< |
assertEquals(l.size(), SIZE); |
778 |
> |
assertEquals(0, q.size()); |
779 |
> |
assertEquals(SIZE, l.size()); |
780 |
|
for (int i = 0; i < SIZE; ++i) |
781 |
|
assertEquals(l.get(i), new Integer(i)); |
782 |
|
q.add(zero); |
786 |
|
assertTrue(q.contains(one)); |
787 |
|
l.clear(); |
788 |
|
q.drainTo(l); |
789 |
< |
assertEquals(q.size(), 0); |
790 |
< |
assertEquals(l.size(), 2); |
789 |
> |
assertEquals(0, q.size()); |
790 |
> |
assertEquals(2, l.size()); |
791 |
|
for (int i = 0; i < 2; ++i) |
792 |
|
assertEquals(l.get(i), new Integer(i)); |
793 |
|
} |
823 |
|
ArrayList l = new ArrayList(); |
824 |
|
q.drainTo(l, i); |
825 |
|
int k = (i < SIZE) ? i : SIZE; |
826 |
< |
assertEquals(l.size(), k); |
827 |
< |
assertEquals(q.size(), SIZE-k); |
826 |
> |
assertEquals(k, l.size()); |
827 |
> |
assertEquals(SIZE-k, q.size()); |
828 |
|
for (int j = 0; j < k; ++j) |
829 |
|
assertEquals(l.get(j), new Integer(j)); |
830 |
< |
while (q.poll() != null) ; |
830 |
> |
do {} while (q.poll() != null); |
831 |
> |
} |
832 |
> |
} |
833 |
> |
|
834 |
> |
/** |
835 |
> |
* remove(null), contains(null) always return false |
836 |
> |
*/ |
837 |
> |
public void testNeverContainsNull() { |
838 |
> |
Collection<?>[] qs = { |
839 |
> |
new LinkedBlockingQueue<Object>(), |
840 |
> |
populatedQueue(2), |
841 |
> |
}; |
842 |
> |
|
843 |
> |
for (Collection<?> q : qs) { |
844 |
> |
assertFalse(q.contains(null)); |
845 |
> |
assertFalse(q.remove(null)); |
846 |
|
} |
847 |
|
} |
848 |
|
|