40 |
|
new Generic().testSuite()); |
41 |
|
} |
42 |
|
|
43 |
< |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
44 |
< |
assertTrue(q.isEmpty()); |
45 |
< |
assertEquals(0, q.size()); |
46 |
< |
assertNull(q.peek()); |
47 |
< |
assertNull(q.poll()); |
48 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
49 |
< |
assertEquals(q.toString(), "[]"); |
50 |
< |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
51 |
< |
assertFalse(q.iterator().hasNext()); |
52 |
< |
try { |
53 |
< |
q.element(); |
54 |
< |
shouldThrow(); |
55 |
< |
} catch (NoSuchElementException success) {} |
43 |
> |
void checkEmpty(BlockingQueue q) { |
44 |
|
try { |
45 |
< |
q.iterator().next(); |
46 |
< |
shouldThrow(); |
47 |
< |
} catch (NoSuchElementException success) {} |
48 |
< |
try { |
49 |
< |
q.remove(); |
50 |
< |
shouldThrow(); |
51 |
< |
} 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 |
|
|
70 |
|
/** |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* take blocks interruptibly when empty |
278 |
< |
*/ |
279 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
280 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
281 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
282 |
< |
public void realRun() throws InterruptedException { |
283 |
< |
q.take(); |
284 |
< |
}}); |
285 |
< |
Thread.sleep(SHORT_DELAY_MS); |
286 |
< |
t.interrupt(); |
287 |
< |
t.join(); |
288 |
< |
} |
289 |
< |
|
290 |
< |
/** |
291 |
< |
* 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 = new Thread(new CheckedRunnable() { |
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 |
|
assertEquals(i, (int) q.take()); |
290 |
|
} |
291 |
+ |
aboutToWait.countDown(); |
292 |
|
try { |
293 |
|
q.take(); |
294 |
|
shouldThrow(); |
295 |
|
} catch (InterruptedException success) {} |
296 |
|
}}); |
297 |
|
|
298 |
< |
t.start(); |
299 |
< |
Thread.sleep(SHORT_DELAY_MS); |
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); |
346 |
< |
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 |
|
public void realRun() throws InterruptedException { |
354 |
|
for (int i = 0; i < SIZE; ++i) { |
355 |
|
long t0 = System.nanoTime(); |
356 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
357 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
364 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
357 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
358 |
|
} |
359 |
+ |
long t0 = System.nanoTime(); |
360 |
+ |
aboutToWait.countDown(); |
361 |
|
try { |
362 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
362 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
363 |
|
shouldThrow(); |
364 |
< |
} catch (InterruptedException success) {} |
364 |
> |
} catch (InterruptedException success) { |
365 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
366 |
> |
} |
367 |
|
}}); |
368 |
|
|
369 |
< |
Thread.sleep(SMALL_DELAY_MS); |
369 |
> |
aboutToWait.await(); |
370 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
371 |
|
t.interrupt(); |
372 |
< |
t.join(); |
372 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
373 |
> |
checkEmpty(q); |
374 |
> |
} |
375 |
> |
|
376 |
> |
/** |
377 |
> |
* timed poll after thread interrupted throws InterruptedException |
378 |
> |
* instead of returning timeout status |
379 |
> |
*/ |
380 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
381 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
382 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
383 |
> |
public void realRun() throws InterruptedException { |
384 |
> |
Thread.currentThread().interrupt(); |
385 |
> |
for (int i = 0; i < SIZE; ++i) { |
386 |
> |
long t0 = System.nanoTime(); |
387 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
388 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
389 |
> |
} |
390 |
> |
try { |
391 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
392 |
> |
shouldThrow(); |
393 |
> |
} catch (InterruptedException success) {} |
394 |
> |
}}); |
395 |
> |
|
396 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
397 |
|
checkEmpty(q); |
398 |
|
} |
399 |
|
|
448 |
|
*/ |
449 |
|
public void testRemoveElement() throws InterruptedException { |
450 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
451 |
< |
for (int i = 1; i < SIZE; i += 2) { |
451 |
> |
for (int i = 1; i < SIZE; i+=2) { |
452 |
> |
assertTrue(q.contains(i)); |
453 |
|
assertTrue(q.remove(i)); |
454 |
+ |
assertFalse(q.contains(i)); |
455 |
+ |
assertTrue(q.contains(i-1)); |
456 |
|
} |
457 |
< |
for (int i = 0; i < SIZE; i += 2) { |
457 |
> |
for (int i = 0; i < SIZE; i+=2) { |
458 |
> |
assertTrue(q.contains(i)); |
459 |
|
assertTrue(q.remove(i)); |
460 |
< |
assertFalse(q.remove(i + 1)); |
460 |
> |
assertFalse(q.contains(i)); |
461 |
> |
assertFalse(q.remove(i+1)); |
462 |
> |
assertFalse(q.contains(i+1)); |
463 |
|
} |
464 |
|
checkEmpty(q); |
465 |
|
} |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* toArray() contains all elements |
559 |
> |
* toArray() contains all elements in FIFO order |
560 |
|
*/ |
561 |
< |
public void testToArray() throws InterruptedException { |
561 |
> |
public void testToArray() { |
562 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
563 |
|
Object[] o = q.toArray(); |
564 |
|
for (int i = 0; i < o.length; i++) { |
565 |
< |
assertEquals(o[i], q.take()); |
565 |
> |
assertSame(o[i], q.poll()); |
566 |
|
} |
567 |
|
} |
568 |
|
|
569 |
|
/** |
570 |
< |
* toArray(a) contains all elements |
570 |
> |
* toArray(a) contains all elements in FIFO order |
571 |
|
*/ |
572 |
< |
public void testToArray2() throws InterruptedException { |
572 |
> |
public void testToArray2() { |
573 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
574 |
|
Integer[] ints = new Integer[SIZE]; |
575 |
< |
ints = q.toArray(ints); |
575 |
> |
Integer[] array = q.toArray(ints); |
576 |
> |
assertSame(ints, array); |
577 |
|
for (int i = 0; i < ints.length; i++) { |
578 |
< |
assertEquals(ints[i], q.take()); |
578 |
> |
assertSame(ints[i], q.poll()); |
579 |
|
} |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
|
* toArray(null) throws NullPointerException |
584 |
|
*/ |
585 |
< |
public void testToArray_BadArg() { |
585 |
> |
public void testToArray_NullArg() { |
586 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
587 |
|
try { |
588 |
< |
Object o[] = q.toArray(null); |
588 |
> |
q.toArray(null); |
589 |
|
shouldThrow(); |
590 |
|
} catch (NullPointerException success) {} |
591 |
|
} |
592 |
|
|
593 |
|
/** |
594 |
< |
* toArray(incompatible array type) throws CCE |
594 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
595 |
|
*/ |
596 |
|
public void testToArray1_BadArg() { |
597 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
598 |
|
try { |
599 |
< |
Object o[] = q.toArray(new String[10]); |
599 |
> |
q.toArray(new String[10]); |
600 |
|
shouldThrow(); |
601 |
|
} catch (ArrayStoreException success) {} |
602 |
|
} |
682 |
|
*/ |
683 |
|
public void testOfferInExecutor() { |
684 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
685 |
< |
q.add(one); |
657 |
< |
q.add(two); |
685 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
686 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
687 |
|
|
688 |
|
executor.execute(new CheckedRunnable() { |
689 |
< |
public void realRun() { |
690 |
< |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
689 |
> |
public void realRun() throws InterruptedException { |
690 |
> |
threadsStarted.countDown(); |
691 |
> |
threadsStarted.await(); |
692 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
693 |
|
}}); |
694 |
|
|
695 |
|
executor.execute(new CheckedRunnable() { |
696 |
|
public void realRun() throws InterruptedException { |
697 |
< |
Thread.sleep(SMALL_DELAY_MS); |
697 |
> |
threadsStarted.countDown(); |
698 |
> |
threadsStarted.await(); |
699 |
|
assertSame(one, q.take()); |
700 |
+ |
checkEmpty(q); |
701 |
|
}}); |
702 |
|
|
703 |
|
joinPool(executor); |
708 |
|
*/ |
709 |
|
public void testPollInExecutor() { |
710 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
711 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
712 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
713 |
|
|
714 |
|
executor.execute(new CheckedRunnable() { |
715 |
|
public void realRun() throws InterruptedException { |
716 |
|
assertNull(q.poll()); |
717 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
718 |
< |
assertTrue(q.isEmpty()); |
717 |
> |
threadsStarted.countDown(); |
718 |
> |
threadsStarted.await(); |
719 |
> |
assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
720 |
> |
checkEmpty(q); |
721 |
|
}}); |
722 |
|
|
723 |
|
executor.execute(new CheckedRunnable() { |
724 |
|
public void realRun() throws InterruptedException { |
725 |
< |
Thread.sleep(SMALL_DELAY_MS); |
725 |
> |
threadsStarted.countDown(); |
726 |
> |
threadsStarted.await(); |
727 |
|
q.put(one); |
728 |
|
}}); |
729 |
|
|
749 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
750 |
|
|
751 |
|
assertEquals(q.size(), r.size()); |
752 |
+ |
assertEquals(q.toString(), r.toString()); |
753 |
+ |
assertTrue(Arrays.equals(q.toArray(), r.toArray())); |
754 |
|
while (!q.isEmpty()) { |
755 |
|
assertEquals(q.remove(), r.remove()); |
756 |
|
} |
819 |
|
for (int i = 0; i < SIZE; ++i) { |
820 |
|
assertEquals(l.get(i), i); |
821 |
|
} |
822 |
< |
t.join(); |
822 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
823 |
|
assertTrue(q.size() + l.size() >= SIZE); |
824 |
|
} |
825 |
|
|
875 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
876 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
877 |
|
assertFalse(q.hasWaitingConsumer()); |
878 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
879 |
|
|
880 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
881 |
|
public void realRun() throws InterruptedException { |
882 |
< |
Thread.sleep(SMALL_DELAY_MS); |
883 |
< |
assertTrue(q.hasWaitingConsumer()); |
845 |
< |
assertEquals(q.getWaitingConsumerCount(), 1); |
846 |
< |
assertTrue(q.offer(one)); |
847 |
< |
assertFalse(q.hasWaitingConsumer()); |
882 |
> |
threadStarted.countDown(); |
883 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
884 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
885 |
+ |
assertFalse(q.hasWaitingConsumer()); |
886 |
|
}}); |
887 |
|
|
888 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
888 |
> |
threadStarted.await(); |
889 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
890 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
891 |
> |
assertTrue(q.hasWaitingConsumer()); |
892 |
> |
|
893 |
> |
assertTrue(q.offer(one)); |
894 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
895 |
|
assertFalse(q.hasWaitingConsumer()); |
896 |
< |
t.join(); |
896 |
> |
|
897 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
898 |
|
} |
899 |
|
|
900 |
|
/** |
915 |
|
public void testTransfer2() throws InterruptedException { |
916 |
|
final LinkedTransferQueue<Integer> q |
917 |
|
= new LinkedTransferQueue<Integer>(); |
918 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
919 |
|
|
920 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
921 |
|
public void realRun() throws InterruptedException { |
922 |
< |
q.transfer(SIZE); |
923 |
< |
assertTrue(q.isEmpty()); |
922 |
> |
threadStarted.countDown(); |
923 |
> |
q.transfer(five); |
924 |
> |
checkEmpty(q); |
925 |
|
}}); |
926 |
|
|
927 |
< |
Thread.sleep(SHORT_DELAY_MS); |
927 |
> |
threadStarted.await(); |
928 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
929 |
|
assertEquals(1, q.size()); |
930 |
< |
assertEquals(SIZE, (int) q.poll()); |
931 |
< |
assertTrue(q.isEmpty()); |
932 |
< |
t.join(); |
930 |
> |
assertSame(five, q.poll()); |
931 |
> |
checkEmpty(q); |
932 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
933 |
|
} |
934 |
|
|
935 |
|
/** |
941 |
|
|
942 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
943 |
|
public void realRun() throws InterruptedException { |
944 |
< |
Integer i = SIZE + 1; |
945 |
< |
q.transfer(i); |
900 |
< |
assertTrue(!q.contains(i)); |
944 |
> |
q.transfer(four); |
945 |
> |
assertTrue(!q.contains(four)); |
946 |
|
assertEquals(1, q.size()); |
947 |
|
}}); |
948 |
|
|
949 |
|
Thread interruptedThread = newStartedThread( |
950 |
|
new CheckedInterruptedRunnable() { |
951 |
|
public void realRun() throws InterruptedException { |
952 |
< |
while (q.size() == 0) |
952 |
> |
while (q.isEmpty()) |
953 |
|
Thread.yield(); |
954 |
< |
q.transfer(SIZE); |
954 |
> |
q.transfer(five); |
955 |
|
}}); |
956 |
|
|
957 |
|
while (q.size() < 2) |
958 |
|
Thread.yield(); |
959 |
|
assertEquals(2, q.size()); |
960 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
960 |
> |
assertSame(four, q.poll()); |
961 |
|
first.join(); |
962 |
|
assertEquals(1, q.size()); |
963 |
|
interruptedThread.interrupt(); |
964 |
|
interruptedThread.join(); |
965 |
< |
assertEquals(0, q.size()); |
921 |
< |
assertTrue(q.isEmpty()); |
965 |
> |
checkEmpty(q); |
966 |
|
} |
967 |
|
|
968 |
|
/** |
979 |
|
assertSame(three, q.poll()); |
980 |
|
}}); |
981 |
|
|
982 |
< |
Thread.sleep(SHORT_DELAY_MS); |
982 |
> |
while (q.isEmpty()) |
983 |
> |
Thread.yield(); |
984 |
> |
assertFalse(q.isEmpty()); |
985 |
> |
assertEquals(1, q.size()); |
986 |
|
assertTrue(q.offer(three)); |
987 |
|
assertSame(four, q.poll()); |
988 |
< |
t.join(); |
988 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
989 |
|
} |
990 |
|
|
991 |
|
/** |
998 |
|
|
999 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1000 |
|
public void realRun() throws InterruptedException { |
1001 |
< |
q.transfer(SIZE); |
1001 |
> |
q.transfer(four); |
1002 |
|
checkEmpty(q); |
1003 |
|
}}); |
1004 |
|
|
1005 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1006 |
< |
assertEquals(SIZE, (int) q.take()); |
1005 |
> |
while (q.isEmpty()) |
1006 |
> |
Thread.yield(); |
1007 |
> |
assertFalse(q.isEmpty()); |
1008 |
> |
assertEquals(1, q.size()); |
1009 |
> |
assertSame(four, q.take()); |
1010 |
|
checkEmpty(q); |
1011 |
< |
t.join(); |
1011 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1012 |
|
} |
1013 |
|
|
1014 |
|
/** |
1046 |
|
while (! q.hasWaitingConsumer()) |
1047 |
|
Thread.yield(); |
1048 |
|
assertTrue(q.hasWaitingConsumer()); |
1049 |
< |
assertTrue(q.isEmpty()); |
1000 |
< |
assertEquals(q.size(), 0); |
1049 |
> |
checkEmpty(q); |
1050 |
|
assertTrue(q.tryTransfer(hotPotato)); |
1051 |
|
}}); |
1052 |
|
|
1053 |
|
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1054 |
|
checkEmpty(q); |
1055 |
< |
t.join(); |
1055 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/** |
1068 |
|
while (! q.hasWaitingConsumer()) |
1069 |
|
Thread.yield(); |
1070 |
|
assertTrue(q.hasWaitingConsumer()); |
1071 |
< |
assertTrue(q.isEmpty()); |
1023 |
< |
assertEquals(q.size(), 0); |
1071 |
> |
checkEmpty(q); |
1072 |
|
assertTrue(q.tryTransfer(hotPotato)); |
1073 |
|
}}); |
1074 |
|
|
1075 |
|
assertSame(q.take(), hotPotato); |
1076 |
|
checkEmpty(q); |
1077 |
< |
t.join(); |
1077 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1078 |
|
} |
1079 |
|
|
1080 |
|
/** |
1081 |
< |
* tryTransfer waits the amount given if interrupted, and |
1082 |
< |
* throws interrupted exception |
1081 |
> |
* tryTransfer waits the amount given, and throws |
1082 |
> |
* InterruptedException when interrupted. |
1083 |
|
*/ |
1084 |
|
public void testTryTransfer5() throws InterruptedException { |
1085 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1086 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1087 |
+ |
assertTrue(q.isEmpty()); |
1088 |
|
|
1089 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1089 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1090 |
|
public void realRun() throws InterruptedException { |
1091 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1091 |
> |
long t0 = System.nanoTime(); |
1092 |
> |
threadStarted.countDown(); |
1093 |
> |
try { |
1094 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1095 |
> |
shouldThrow(); |
1096 |
> |
} catch (InterruptedException success) {} |
1097 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1098 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
1099 |
|
}}); |
1100 |
|
|
1101 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1102 |
< |
toInterrupt.interrupt(); |
1103 |
< |
toInterrupt.join(); |
1101 |
> |
threadStarted.await(); |
1102 |
> |
while (q.isEmpty()) |
1103 |
> |
Thread.yield(); |
1104 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1105 |
> |
t.interrupt(); |
1106 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1107 |
> |
checkEmpty(q); |
1108 |
|
} |
1109 |
|
|
1110 |
|
/** |
1111 |
< |
* tryTransfer gives up after the timeout and return false |
1111 |
> |
* tryTransfer gives up after the timeout and returns false |
1112 |
|
*/ |
1113 |
|
public void testTryTransfer6() throws InterruptedException { |
1114 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1118 |
|
long t0 = System.nanoTime(); |
1119 |
|
assertFalse(q.tryTransfer(new Object(), |
1120 |
|
SHORT_DELAY_MS, MILLISECONDS)); |
1121 |
< |
long elapsed = NANOSECONDS.toMillis(System.nanoTime() - t0); |
1122 |
< |
assertTrue(elapsed >= SHORT_DELAY_MS); |
1121 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1122 |
> |
checkEmpty(q); |
1123 |
|
}}); |
1124 |
|
|
1064 |
– |
checkEmpty(q); |
1125 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1126 |
|
checkEmpty(q); |
1127 |
|
} |
1137 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1138 |
|
public void realRun() throws InterruptedException { |
1139 |
|
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1140 |
< |
assertTrue(q.isEmpty()); |
1140 |
> |
checkEmpty(q); |
1141 |
|
}}); |
1142 |
|
|
1143 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1143 |
> |
while (q.size() != 2) |
1144 |
> |
Thread.yield(); |
1145 |
|
assertEquals(2, q.size()); |
1146 |
|
assertSame(four, q.poll()); |
1147 |
|
assertSame(five, q.poll()); |
1148 |
|
checkEmpty(q); |
1149 |
< |
t.join(); |
1149 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1150 |
|
} |
1151 |
|
|
1152 |
|
/** |
1153 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1154 |
< |
* false not enqueueing and the successive poll is null |
1153 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1154 |
> |
* returning false not enqueueing and the successive poll is null |
1155 |
|
*/ |
1156 |
|
public void testTryTransfer8() throws InterruptedException { |
1157 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1158 |
|
assertTrue(q.offer(four)); |
1159 |
|
assertEquals(1, q.size()); |
1160 |
+ |
long t0 = System.nanoTime(); |
1161 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1162 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1163 |
|
assertEquals(1, q.size()); |
1164 |
|
assertSame(four, q.poll()); |
1165 |
|
assertNull(q.poll()); |
1168 |
|
|
1169 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1170 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1171 |
< |
assertTrue(q.isEmpty()); |
1171 |
> |
checkEmpty(q); |
1172 |
|
for (int i = 0; i < n; i++) { |
1173 |
|
assertEquals(i, q.size()); |
1174 |
|
assertTrue(q.offer(i)); |