6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.io.*; |
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.concurrent.BlockingQueue; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.Executors; |
19 |
> |
import java.util.concurrent.ExecutorService; |
20 |
> |
import java.util.concurrent.SynchronousQueue; |
21 |
> |
|
22 |
> |
import junit.framework.Test; |
23 |
|
|
24 |
|
public class SynchronousQueueTest extends JSR166TestCase { |
25 |
|
|
59 |
|
} |
60 |
|
|
61 |
|
/** |
53 |
– |
* offer(null) throws NullPointerException |
54 |
– |
*/ |
55 |
– |
public void testOfferNull() { testOfferNull(false); } |
56 |
– |
public void testOfferNull_fair() { testOfferNull(true); } |
57 |
– |
public void testOfferNull(boolean fair) { |
58 |
– |
SynchronousQueue q = new SynchronousQueue(fair); |
59 |
– |
try { |
60 |
– |
q.offer(null); |
61 |
– |
shouldThrow(); |
62 |
– |
} catch (NullPointerException success) {} |
63 |
– |
} |
64 |
– |
|
65 |
– |
/** |
66 |
– |
* add(null) throws NullPointerException |
67 |
– |
*/ |
68 |
– |
public void testAddNull() { testAddNull(false); } |
69 |
– |
public void testAddNull_fair() { testAddNull(true); } |
70 |
– |
public void testAddNull(boolean fair) { |
71 |
– |
SynchronousQueue q = new SynchronousQueue(fair); |
72 |
– |
try { |
73 |
– |
q.add(null); |
74 |
– |
shouldThrow(); |
75 |
– |
} catch (NullPointerException success) {} |
76 |
– |
} |
77 |
– |
|
78 |
– |
/** |
62 |
|
* offer fails if no active taker |
63 |
|
*/ |
64 |
|
public void testOffer() { testOffer(false); } |
83 |
|
} |
84 |
|
|
85 |
|
/** |
103 |
– |
* addAll(null) throws NullPointerException |
104 |
– |
*/ |
105 |
– |
public void testAddAll_null() { testAddAll_null(false); } |
106 |
– |
public void testAddAll_null_fair() { testAddAll_null(true); } |
107 |
– |
public void testAddAll_null(boolean fair) { |
108 |
– |
SynchronousQueue q = new SynchronousQueue(fair); |
109 |
– |
try { |
110 |
– |
q.addAll(null); |
111 |
– |
shouldThrow(); |
112 |
– |
} catch (NullPointerException success) {} |
113 |
– |
} |
114 |
– |
|
115 |
– |
/** |
86 |
|
* addAll(this) throws IllegalArgumentException |
87 |
|
*/ |
88 |
|
public void testAddAll_self() { testAddAll_self(false); } |
96 |
|
} |
97 |
|
|
98 |
|
/** |
129 |
– |
* addAll of a collection with null elements throws NullPointerException |
130 |
– |
*/ |
131 |
– |
public void testAddAll_null2() { testAddAll_null2(false); } |
132 |
– |
public void testAddAll_null2_fair() { testAddAll_null2(true); } |
133 |
– |
public void testAddAll_null2(boolean fair) { |
134 |
– |
SynchronousQueue q = new SynchronousQueue(fair); |
135 |
– |
Collection<Integer> ints = Arrays.asList(new Integer[1]); |
136 |
– |
try { |
137 |
– |
q.addAll(ints); |
138 |
– |
shouldThrow(); |
139 |
– |
} catch (NullPointerException success) {} |
140 |
– |
} |
141 |
– |
|
142 |
– |
/** |
99 |
|
* addAll throws ISE if no active taker |
100 |
|
*/ |
101 |
|
public void testAddAll_ISE() { testAddAll_ISE(false); } |
113 |
|
} |
114 |
|
|
115 |
|
/** |
160 |
– |
* put(null) throws NPE |
161 |
– |
*/ |
162 |
– |
public void testPutNull() throws InterruptedException { |
163 |
– |
try { |
164 |
– |
SynchronousQueue q = new SynchronousQueue(); |
165 |
– |
q.put(null); |
166 |
– |
shouldThrow(); |
167 |
– |
} catch (NullPointerException success) {} |
168 |
– |
} |
169 |
– |
|
170 |
– |
/** |
116 |
|
* put blocks interruptibly if no active taker |
117 |
|
*/ |
118 |
|
public void testBlockingPut() { testBlockingPut(false); } |
167 |
|
}}); |
168 |
|
|
169 |
|
await(pleaseTake); |
170 |
< |
assertEquals(q.remainingCapacity(), 0); |
170 |
> |
assertEquals(0, q.remainingCapacity()); |
171 |
|
try { assertSame(one, q.take()); } |
172 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
173 |
|
|
175 |
|
assertThreadStaysAlive(t); |
176 |
|
t.interrupt(); |
177 |
|
awaitTermination(t); |
178 |
< |
assertEquals(q.remainingCapacity(), 0); |
178 |
> |
assertEquals(0, q.remainingCapacity()); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
323 |
|
} |
324 |
|
|
325 |
|
/** |
381 |
– |
* remove(x) returns false |
382 |
– |
*/ |
383 |
– |
public void testRemoveElement() { testRemoveElement(false); } |
384 |
– |
public void testRemoveElement_fair() { testRemoveElement(true); } |
385 |
– |
public void testRemoveElement(boolean fair) { |
386 |
– |
final SynchronousQueue q = new SynchronousQueue(fair); |
387 |
– |
assertFalse(q.remove(zero)); |
388 |
– |
assertTrue(q.isEmpty()); |
389 |
– |
} |
390 |
– |
|
391 |
– |
/** |
326 |
|
* contains returns false |
327 |
|
*/ |
328 |
|
public void testContains() { testContains(false); } |
390 |
|
public void testToArray(boolean fair) { |
391 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
392 |
|
Object[] o = q.toArray(); |
393 |
< |
assertEquals(o.length, 0); |
393 |
> |
assertEquals(0, o.length); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
< |
* toArray(a) is nulled at position 0 |
397 |
> |
* toArray(Integer array) returns its argument with the first |
398 |
> |
* element (if present) nulled out |
399 |
|
*/ |
400 |
|
public void testToArray2() { testToArray2(false); } |
401 |
|
public void testToArray2_fair() { testToArray2(true); } |
402 |
|
public void testToArray2(boolean fair) { |
403 |
< |
final SynchronousQueue q = new SynchronousQueue(fair); |
404 |
< |
Integer[] ints = new Integer[1]; |
405 |
< |
assertNull(ints[0]); |
403 |
> |
final SynchronousQueue<Integer> q |
404 |
> |
= new SynchronousQueue<Integer>(fair); |
405 |
> |
Integer[] a; |
406 |
> |
|
407 |
> |
a = new Integer[0]; |
408 |
> |
assertSame(a, q.toArray(a)); |
409 |
> |
|
410 |
> |
a = new Integer[3]; |
411 |
> |
Arrays.fill(a, 42); |
412 |
> |
assertSame(a, q.toArray(a)); |
413 |
> |
assertNull(a[0]); |
414 |
> |
for (int i = 1; i < a.length; i++) |
415 |
> |
assertEquals(42, (int) a[i]); |
416 |
|
} |
417 |
|
|
418 |
|
/** |
524 |
|
/** |
525 |
|
* a deserialized serialized queue is usable |
526 |
|
*/ |
527 |
< |
public void testSerialization() { testSerialization(false); } |
528 |
< |
public void testSerialization_fair() { testSerialization(true); } |
529 |
< |
public void testSerialization(boolean fair) { |
530 |
< |
final SynchronousQueue q = new SynchronousQueue(fair); |
531 |
< |
final SynchronousQueue r = serialClone(q); |
532 |
< |
assertTrue(q != r); |
533 |
< |
assertEquals(q.size(), r.size()); |
534 |
< |
while (!q.isEmpty()) |
535 |
< |
assertEquals(q.remove(), r.remove()); |
536 |
< |
} |
537 |
< |
|
538 |
< |
/** |
539 |
< |
* drainTo(null) throws NPE |
540 |
< |
*/ |
541 |
< |
public void testDrainToNull() { testDrainToNull(false); } |
542 |
< |
public void testDrainToNull_fair() { testDrainToNull(true); } |
598 |
< |
public void testDrainToNull(boolean fair) { |
599 |
< |
final SynchronousQueue q = new SynchronousQueue(fair); |
600 |
< |
try { |
601 |
< |
q.drainTo(null); |
602 |
< |
shouldThrow(); |
603 |
< |
} catch (NullPointerException success) {} |
604 |
< |
} |
605 |
< |
|
606 |
< |
/** |
607 |
< |
* drainTo(this) throws IAE |
608 |
< |
*/ |
609 |
< |
public void testDrainToSelf() { testDrainToSelf(false); } |
610 |
< |
public void testDrainToSelf_fair() { testDrainToSelf(true); } |
611 |
< |
public void testDrainToSelf(boolean fair) { |
612 |
< |
final SynchronousQueue q = new SynchronousQueue(fair); |
613 |
< |
try { |
614 |
< |
q.drainTo(q); |
615 |
< |
shouldThrow(); |
616 |
< |
} catch (IllegalArgumentException success) {} |
527 |
> |
public void testSerialization() { |
528 |
> |
final SynchronousQueue x = new SynchronousQueue(); |
529 |
> |
final SynchronousQueue y = new SynchronousQueue(false); |
530 |
> |
final SynchronousQueue z = new SynchronousQueue(true); |
531 |
> |
assertSerialEquals(x, y); |
532 |
> |
assertNotSerialEquals(x, z); |
533 |
> |
SynchronousQueue[] qs = { x, y, z }; |
534 |
> |
for (SynchronousQueue q : qs) { |
535 |
> |
SynchronousQueue clone = serialClone(q); |
536 |
> |
assertNotSame(q, clone); |
537 |
> |
assertSerialEquals(q, clone); |
538 |
> |
assertTrue(clone.isEmpty()); |
539 |
> |
assertEquals(0, clone.size()); |
540 |
> |
assertEquals(0, clone.remainingCapacity()); |
541 |
> |
assertFalse(clone.offer(zero)); |
542 |
> |
} |
543 |
|
} |
544 |
|
|
545 |
|
/** |
551 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
552 |
|
ArrayList l = new ArrayList(); |
553 |
|
q.drainTo(l); |
554 |
< |
assertEquals(q.size(), 0); |
555 |
< |
assertEquals(l.size(), 0); |
554 |
> |
assertEquals(0, q.size()); |
555 |
> |
assertEquals(0, l.size()); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
581 |
|
} |
582 |
|
|
583 |
|
/** |
658 |
– |
* drainTo(null, n) throws NullPointerException |
659 |
– |
*/ |
660 |
– |
public void testDrainToNullN() { testDrainToNullN(false); } |
661 |
– |
public void testDrainToNullN_fair() { testDrainToNullN(true); } |
662 |
– |
public void testDrainToNullN(boolean fair) { |
663 |
– |
final SynchronousQueue q = new SynchronousQueue(fair); |
664 |
– |
try { |
665 |
– |
q.drainTo(null, 0); |
666 |
– |
shouldThrow(); |
667 |
– |
} catch (NullPointerException success) {} |
668 |
– |
} |
669 |
– |
|
670 |
– |
/** |
671 |
– |
* drainTo(this, n) throws IllegalArgumentException |
672 |
– |
*/ |
673 |
– |
public void testDrainToSelfN() { testDrainToSelfN(false); } |
674 |
– |
public void testDrainToSelfN_fair() { testDrainToSelfN(true); } |
675 |
– |
public void testDrainToSelfN(boolean fair) { |
676 |
– |
final SynchronousQueue q = new SynchronousQueue(fair); |
677 |
– |
try { |
678 |
– |
q.drainTo(q, 0); |
679 |
– |
shouldThrow(); |
680 |
– |
} catch (IllegalArgumentException success) {} |
681 |
– |
} |
682 |
– |
|
683 |
– |
/** |
584 |
|
* drainTo(c, n) empties up to n elements of queue into c |
585 |
|
*/ |
586 |
|
public void testDrainToN() throws InterruptedException { |
607 |
|
awaitTermination(t2); |
608 |
|
} |
609 |
|
|
610 |
+ |
/** |
611 |
+ |
* remove(null), contains(null) always return false |
612 |
+ |
*/ |
613 |
+ |
public void testNeverContainsNull() { |
614 |
+ |
Collection<?> q = new SynchronousQueue(); |
615 |
+ |
assertFalse(q.contains(null)); |
616 |
+ |
assertFalse(q.remove(null)); |
617 |
+ |
} |
618 |
+ |
|
619 |
|
} |