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.BlockingQueue; |
17 |
|
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.io.*; |
22 |
|
|
23 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
24 |
|
|
30 |
|
|
31 |
|
public static class Bounded extends BlockingQueueTest { |
32 |
|
protected BlockingQueue emptyCollection() { |
33 |
< |
return new LinkedBlockingQueue(20); |
33 |
> |
return new LinkedBlockingQueue(SIZE); |
34 |
|
} |
35 |
|
} |
36 |
|
|
504 |
|
} |
505 |
|
|
506 |
|
/** |
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 |
– |
/** |
507 |
|
* An add following remove(x) succeeds |
508 |
|
*/ |
509 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
752 |
|
* A deserialized serialized queue has same elements in same order |
753 |
|
*/ |
754 |
|
public void testSerialization() throws Exception { |
755 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
755 |
> |
Queue x = populatedQueue(SIZE); |
756 |
> |
Queue y = serialClone(x); |
757 |
|
|
758 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
759 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
760 |
< |
out.writeObject(q); |
761 |
< |
out.close(); |
762 |
< |
|
763 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
764 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
765 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
766 |
< |
assertEquals(q.size(), r.size()); |
787 |
< |
while (!q.isEmpty()) |
788 |
< |
assertEquals(q.remove(), r.remove()); |
758 |
> |
assertTrue(x != y); |
759 |
> |
assertEquals(x.size(), y.size()); |
760 |
> |
assertEquals(x.toString(), y.toString()); |
761 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
762 |
> |
while (!x.isEmpty()) { |
763 |
> |
assertFalse(y.isEmpty()); |
764 |
> |
assertEquals(x.remove(), y.remove()); |
765 |
> |
} |
766 |
> |
assertTrue(y.isEmpty()); |
767 |
|
} |
768 |
|
|
769 |
|
/** |