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 |
|
} |
286 |
|
*/ |
287 |
|
public void testRemoveFirstOccurrence() { |
288 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
289 |
< |
for (int i = 1; i < SIZE; i+=2) { |
289 |
> |
for (int i = 1; i < SIZE; i += 2) { |
290 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
291 |
|
} |
292 |
< |
for (int i = 0; i < SIZE; i+=2) { |
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 |
|
} |
301 |
|
*/ |
302 |
|
public void testRemoveLastOccurrence() { |
303 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
304 |
< |
for (int i = 1; i < SIZE; i+=2) { |
304 |
> |
for (int i = 1; i < SIZE; i += 2) { |
305 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
306 |
|
} |
307 |
< |
for (int i = 0; i < SIZE; i+=2) { |
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) {} |
442 |
|
} |
443 |
|
|
444 |
|
/** |
445 |
< |
* push succeeds if not full; throws ISE if full |
445 |
> |
* push succeeds if not full; throws IllegalStateException 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 I = new Integer(i); |
442 |
– |
q.push(I); |
443 |
– |
assertEquals(I, q.peek()); |
444 |
– |
} |
445 |
– |
assertEquals(0, q.remainingCapacity()); |
456 |
|
q.push(new Integer(SIZE)); |
457 |
|
shouldThrow(); |
458 |
|
} catch (IllegalStateException success) {} |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
< |
* add succeeds if not full; throws ISE if full |
495 |
> |
* add succeeds if not full; throws IllegalStateException if full |
496 |
|
*/ |
497 |
|
public void testAdd() { |
498 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
< |
* addAll(this) throws IAE |
509 |
> |
* addAll(this) throws IllegalArgumentException |
510 |
|
*/ |
511 |
|
public void testAddAllSelf() { |
512 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
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 { |
568 |
|
public void testPut() throws InterruptedException { |
569 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
570 |
|
for (int i = 0; i < SIZE; ++i) { |
571 |
< |
Integer I = new Integer(i); |
572 |
< |
q.put(I); |
573 |
< |
assertTrue(q.contains(I)); |
571 |
> |
Integer x = new Integer(i); |
572 |
> |
q.put(x); |
573 |
> |
assertTrue(q.contains(x)); |
574 |
|
} |
575 |
|
assertEquals(0, q.remainingCapacity()); |
576 |
|
} |
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()); |
626 |
|
pleaseTake.countDown(); |
627 |
|
q.put(86); |
628 |
|
|
629 |
+ |
Thread.currentThread().interrupt(); |
630 |
+ |
try { |
631 |
+ |
q.put(99); |
632 |
+ |
shouldThrow(); |
633 |
+ |
} catch (InterruptedException success) {} |
634 |
+ |
assertFalse(Thread.interrupted()); |
635 |
+ |
|
636 |
|
pleaseInterrupt.countDown(); |
637 |
|
try { |
638 |
|
q.put(99); |
646 |
|
assertEquals(0, q.take()); |
647 |
|
|
648 |
|
await(pleaseInterrupt); |
649 |
< |
assertThreadStaysAlive(t); |
649 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
650 |
|
t.interrupt(); |
651 |
|
awaitTermination(t); |
652 |
|
assertEquals(0, q.remainingCapacity()); |
655 |
|
/** |
656 |
|
* timed offer times out if full and elements not taken |
657 |
|
*/ |
658 |
< |
public void testTimedOffer() throws InterruptedException { |
658 |
> |
public void testTimedOffer() { |
659 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
660 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
661 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
665 |
|
long startTime = System.nanoTime(); |
666 |
|
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
667 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
668 |
+ |
|
669 |
+ |
Thread.currentThread().interrupt(); |
670 |
+ |
try { |
671 |
+ |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
672 |
+ |
shouldThrow(); |
673 |
+ |
} catch (InterruptedException success) {} |
674 |
+ |
assertFalse(Thread.interrupted()); |
675 |
+ |
|
676 |
|
pleaseInterrupt.countDown(); |
677 |
|
try { |
678 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
679 |
|
shouldThrow(); |
680 |
|
} catch (InterruptedException success) {} |
681 |
+ |
assertFalse(Thread.interrupted()); |
682 |
|
}}); |
683 |
|
|
684 |
|
await(pleaseInterrupt); |
685 |
< |
assertThreadStaysAlive(t); |
685 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
686 |
|
t.interrupt(); |
687 |
|
awaitTermination(t); |
688 |
|
} |
705 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
706 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
707 |
|
public void realRun() throws InterruptedException { |
708 |
< |
for (int i = 0; i < SIZE; ++i) { |
683 |
< |
assertEquals(i, q.take()); |
684 |
< |
} |
708 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
709 |
|
|
710 |
|
Thread.currentThread().interrupt(); |
711 |
|
try { |
723 |
|
}}); |
724 |
|
|
725 |
|
await(pleaseInterrupt); |
726 |
< |
assertThreadStaysAlive(t); |
726 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
727 |
|
t.interrupt(); |
728 |
|
awaitTermination(t); |
729 |
|
} |
772 |
|
*/ |
773 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
774 |
|
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
775 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
775 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
776 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
777 |
|
public void realRun() throws InterruptedException { |
778 |
< |
for (int i = 0; i < SIZE; ++i) { |
779 |
< |
long t0 = System.nanoTime(); |
778 |
> |
long startTime = System.nanoTime(); |
779 |
> |
for (int i = 0; i < SIZE; i++) |
780 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
781 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
782 |
< |
} |
783 |
< |
long t0 = System.nanoTime(); |
784 |
< |
aboutToWait.countDown(); |
781 |
> |
|
782 |
> |
Thread.currentThread().interrupt(); |
783 |
> |
try { |
784 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
785 |
> |
shouldThrow(); |
786 |
> |
} catch (InterruptedException success) {} |
787 |
> |
assertFalse(Thread.interrupted()); |
788 |
> |
|
789 |
> |
pleaseInterrupt.countDown(); |
790 |
|
try { |
791 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
791 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
792 |
|
shouldThrow(); |
793 |
< |
} catch (InterruptedException success) { |
794 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
795 |
< |
} |
793 |
> |
} catch (InterruptedException success) {} |
794 |
> |
assertFalse(Thread.interrupted()); |
795 |
> |
|
796 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
797 |
|
}}); |
798 |
|
|
799 |
< |
aboutToWait.await(); |
800 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
799 |
> |
await(pleaseInterrupt); |
800 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
801 |
|
t.interrupt(); |
802 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
802 |
> |
awaitTermination(t); |
803 |
|
checkEmpty(q); |
804 |
|
} |
805 |
|
|
820 |
|
public void testPutFirst() throws InterruptedException { |
821 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
822 |
|
for (int i = 0; i < SIZE; ++i) { |
823 |
< |
Integer I = new Integer(i); |
824 |
< |
q.putFirst(I); |
825 |
< |
assertTrue(q.contains(I)); |
823 |
> |
Integer x = new Integer(i); |
824 |
> |
q.putFirst(x); |
825 |
> |
assertTrue(q.contains(x)); |
826 |
|
} |
827 |
|
assertEquals(0, q.remainingCapacity()); |
828 |
|
} |
856 |
|
}}); |
857 |
|
|
858 |
|
await(pleaseInterrupt); |
859 |
< |
assertThreadStaysAlive(t); |
859 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
860 |
|
t.interrupt(); |
861 |
|
awaitTermination(t); |
862 |
|
assertEquals(SIZE, q.size()); |
891 |
|
assertEquals(capacity - 1, q.take()); |
892 |
|
|
893 |
|
await(pleaseInterrupt); |
894 |
< |
assertThreadStaysAlive(t); |
894 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
895 |
|
t.interrupt(); |
896 |
|
awaitTermination(t); |
897 |
|
assertEquals(0, q.remainingCapacity()); |
900 |
|
/** |
901 |
|
* timed offerFirst times out if full and elements not taken |
902 |
|
*/ |
903 |
< |
public void testTimedOfferFirst() throws InterruptedException { |
903 |
> |
public void testTimedOfferFirst() { |
904 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
905 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
906 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
910 |
|
long startTime = System.nanoTime(); |
911 |
|
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
912 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
913 |
+ |
|
914 |
+ |
Thread.currentThread().interrupt(); |
915 |
+ |
try { |
916 |
+ |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
917 |
+ |
shouldThrow(); |
918 |
+ |
} catch (InterruptedException success) {} |
919 |
+ |
assertFalse(Thread.interrupted()); |
920 |
+ |
|
921 |
|
pleaseInterrupt.countDown(); |
922 |
|
try { |
923 |
|
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
924 |
|
shouldThrow(); |
925 |
|
} catch (InterruptedException success) {} |
926 |
+ |
assertFalse(Thread.interrupted()); |
927 |
|
}}); |
928 |
|
|
929 |
|
await(pleaseInterrupt); |
930 |
< |
assertThreadStaysAlive(t); |
930 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
931 |
|
t.interrupt(); |
932 |
|
awaitTermination(t); |
933 |
|
} |
959 |
|
}}); |
960 |
|
|
961 |
|
await(threadStarted); |
962 |
< |
assertThreadStaysAlive(t); |
962 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
963 |
|
t.interrupt(); |
964 |
|
awaitTermination(t); |
965 |
|
} |
1000 |
|
}}); |
1001 |
|
|
1002 |
|
await(threadStarted); |
1003 |
< |
assertThreadStaysAlive(t); |
1003 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1004 |
|
t.interrupt(); |
1005 |
|
awaitTermination(t); |
1006 |
|
} |
1032 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1033 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1034 |
|
public void realRun() throws InterruptedException { |
1035 |
< |
for (int i = 0; i < SIZE; ++i) { |
997 |
< |
assertEquals(i, q.takeFirst()); |
998 |
< |
} |
1035 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst()); |
1036 |
|
|
1037 |
|
Thread.currentThread().interrupt(); |
1038 |
|
try { |
1050 |
|
}}); |
1051 |
|
|
1052 |
|
await(pleaseInterrupt); |
1053 |
< |
assertThreadStaysAlive(t); |
1053 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1054 |
|
t.interrupt(); |
1055 |
|
awaitTermination(t); |
1056 |
|
} |
1087 |
|
* returning timeout status |
1088 |
|
*/ |
1089 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1090 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1091 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1092 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1093 |
|
public void realRun() throws InterruptedException { |
1094 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1095 |
< |
for (int i = 0; i < SIZE; ++i) { |
1094 |
> |
long startTime = System.nanoTime(); |
1095 |
> |
for (int i = 0; i < SIZE; i++) |
1096 |
|
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1059 |
– |
} |
1097 |
|
|
1098 |
|
Thread.currentThread().interrupt(); |
1099 |
|
try { |
1100 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1100 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1101 |
|
shouldThrow(); |
1102 |
|
} catch (InterruptedException success) {} |
1103 |
|
assertFalse(Thread.interrupted()); |
1108 |
|
shouldThrow(); |
1109 |
|
} catch (InterruptedException success) {} |
1110 |
|
assertFalse(Thread.interrupted()); |
1111 |
+ |
|
1112 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1113 |
|
}}); |
1114 |
|
|
1115 |
|
await(pleaseInterrupt); |
1116 |
< |
assertThreadStaysAlive(t); |
1116 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1117 |
|
t.interrupt(); |
1118 |
|
awaitTermination(t); |
1119 |
|
} |
1146 |
|
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1147 |
|
shouldThrow(); |
1148 |
|
} catch (InterruptedException success) {} |
1149 |
+ |
assertFalse(Thread.interrupted()); |
1150 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1151 |
|
}}); |
1152 |
|
|
1155 |
|
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1156 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1157 |
|
barrier.await(); |
1158 |
< |
assertThreadStaysAlive(t); |
1158 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1159 |
|
t.interrupt(); |
1160 |
|
awaitTermination(t); |
1161 |
|
} |
1177 |
|
public void testPutLast() throws InterruptedException { |
1178 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1179 |
|
for (int i = 0; i < SIZE; ++i) { |
1180 |
< |
Integer I = new Integer(i); |
1181 |
< |
q.putLast(I); |
1182 |
< |
assertTrue(q.contains(I)); |
1180 |
> |
Integer x = new Integer(i); |
1181 |
> |
q.putLast(x); |
1182 |
> |
assertTrue(q.contains(x)); |
1183 |
|
} |
1184 |
|
assertEquals(0, q.remainingCapacity()); |
1185 |
|
} |
1213 |
|
}}); |
1214 |
|
|
1215 |
|
await(pleaseInterrupt); |
1216 |
< |
assertThreadStaysAlive(t); |
1216 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1217 |
|
t.interrupt(); |
1218 |
|
awaitTermination(t); |
1219 |
|
assertEquals(SIZE, q.size()); |
1235 |
|
pleaseTake.countDown(); |
1236 |
|
q.putLast(86); |
1237 |
|
|
1238 |
+ |
Thread.currentThread().interrupt(); |
1239 |
+ |
try { |
1240 |
+ |
q.putLast(99); |
1241 |
+ |
shouldThrow(); |
1242 |
+ |
} catch (InterruptedException success) {} |
1243 |
+ |
assertFalse(Thread.interrupted()); |
1244 |
+ |
|
1245 |
|
pleaseInterrupt.countDown(); |
1246 |
|
try { |
1247 |
|
q.putLast(99); |
1255 |
|
assertEquals(0, q.take()); |
1256 |
|
|
1257 |
|
await(pleaseInterrupt); |
1258 |
< |
assertThreadStaysAlive(t); |
1258 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1259 |
|
t.interrupt(); |
1260 |
|
awaitTermination(t); |
1261 |
|
assertEquals(0, q.remainingCapacity()); |
1264 |
|
/** |
1265 |
|
* timed offerLast times out if full and elements not taken |
1266 |
|
*/ |
1267 |
< |
public void testTimedOfferLast() throws InterruptedException { |
1267 |
> |
public void testTimedOfferLast() { |
1268 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1269 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1270 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1274 |
|
long startTime = System.nanoTime(); |
1275 |
|
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1276 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1277 |
+ |
|
1278 |
+ |
Thread.currentThread().interrupt(); |
1279 |
+ |
try { |
1280 |
+ |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1281 |
+ |
shouldThrow(); |
1282 |
+ |
} catch (InterruptedException success) {} |
1283 |
+ |
|
1284 |
|
pleaseInterrupt.countDown(); |
1285 |
|
try { |
1286 |
|
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1289 |
|
}}); |
1290 |
|
|
1291 |
|
await(pleaseInterrupt); |
1292 |
< |
assertThreadStaysAlive(t); |
1292 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1293 |
|
t.interrupt(); |
1294 |
|
awaitTermination(t); |
1295 |
|
} |
1300 |
|
public void testTakeLast() throws InterruptedException { |
1301 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1302 |
|
for (int i = 0; i < SIZE; ++i) { |
1303 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1303 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1304 |
|
} |
1305 |
|
} |
1306 |
|
|
1312 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1313 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1314 |
|
public void realRun() throws InterruptedException { |
1315 |
< |
for (int i = 0; i < SIZE; ++i) { |
1316 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1263 |
< |
} |
1315 |
> |
for (int i = 0; i < SIZE; i++) |
1316 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1317 |
|
|
1318 |
|
Thread.currentThread().interrupt(); |
1319 |
|
try { |
1331 |
|
}}); |
1332 |
|
|
1333 |
|
await(pleaseInterrupt); |
1334 |
< |
assertThreadStaysAlive(t); |
1334 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1335 |
|
t.interrupt(); |
1336 |
|
awaitTermination(t); |
1337 |
|
} |
1342 |
|
public void testTimedPollLast0() throws InterruptedException { |
1343 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1344 |
|
for (int i = 0; i < SIZE; ++i) { |
1345 |
< |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1345 |
> |
assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); |
1346 |
|
} |
1347 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1348 |
|
} |
1354 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1355 |
|
for (int i = 0; i < SIZE; ++i) { |
1356 |
|
long startTime = System.nanoTime(); |
1357 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1357 |
> |
assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1358 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1359 |
|
} |
1360 |
|
long startTime = System.nanoTime(); |
1368 |
|
* returning timeout status |
1369 |
|
*/ |
1370 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1371 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1372 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1373 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1374 |
|
public void realRun() throws InterruptedException { |
1375 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1376 |
< |
for (int i = 0; i < SIZE; ++i) { |
1377 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1378 |
< |
} |
1375 |
> |
long startTime = System.nanoTime(); |
1376 |
> |
for (int i = 0; i < SIZE; i++) |
1377 |
> |
assertEquals(SIZE - i - 1, |
1378 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1379 |
|
|
1380 |
|
Thread.currentThread().interrupt(); |
1381 |
|
try { |
1390 |
|
shouldThrow(); |
1391 |
|
} catch (InterruptedException success) {} |
1392 |
|
assertFalse(Thread.interrupted()); |
1393 |
+ |
|
1394 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1395 |
|
}}); |
1396 |
|
|
1397 |
|
await(pleaseInterrupt); |
1398 |
< |
assertThreadStaysAlive(t); |
1398 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1399 |
|
t.interrupt(); |
1400 |
|
awaitTermination(t); |
1401 |
+ |
checkEmpty(q); |
1402 |
|
} |
1403 |
|
|
1404 |
|
/** |
1431 |
|
shouldThrow(); |
1432 |
|
} catch (InterruptedException success) {} |
1433 |
|
assertFalse(Thread.interrupted()); |
1434 |
+ |
|
1435 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1436 |
|
}}); |
1437 |
|
|
1438 |
|
barrier.await(); |
1441 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1442 |
|
|
1443 |
|
barrier.await(); |
1444 |
< |
assertThreadStaysAlive(t); |
1444 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1445 |
|
t.interrupt(); |
1446 |
|
awaitTermination(t); |
1447 |
|
} |
1517 |
|
assertTrue(changed); |
1518 |
|
|
1519 |
|
assertTrue(q.containsAll(p)); |
1520 |
< |
assertEquals(SIZE-i, q.size()); |
1520 |
> |
assertEquals(SIZE - i, q.size()); |
1521 |
|
p.remove(); |
1522 |
|
} |
1523 |
|
} |
1530 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1531 |
|
LinkedBlockingDeque p = populatedDeque(i); |
1532 |
|
assertTrue(q.removeAll(p)); |
1533 |
< |
assertEquals(SIZE-i, q.size()); |
1533 |
> |
assertEquals(SIZE - i, q.size()); |
1534 |
|
for (int j = 0; j < i; ++j) { |
1535 |
< |
Integer I = (Integer)(p.remove()); |
1536 |
< |
assertFalse(q.contains(I)); |
1535 |
> |
Integer x = (Integer)(p.remove()); |
1536 |
> |
assertFalse(q.contains(x)); |
1537 |
|
} |
1538 |
|
} |
1539 |
|
} |
1543 |
|
*/ |
1544 |
|
public void testToArray() throws InterruptedException { |
1545 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1546 |
< |
Object[] o = q.toArray(); |
1547 |
< |
for (int i = 0; i < o.length; i++) |
1548 |
< |
assertSame(o[i], q.poll()); |
1546 |
> |
Object[] a = q.toArray(); |
1547 |
> |
assertSame(Object[].class, a.getClass()); |
1548 |
> |
for (Object o : a) |
1549 |
> |
assertSame(o, q.poll()); |
1550 |
> |
assertTrue(q.isEmpty()); |
1551 |
|
} |
1552 |
|
|
1553 |
|
/** |
1558 |
|
Integer[] ints = new Integer[SIZE]; |
1559 |
|
Integer[] array = q.toArray(ints); |
1560 |
|
assertSame(ints, array); |
1561 |
< |
for (int i = 0; i < ints.length; i++) |
1562 |
< |
assertSame(ints[i], q.remove()); |
1561 |
> |
for (Integer o : ints) |
1562 |
> |
assertSame(o, q.remove()); |
1563 |
> |
assertTrue(q.isEmpty()); |
1564 |
|
} |
1565 |
|
|
1566 |
|
/** |
1580 |
|
public void testIterator() throws InterruptedException { |
1581 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1582 |
|
Iterator it = q.iterator(); |
1583 |
< |
while (it.hasNext()) { |
1583 |
> |
int i; |
1584 |
> |
for (i = 0; it.hasNext(); i++) |
1585 |
> |
assertTrue(q.contains(it.next())); |
1586 |
> |
assertEquals(i, SIZE); |
1587 |
> |
assertIteratorExhausted(it); |
1588 |
> |
|
1589 |
> |
it = q.iterator(); |
1590 |
> |
for (i = 0; it.hasNext(); i++) |
1591 |
|
assertEquals(it.next(), q.take()); |
1592 |
< |
} |
1592 |
> |
assertEquals(i, SIZE); |
1593 |
> |
assertIteratorExhausted(it); |
1594 |
> |
} |
1595 |
> |
|
1596 |
> |
/** |
1597 |
> |
* iterator of empty collection has no elements |
1598 |
> |
*/ |
1599 |
> |
public void testEmptyIterator() { |
1600 |
> |
Deque c = new LinkedBlockingDeque(); |
1601 |
> |
assertIteratorExhausted(c.iterator()); |
1602 |
> |
assertIteratorExhausted(c.descendingIterator()); |
1603 |
|
} |
1604 |
|
|
1605 |
|
/** |
1732 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1733 |
|
q.add(one); |
1734 |
|
q.add(two); |
1656 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1735 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1736 |
< |
executor.execute(new CheckedRunnable() { |
1737 |
< |
public void realRun() throws InterruptedException { |
1738 |
< |
assertFalse(q.offer(three)); |
1739 |
< |
threadsStarted.await(); |
1740 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1741 |
< |
assertEquals(0, q.remainingCapacity()); |
1742 |
< |
}}); |
1743 |
< |
|
1744 |
< |
executor.execute(new CheckedRunnable() { |
1745 |
< |
public void realRun() throws InterruptedException { |
1746 |
< |
threadsStarted.await(); |
1747 |
< |
assertSame(one, q.take()); |
1748 |
< |
}}); |
1749 |
< |
|
1750 |
< |
joinPool(executor); |
1736 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1737 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1738 |
> |
executor.execute(new CheckedRunnable() { |
1739 |
> |
public void realRun() throws InterruptedException { |
1740 |
> |
assertFalse(q.offer(three)); |
1741 |
> |
threadsStarted.await(); |
1742 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1743 |
> |
assertEquals(0, q.remainingCapacity()); |
1744 |
> |
}}); |
1745 |
> |
|
1746 |
> |
executor.execute(new CheckedRunnable() { |
1747 |
> |
public void realRun() throws InterruptedException { |
1748 |
> |
threadsStarted.await(); |
1749 |
> |
assertSame(one, q.take()); |
1750 |
> |
}}); |
1751 |
> |
} |
1752 |
|
} |
1753 |
|
|
1754 |
|
/** |
1757 |
|
public void testPollInExecutor() { |
1758 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1759 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1760 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1761 |
< |
executor.execute(new CheckedRunnable() { |
1762 |
< |
public void realRun() throws InterruptedException { |
1763 |
< |
assertNull(q.poll()); |
1764 |
< |
threadsStarted.await(); |
1765 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1766 |
< |
checkEmpty(q); |
1767 |
< |
}}); |
1768 |
< |
|
1769 |
< |
executor.execute(new CheckedRunnable() { |
1770 |
< |
public void realRun() throws InterruptedException { |
1771 |
< |
threadsStarted.await(); |
1772 |
< |
q.put(one); |
1773 |
< |
}}); |
1774 |
< |
|
1775 |
< |
joinPool(executor); |
1760 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1761 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1762 |
> |
executor.execute(new CheckedRunnable() { |
1763 |
> |
public void realRun() throws InterruptedException { |
1764 |
> |
assertNull(q.poll()); |
1765 |
> |
threadsStarted.await(); |
1766 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1767 |
> |
checkEmpty(q); |
1768 |
> |
}}); |
1769 |
> |
|
1770 |
> |
executor.execute(new CheckedRunnable() { |
1771 |
> |
public void realRun() throws InterruptedException { |
1772 |
> |
threadsStarted.await(); |
1773 |
> |
q.put(one); |
1774 |
> |
}}); |
1775 |
> |
} |
1776 |
|
} |
1777 |
|
|
1778 |
|
/** |
1779 |
< |
* A deserialized serialized deque has same elements in same order |
1779 |
> |
* A deserialized/reserialized deque has same elements in same order |
1780 |
|
*/ |
1781 |
|
public void testSerialization() throws Exception { |
1782 |
|
Queue x = populatedDeque(SIZE); |
1824 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1825 |
|
Thread t = new Thread(new CheckedRunnable() { |
1826 |
|
public void realRun() throws InterruptedException { |
1827 |
< |
q.put(new Integer(SIZE+1)); |
1827 |
> |
q.put(new Integer(SIZE + 1)); |
1828 |
|
}}); |
1829 |
|
|
1830 |
|
t.start(); |
1849 |
|
q.drainTo(l, i); |
1850 |
|
int k = (i < SIZE) ? i : SIZE; |
1851 |
|
assertEquals(k, l.size()); |
1852 |
< |
assertEquals(SIZE-k, q.size()); |
1852 |
> |
assertEquals(SIZE - k, q.size()); |
1853 |
|
for (int j = 0; j < k; ++j) |
1854 |
|
assertEquals(l.get(j), new Integer(j)); |
1855 |
< |
while (q.poll() != null) ; |
1855 |
> |
do {} while (q.poll() != null); |
1856 |
|
} |
1857 |
|
} |
1858 |
|
|