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; |
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() { |
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()); |
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(0, q.remainingCapacity()); |
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)); |
258 |
– |
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 |
|
} |
402 |
|
public void testToArray2() { testToArray2(false); } |
403 |
|
public void testToArray2_fair() { testToArray2(true); } |
404 |
|
public void testToArray2(boolean fair) { |
405 |
< |
final SynchronousQueue<Integer> q |
402 |
< |
= new SynchronousQueue<Integer>(fair); |
405 |
> |
final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair); |
406 |
|
Integer[] a; |
407 |
|
|
408 |
|
a = new Integer[0]; |
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); |
436 |
< |
Iterator it = q.iterator(); |
437 |
< |
assertFalse(it.hasNext()); |
438 |
< |
try { |
439 |
< |
Object x = it.next(); |
440 |
< |
shouldThrow(); |
441 |
< |
} 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); |
476 |
– |
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 |
< |
|
493 |
< |
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 |
|
/** |
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 |
|
} |
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)); |