18 |
|
import java.util.NoSuchElementException; |
19 |
|
import java.util.concurrent.*; |
20 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
22 |
|
import junit.framework.Test; |
23 |
|
import junit.framework.TestSuite; |
24 |
|
|
25 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
|
|
28 |
+ |
public static class Generic extends BlockingQueueTest { |
29 |
+ |
protected BlockingQueue emptyCollection() { |
30 |
+ |
return new LinkedTransferQueue(); |
31 |
+ |
} |
32 |
+ |
} |
33 |
+ |
|
34 |
|
public static void main(String[] args) { |
35 |
|
junit.textui.TestRunner.run(suite()); |
36 |
|
} |
37 |
|
|
38 |
|
public static Test suite() { |
39 |
< |
return new TestSuite(LinkedTransferQueueTest.class); |
39 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
40 |
> |
new Generic().testSuite()); |
41 |
|
} |
42 |
|
|
43 |
< |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
36 |
< |
assertTrue(q.isEmpty()); |
37 |
< |
assertEquals(0, q.size()); |
38 |
< |
assertNull(q.peek()); |
39 |
< |
assertNull(q.poll()); |
40 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
41 |
< |
assertEquals(q.toString(), "[]"); |
42 |
< |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
43 |
< |
assertFalse(q.iterator().hasNext()); |
43 |
> |
void checkEmpty(BlockingQueue q) { |
44 |
|
try { |
45 |
< |
q.element(); |
46 |
< |
shouldThrow(); |
47 |
< |
} catch (NoSuchElementException success) { |
48 |
< |
} |
49 |
< |
try { |
50 |
< |
q.iterator().next(); |
51 |
< |
shouldThrow(); |
52 |
< |
} catch (NoSuchElementException success) { |
53 |
< |
} |
54 |
< |
try { |
55 |
< |
q.remove(); |
56 |
< |
shouldThrow(); |
57 |
< |
} catch (NoSuchElementException success) { |
45 |
> |
assertTrue(q.isEmpty()); |
46 |
> |
assertEquals(0, q.size()); |
47 |
> |
assertNull(q.peek()); |
48 |
> |
assertNull(q.poll()); |
49 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
50 |
> |
assertEquals(q.toString(), "[]"); |
51 |
> |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
52 |
> |
assertFalse(q.iterator().hasNext()); |
53 |
> |
try { |
54 |
> |
q.element(); |
55 |
> |
shouldThrow(); |
56 |
> |
} catch (NoSuchElementException success) {} |
57 |
> |
try { |
58 |
> |
q.iterator().next(); |
59 |
> |
shouldThrow(); |
60 |
> |
} catch (NoSuchElementException success) {} |
61 |
> |
try { |
62 |
> |
q.remove(); |
63 |
> |
shouldThrow(); |
64 |
> |
} catch (NoSuchElementException success) {} |
65 |
> |
} catch (InterruptedException ie) { |
66 |
> |
threadUnexpectedException(ie); |
67 |
|
} |
68 |
|
} |
69 |
|
|
84 |
|
try { |
85 |
|
new LinkedTransferQueue(null); |
86 |
|
shouldThrow(); |
87 |
< |
} catch (NullPointerException success) { |
79 |
< |
} |
87 |
> |
} catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
96 |
|
Integer[] ints = new Integer[SIZE]; |
97 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
98 |
|
shouldThrow(); |
99 |
< |
} catch (NullPointerException success) { |
92 |
< |
} |
99 |
> |
} catch (NullPointerException success) {} |
100 |
|
} |
101 |
|
|
102 |
|
/** |
111 |
|
} |
112 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
113 |
|
shouldThrow(); |
114 |
< |
} catch (NullPointerException success) { |
108 |
< |
} |
114 |
> |
} catch (NullPointerException success) {} |
115 |
|
} |
116 |
|
|
117 |
|
/** |
163 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
164 |
|
q.offer(null); |
165 |
|
shouldThrow(); |
166 |
< |
} catch (NullPointerException success) { |
161 |
< |
} |
166 |
> |
} catch (NullPointerException success) {} |
167 |
|
} |
168 |
|
|
169 |
|
/** |
174 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
175 |
|
q.add(null); |
176 |
|
shouldThrow(); |
177 |
< |
} catch (NullPointerException success) { |
173 |
< |
} |
177 |
> |
} catch (NullPointerException success) {} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
185 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
186 |
|
q.addAll(null); |
187 |
|
shouldThrow(); |
188 |
< |
} catch (NullPointerException success) { |
185 |
< |
} |
188 |
> |
} catch (NullPointerException success) {} |
189 |
|
} |
190 |
|
|
191 |
|
/** |
196 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
197 |
|
q.addAll(q); |
198 |
|
shouldThrow(); |
199 |
< |
} catch (IllegalArgumentException success) { |
197 |
< |
} |
199 |
> |
} catch (IllegalArgumentException success) {} |
200 |
|
} |
201 |
|
|
202 |
|
/** |
208 |
|
Integer[] ints = new Integer[SIZE]; |
209 |
|
q.addAll(Arrays.asList(ints)); |
210 |
|
shouldThrow(); |
211 |
< |
} catch (NullPointerException success) { |
210 |
< |
} |
211 |
> |
} catch (NullPointerException success) {} |
212 |
|
} |
213 |
|
|
214 |
|
/** |
224 |
|
} |
225 |
|
q.addAll(Arrays.asList(ints)); |
226 |
|
shouldThrow(); |
227 |
< |
} catch (NullPointerException success) { |
227 |
< |
} |
227 |
> |
} catch (NullPointerException success) {} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* take blocks interruptibly when empty |
282 |
< |
*/ |
283 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
284 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
285 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
286 |
< |
void realRun() throws InterruptedException { |
287 |
< |
q.take(); |
288 |
< |
}}); |
289 |
< |
Thread.sleep(SHORT_DELAY_MS); |
290 |
< |
t.interrupt(); |
291 |
< |
t.join(); |
292 |
< |
} |
293 |
< |
|
294 |
< |
/** |
295 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
281 |
> |
* take removes existing elements until empty, then blocks interruptibly |
282 |
|
*/ |
283 |
|
public void testBlockingTake() throws InterruptedException { |
284 |
< |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
285 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
286 |
< |
void realRun() throws InterruptedException { |
284 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
285 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
286 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
287 |
> |
public void realRun() throws InterruptedException { |
288 |
|
for (int i = 0; i < SIZE; ++i) { |
289 |
< |
threadAssertEquals(i, (int) q.take()); |
289 |
> |
assertEquals(i, (int) q.take()); |
290 |
|
} |
291 |
< |
q.take(); |
291 |
> |
aboutToWait.countDown(); |
292 |
> |
try { |
293 |
> |
q.take(); |
294 |
> |
shouldThrow(); |
295 |
> |
} catch (InterruptedException success) {} |
296 |
|
}}); |
297 |
< |
Thread.sleep(SMALL_DELAY_MS); |
297 |
> |
|
298 |
> |
aboutToWait.await(); |
299 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
300 |
|
t.interrupt(); |
301 |
< |
t.join(); |
301 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
302 |
|
checkEmpty(q); |
303 |
|
} |
304 |
|
|
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
318 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
319 |
|
*/ |
320 |
|
public void testTimedPoll0() throws InterruptedException { |
321 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
330 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
331 |
|
*/ |
332 |
|
public void testTimedPoll() throws InterruptedException { |
333 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
334 |
|
for (int i = 0; i < SIZE; ++i) { |
335 |
|
long t0 = System.nanoTime(); |
336 |
< |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
337 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
345 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
336 |
> |
assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
337 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
338 |
|
} |
339 |
+ |
long t0 = System.nanoTime(); |
340 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
341 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
342 |
|
checkEmpty(q); |
343 |
|
} |
344 |
|
|
347 |
|
* returning timeout status |
348 |
|
*/ |
349 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
350 |
< |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
350 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
351 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
352 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
353 |
< |
void realRun() throws InterruptedException { |
353 |
> |
public void realRun() throws InterruptedException { |
354 |
|
for (int i = 0; i < SIZE; ++i) { |
355 |
|
long t0 = System.nanoTime(); |
356 |
< |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
357 |
< |
MILLISECONDS)); |
363 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
364 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
356 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
357 |
> |
assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS); |
358 |
|
} |
359 |
+ |
aboutToWait.countDown(); |
360 |
|
try { |
361 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
361 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
362 |
> |
shouldThrow(); |
363 |
|
} catch (InterruptedException success) {} |
364 |
|
}}); |
365 |
< |
Thread.sleep(SMALL_DELAY_MS); |
365 |
> |
|
366 |
> |
aboutToWait.await(); |
367 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
368 |
|
t.interrupt(); |
369 |
< |
t.join(); |
369 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
370 |
|
checkEmpty(q); |
371 |
|
} |
372 |
|
|
373 |
|
/** |
374 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
375 |
< |
* on interruption throws |
374 |
> |
* timed poll after thread interrupted throws InterruptedException |
375 |
> |
* instead of returning timeout status |
376 |
|
*/ |
377 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
378 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
379 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
380 |
< |
void realRun() throws InterruptedException { |
381 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
382 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
383 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
377 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
378 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
379 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
380 |
> |
public void realRun() throws InterruptedException { |
381 |
> |
Thread.currentThread().interrupt(); |
382 |
> |
for (int i = 0; i < SIZE; ++i) { |
383 |
> |
long t0 = System.nanoTime(); |
384 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
385 |
> |
assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS); |
386 |
> |
} |
387 |
> |
try { |
388 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
389 |
> |
shouldThrow(); |
390 |
> |
} catch (InterruptedException success) {} |
391 |
|
}}); |
392 |
< |
Thread.sleep(SMALL_DELAY_MS); |
393 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
394 |
< |
t.interrupt(); |
391 |
< |
t.join(); |
392 |
> |
|
393 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
394 |
> |
checkEmpty(q); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
421 |
|
try { |
422 |
|
q.element(); |
423 |
|
shouldThrow(); |
424 |
< |
} catch (NoSuchElementException success) { |
422 |
< |
} |
424 |
> |
} catch (NoSuchElementException success) {} |
425 |
|
checkEmpty(q); |
426 |
|
} |
427 |
|
|
436 |
|
try { |
437 |
|
q.remove(); |
438 |
|
shouldThrow(); |
439 |
< |
} catch (NoSuchElementException success) { |
438 |
< |
} |
439 |
> |
} catch (NoSuchElementException success) {} |
440 |
|
checkEmpty(q); |
441 |
|
} |
442 |
|
|
465 |
|
assertTrue(q.remove(one)); |
466 |
|
assertTrue(q.remove(two)); |
467 |
|
assertTrue(q.add(three)); |
468 |
< |
assertTrue(q.take() == three); |
468 |
> |
assertSame(q.take(), three); |
469 |
|
} |
470 |
|
|
471 |
|
/** |
573 |
|
* toArray(null) throws NullPointerException |
574 |
|
*/ |
575 |
|
public void testToArray_BadArg() { |
576 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
577 |
|
try { |
576 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
578 |
|
Object o[] = q.toArray(null); |
579 |
|
shouldThrow(); |
580 |
< |
} catch (NullPointerException success) { |
580 |
< |
} |
580 |
> |
} catch (NullPointerException success) {} |
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
|
* toArray(incompatible array type) throws CCE |
585 |
|
*/ |
586 |
|
public void testToArray1_BadArg() { |
587 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
588 |
|
try { |
588 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
589 |
|
Object o[] = q.toArray(new String[10]); |
590 |
|
shouldThrow(); |
591 |
< |
} catch (ArrayStoreException success) { |
592 |
< |
} |
591 |
> |
} catch (ArrayStoreException success) {} |
592 |
|
} |
593 |
|
|
594 |
|
/** |
618 |
|
it.remove(); |
619 |
|
|
620 |
|
it = q.iterator(); |
621 |
< |
assertEquals(it.next(), one); |
622 |
< |
assertEquals(it.next(), three); |
621 |
> |
assertSame(it.next(), one); |
622 |
> |
assertSame(it.next(), three); |
623 |
|
assertFalse(it.hasNext()); |
624 |
|
} |
625 |
|
|
672 |
|
*/ |
673 |
|
public void testOfferInExecutor() { |
674 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
675 |
< |
q.add(one); |
677 |
< |
q.add(two); |
675 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
676 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
677 |
|
|
678 |
|
executor.execute(new CheckedRunnable() { |
679 |
< |
void realRun() { |
680 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
681 |
< |
MILLISECONDS)); |
679 |
> |
public void realRun() throws InterruptedException { |
680 |
> |
threadsStarted.countDown(); |
681 |
> |
threadsStarted.await(); |
682 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
683 |
|
}}); |
684 |
|
|
685 |
|
executor.execute(new CheckedRunnable() { |
686 |
< |
void realRun() throws InterruptedException { |
687 |
< |
Thread.sleep(SMALL_DELAY_MS); |
688 |
< |
threadAssertEquals(one, q.take()); |
686 |
> |
public void realRun() throws InterruptedException { |
687 |
> |
threadsStarted.countDown(); |
688 |
> |
threadsStarted.await(); |
689 |
> |
assertSame(one, q.take()); |
690 |
> |
checkEmpty(q); |
691 |
|
}}); |
692 |
|
|
693 |
|
joinPool(executor); |
698 |
|
*/ |
699 |
|
public void testPollInExecutor() { |
700 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
701 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
702 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
703 |
|
|
704 |
|
executor.execute(new CheckedRunnable() { |
705 |
< |
void realRun() throws InterruptedException { |
706 |
< |
threadAssertNull(q.poll()); |
707 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
708 |
< |
MILLISECONDS)); |
709 |
< |
threadAssertTrue(q.isEmpty()); |
705 |
> |
public void realRun() throws InterruptedException { |
706 |
> |
assertNull(q.poll()); |
707 |
> |
threadsStarted.countDown(); |
708 |
> |
threadsStarted.await(); |
709 |
> |
assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
710 |
> |
checkEmpty(q); |
711 |
|
}}); |
712 |
|
|
713 |
|
executor.execute(new CheckedRunnable() { |
714 |
< |
void realRun() throws InterruptedException { |
715 |
< |
Thread.sleep(SMALL_DELAY_MS); |
714 |
> |
public void realRun() throws InterruptedException { |
715 |
> |
threadsStarted.countDown(); |
716 |
> |
threadsStarted.await(); |
717 |
|
q.put(one); |
718 |
|
}}); |
719 |
|
|
739 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
740 |
|
|
741 |
|
assertEquals(q.size(), r.size()); |
742 |
+ |
assertEquals(q.toString(), r.toString()); |
743 |
+ |
assertTrue(Arrays.equals(q.toArray(), r.toArray())); |
744 |
|
while (!q.isEmpty()) { |
745 |
|
assertEquals(q.remove(), r.remove()); |
746 |
|
} |
754 |
|
try { |
755 |
|
q.drainTo(null); |
756 |
|
shouldThrow(); |
757 |
< |
} catch (NullPointerException success) { |
752 |
< |
} |
757 |
> |
} catch (NullPointerException success) {} |
758 |
|
} |
759 |
|
|
760 |
|
/** |
765 |
|
try { |
766 |
|
q.drainTo(q); |
767 |
|
shouldThrow(); |
768 |
< |
} catch (IllegalArgumentException success) { |
764 |
< |
} |
768 |
> |
} catch (IllegalArgumentException success) {} |
769 |
|
} |
770 |
|
|
771 |
|
/** |
800 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
801 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
802 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
803 |
< |
void realRun() { |
803 |
> |
public void realRun() { |
804 |
|
q.put(SIZE + 1); |
805 |
|
}}); |
806 |
|
ArrayList l = new ArrayList(); |
809 |
|
for (int i = 0; i < SIZE; ++i) { |
810 |
|
assertEquals(l.get(i), i); |
811 |
|
} |
812 |
< |
t.join(); |
812 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
813 |
|
assertTrue(q.size() + l.size() >= SIZE); |
814 |
|
} |
815 |
|
|
821 |
|
try { |
822 |
|
q.drainTo(null, SIZE); |
823 |
|
shouldThrow(); |
824 |
< |
} catch (NullPointerException success) { |
821 |
< |
} |
824 |
> |
} catch (NullPointerException success) {} |
825 |
|
} |
826 |
|
|
827 |
|
/** |
832 |
|
try { |
833 |
|
q.drainTo(q, SIZE); |
834 |
|
shouldThrow(); |
835 |
< |
} catch (IllegalArgumentException success) { |
833 |
< |
} |
835 |
> |
} catch (IllegalArgumentException success) {} |
836 |
|
} |
837 |
|
|
838 |
|
/** |
839 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
839 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
840 |
|
*/ |
841 |
|
public void testDrainToN() { |
842 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
865 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
866 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
867 |
|
assertFalse(q.hasWaitingConsumer()); |
868 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
869 |
|
|
870 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
871 |
< |
void realRun() throws InterruptedException { |
872 |
< |
Thread.sleep(SMALL_DELAY_MS); |
873 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
874 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
875 |
< |
threadAssertTrue(q.offer(new Object())); |
873 |
< |
threadAssertFalse(q.hasWaitingConsumer()); |
874 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
871 |
> |
public void realRun() throws InterruptedException { |
872 |
> |
threadStarted.countDown(); |
873 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
874 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
875 |
> |
assertFalse(q.hasWaitingConsumer()); |
876 |
|
}}); |
877 |
|
|
878 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
878 |
> |
threadStarted.await(); |
879 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
880 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
881 |
> |
assertTrue(q.hasWaitingConsumer()); |
882 |
> |
|
883 |
> |
assertTrue(q.offer(one)); |
884 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
885 |
|
assertFalse(q.hasWaitingConsumer()); |
886 |
< |
t.join(); |
886 |
> |
|
887 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
888 |
|
} |
889 |
|
|
890 |
|
/** |
905 |
|
public void testTransfer2() throws InterruptedException { |
906 |
|
final LinkedTransferQueue<Integer> q |
907 |
|
= new LinkedTransferQueue<Integer>(); |
908 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
909 |
|
|
910 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
911 |
< |
void realRun() throws InterruptedException { |
912 |
< |
q.transfer(SIZE); |
913 |
< |
threadAssertTrue(q.isEmpty()); |
911 |
> |
public void realRun() throws InterruptedException { |
912 |
> |
threadStarted.countDown(); |
913 |
> |
q.transfer(five); |
914 |
> |
checkEmpty(q); |
915 |
|
}}); |
916 |
|
|
917 |
< |
Thread.sleep(SHORT_DELAY_MS); |
917 |
> |
threadStarted.await(); |
918 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
919 |
|
assertEquals(1, q.size()); |
920 |
< |
assertEquals(SIZE, (int) q.poll()); |
921 |
< |
assertTrue(q.isEmpty()); |
922 |
< |
t.join(); |
920 |
> |
assertSame(five, q.poll()); |
921 |
> |
checkEmpty(q); |
922 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
923 |
|
} |
924 |
|
|
925 |
|
/** |
930 |
|
= new LinkedTransferQueue<Integer>(); |
931 |
|
|
932 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
933 |
< |
void realRun() throws InterruptedException { |
934 |
< |
Integer i = SIZE + 1; |
935 |
< |
q.transfer(i); |
936 |
< |
threadAssertTrue(!q.contains(i)); |
927 |
< |
threadAssertEquals(1, q.size()); |
933 |
> |
public void realRun() throws InterruptedException { |
934 |
> |
q.transfer(four); |
935 |
> |
assertTrue(!q.contains(four)); |
936 |
> |
assertEquals(1, q.size()); |
937 |
|
}}); |
938 |
|
|
939 |
|
Thread interruptedThread = newStartedThread( |
940 |
|
new CheckedInterruptedRunnable() { |
941 |
< |
void realRun() throws InterruptedException { |
942 |
< |
while (q.size() == 0) |
941 |
> |
public void realRun() throws InterruptedException { |
942 |
> |
while (q.isEmpty()) |
943 |
|
Thread.yield(); |
944 |
< |
q.transfer(SIZE); |
944 |
> |
q.transfer(five); |
945 |
|
}}); |
946 |
|
|
947 |
|
while (q.size() < 2) |
948 |
|
Thread.yield(); |
949 |
|
assertEquals(2, q.size()); |
950 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
950 |
> |
assertSame(four, q.poll()); |
951 |
|
first.join(); |
952 |
|
assertEquals(1, q.size()); |
953 |
|
interruptedThread.interrupt(); |
954 |
|
interruptedThread.join(); |
955 |
< |
assertEquals(0, q.size()); |
947 |
< |
assertTrue(q.isEmpty()); |
955 |
> |
checkEmpty(q); |
956 |
|
} |
957 |
|
|
958 |
|
/** |
963 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
964 |
|
|
965 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
966 |
< |
void realRun() throws InterruptedException { |
966 |
> |
public void realRun() throws InterruptedException { |
967 |
|
q.transfer(four); |
968 |
< |
threadAssertFalse(q.contains(four)); |
969 |
< |
threadAssertEquals(three, q.poll()); |
968 |
> |
assertFalse(q.contains(four)); |
969 |
> |
assertSame(three, q.poll()); |
970 |
|
}}); |
971 |
|
|
972 |
< |
Thread.sleep(SHORT_DELAY_MS); |
972 |
> |
while (q.isEmpty()) |
973 |
> |
Thread.yield(); |
974 |
> |
assertFalse(q.isEmpty()); |
975 |
> |
assertEquals(1, q.size()); |
976 |
|
assertTrue(q.offer(three)); |
977 |
< |
assertEquals(four, q.poll()); |
978 |
< |
t.join(); |
977 |
> |
assertSame(four, q.poll()); |
978 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
979 |
|
} |
980 |
|
|
981 |
|
/** |
987 |
|
= new LinkedTransferQueue<Integer>(); |
988 |
|
|
989 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
990 |
< |
void realRun() throws InterruptedException { |
991 |
< |
q.transfer(SIZE); |
990 |
> |
public void realRun() throws InterruptedException { |
991 |
> |
q.transfer(four); |
992 |
|
checkEmpty(q); |
993 |
|
}}); |
994 |
|
|
995 |
< |
Thread.sleep(SHORT_DELAY_MS); |
996 |
< |
assertEquals(SIZE, (int) q.take()); |
995 |
> |
while (q.isEmpty()) |
996 |
> |
Thread.yield(); |
997 |
> |
assertFalse(q.isEmpty()); |
998 |
> |
assertEquals(1, q.size()); |
999 |
> |
assertSame(four, q.take()); |
1000 |
|
checkEmpty(q); |
1001 |
< |
t.join(); |
1001 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1002 |
|
} |
1003 |
|
|
1004 |
|
/** |
1032 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1033 |
|
|
1034 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1035 |
< |
void realRun() { |
1035 |
> |
public void realRun() { |
1036 |
|
while (! q.hasWaitingConsumer()) |
1037 |
|
Thread.yield(); |
1038 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1039 |
< |
threadAssertTrue(q.isEmpty()); |
1040 |
< |
threadAssertTrue(q.size() == 0); |
1027 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1038 |
> |
assertTrue(q.hasWaitingConsumer()); |
1039 |
> |
checkEmpty(q); |
1040 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1041 |
|
}}); |
1042 |
|
|
1043 |
< |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1043 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1044 |
|
checkEmpty(q); |
1045 |
< |
t.join(); |
1045 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1046 |
|
} |
1047 |
|
|
1048 |
|
/** |
1054 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1055 |
|
|
1056 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1057 |
< |
void realRun() { |
1057 |
> |
public void realRun() { |
1058 |
|
while (! q.hasWaitingConsumer()) |
1059 |
|
Thread.yield(); |
1060 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1061 |
< |
threadAssertTrue(q.isEmpty()); |
1062 |
< |
threadAssertTrue(q.size() == 0); |
1050 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1060 |
> |
assertTrue(q.hasWaitingConsumer()); |
1061 |
> |
checkEmpty(q); |
1062 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1063 |
|
}}); |
1064 |
|
|
1065 |
< |
assertTrue(q.take() == hotPotato); |
1065 |
> |
assertSame(q.take(), hotPotato); |
1066 |
|
checkEmpty(q); |
1067 |
< |
t.join(); |
1067 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1068 |
|
} |
1069 |
|
|
1070 |
|
/** |
1073 |
|
*/ |
1074 |
|
public void testTryTransfer5() throws InterruptedException { |
1075 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1076 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1077 |
|
|
1078 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1079 |
< |
void realRun() throws InterruptedException { |
1080 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1078 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1079 |
> |
public void realRun() throws InterruptedException { |
1080 |
> |
long t0 = System.nanoTime(); |
1081 |
> |
threadStarted.countDown(); |
1082 |
> |
try { |
1083 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1084 |
> |
shouldThrow(); |
1085 |
> |
} catch (InterruptedException success) {} |
1086 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1087 |
|
}}); |
1088 |
|
|
1089 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1090 |
< |
toInterrupt.interrupt(); |
1091 |
< |
toInterrupt.join(); |
1089 |
> |
threadStarted.await(); |
1090 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1091 |
> |
t.interrupt(); |
1092 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1093 |
> |
checkEmpty(q); |
1094 |
|
} |
1095 |
|
|
1096 |
|
/** |
1097 |
< |
* tryTransfer gives up after the timeout and return false |
1097 |
> |
* tryTransfer gives up after the timeout and returns false |
1098 |
|
*/ |
1099 |
|
public void testTryTransfer6() throws InterruptedException { |
1100 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1101 |
|
|
1102 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1103 |
< |
void realRun() throws InterruptedException { |
1104 |
< |
threadAssertFalse |
1105 |
< |
(q.tryTransfer(new Object(), |
1106 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
1103 |
> |
public void realRun() throws InterruptedException { |
1104 |
> |
long t0 = System.nanoTime(); |
1105 |
> |
assertFalse(q.tryTransfer(new Object(), |
1106 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1107 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1108 |
> |
checkEmpty(q); |
1109 |
|
}}); |
1110 |
|
|
1111 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1111 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1112 |
|
checkEmpty(q); |
1090 |
– |
t.join(); |
1113 |
|
} |
1114 |
|
|
1115 |
|
/** |
1121 |
|
assertTrue(q.offer(four)); |
1122 |
|
|
1123 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1124 |
< |
void realRun() throws InterruptedException { |
1125 |
< |
threadAssertTrue(q.tryTransfer(five, |
1126 |
< |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1105 |
< |
threadAssertTrue(q.isEmpty()); |
1124 |
> |
public void realRun() throws InterruptedException { |
1125 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1126 |
> |
checkEmpty(q); |
1127 |
|
}}); |
1128 |
|
|
1129 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1129 |
> |
while (q.size() != 2) |
1130 |
> |
Thread.yield(); |
1131 |
|
assertEquals(2, q.size()); |
1132 |
< |
assertEquals(four, q.poll()); |
1133 |
< |
assertEquals(five, q.poll()); |
1132 |
> |
assertSame(four, q.poll()); |
1133 |
> |
assertSame(five, q.poll()); |
1134 |
|
checkEmpty(q); |
1135 |
< |
t.join(); |
1135 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1136 |
|
} |
1137 |
|
|
1138 |
|
/** |
1143 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1144 |
|
assertTrue(q.offer(four)); |
1145 |
|
assertEquals(1, q.size()); |
1146 |
+ |
long t0 = System.nanoTime(); |
1147 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1148 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1149 |
|
assertEquals(1, q.size()); |
1150 |
< |
assertEquals(four, q.poll()); |
1150 |
> |
assertSame(four, q.poll()); |
1151 |
|
assertNull(q.poll()); |
1152 |
|
checkEmpty(q); |
1153 |
|
} |
1154 |
|
|
1155 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1156 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1157 |
< |
assertTrue(q.isEmpty()); |
1157 |
> |
checkEmpty(q); |
1158 |
|
for (int i = 0; i < n; i++) { |
1159 |
|
assertEquals(i, q.size()); |
1160 |
|
assertTrue(q.offer(i)); |