37 |
|
} |
38 |
|
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run(suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
44 |
+ |
class Implementation implements CollectionImplementation { |
45 |
+ |
public Class<?> klazz() { return LinkedBlockingDeque.class; } |
46 |
+ |
public Collection emptyCollection() { return new LinkedBlockingDeque(); } |
47 |
+ |
public Object makeElement(int i) { return i; } |
48 |
+ |
public boolean isConcurrent() { return true; } |
49 |
+ |
public boolean permitsNulls() { return false; } |
50 |
+ |
} |
51 |
|
return newTestSuite(LinkedBlockingDequeTest.class, |
52 |
|
new Unbounded().testSuite(), |
53 |
< |
new Bounded().testSuite()); |
53 |
> |
new Bounded().testSuite(), |
54 |
> |
CollectionTest.testSuite(new Implementation())); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
|
* Returns a new deque of given size containing consecutive |
59 |
< |
* Integers 0 ... n. |
59 |
> |
* Integers 0 ... n - 1. |
60 |
|
*/ |
61 |
< |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
61 |
> |
private static LinkedBlockingDeque<Integer> populatedDeque(int n) { |
62 |
|
LinkedBlockingDeque<Integer> q = |
63 |
|
new LinkedBlockingDeque<Integer>(n); |
64 |
|
assertTrue(q.isEmpty()); |
67 |
|
assertFalse(q.isEmpty()); |
68 |
|
assertEquals(0, q.remainingCapacity()); |
69 |
|
assertEquals(n, q.size()); |
70 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
71 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
72 |
|
return q; |
73 |
|
} |
74 |
|
|
92 |
|
public void testSize() { |
93 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
94 |
|
for (int i = 0; i < SIZE; ++i) { |
95 |
< |
assertEquals(SIZE-i, q.size()); |
95 |
> |
assertEquals(SIZE - i, q.size()); |
96 |
|
q.removeFirst(); |
97 |
|
} |
98 |
|
for (int i = 0; i < SIZE; ++i) { |
157 |
|
*/ |
158 |
|
public void testPollLast() { |
159 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
160 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
160 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
161 |
|
assertEquals(i, q.pollLast()); |
162 |
|
} |
163 |
|
assertNull(q.pollLast()); |
196 |
|
*/ |
197 |
|
public void testPeekLast() { |
198 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
199 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
199 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
200 |
|
assertEquals(i, q.peekLast()); |
201 |
|
assertEquals(i, q.pollLast()); |
202 |
|
assertTrue(q.peekLast() == null || |
226 |
|
*/ |
227 |
|
public void testLastElement() { |
228 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
229 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
229 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
230 |
|
assertEquals(i, q.getLast()); |
231 |
|
assertEquals(i, q.pollLast()); |
232 |
|
} |
291 |
|
} |
292 |
|
for (int i = 0; i < SIZE; i += 2) { |
293 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
294 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
294 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
295 |
|
} |
296 |
|
assertTrue(q.isEmpty()); |
297 |
|
} |
306 |
|
} |
307 |
|
for (int i = 0; i < SIZE; i += 2) { |
308 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
309 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
309 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
310 |
|
} |
311 |
|
assertTrue(q.isEmpty()); |
312 |
|
} |
377 |
|
*/ |
378 |
|
public void testConstructor5() { |
379 |
|
Integer[] ints = new Integer[SIZE]; |
380 |
< |
for (int i = 0; i < SIZE-1; ++i) |
380 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
381 |
|
ints[i] = i; |
382 |
|
Collection<Integer> elements = Arrays.asList(ints); |
383 |
|
try { |
417 |
|
* remainingCapacity decreases on add, increases on remove |
418 |
|
*/ |
419 |
|
public void testRemainingCapacity() { |
420 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
420 |
> |
BlockingQueue q = populatedDeque(SIZE); |
421 |
|
for (int i = 0; i < SIZE; ++i) { |
422 |
|
assertEquals(i, q.remainingCapacity()); |
423 |
< |
assertEquals(SIZE-i, q.size()); |
424 |
< |
q.remove(); |
423 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
424 |
> |
assertEquals(i, q.remove()); |
425 |
|
} |
426 |
|
for (int i = 0; i < SIZE; ++i) { |
427 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
428 |
< |
assertEquals(i, q.size()); |
429 |
< |
q.add(new Integer(i)); |
427 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
428 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
429 |
> |
assertTrue(q.add(i)); |
430 |
|
} |
431 |
|
} |
432 |
|
|
434 |
|
* push(null) throws NPE |
435 |
|
*/ |
436 |
|
public void testPushNull() { |
437 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
438 |
|
try { |
428 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
439 |
|
q.push(null); |
440 |
|
shouldThrow(); |
441 |
|
} catch (NullPointerException success) {} |
445 |
|
* push succeeds if not full; throws ISE if full |
446 |
|
*/ |
447 |
|
public void testPush() { |
448 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
449 |
+ |
for (int i = 0; i < SIZE; ++i) { |
450 |
+ |
Integer x = new Integer(i); |
451 |
+ |
q.push(x); |
452 |
+ |
assertEquals(x, q.peek()); |
453 |
+ |
} |
454 |
+ |
assertEquals(0, q.remainingCapacity()); |
455 |
|
try { |
439 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
440 |
– |
for (int i = 0; i < SIZE; ++i) { |
441 |
– |
Integer x = new Integer(i); |
442 |
– |
q.push(x); |
443 |
– |
assertEquals(x, q.peek()); |
444 |
– |
} |
445 |
– |
assertEquals(0, q.remainingCapacity()); |
456 |
|
q.push(new Integer(SIZE)); |
457 |
|
shouldThrow(); |
458 |
|
} catch (IllegalStateException success) {} |
523 |
|
public void testAddAll3() { |
524 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
525 |
|
Integer[] ints = new Integer[SIZE]; |
526 |
< |
for (int i = 0; i < SIZE-1; ++i) |
526 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
527 |
|
ints[i] = new Integer(i); |
528 |
|
Collection<Integer> elements = Arrays.asList(ints); |
529 |
|
try { |
604 |
|
}}); |
605 |
|
|
606 |
|
await(pleaseInterrupt); |
607 |
< |
assertThreadStaysAlive(t); |
607 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
608 |
|
t.interrupt(); |
609 |
|
awaitTermination(t); |
610 |
|
assertEquals(SIZE, q.size()); |
639 |
|
assertEquals(0, q.take()); |
640 |
|
|
641 |
|
await(pleaseInterrupt); |
642 |
< |
assertThreadStaysAlive(t); |
642 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
643 |
|
t.interrupt(); |
644 |
|
awaitTermination(t); |
645 |
|
assertEquals(0, q.remainingCapacity()); |
663 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
664 |
|
shouldThrow(); |
665 |
|
} catch (InterruptedException success) {} |
666 |
+ |
assertFalse(Thread.interrupted()); |
667 |
|
}}); |
668 |
|
|
669 |
|
await(pleaseInterrupt); |
670 |
< |
assertThreadStaysAlive(t); |
670 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
671 |
|
t.interrupt(); |
672 |
|
awaitTermination(t); |
673 |
|
} |
690 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
691 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
692 |
|
public void realRun() throws InterruptedException { |
693 |
< |
for (int i = 0; i < SIZE; ++i) { |
683 |
< |
assertEquals(i, q.take()); |
684 |
< |
} |
693 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
694 |
|
|
695 |
|
Thread.currentThread().interrupt(); |
696 |
|
try { |
708 |
|
}}); |
709 |
|
|
710 |
|
await(pleaseInterrupt); |
711 |
< |
assertThreadStaysAlive(t); |
711 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
712 |
|
t.interrupt(); |
713 |
|
awaitTermination(t); |
714 |
|
} |
757 |
|
*/ |
758 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
759 |
|
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
760 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
760 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
761 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
762 |
|
public void realRun() throws InterruptedException { |
763 |
< |
for (int i = 0; i < SIZE; ++i) { |
764 |
< |
long t0 = System.nanoTime(); |
763 |
> |
long startTime = System.nanoTime(); |
764 |
> |
for (int i = 0; i < SIZE; i++) |
765 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
766 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
767 |
< |
} |
768 |
< |
long t0 = System.nanoTime(); |
769 |
< |
aboutToWait.countDown(); |
766 |
> |
|
767 |
> |
Thread.currentThread().interrupt(); |
768 |
> |
try { |
769 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
770 |
> |
shouldThrow(); |
771 |
> |
} catch (InterruptedException success) {} |
772 |
> |
assertFalse(Thread.interrupted()); |
773 |
> |
|
774 |
> |
pleaseInterrupt.countDown(); |
775 |
|
try { |
776 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
776 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
777 |
|
shouldThrow(); |
778 |
< |
} catch (InterruptedException success) { |
779 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
780 |
< |
} |
778 |
> |
} catch (InterruptedException success) {} |
779 |
> |
assertFalse(Thread.interrupted()); |
780 |
> |
|
781 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
782 |
|
}}); |
783 |
|
|
784 |
< |
aboutToWait.await(); |
785 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
784 |
> |
await(pleaseInterrupt); |
785 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
786 |
|
t.interrupt(); |
787 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
787 |
> |
awaitTermination(t); |
788 |
|
checkEmpty(q); |
789 |
|
} |
790 |
|
|
841 |
|
}}); |
842 |
|
|
843 |
|
await(pleaseInterrupt); |
844 |
< |
assertThreadStaysAlive(t); |
844 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
845 |
|
t.interrupt(); |
846 |
|
awaitTermination(t); |
847 |
|
assertEquals(SIZE, q.size()); |
876 |
|
assertEquals(capacity - 1, q.take()); |
877 |
|
|
878 |
|
await(pleaseInterrupt); |
879 |
< |
assertThreadStaysAlive(t); |
879 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
880 |
|
t.interrupt(); |
881 |
|
awaitTermination(t); |
882 |
|
assertEquals(0, q.remainingCapacity()); |
900 |
|
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
901 |
|
shouldThrow(); |
902 |
|
} catch (InterruptedException success) {} |
903 |
+ |
assertFalse(Thread.interrupted()); |
904 |
|
}}); |
905 |
|
|
906 |
|
await(pleaseInterrupt); |
907 |
< |
assertThreadStaysAlive(t); |
907 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
908 |
|
t.interrupt(); |
909 |
|
awaitTermination(t); |
910 |
|
} |
936 |
|
}}); |
937 |
|
|
938 |
|
await(threadStarted); |
939 |
< |
assertThreadStaysAlive(t); |
939 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
940 |
|
t.interrupt(); |
941 |
|
awaitTermination(t); |
942 |
|
} |
977 |
|
}}); |
978 |
|
|
979 |
|
await(threadStarted); |
980 |
< |
assertThreadStaysAlive(t); |
980 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
981 |
|
t.interrupt(); |
982 |
|
awaitTermination(t); |
983 |
|
} |
1009 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1010 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1011 |
|
public void realRun() throws InterruptedException { |
1012 |
< |
for (int i = 0; i < SIZE; ++i) { |
997 |
< |
assertEquals(i, q.takeFirst()); |
998 |
< |
} |
1012 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst()); |
1013 |
|
|
1014 |
|
Thread.currentThread().interrupt(); |
1015 |
|
try { |
1027 |
|
}}); |
1028 |
|
|
1029 |
|
await(pleaseInterrupt); |
1030 |
< |
assertThreadStaysAlive(t); |
1030 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1031 |
|
t.interrupt(); |
1032 |
|
awaitTermination(t); |
1033 |
|
} |
1064 |
|
* returning timeout status |
1065 |
|
*/ |
1066 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1067 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1068 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1069 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1070 |
|
public void realRun() throws InterruptedException { |
1071 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1072 |
< |
for (int i = 0; i < SIZE; ++i) { |
1071 |
> |
long startTime = System.nanoTime(); |
1072 |
> |
for (int i = 0; i < SIZE; i++) |
1073 |
|
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1059 |
– |
} |
1074 |
|
|
1075 |
|
Thread.currentThread().interrupt(); |
1076 |
|
try { |
1077 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1077 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1078 |
|
shouldThrow(); |
1079 |
|
} catch (InterruptedException success) {} |
1080 |
|
assertFalse(Thread.interrupted()); |
1085 |
|
shouldThrow(); |
1086 |
|
} catch (InterruptedException success) {} |
1087 |
|
assertFalse(Thread.interrupted()); |
1088 |
+ |
|
1089 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1090 |
|
}}); |
1091 |
|
|
1092 |
|
await(pleaseInterrupt); |
1093 |
< |
assertThreadStaysAlive(t); |
1093 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1094 |
|
t.interrupt(); |
1095 |
|
awaitTermination(t); |
1096 |
|
} |
1123 |
|
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1124 |
|
shouldThrow(); |
1125 |
|
} catch (InterruptedException success) {} |
1126 |
+ |
assertFalse(Thread.interrupted()); |
1127 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1128 |
|
}}); |
1129 |
|
|
1132 |
|
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1133 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1134 |
|
barrier.await(); |
1135 |
< |
assertThreadStaysAlive(t); |
1135 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1136 |
|
t.interrupt(); |
1137 |
|
awaitTermination(t); |
1138 |
|
} |
1190 |
|
}}); |
1191 |
|
|
1192 |
|
await(pleaseInterrupt); |
1193 |
< |
assertThreadStaysAlive(t); |
1193 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1194 |
|
t.interrupt(); |
1195 |
|
awaitTermination(t); |
1196 |
|
assertEquals(SIZE, q.size()); |
1225 |
|
assertEquals(0, q.take()); |
1226 |
|
|
1227 |
|
await(pleaseInterrupt); |
1228 |
< |
assertThreadStaysAlive(t); |
1228 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1229 |
|
t.interrupt(); |
1230 |
|
awaitTermination(t); |
1231 |
|
assertEquals(0, q.remainingCapacity()); |
1252 |
|
}}); |
1253 |
|
|
1254 |
|
await(pleaseInterrupt); |
1255 |
< |
assertThreadStaysAlive(t); |
1255 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1256 |
|
t.interrupt(); |
1257 |
|
awaitTermination(t); |
1258 |
|
} |
1263 |
|
public void testTakeLast() throws InterruptedException { |
1264 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1265 |
|
for (int i = 0; i < SIZE; ++i) { |
1266 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1266 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1267 |
|
} |
1268 |
|
} |
1269 |
|
|
1275 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1276 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1277 |
|
public void realRun() throws InterruptedException { |
1278 |
< |
for (int i = 0; i < SIZE; ++i) { |
1279 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1263 |
< |
} |
1278 |
> |
for (int i = 0; i < SIZE; i++) |
1279 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1280 |
|
|
1281 |
|
Thread.currentThread().interrupt(); |
1282 |
|
try { |
1294 |
|
}}); |
1295 |
|
|
1296 |
|
await(pleaseInterrupt); |
1297 |
< |
assertThreadStaysAlive(t); |
1297 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1298 |
|
t.interrupt(); |
1299 |
|
awaitTermination(t); |
1300 |
|
} |
1305 |
|
public void testTimedPollLast0() throws InterruptedException { |
1306 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1307 |
|
for (int i = 0; i < SIZE; ++i) { |
1308 |
< |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1308 |
> |
assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); |
1309 |
|
} |
1310 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1311 |
|
} |
1317 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1318 |
|
for (int i = 0; i < SIZE; ++i) { |
1319 |
|
long startTime = System.nanoTime(); |
1320 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1320 |
> |
assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1321 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1322 |
|
} |
1323 |
|
long startTime = System.nanoTime(); |
1331 |
|
* returning timeout status |
1332 |
|
*/ |
1333 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1334 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1335 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1336 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1337 |
|
public void realRun() throws InterruptedException { |
1338 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1339 |
< |
for (int i = 0; i < SIZE; ++i) { |
1340 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1341 |
< |
} |
1338 |
> |
long startTime = System.nanoTime(); |
1339 |
> |
for (int i = 0; i < SIZE; i++) |
1340 |
> |
assertEquals(SIZE - i - 1, |
1341 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1342 |
|
|
1343 |
|
Thread.currentThread().interrupt(); |
1344 |
|
try { |
1353 |
|
shouldThrow(); |
1354 |
|
} catch (InterruptedException success) {} |
1355 |
|
assertFalse(Thread.interrupted()); |
1356 |
+ |
|
1357 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1358 |
|
}}); |
1359 |
|
|
1360 |
|
await(pleaseInterrupt); |
1361 |
< |
assertThreadStaysAlive(t); |
1361 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1362 |
|
t.interrupt(); |
1363 |
|
awaitTermination(t); |
1364 |
+ |
checkEmpty(q); |
1365 |
|
} |
1366 |
|
|
1367 |
|
/** |
1394 |
|
shouldThrow(); |
1395 |
|
} catch (InterruptedException success) {} |
1396 |
|
assertFalse(Thread.interrupted()); |
1397 |
+ |
|
1398 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1399 |
|
}}); |
1400 |
|
|
1401 |
|
barrier.await(); |
1404 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1405 |
|
|
1406 |
|
barrier.await(); |
1407 |
< |
assertThreadStaysAlive(t); |
1407 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1408 |
|
t.interrupt(); |
1409 |
|
awaitTermination(t); |
1410 |
|
} |
1480 |
|
assertTrue(changed); |
1481 |
|
|
1482 |
|
assertTrue(q.containsAll(p)); |
1483 |
< |
assertEquals(SIZE-i, q.size()); |
1483 |
> |
assertEquals(SIZE - i, q.size()); |
1484 |
|
p.remove(); |
1485 |
|
} |
1486 |
|
} |
1493 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1494 |
|
LinkedBlockingDeque p = populatedDeque(i); |
1495 |
|
assertTrue(q.removeAll(p)); |
1496 |
< |
assertEquals(SIZE-i, q.size()); |
1496 |
> |
assertEquals(SIZE - i, q.size()); |
1497 |
|
for (int j = 0; j < i; ++j) { |
1498 |
|
Integer x = (Integer)(p.remove()); |
1499 |
|
assertFalse(q.contains(x)); |
1540 |
|
public void testIterator() throws InterruptedException { |
1541 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1542 |
|
Iterator it = q.iterator(); |
1543 |
< |
while (it.hasNext()) { |
1543 |
> |
int i; |
1544 |
> |
for (i = 0; it.hasNext(); i++) |
1545 |
> |
assertTrue(q.contains(it.next())); |
1546 |
> |
assertEquals(i, SIZE); |
1547 |
> |
assertIteratorExhausted(it); |
1548 |
> |
|
1549 |
> |
it = q.iterator(); |
1550 |
> |
for (i = 0; it.hasNext(); i++) |
1551 |
|
assertEquals(it.next(), q.take()); |
1552 |
< |
} |
1552 |
> |
assertEquals(i, SIZE); |
1553 |
> |
assertIteratorExhausted(it); |
1554 |
> |
} |
1555 |
> |
|
1556 |
> |
/** |
1557 |
> |
* iterator of empty collection has no elements |
1558 |
> |
*/ |
1559 |
> |
public void testEmptyIterator() { |
1560 |
> |
Deque c = new LinkedBlockingDeque(); |
1561 |
> |
assertIteratorExhausted(c.iterator()); |
1562 |
> |
assertIteratorExhausted(c.descendingIterator()); |
1563 |
|
} |
1564 |
|
|
1565 |
|
/** |
1692 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1693 |
|
q.add(one); |
1694 |
|
q.add(two); |
1656 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1695 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1696 |
< |
executor.execute(new CheckedRunnable() { |
1697 |
< |
public void realRun() throws InterruptedException { |
1698 |
< |
assertFalse(q.offer(three)); |
1699 |
< |
threadsStarted.await(); |
1700 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1701 |
< |
assertEquals(0, q.remainingCapacity()); |
1702 |
< |
}}); |
1703 |
< |
|
1704 |
< |
executor.execute(new CheckedRunnable() { |
1705 |
< |
public void realRun() throws InterruptedException { |
1706 |
< |
threadsStarted.await(); |
1707 |
< |
assertSame(one, q.take()); |
1708 |
< |
}}); |
1709 |
< |
|
1710 |
< |
joinPool(executor); |
1696 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1697 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1698 |
> |
executor.execute(new CheckedRunnable() { |
1699 |
> |
public void realRun() throws InterruptedException { |
1700 |
> |
assertFalse(q.offer(three)); |
1701 |
> |
threadsStarted.await(); |
1702 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1703 |
> |
assertEquals(0, q.remainingCapacity()); |
1704 |
> |
}}); |
1705 |
> |
|
1706 |
> |
executor.execute(new CheckedRunnable() { |
1707 |
> |
public void realRun() throws InterruptedException { |
1708 |
> |
threadsStarted.await(); |
1709 |
> |
assertSame(one, q.take()); |
1710 |
> |
}}); |
1711 |
> |
} |
1712 |
|
} |
1713 |
|
|
1714 |
|
/** |
1717 |
|
public void testPollInExecutor() { |
1718 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1719 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1720 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1721 |
< |
executor.execute(new CheckedRunnable() { |
1722 |
< |
public void realRun() throws InterruptedException { |
1723 |
< |
assertNull(q.poll()); |
1724 |
< |
threadsStarted.await(); |
1725 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1726 |
< |
checkEmpty(q); |
1727 |
< |
}}); |
1728 |
< |
|
1729 |
< |
executor.execute(new CheckedRunnable() { |
1730 |
< |
public void realRun() throws InterruptedException { |
1731 |
< |
threadsStarted.await(); |
1732 |
< |
q.put(one); |
1733 |
< |
}}); |
1734 |
< |
|
1735 |
< |
joinPool(executor); |
1720 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1721 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1722 |
> |
executor.execute(new CheckedRunnable() { |
1723 |
> |
public void realRun() throws InterruptedException { |
1724 |
> |
assertNull(q.poll()); |
1725 |
> |
threadsStarted.await(); |
1726 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1727 |
> |
checkEmpty(q); |
1728 |
> |
}}); |
1729 |
> |
|
1730 |
> |
executor.execute(new CheckedRunnable() { |
1731 |
> |
public void realRun() throws InterruptedException { |
1732 |
> |
threadsStarted.await(); |
1733 |
> |
q.put(one); |
1734 |
> |
}}); |
1735 |
> |
} |
1736 |
|
} |
1737 |
|
|
1738 |
|
/** |
1784 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1785 |
|
Thread t = new Thread(new CheckedRunnable() { |
1786 |
|
public void realRun() throws InterruptedException { |
1787 |
< |
q.put(new Integer(SIZE+1)); |
1787 |
> |
q.put(new Integer(SIZE + 1)); |
1788 |
|
}}); |
1789 |
|
|
1790 |
|
t.start(); |
1809 |
|
q.drainTo(l, i); |
1810 |
|
int k = (i < SIZE) ? i : SIZE; |
1811 |
|
assertEquals(k, l.size()); |
1812 |
< |
assertEquals(SIZE-k, q.size()); |
1812 |
> |
assertEquals(SIZE - k, q.size()); |
1813 |
|
for (int j = 0; j < k; ++j) |
1814 |
|
assertEquals(l.get(j), new Integer(j)); |
1815 |
|
do {} while (q.poll() != null); |