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; |
15 |
– |
import java.util.Queue; |
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 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
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() { |
423 |
|
public void testToArray_null(boolean fair) { |
424 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
425 |
|
try { |
426 |
< |
Object o[] = q.toArray(null); |
426 |
> |
Object[] o = q.toArray(null); |
427 |
|
shouldThrow(); |
428 |
|
} catch (NullPointerException success) {} |
429 |
|
} |
434 |
|
public void testIterator() { testIterator(false); } |
435 |
|
public void testIterator_fair() { testIterator(true); } |
436 |
|
public void testIterator(boolean fair) { |
437 |
< |
final SynchronousQueue q = new SynchronousQueue(fair); |
437 |
< |
Iterator it = q.iterator(); |
438 |
< |
assertFalse(it.hasNext()); |
439 |
< |
try { |
440 |
< |
Object x = it.next(); |
441 |
< |
shouldThrow(); |
442 |
< |
} catch (NoSuchElementException success) {} |
437 |
> |
assertIteratorExhausted(new SynchronousQueue(fair).iterator()); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
590 |
|
}}); |
591 |
|
|
592 |
|
ArrayList l = new ArrayList(); |
593 |
< |
delay(SHORT_DELAY_MS); |
594 |
< |
q.drainTo(l, 1); |
593 |
> |
int drained; |
594 |
> |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
595 |
> |
assertEquals(1, drained); |
596 |
|
assertEquals(1, l.size()); |
597 |
< |
q.drainTo(l, 1); |
597 |
> |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
598 |
> |
assertEquals(1, drained); |
599 |
|
assertEquals(2, l.size()); |
600 |
|
assertTrue(l.contains(one)); |
601 |
|
assertTrue(l.contains(two)); |
603 |
|
awaitTermination(t2); |
604 |
|
} |
605 |
|
|
606 |
+ |
/** |
607 |
+ |
* remove(null), contains(null) always return false |
608 |
+ |
*/ |
609 |
+ |
public void testNeverContainsNull() { |
610 |
+ |
Collection<?> q = new SynchronousQueue(); |
611 |
+ |
assertFalse(q.contains(null)); |
612 |
+ |
assertFalse(q.remove(null)); |
613 |
+ |
} |
614 |
+ |
|
615 |
|
} |