24 |
|
|
25 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
+ |
static class Implementation implements CollectionImplementation { |
28 |
+ |
public Class<?> klazz() { return LinkedTransferQueue.class; } |
29 |
+ |
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
30 |
+ |
public Object makeElement(int i) { return i; } |
31 |
+ |
public boolean isConcurrent() { return true; } |
32 |
+ |
public boolean permitsNulls() { return false; } |
33 |
+ |
} |
34 |
|
|
35 |
|
public static class Generic extends BlockingQueueTest { |
36 |
|
protected BlockingQueue emptyCollection() { |
44 |
|
|
45 |
|
public static Test suite() { |
46 |
|
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
< |
new Generic().testSuite()); |
47 |
> |
new Generic().testSuite(), |
48 |
> |
CollectionTest.testSuite(new Implementation())); |
49 |
|
} |
50 |
|
|
51 |
|
/** |
86 |
|
*/ |
87 |
|
public void testConstructor4() { |
88 |
|
Integer[] ints = new Integer[SIZE]; |
89 |
< |
for (int i = 0; i < SIZE-1; ++i) |
89 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
90 |
|
ints[i] = i; |
91 |
|
Collection<Integer> elements = Arrays.asList(ints); |
92 |
|
try { |
283 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
284 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
285 |
|
public void realRun() throws InterruptedException { |
286 |
+ |
long startTime = System.nanoTime(); |
287 |
|
for (int i = 0; i < SIZE; ++i) { |
279 |
– |
long t0 = System.nanoTime(); |
288 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
281 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
289 |
|
} |
283 |
– |
long t0 = System.nanoTime(); |
290 |
|
aboutToWait.countDown(); |
291 |
|
try { |
292 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
292 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
293 |
|
shouldThrow(); |
294 |
|
} catch (InterruptedException success) { |
295 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
295 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
296 |
|
} |
297 |
|
}}); |
298 |
|
|
299 |
|
aboutToWait.await(); |
300 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
300 |
> |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
301 |
|
t.interrupt(); |
302 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
302 |
> |
awaitTermination(t); |
303 |
|
checkEmpty(q); |
304 |
|
} |
305 |
|
|
594 |
|
public void testOfferInExecutor() { |
595 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
596 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
597 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
597 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
598 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
599 |
|
|
600 |
< |
executor.execute(new CheckedRunnable() { |
601 |
< |
public void realRun() throws InterruptedException { |
602 |
< |
threadsStarted.await(); |
603 |
< |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
604 |
< |
}}); |
605 |
< |
|
606 |
< |
executor.execute(new CheckedRunnable() { |
607 |
< |
public void realRun() throws InterruptedException { |
608 |
< |
threadsStarted.await(); |
609 |
< |
assertSame(one, q.take()); |
610 |
< |
checkEmpty(q); |
611 |
< |
}}); |
612 |
< |
|
606 |
< |
joinPool(executor); |
600 |
> |
executor.execute(new CheckedRunnable() { |
601 |
> |
public void realRun() throws InterruptedException { |
602 |
> |
threadsStarted.await(); |
603 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
604 |
> |
}}); |
605 |
> |
|
606 |
> |
executor.execute(new CheckedRunnable() { |
607 |
> |
public void realRun() throws InterruptedException { |
608 |
> |
threadsStarted.await(); |
609 |
> |
assertSame(one, q.take()); |
610 |
> |
checkEmpty(q); |
611 |
> |
}}); |
612 |
> |
} |
613 |
|
} |
614 |
|
|
615 |
|
/** |
618 |
|
public void testPollInExecutor() { |
619 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
620 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
621 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
622 |
< |
|
617 |
< |
executor.execute(new CheckedRunnable() { |
618 |
< |
public void realRun() throws InterruptedException { |
619 |
< |
assertNull(q.poll()); |
620 |
< |
threadsStarted.await(); |
621 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
622 |
< |
checkEmpty(q); |
623 |
< |
}}); |
621 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
622 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
623 |
|
|
624 |
< |
executor.execute(new CheckedRunnable() { |
625 |
< |
public void realRun() throws InterruptedException { |
626 |
< |
threadsStarted.await(); |
627 |
< |
q.put(one); |
628 |
< |
}}); |
629 |
< |
|
630 |
< |
joinPool(executor); |
624 |
> |
executor.execute(new CheckedRunnable() { |
625 |
> |
public void realRun() throws InterruptedException { |
626 |
> |
assertNull(q.poll()); |
627 |
> |
threadsStarted.await(); |
628 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
629 |
> |
checkEmpty(q); |
630 |
> |
}}); |
631 |
> |
|
632 |
> |
executor.execute(new CheckedRunnable() { |
633 |
> |
public void realRun() throws InterruptedException { |
634 |
> |
threadsStarted.await(); |
635 |
> |
q.put(one); |
636 |
> |
}}); |
637 |
> |
} |
638 |
|
} |
639 |
|
|
640 |
|
/** |