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() { |
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 |
|
/** |
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 |
|
} |
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); |
426 |
< |
Iterator it = q.iterator(); |
427 |
< |
assertFalse(it.hasNext()); |
428 |
< |
try { |
429 |
< |
Object x = it.next(); |
430 |
< |
shouldThrow(); |
431 |
< |
} catch (NoSuchElementException success) {} |
437 |
> |
assertIteratorExhausted(new SynchronousQueue(fair).iterator()); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
469 |
|
public void testOfferInExecutor_fair() { testOfferInExecutor(true); } |
470 |
|
public void testOfferInExecutor(boolean fair) { |
471 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
466 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
472 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
473 |
+ |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
474 |
+ |
try (PoolCleaner cleaner = cleaner(executor)) { |
475 |
|
|
476 |
< |
executor.execute(new CheckedRunnable() { |
477 |
< |
public void realRun() throws InterruptedException { |
478 |
< |
assertFalse(q.offer(one)); |
479 |
< |
threadsStarted.await(); |
480 |
< |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
481 |
< |
assertEquals(0, q.remainingCapacity()); |
482 |
< |
}}); |
483 |
< |
|
484 |
< |
executor.execute(new CheckedRunnable() { |
485 |
< |
public void realRun() throws InterruptedException { |
486 |
< |
threadsStarted.await(); |
487 |
< |
assertSame(one, q.take()); |
488 |
< |
}}); |
489 |
< |
|
483 |
< |
joinPool(executor); |
476 |
> |
executor.execute(new CheckedRunnable() { |
477 |
> |
public void realRun() throws InterruptedException { |
478 |
> |
assertFalse(q.offer(one)); |
479 |
> |
threadsStarted.await(); |
480 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
481 |
> |
assertEquals(0, q.remainingCapacity()); |
482 |
> |
}}); |
483 |
> |
|
484 |
> |
executor.execute(new CheckedRunnable() { |
485 |
> |
public void realRun() throws InterruptedException { |
486 |
> |
threadsStarted.await(); |
487 |
> |
assertSame(one, q.take()); |
488 |
> |
}}); |
489 |
> |
} |
490 |
|
} |
491 |
|
|
492 |
|
/** |
497 |
|
public void testPollInExecutor(boolean fair) { |
498 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
499 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
500 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
501 |
< |
executor.execute(new CheckedRunnable() { |
502 |
< |
public void realRun() throws InterruptedException { |
503 |
< |
assertNull(q.poll()); |
504 |
< |
threadsStarted.await(); |
505 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
506 |
< |
assertTrue(q.isEmpty()); |
507 |
< |
}}); |
508 |
< |
|
509 |
< |
executor.execute(new CheckedRunnable() { |
510 |
< |
public void realRun() throws InterruptedException { |
511 |
< |
threadsStarted.await(); |
512 |
< |
q.put(one); |
513 |
< |
}}); |
514 |
< |
|
515 |
< |
joinPool(executor); |
500 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
501 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
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 |
|
} |
517 |
|
|
518 |
|
/** |
519 |
|
* a deserialized serialized queue is usable |
520 |
|
*/ |
521 |
< |
public void testSerialization() { testSerialization(false); } |
522 |
< |
public void testSerialization_fair() { testSerialization(true); } |
523 |
< |
public void testSerialization(boolean fair) { |
524 |
< |
final SynchronousQueue x = new SynchronousQueue(fair); |
525 |
< |
final SynchronousQueue y = serialClone(x); |
526 |
< |
assertTrue(x != y); |
527 |
< |
assertTrue(x.isEmpty()); |
528 |
< |
assertTrue(y.isEmpty()); |
521 |
> |
public void testSerialization() { |
522 |
> |
final SynchronousQueue x = new SynchronousQueue(); |
523 |
> |
final SynchronousQueue y = new SynchronousQueue(false); |
524 |
> |
final SynchronousQueue z = new SynchronousQueue(true); |
525 |
> |
assertSerialEquals(x, y); |
526 |
> |
assertNotSerialEquals(x, z); |
527 |
> |
SynchronousQueue[] qs = { x, y, z }; |
528 |
> |
for (SynchronousQueue q : qs) { |
529 |
> |
SynchronousQueue clone = serialClone(q); |
530 |
> |
assertNotSame(q, clone); |
531 |
> |
assertSerialEquals(q, clone); |
532 |
> |
assertTrue(clone.isEmpty()); |
533 |
> |
assertEquals(0, clone.size()); |
534 |
> |
assertEquals(0, clone.remainingCapacity()); |
535 |
> |
assertFalse(clone.offer(zero)); |
536 |
> |
} |
537 |
|
} |
538 |
|
|
539 |
|
/** |
545 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
546 |
|
ArrayList l = new ArrayList(); |
547 |
|
q.drainTo(l); |
548 |
< |
assertEquals(q.size(), 0); |
549 |
< |
assertEquals(l.size(), 0); |
548 |
> |
assertEquals(0, q.size()); |
549 |
> |
assertEquals(0, l.size()); |
550 |
|
} |
551 |
|
|
552 |
|
/** |
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 |
|
} |