12 |
|
import java.util.Collection; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.NoSuchElementException; |
15 |
+ |
import java.util.Queue; |
16 |
|
import java.util.concurrent.ArrayBlockingQueue; |
17 |
|
import java.util.concurrent.BlockingQueue; |
18 |
|
import java.util.concurrent.CountDownLatch; |
19 |
|
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
– |
import java.io.*; |
22 |
|
|
23 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
24 |
|
|
25 |
|
public static class Fair extends BlockingQueueTest { |
26 |
|
protected BlockingQueue emptyCollection() { |
27 |
< |
return new ArrayBlockingQueue(20, true); |
27 |
> |
return new ArrayBlockingQueue(SIZE, true); |
28 |
|
} |
29 |
|
} |
30 |
|
|
31 |
|
public static class NonFair extends BlockingQueueTest { |
32 |
|
protected BlockingQueue emptyCollection() { |
33 |
< |
return new ArrayBlockingQueue(20, false); |
33 |
> |
return new ArrayBlockingQueue(SIZE, false); |
34 |
|
} |
35 |
|
} |
36 |
|
|
322 |
|
}}); |
323 |
|
|
324 |
|
await(pleaseTake); |
325 |
< |
assertEquals(q.remainingCapacity(), 0); |
325 |
> |
assertEquals(0, q.remainingCapacity()); |
326 |
|
assertEquals(0, q.take()); |
327 |
|
|
328 |
|
await(pleaseInterrupt); |
329 |
|
assertThreadStaysAlive(t); |
330 |
|
t.interrupt(); |
331 |
|
awaitTermination(t); |
332 |
< |
assertEquals(q.remainingCapacity(), 0); |
332 |
> |
assertEquals(0, q.remainingCapacity()); |
333 |
|
} |
334 |
|
|
335 |
|
/** |
515 |
|
} |
516 |
|
|
517 |
|
/** |
518 |
– |
* remove(x) removes x and returns true if present |
519 |
– |
*/ |
520 |
– |
public void testRemoveElement() { |
521 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
522 |
– |
for (int i = 1; i < SIZE; i+=2) { |
523 |
– |
assertTrue(q.remove(new Integer(i))); |
524 |
– |
} |
525 |
– |
for (int i = 0; i < SIZE; i+=2) { |
526 |
– |
assertTrue(q.remove(new Integer(i))); |
527 |
– |
assertFalse(q.remove(new Integer(i+1))); |
528 |
– |
} |
529 |
– |
assertTrue(q.isEmpty()); |
530 |
– |
} |
531 |
– |
|
532 |
– |
/** |
518 |
|
* contains(x) reports true when elements added but not yet removed |
519 |
|
*/ |
520 |
|
public void testContains() { |
753 |
|
* A deserialized serialized queue has same elements in same order |
754 |
|
*/ |
755 |
|
public void testSerialization() throws Exception { |
756 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
756 |
> |
Queue x = populatedQueue(SIZE); |
757 |
> |
Queue y = serialClone(x); |
758 |
|
|
759 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
760 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
761 |
< |
out.writeObject(q); |
762 |
< |
out.close(); |
763 |
< |
|
764 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
765 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
766 |
< |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
767 |
< |
assertEquals(q.size(), r.size()); |
782 |
< |
while (!q.isEmpty()) |
783 |
< |
assertEquals(q.remove(), r.remove()); |
759 |
> |
assertTrue(x != y); |
760 |
> |
assertEquals(x.size(), y.size()); |
761 |
> |
assertEquals(x.toString(), y.toString()); |
762 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
763 |
> |
while (!x.isEmpty()) { |
764 |
> |
assertFalse(y.isEmpty()); |
765 |
> |
assertEquals(x.remove(), y.remove()); |
766 |
> |
} |
767 |
> |
assertTrue(y.isEmpty()); |
768 |
|
} |
769 |
|
|
770 |
|
/** |
774 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
775 |
|
ArrayList l = new ArrayList(); |
776 |
|
q.drainTo(l); |
777 |
< |
assertEquals(q.size(), 0); |
778 |
< |
assertEquals(l.size(), SIZE); |
777 |
> |
assertEquals(0, q.size()); |
778 |
> |
assertEquals(SIZE, l.size()); |
779 |
|
for (int i = 0; i < SIZE; ++i) |
780 |
|
assertEquals(l.get(i), new Integer(i)); |
781 |
|
q.add(zero); |
785 |
|
assertTrue(q.contains(one)); |
786 |
|
l.clear(); |
787 |
|
q.drainTo(l); |
788 |
< |
assertEquals(q.size(), 0); |
789 |
< |
assertEquals(l.size(), 2); |
788 |
> |
assertEquals(0, q.size()); |
789 |
> |
assertEquals(2, l.size()); |
790 |
|
for (int i = 0; i < 2; ++i) |
791 |
|
assertEquals(l.get(i), new Integer(i)); |
792 |
|
} |