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 |
|
|
36 |
|
} |
37 |
|
|
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run(suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
|
42 |
|
public static Test suite() { |
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); } |
138 |
|
}}); |
139 |
|
|
140 |
|
await(pleaseInterrupt); |
141 |
< |
assertThreadStaysAlive(t); |
141 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
142 |
|
t.interrupt(); |
143 |
|
awaitTermination(t); |
144 |
|
assertEquals(0, q.remainingCapacity()); |
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 |
|
|
174 |
|
await(pleaseInterrupt); |
175 |
< |
assertThreadStaysAlive(t); |
175 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
176 |
|
t.interrupt(); |
177 |
|
awaitTermination(t); |
178 |
< |
assertEquals(q.remainingCapacity(), 0); |
178 |
> |
assertEquals(0, q.remainingCapacity()); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
196 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
197 |
|
shouldThrow(); |
198 |
|
} catch (InterruptedException success) {} |
199 |
+ |
assertFalse(Thread.interrupted()); |
200 |
|
}}); |
201 |
|
|
202 |
|
await(pleaseInterrupt); |
203 |
< |
assertThreadStaysAlive(t); |
203 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
204 |
|
t.interrupt(); |
205 |
|
awaitTermination(t); |
206 |
|
} |
258 |
|
pleaseOffer.countDown(); |
259 |
|
startTime = System.nanoTime(); |
260 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
315 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
261 |
|
|
262 |
|
Thread.currentThread().interrupt(); |
263 |
|
try { |
272 |
|
shouldThrow(); |
273 |
|
} catch (InterruptedException success) {} |
274 |
|
assertFalse(Thread.interrupted()); |
275 |
+ |
|
276 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
277 |
|
}}); |
278 |
|
|
279 |
|
await(pleaseOffer); |
280 |
|
long startTime = System.nanoTime(); |
281 |
|
try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); } |
282 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
283 |
< |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
283 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
284 |
|
|
285 |
|
await(pleaseInterrupt); |
286 |
< |
assertThreadStaysAlive(t); |
286 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
287 |
|
t.interrupt(); |
288 |
|
awaitTermination(t); |
289 |
|
} |
325 |
|
} |
326 |
|
|
327 |
|
/** |
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 |
– |
/** |
328 |
|
* contains returns false |
329 |
|
*/ |
330 |
|
public void testContains() { testContains(false); } |
392 |
|
public void testToArray(boolean fair) { |
393 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
394 |
|
Object[] o = q.toArray(); |
395 |
< |
assertEquals(o.length, 0); |
395 |
> |
assertEquals(0, o.length); |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
< |
* toArray(a) is nulled at position 0 |
399 |
> |
* toArray(Integer array) returns its argument with the first |
400 |
> |
* element (if present) nulled out |
401 |
|
*/ |
402 |
|
public void testToArray2() { testToArray2(false); } |
403 |
|
public void testToArray2_fair() { testToArray2(true); } |
404 |
|
public void testToArray2(boolean fair) { |
405 |
< |
final SynchronousQueue q = new SynchronousQueue(fair); |
406 |
< |
Integer[] ints = new Integer[1]; |
407 |
< |
assertNull(ints[0]); |
405 |
> |
final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair); |
406 |
> |
Integer[] a; |
407 |
> |
|
408 |
> |
a = new Integer[0]; |
409 |
> |
assertSame(a, q.toArray(a)); |
410 |
> |
|
411 |
> |
a = new Integer[3]; |
412 |
> |
Arrays.fill(a, 42); |
413 |
> |
assertSame(a, q.toArray(a)); |
414 |
> |
assertNull(a[0]); |
415 |
> |
for (int i = 1; i < a.length; i++) |
416 |
> |
assertEquals(42, (int) a[i]); |
417 |
|
} |
418 |
|
|
419 |
|
/** |
424 |
|
public void testToArray_null(boolean fair) { |
425 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
426 |
|
try { |
427 |
< |
Object o[] = q.toArray(null); |
427 |
> |
Object[] o = q.toArray(null); |
428 |
|
shouldThrow(); |
429 |
|
} catch (NullPointerException success) {} |
430 |
|
} |
435 |
|
public void testIterator() { testIterator(false); } |
436 |
|
public void testIterator_fair() { testIterator(true); } |
437 |
|
public void testIterator(boolean fair) { |
438 |
< |
final SynchronousQueue q = new SynchronousQueue(fair); |
493 |
< |
Iterator it = q.iterator(); |
494 |
< |
assertFalse(it.hasNext()); |
495 |
< |
try { |
496 |
< |
Object x = it.next(); |
497 |
< |
shouldThrow(); |
498 |
< |
} catch (NoSuchElementException success) {} |
438 |
> |
assertIteratorExhausted(new SynchronousQueue(fair).iterator()); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
470 |
|
public void testOfferInExecutor_fair() { testOfferInExecutor(true); } |
471 |
|
public void testOfferInExecutor(boolean fair) { |
472 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
533 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
473 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
474 |
+ |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
475 |
+ |
try (PoolCleaner cleaner = cleaner(executor)) { |
476 |
|
|
477 |
< |
executor.execute(new CheckedRunnable() { |
478 |
< |
public void realRun() throws InterruptedException { |
479 |
< |
assertFalse(q.offer(one)); |
480 |
< |
threadsStarted.await(); |
481 |
< |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
482 |
< |
assertEquals(0, q.remainingCapacity()); |
483 |
< |
}}); |
484 |
< |
|
485 |
< |
executor.execute(new CheckedRunnable() { |
486 |
< |
public void realRun() throws InterruptedException { |
487 |
< |
threadsStarted.await(); |
488 |
< |
assertSame(one, q.take()); |
489 |
< |
}}); |
490 |
< |
|
550 |
< |
joinPool(executor); |
477 |
> |
executor.execute(new CheckedRunnable() { |
478 |
> |
public void realRun() throws InterruptedException { |
479 |
> |
assertFalse(q.offer(one)); |
480 |
> |
threadsStarted.await(); |
481 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
482 |
> |
assertEquals(0, q.remainingCapacity()); |
483 |
> |
}}); |
484 |
> |
|
485 |
> |
executor.execute(new CheckedRunnable() { |
486 |
> |
public void realRun() throws InterruptedException { |
487 |
> |
threadsStarted.await(); |
488 |
> |
assertSame(one, q.take()); |
489 |
> |
}}); |
490 |
> |
} |
491 |
|
} |
492 |
|
|
493 |
|
/** |
498 |
|
public void testPollInExecutor(boolean fair) { |
499 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
500 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
501 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
502 |
< |
executor.execute(new CheckedRunnable() { |
503 |
< |
public void realRun() throws InterruptedException { |
504 |
< |
assertNull(q.poll()); |
505 |
< |
threadsStarted.await(); |
506 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
507 |
< |
assertTrue(q.isEmpty()); |
508 |
< |
}}); |
509 |
< |
|
510 |
< |
executor.execute(new CheckedRunnable() { |
511 |
< |
public void realRun() throws InterruptedException { |
512 |
< |
threadsStarted.await(); |
513 |
< |
q.put(one); |
514 |
< |
}}); |
515 |
< |
|
516 |
< |
joinPool(executor); |
501 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
502 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
503 |
> |
executor.execute(new CheckedRunnable() { |
504 |
> |
public void realRun() throws InterruptedException { |
505 |
> |
assertNull(q.poll()); |
506 |
> |
threadsStarted.await(); |
507 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
508 |
> |
assertTrue(q.isEmpty()); |
509 |
> |
}}); |
510 |
> |
|
511 |
> |
executor.execute(new CheckedRunnable() { |
512 |
> |
public void realRun() throws InterruptedException { |
513 |
> |
threadsStarted.await(); |
514 |
> |
q.put(one); |
515 |
> |
}}); |
516 |
> |
} |
517 |
|
} |
518 |
|
|
519 |
|
/** |
520 |
|
* a deserialized serialized queue is usable |
521 |
|
*/ |
522 |
< |
public void testSerialization() { testSerialization(false); } |
523 |
< |
public void testSerialization_fair() { testSerialization(true); } |
524 |
< |
public void testSerialization(boolean fair) { |
525 |
< |
final SynchronousQueue q = new SynchronousQueue(fair); |
526 |
< |
final SynchronousQueue r = serialClone(q); |
527 |
< |
assertTrue(q != r); |
528 |
< |
assertEquals(q.size(), r.size()); |
529 |
< |
while (!q.isEmpty()) |
530 |
< |
assertEquals(q.remove(), r.remove()); |
531 |
< |
} |
532 |
< |
|
533 |
< |
/** |
534 |
< |
* drainTo(null) throws NPE |
535 |
< |
*/ |
536 |
< |
public void testDrainToNull() { testDrainToNull(false); } |
537 |
< |
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) {} |
522 |
> |
public void testSerialization() { |
523 |
> |
final SynchronousQueue x = new SynchronousQueue(); |
524 |
> |
final SynchronousQueue y = new SynchronousQueue(false); |
525 |
> |
final SynchronousQueue z = new SynchronousQueue(true); |
526 |
> |
assertSerialEquals(x, y); |
527 |
> |
assertNotSerialEquals(x, z); |
528 |
> |
SynchronousQueue[] qs = { x, y, z }; |
529 |
> |
for (SynchronousQueue q : qs) { |
530 |
> |
SynchronousQueue clone = serialClone(q); |
531 |
> |
assertNotSame(q, clone); |
532 |
> |
assertSerialEquals(q, clone); |
533 |
> |
assertTrue(clone.isEmpty()); |
534 |
> |
assertEquals(0, clone.size()); |
535 |
> |
assertEquals(0, clone.remainingCapacity()); |
536 |
> |
assertFalse(clone.offer(zero)); |
537 |
> |
} |
538 |
|
} |
539 |
|
|
540 |
|
/** |
546 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
547 |
|
ArrayList l = new ArrayList(); |
548 |
|
q.drainTo(l); |
549 |
< |
assertEquals(q.size(), 0); |
550 |
< |
assertEquals(l.size(), 0); |
549 |
> |
assertEquals(0, q.size()); |
550 |
> |
assertEquals(0, l.size()); |
551 |
|
} |
552 |
|
|
553 |
|
/** |
570 |
|
fail("timed out"); |
571 |
|
Thread.yield(); |
572 |
|
} |
573 |
< |
assertTrue(l.size() == 1); |
573 |
> |
assertEquals(1, l.size()); |
574 |
|
assertSame(one, l.get(0)); |
575 |
|
awaitTermination(t); |
576 |
|
} |
577 |
|
|
578 |
|
/** |
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 |
– |
/** |
579 |
|
* drainTo(c, n) empties up to n elements of queue into c |
580 |
|
*/ |
581 |
|
public void testDrainToN() throws InterruptedException { |
591 |
|
}}); |
592 |
|
|
593 |
|
ArrayList l = new ArrayList(); |
594 |
< |
delay(SHORT_DELAY_MS); |
595 |
< |
q.drainTo(l, 1); |
594 |
> |
int drained; |
595 |
> |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
596 |
> |
assertEquals(1, drained); |
597 |
|
assertEquals(1, l.size()); |
598 |
< |
q.drainTo(l, 1); |
598 |
> |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
599 |
> |
assertEquals(1, drained); |
600 |
|
assertEquals(2, l.size()); |
601 |
|
assertTrue(l.contains(one)); |
602 |
|
assertTrue(l.contains(two)); |
604 |
|
awaitTermination(t2); |
605 |
|
} |
606 |
|
|
607 |
+ |
/** |
608 |
+ |
* remove(null), contains(null) always return false |
609 |
+ |
*/ |
610 |
+ |
public void testNeverContainsNull() { |
611 |
+ |
Collection<?> q = new SynchronousQueue(); |
612 |
+ |
assertFalse(q.contains(null)); |
613 |
+ |
assertFalse(q.remove(null)); |
614 |
+ |
} |
615 |
+ |
|
616 |
|
} |