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 |
|
|
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 |
|
/** |
325 |
– |
* remove(x) returns false |
326 |
– |
*/ |
327 |
– |
public void testRemoveElement() { testRemoveElement(false); } |
328 |
– |
public void testRemoveElement_fair() { testRemoveElement(true); } |
329 |
– |
public void testRemoveElement(boolean fair) { |
330 |
– |
final SynchronousQueue q = new SynchronousQueue(fair); |
331 |
– |
assertFalse(q.remove(zero)); |
332 |
– |
assertTrue(q.isEmpty()); |
333 |
– |
} |
334 |
– |
|
335 |
– |
/** |
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 |
|
/** |
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 |
|
} |
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 x = new SynchronousQueue(fair); |
531 |
< |
final SynchronousQueue y = serialClone(x); |
532 |
< |
assertTrue(x != y); |
533 |
< |
assertTrue(x.isEmpty()); |
534 |
< |
assertTrue(y.isEmpty()); |
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 |
|
/** |
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 |
|
} |