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) { |
355 |
|
long t0 = System.nanoTime(); |
356 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
357 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
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 |
|
|
366 |
< |
Thread.sleep(SMALL_DELAY_MS); |
366 |
> |
aboutToWait.await(); |
367 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
368 |
|
t.interrupt(); |
369 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
370 |
|
checkEmpty(q); |
371 |
|
} |
372 |
|
|
373 |
|
/** |
374 |
+ |
* timed poll after thread interrupted throws InterruptedException |
375 |
+ |
* instead of returning timeout status |
376 |
+ |
*/ |
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 |
+ |
|
393 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
394 |
+ |
checkEmpty(q); |
395 |
+ |
} |
396 |
+ |
|
397 |
+ |
/** |
398 |
|
* peek returns next element, or null if empty |
399 |
|
*/ |
400 |
|
public void testPeek() throws InterruptedException { |
672 |
|
*/ |
673 |
|
public void testOfferInExecutor() { |
674 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
675 |
< |
q.add(one); |
641 |
< |
q.add(two); |
675 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
676 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
677 |
|
|
678 |
|
executor.execute(new CheckedRunnable() { |
679 |
< |
public void realRun() { |
680 |
< |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, 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 |
|
public void realRun() throws InterruptedException { |
687 |
< |
Thread.sleep(SMALL_DELAY_MS); |
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 |
|
public void realRun() throws InterruptedException { |
706 |
|
assertNull(q.poll()); |
707 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
708 |
< |
assertTrue(q.isEmpty()); |
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 |
|
public void realRun() throws InterruptedException { |
715 |
< |
Thread.sleep(SMALL_DELAY_MS); |
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 |
|
} |
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 |
|
public void realRun() throws InterruptedException { |
872 |
< |
Thread.sleep(SMALL_DELAY_MS); |
873 |
< |
assertTrue(q.hasWaitingConsumer()); |
829 |
< |
assertEquals(q.getWaitingConsumerCount(), 1); |
830 |
< |
assertTrue(q.offer(one)); |
831 |
< |
assertFalse(q.hasWaitingConsumer()); |
872 |
> |
threadStarted.countDown(); |
873 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
874 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
875 |
+ |
assertFalse(q.hasWaitingConsumer()); |
876 |
|
}}); |
877 |
|
|
878 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
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 |
+ |
|
887 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
888 |
|
} |
889 |
|
|
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 |
|
public void realRun() throws InterruptedException { |
912 |
+ |
threadStarted.countDown(); |
913 |
|
q.transfer(SIZE); |
914 |
< |
assertTrue(q.isEmpty()); |
914 |
> |
checkEmpty(q); |
915 |
|
}}); |
916 |
|
|
917 |
< |
Thread.sleep(SHORT_DELAY_MS); |
917 |
> |
threadStarted.await(); |
918 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
919 |
> |
assertTrue(t.isAlive()); |
920 |
|
assertEquals(1, q.size()); |
921 |
|
assertEquals(SIZE, (int) q.poll()); |
922 |
< |
assertTrue(q.isEmpty()); |
922 |
> |
checkEmpty(q); |
923 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
924 |
|
} |
925 |
|
|
932 |
|
|
933 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
934 |
|
public void realRun() throws InterruptedException { |
935 |
< |
Integer i = SIZE + 1; |
936 |
< |
q.transfer(i); |
884 |
< |
assertTrue(!q.contains(i)); |
935 |
> |
q.transfer(four); |
936 |
> |
assertTrue(!q.contains(four)); |
937 |
|
assertEquals(1, q.size()); |
938 |
|
}}); |
939 |
|
|
940 |
|
Thread interruptedThread = newStartedThread( |
941 |
|
new CheckedInterruptedRunnable() { |
942 |
|
public void realRun() throws InterruptedException { |
943 |
< |
while (q.size() == 0) |
943 |
> |
while (q.isEmpty()) |
944 |
|
Thread.yield(); |
945 |
< |
q.transfer(SIZE); |
945 |
> |
q.transfer(five); |
946 |
|
}}); |
947 |
|
|
948 |
|
while (q.size() < 2) |
949 |
|
Thread.yield(); |
950 |
|
assertEquals(2, q.size()); |
951 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
951 |
> |
assertSame(four, q.poll()); |
952 |
|
first.join(); |
953 |
|
assertEquals(1, q.size()); |
954 |
|
interruptedThread.interrupt(); |
955 |
|
interruptedThread.join(); |
956 |
< |
assertEquals(0, q.size()); |
905 |
< |
assertTrue(q.isEmpty()); |
956 |
> |
checkEmpty(q); |
957 |
|
} |
958 |
|
|
959 |
|
/** |
970 |
|
assertSame(three, q.poll()); |
971 |
|
}}); |
972 |
|
|
973 |
< |
Thread.sleep(SHORT_DELAY_MS); |
973 |
> |
while (q.isEmpty()) |
974 |
> |
Thread.yield(); |
975 |
> |
assertFalse(q.isEmpty()); |
976 |
> |
assertEquals(1, q.size()); |
977 |
|
assertTrue(q.offer(three)); |
978 |
|
assertSame(four, q.poll()); |
979 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
989 |
|
|
990 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
991 |
|
public void realRun() throws InterruptedException { |
992 |
< |
q.transfer(SIZE); |
992 |
> |
q.transfer(four); |
993 |
|
checkEmpty(q); |
994 |
|
}}); |
995 |
|
|
996 |
< |
Thread.sleep(SHORT_DELAY_MS); |
997 |
< |
assertEquals(SIZE, (int) q.take()); |
996 |
> |
while (q.isEmpty()) |
997 |
> |
Thread.yield(); |
998 |
> |
assertFalse(q.isEmpty()); |
999 |
> |
assertEquals(1, q.size()); |
1000 |
> |
assertSame(four, q.take()); |
1001 |
|
checkEmpty(q); |
1002 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1003 |
|
} |
1037 |
|
while (! q.hasWaitingConsumer()) |
1038 |
|
Thread.yield(); |
1039 |
|
assertTrue(q.hasWaitingConsumer()); |
1040 |
< |
assertTrue(q.isEmpty()); |
984 |
< |
assertEquals(q.size(), 0); |
1040 |
> |
checkEmpty(q); |
1041 |
|
assertTrue(q.tryTransfer(hotPotato)); |
1042 |
|
}}); |
1043 |
|
|
1059 |
|
while (! q.hasWaitingConsumer()) |
1060 |
|
Thread.yield(); |
1061 |
|
assertTrue(q.hasWaitingConsumer()); |
1062 |
< |
assertTrue(q.isEmpty()); |
1007 |
< |
assertEquals(q.size(), 0); |
1062 |
> |
checkEmpty(q); |
1063 |
|
assertTrue(q.tryTransfer(hotPotato)); |
1064 |
|
}}); |
1065 |
|
|
1074 |
|
*/ |
1075 |
|
public void testTryTransfer5() throws InterruptedException { |
1076 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1077 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1078 |
|
|
1079 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1079 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1080 |
|
public void realRun() throws InterruptedException { |
1081 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1081 |
> |
long t0 = System.nanoTime(); |
1082 |
> |
threadStarted.countDown(); |
1083 |
> |
try { |
1084 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1085 |
> |
shouldThrow(); |
1086 |
> |
} catch (InterruptedException success) {} |
1087 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1088 |
|
}}); |
1089 |
|
|
1090 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1091 |
< |
toInterrupt.interrupt(); |
1092 |
< |
toInterrupt.join(); |
1090 |
> |
threadStarted.await(); |
1091 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1092 |
> |
t.interrupt(); |
1093 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1094 |
> |
checkEmpty(q); |
1095 |
|
} |
1096 |
|
|
1097 |
|
/** |
1106 |
|
assertFalse(q.tryTransfer(new Object(), |
1107 |
|
SHORT_DELAY_MS, MILLISECONDS)); |
1108 |
|
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1109 |
+ |
checkEmpty(q); |
1110 |
|
}}); |
1111 |
|
|
1047 |
– |
checkEmpty(q); |
1112 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1113 |
|
checkEmpty(q); |
1114 |
|
} |
1124 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1125 |
|
public void realRun() throws InterruptedException { |
1126 |
|
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1127 |
< |
assertTrue(q.isEmpty()); |
1127 |
> |
checkEmpty(q); |
1128 |
|
}}); |
1129 |
|
|
1130 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1130 |
> |
while (q.size() != 2) |
1131 |
> |
Thread.yield(); |
1132 |
|
assertEquals(2, q.size()); |
1133 |
|
assertSame(four, q.poll()); |
1134 |
|
assertSame(five, q.poll()); |
1144 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1145 |
|
assertTrue(q.offer(four)); |
1146 |
|
assertEquals(1, q.size()); |
1147 |
+ |
long t0 = System.nanoTime(); |
1148 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1149 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1150 |
|
assertEquals(1, q.size()); |
1151 |
|
assertSame(four, q.poll()); |
1152 |
|
assertNull(q.poll()); |
1155 |
|
|
1156 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1157 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1158 |
< |
assertTrue(q.isEmpty()); |
1158 |
> |
checkEmpty(q); |
1159 |
|
for (int i = 0; i < n; i++) { |
1160 |
|
assertEquals(i, q.size()); |
1161 |
|
assertTrue(q.offer(i)); |