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) {} |
56 |
< |
try { |
57 |
< |
q.iterator().next(); |
58 |
< |
shouldThrow(); |
59 |
< |
} catch (NoSuchElementException success) {} |
43 |
> |
void checkEmpty(BlockingQueue q) { |
44 |
|
try { |
45 |
< |
q.remove(); |
46 |
< |
shouldThrow(); |
47 |
< |
} 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 |
|
/** |
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 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
302 |
|
checkEmpty(q); |
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)); |
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) { |
356 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
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 |
|
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 |
+ |
|
400 |
+ |
/** |
401 |
|
* peek returns next element, or null if empty |
402 |
|
*/ |
403 |
|
public void testPeek() throws InterruptedException { |
575 |
|
/** |
576 |
|
* toArray(null) throws NullPointerException |
577 |
|
*/ |
578 |
< |
public void testToArray_BadArg() { |
578 |
> |
public void testToArray_NullArg() { |
579 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
580 |
|
try { |
581 |
< |
Object o[] = q.toArray(null); |
581 |
> |
q.toArray(null); |
582 |
|
shouldThrow(); |
583 |
|
} catch (NullPointerException success) {} |
584 |
|
} |
585 |
|
|
586 |
|
/** |
587 |
< |
* toArray(incompatible array type) throws CCE |
587 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
588 |
|
*/ |
589 |
|
public void testToArray1_BadArg() { |
590 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
591 |
|
try { |
592 |
< |
Object o[] = q.toArray(new String[10]); |
592 |
> |
q.toArray(new String[10]); |
593 |
|
shouldThrow(); |
594 |
|
} catch (ArrayStoreException success) {} |
595 |
|
} |
675 |
|
*/ |
676 |
|
public void testOfferInExecutor() { |
677 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
678 |
< |
q.add(one); |
641 |
< |
q.add(two); |
678 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
679 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
680 |
|
|
681 |
|
executor.execute(new CheckedRunnable() { |
682 |
< |
public void realRun() { |
683 |
< |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
682 |
> |
public void realRun() throws InterruptedException { |
683 |
> |
threadsStarted.countDown(); |
684 |
> |
threadsStarted.await(); |
685 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
686 |
|
}}); |
687 |
|
|
688 |
|
executor.execute(new CheckedRunnable() { |
689 |
|
public void realRun() throws InterruptedException { |
690 |
< |
Thread.sleep(SMALL_DELAY_MS); |
690 |
> |
threadsStarted.countDown(); |
691 |
> |
threadsStarted.await(); |
692 |
|
assertSame(one, q.take()); |
693 |
+ |
checkEmpty(q); |
694 |
|
}}); |
695 |
|
|
696 |
|
joinPool(executor); |
701 |
|
*/ |
702 |
|
public void testPollInExecutor() { |
703 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
704 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
705 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
706 |
|
|
707 |
|
executor.execute(new CheckedRunnable() { |
708 |
|
public void realRun() throws InterruptedException { |
709 |
|
assertNull(q.poll()); |
710 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
711 |
< |
assertTrue(q.isEmpty()); |
710 |
> |
threadsStarted.countDown(); |
711 |
> |
threadsStarted.await(); |
712 |
> |
assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
713 |
> |
checkEmpty(q); |
714 |
|
}}); |
715 |
|
|
716 |
|
executor.execute(new CheckedRunnable() { |
717 |
|
public void realRun() throws InterruptedException { |
718 |
< |
Thread.sleep(SMALL_DELAY_MS); |
718 |
> |
threadsStarted.countDown(); |
719 |
> |
threadsStarted.await(); |
720 |
|
q.put(one); |
721 |
|
}}); |
722 |
|
|
742 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
743 |
|
|
744 |
|
assertEquals(q.size(), r.size()); |
745 |
+ |
assertEquals(q.toString(), r.toString()); |
746 |
+ |
assertTrue(Arrays.equals(q.toArray(), r.toArray())); |
747 |
|
while (!q.isEmpty()) { |
748 |
|
assertEquals(q.remove(), r.remove()); |
749 |
|
} |
868 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
869 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
870 |
|
assertFalse(q.hasWaitingConsumer()); |
871 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
872 |
|
|
873 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
874 |
|
public void realRun() throws InterruptedException { |
875 |
< |
Thread.sleep(SMALL_DELAY_MS); |
876 |
< |
assertTrue(q.hasWaitingConsumer()); |
829 |
< |
assertEquals(q.getWaitingConsumerCount(), 1); |
830 |
< |
assertTrue(q.offer(one)); |
831 |
< |
assertFalse(q.hasWaitingConsumer()); |
875 |
> |
threadStarted.countDown(); |
876 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
877 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
878 |
+ |
assertFalse(q.hasWaitingConsumer()); |
879 |
|
}}); |
880 |
|
|
881 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
881 |
> |
threadStarted.await(); |
882 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
883 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
884 |
> |
assertTrue(q.hasWaitingConsumer()); |
885 |
> |
|
886 |
> |
assertTrue(q.offer(one)); |
887 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
888 |
|
assertFalse(q.hasWaitingConsumer()); |
889 |
+ |
|
890 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
891 |
|
} |
892 |
|
|
908 |
|
public void testTransfer2() throws InterruptedException { |
909 |
|
final LinkedTransferQueue<Integer> q |
910 |
|
= new LinkedTransferQueue<Integer>(); |
911 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
912 |
|
|
913 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
914 |
|
public void realRun() throws InterruptedException { |
915 |
< |
q.transfer(SIZE); |
916 |
< |
assertTrue(q.isEmpty()); |
915 |
> |
threadStarted.countDown(); |
916 |
> |
q.transfer(five); |
917 |
> |
checkEmpty(q); |
918 |
|
}}); |
919 |
|
|
920 |
< |
Thread.sleep(SHORT_DELAY_MS); |
920 |
> |
threadStarted.await(); |
921 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
922 |
|
assertEquals(1, q.size()); |
923 |
< |
assertEquals(SIZE, (int) q.poll()); |
924 |
< |
assertTrue(q.isEmpty()); |
923 |
> |
assertSame(five, q.poll()); |
924 |
> |
checkEmpty(q); |
925 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
926 |
|
} |
927 |
|
|
934 |
|
|
935 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
936 |
|
public void realRun() throws InterruptedException { |
937 |
< |
Integer i = SIZE + 1; |
938 |
< |
q.transfer(i); |
884 |
< |
assertTrue(!q.contains(i)); |
937 |
> |
q.transfer(four); |
938 |
> |
assertTrue(!q.contains(four)); |
939 |
|
assertEquals(1, q.size()); |
940 |
|
}}); |
941 |
|
|
942 |
|
Thread interruptedThread = newStartedThread( |
943 |
|
new CheckedInterruptedRunnable() { |
944 |
|
public void realRun() throws InterruptedException { |
945 |
< |
while (q.size() == 0) |
945 |
> |
while (q.isEmpty()) |
946 |
|
Thread.yield(); |
947 |
< |
q.transfer(SIZE); |
947 |
> |
q.transfer(five); |
948 |
|
}}); |
949 |
|
|
950 |
|
while (q.size() < 2) |
951 |
|
Thread.yield(); |
952 |
|
assertEquals(2, q.size()); |
953 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
953 |
> |
assertSame(four, q.poll()); |
954 |
|
first.join(); |
955 |
|
assertEquals(1, q.size()); |
956 |
|
interruptedThread.interrupt(); |
957 |
|
interruptedThread.join(); |
958 |
< |
assertEquals(0, q.size()); |
905 |
< |
assertTrue(q.isEmpty()); |
958 |
> |
checkEmpty(q); |
959 |
|
} |
960 |
|
|
961 |
|
/** |
972 |
|
assertSame(three, q.poll()); |
973 |
|
}}); |
974 |
|
|
975 |
< |
Thread.sleep(SHORT_DELAY_MS); |
975 |
> |
while (q.isEmpty()) |
976 |
> |
Thread.yield(); |
977 |
> |
assertFalse(q.isEmpty()); |
978 |
> |
assertEquals(1, q.size()); |
979 |
|
assertTrue(q.offer(three)); |
980 |
|
assertSame(four, q.poll()); |
981 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
991 |
|
|
992 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
993 |
|
public void realRun() throws InterruptedException { |
994 |
< |
q.transfer(SIZE); |
994 |
> |
q.transfer(four); |
995 |
|
checkEmpty(q); |
996 |
|
}}); |
997 |
|
|
998 |
< |
Thread.sleep(SHORT_DELAY_MS); |
999 |
< |
assertEquals(SIZE, (int) q.take()); |
998 |
> |
while (q.isEmpty()) |
999 |
> |
Thread.yield(); |
1000 |
> |
assertFalse(q.isEmpty()); |
1001 |
> |
assertEquals(1, q.size()); |
1002 |
> |
assertSame(four, q.take()); |
1003 |
|
checkEmpty(q); |
1004 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1005 |
|
} |
1039 |
|
while (! q.hasWaitingConsumer()) |
1040 |
|
Thread.yield(); |
1041 |
|
assertTrue(q.hasWaitingConsumer()); |
1042 |
< |
assertTrue(q.isEmpty()); |
984 |
< |
assertEquals(q.size(), 0); |
1042 |
> |
checkEmpty(q); |
1043 |
|
assertTrue(q.tryTransfer(hotPotato)); |
1044 |
|
}}); |
1045 |
|
|
1061 |
|
while (! q.hasWaitingConsumer()) |
1062 |
|
Thread.yield(); |
1063 |
|
assertTrue(q.hasWaitingConsumer()); |
1064 |
< |
assertTrue(q.isEmpty()); |
1007 |
< |
assertEquals(q.size(), 0); |
1064 |
> |
checkEmpty(q); |
1065 |
|
assertTrue(q.tryTransfer(hotPotato)); |
1066 |
|
}}); |
1067 |
|
|
1076 |
|
*/ |
1077 |
|
public void testTryTransfer5() throws InterruptedException { |
1078 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1079 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1080 |
|
|
1081 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1081 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1082 |
|
public void realRun() throws InterruptedException { |
1083 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1083 |
> |
long t0 = System.nanoTime(); |
1084 |
> |
threadStarted.countDown(); |
1085 |
> |
try { |
1086 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1087 |
> |
shouldThrow(); |
1088 |
> |
} catch (InterruptedException success) {} |
1089 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1090 |
|
}}); |
1091 |
|
|
1092 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1093 |
< |
toInterrupt.interrupt(); |
1094 |
< |
toInterrupt.join(); |
1092 |
> |
threadStarted.await(); |
1093 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1094 |
> |
t.interrupt(); |
1095 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1096 |
> |
checkEmpty(q); |
1097 |
|
} |
1098 |
|
|
1099 |
|
/** |
1100 |
< |
* tryTransfer gives up after the timeout and return false |
1100 |
> |
* tryTransfer gives up after the timeout and returns false |
1101 |
|
*/ |
1102 |
|
public void testTryTransfer6() throws InterruptedException { |
1103 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1108 |
|
assertFalse(q.tryTransfer(new Object(), |
1109 |
|
SHORT_DELAY_MS, MILLISECONDS)); |
1110 |
|
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1111 |
+ |
checkEmpty(q); |
1112 |
|
}}); |
1113 |
|
|
1047 |
– |
checkEmpty(q); |
1114 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1115 |
|
checkEmpty(q); |
1116 |
|
} |
1126 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1127 |
|
public void realRun() throws InterruptedException { |
1128 |
|
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1129 |
< |
assertTrue(q.isEmpty()); |
1129 |
> |
checkEmpty(q); |
1130 |
|
}}); |
1131 |
|
|
1132 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1132 |
> |
while (q.size() != 2) |
1133 |
> |
Thread.yield(); |
1134 |
|
assertEquals(2, q.size()); |
1135 |
|
assertSame(four, q.poll()); |
1136 |
|
assertSame(five, q.poll()); |
1146 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1147 |
|
assertTrue(q.offer(four)); |
1148 |
|
assertEquals(1, q.size()); |
1149 |
+ |
long t0 = System.nanoTime(); |
1150 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1151 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1152 |
|
assertEquals(1, q.size()); |
1153 |
|
assertSame(four, q.poll()); |
1154 |
|
assertNull(q.poll()); |
1157 |
|
|
1158 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1159 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1160 |
< |
assertTrue(q.isEmpty()); |
1160 |
> |
checkEmpty(q); |
1161 |
|
for (int i = 0; i < n; i++) { |
1162 |
|
assertEquals(i, q.size()); |
1163 |
|
assertTrue(q.offer(i)); |