4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.Arrays; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
|
9 |
|
import java.util.ArrayList; |
10 |
+ |
import java.util.Arrays; |
11 |
|
import java.util.Collection; |
12 |
+ |
import java.util.Deque; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.NoSuchElementException; |
15 |
|
import java.util.Queue; |
19 |
|
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
|
import java.util.concurrent.LinkedBlockingDeque; |
22 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
26 |
|
|
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 |
< |
* Create a deque of given size containing consecutive |
59 |
< |
* Integers 0 ... n. |
58 |
> |
* Returns a new deque of given size containing consecutive |
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 { |
425 |
– |
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 { |
436 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
437 |
– |
for (int i = 0; i < SIZE; ++i) { |
438 |
– |
Integer I = new Integer(i); |
439 |
– |
q.push(I); |
440 |
– |
assertEquals(I, q.peek()); |
441 |
– |
} |
442 |
– |
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 |
> |
if (randomBoolean()) 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 |
> |
if (randomBoolean()) 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() { |
663 |
|
q.put(new Object()); |
664 |
|
q.put(new Object()); |
665 |
|
long startTime = System.nanoTime(); |
666 |
+ |
|
667 |
|
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
668 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
669 |
+ |
|
670 |
+ |
Thread.currentThread().interrupt(); |
671 |
+ |
try { |
672 |
+ |
q.offer(new Object(), randomTimeout(), randomTimeUnit()); |
673 |
+ |
shouldThrow(); |
674 |
+ |
} catch (InterruptedException success) {} |
675 |
+ |
assertFalse(Thread.interrupted()); |
676 |
+ |
|
677 |
|
pleaseInterrupt.countDown(); |
678 |
|
try { |
679 |
< |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
679 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
680 |
|
shouldThrow(); |
681 |
|
} catch (InterruptedException success) {} |
682 |
+ |
assertFalse(Thread.interrupted()); |
683 |
+ |
|
684 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
685 |
|
}}); |
686 |
|
|
687 |
|
await(pleaseInterrupt); |
688 |
< |
assertThreadStaysAlive(t); |
688 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
689 |
|
t.interrupt(); |
690 |
|
awaitTermination(t); |
691 |
|
} |
708 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
709 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
710 |
|
public void realRun() throws InterruptedException { |
711 |
< |
for (int i = 0; i < SIZE; ++i) { |
680 |
< |
assertEquals(i, q.take()); |
681 |
< |
} |
711 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
712 |
|
|
713 |
|
Thread.currentThread().interrupt(); |
714 |
|
try { |
726 |
|
}}); |
727 |
|
|
728 |
|
await(pleaseInterrupt); |
729 |
< |
assertThreadStaysAlive(t); |
729 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
730 |
|
t.interrupt(); |
731 |
|
awaitTermination(t); |
732 |
|
} |
775 |
|
*/ |
776 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
777 |
|
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
778 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
778 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
779 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
780 |
|
public void realRun() throws InterruptedException { |
781 |
< |
for (int i = 0; i < SIZE; ++i) { |
782 |
< |
long t0 = System.nanoTime(); |
781 |
> |
long startTime = System.nanoTime(); |
782 |
> |
for (int i = 0; i < SIZE; i++) |
783 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
784 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
785 |
< |
} |
756 |
< |
long t0 = System.nanoTime(); |
757 |
< |
aboutToWait.countDown(); |
784 |
> |
|
785 |
> |
Thread.currentThread().interrupt(); |
786 |
|
try { |
787 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
787 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
788 |
|
shouldThrow(); |
789 |
< |
} catch (InterruptedException success) { |
790 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
791 |
< |
} |
789 |
> |
} catch (InterruptedException success) {} |
790 |
> |
assertFalse(Thread.interrupted()); |
791 |
> |
|
792 |
> |
pleaseInterrupt.countDown(); |
793 |
> |
try { |
794 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
795 |
> |
shouldThrow(); |
796 |
> |
} catch (InterruptedException success) {} |
797 |
> |
assertFalse(Thread.interrupted()); |
798 |
> |
|
799 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
800 |
|
}}); |
801 |
|
|
802 |
< |
aboutToWait.await(); |
803 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
802 |
> |
await(pleaseInterrupt); |
803 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
804 |
|
t.interrupt(); |
805 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
805 |
> |
awaitTermination(t); |
806 |
|
checkEmpty(q); |
807 |
|
} |
808 |
|
|
823 |
|
public void testPutFirst() throws InterruptedException { |
824 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
825 |
|
for (int i = 0; i < SIZE; ++i) { |
826 |
< |
Integer I = new Integer(i); |
827 |
< |
q.putFirst(I); |
828 |
< |
assertTrue(q.contains(I)); |
826 |
> |
Integer x = new Integer(i); |
827 |
> |
q.putFirst(x); |
828 |
> |
assertTrue(q.contains(x)); |
829 |
|
} |
830 |
|
assertEquals(0, q.remainingCapacity()); |
831 |
|
} |
859 |
|
}}); |
860 |
|
|
861 |
|
await(pleaseInterrupt); |
862 |
< |
assertThreadStaysAlive(t); |
862 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
863 |
|
t.interrupt(); |
864 |
|
awaitTermination(t); |
865 |
|
assertEquals(SIZE, q.size()); |
894 |
|
assertEquals(capacity - 1, q.take()); |
895 |
|
|
896 |
|
await(pleaseInterrupt); |
897 |
< |
assertThreadStaysAlive(t); |
897 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
898 |
|
t.interrupt(); |
899 |
|
awaitTermination(t); |
900 |
|
assertEquals(0, q.remainingCapacity()); |
903 |
|
/** |
904 |
|
* timed offerFirst times out if full and elements not taken |
905 |
|
*/ |
906 |
< |
public void testTimedOfferFirst() throws InterruptedException { |
906 |
> |
public void testTimedOfferFirst() { |
907 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
908 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
909 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
911 |
|
q.putFirst(new Object()); |
912 |
|
q.putFirst(new Object()); |
913 |
|
long startTime = System.nanoTime(); |
914 |
+ |
|
915 |
|
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
916 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
917 |
+ |
|
918 |
+ |
Thread.currentThread().interrupt(); |
919 |
+ |
try { |
920 |
+ |
q.offerFirst(new Object(), randomTimeout(), randomTimeUnit()); |
921 |
+ |
shouldThrow(); |
922 |
+ |
} catch (InterruptedException success) {} |
923 |
+ |
assertFalse(Thread.interrupted()); |
924 |
+ |
|
925 |
|
pleaseInterrupt.countDown(); |
926 |
|
try { |
927 |
< |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
927 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
928 |
|
shouldThrow(); |
929 |
|
} catch (InterruptedException success) {} |
930 |
+ |
assertFalse(Thread.interrupted()); |
931 |
+ |
|
932 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
933 |
|
}}); |
934 |
|
|
935 |
|
await(pleaseInterrupt); |
936 |
< |
assertThreadStaysAlive(t); |
936 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
937 |
|
t.interrupt(); |
938 |
|
awaitTermination(t); |
939 |
|
} |
965 |
|
}}); |
966 |
|
|
967 |
|
await(threadStarted); |
968 |
< |
assertThreadStaysAlive(t); |
968 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
969 |
|
t.interrupt(); |
970 |
|
awaitTermination(t); |
971 |
|
} |
1006 |
|
}}); |
1007 |
|
|
1008 |
|
await(threadStarted); |
1009 |
< |
assertThreadStaysAlive(t); |
1009 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1010 |
|
t.interrupt(); |
1011 |
|
awaitTermination(t); |
1012 |
|
} |
1038 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1039 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1040 |
|
public void realRun() throws InterruptedException { |
1041 |
< |
for (int i = 0; i < SIZE; ++i) { |
994 |
< |
assertEquals(i, q.takeFirst()); |
995 |
< |
} |
1041 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst()); |
1042 |
|
|
1043 |
|
Thread.currentThread().interrupt(); |
1044 |
|
try { |
1056 |
|
}}); |
1057 |
|
|
1058 |
|
await(pleaseInterrupt); |
1059 |
< |
assertThreadStaysAlive(t); |
1059 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1060 |
|
t.interrupt(); |
1061 |
|
awaitTermination(t); |
1062 |
|
} |
1093 |
|
* returning timeout status |
1094 |
|
*/ |
1095 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1096 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1097 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1098 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1099 |
|
public void realRun() throws InterruptedException { |
1100 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1101 |
< |
for (int i = 0; i < SIZE; ++i) { |
1100 |
> |
long startTime = System.nanoTime(); |
1101 |
> |
for (int i = 0; i < SIZE; i++) |
1102 |
|
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1056 |
– |
} |
1103 |
|
|
1104 |
|
Thread.currentThread().interrupt(); |
1105 |
|
try { |
1106 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1106 |
> |
q.pollFirst(randomTimeout(), randomTimeUnit()); |
1107 |
|
shouldThrow(); |
1108 |
|
} catch (InterruptedException success) {} |
1109 |
|
assertFalse(Thread.interrupted()); |
1110 |
|
|
1111 |
|
pleaseInterrupt.countDown(); |
1112 |
|
try { |
1113 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1113 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1114 |
|
shouldThrow(); |
1115 |
|
} catch (InterruptedException success) {} |
1116 |
|
assertFalse(Thread.interrupted()); |
1117 |
+ |
|
1118 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1119 |
|
}}); |
1120 |
|
|
1121 |
|
await(pleaseInterrupt); |
1122 |
< |
assertThreadStaysAlive(t); |
1122 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1123 |
|
t.interrupt(); |
1124 |
|
awaitTermination(t); |
1125 |
|
} |
1143 |
|
|
1144 |
|
Thread.currentThread().interrupt(); |
1145 |
|
try { |
1146 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1146 |
> |
q.pollFirst(randomTimeout(), randomTimeUnit()); |
1147 |
|
shouldThrow(); |
1148 |
|
} catch (InterruptedException success) {} |
1149 |
|
|
1152 |
|
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1153 |
|
shouldThrow(); |
1154 |
|
} catch (InterruptedException success) {} |
1155 |
+ |
assertFalse(Thread.interrupted()); |
1156 |
+ |
|
1157 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1158 |
|
}}); |
1159 |
|
|
1162 |
|
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1163 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1164 |
|
barrier.await(); |
1165 |
< |
assertThreadStaysAlive(t); |
1165 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1166 |
|
t.interrupt(); |
1167 |
|
awaitTermination(t); |
1168 |
|
} |
1184 |
|
public void testPutLast() throws InterruptedException { |
1185 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1186 |
|
for (int i = 0; i < SIZE; ++i) { |
1187 |
< |
Integer I = new Integer(i); |
1188 |
< |
q.putLast(I); |
1189 |
< |
assertTrue(q.contains(I)); |
1187 |
> |
Integer x = new Integer(i); |
1188 |
> |
q.putLast(x); |
1189 |
> |
assertTrue(q.contains(x)); |
1190 |
|
} |
1191 |
|
assertEquals(0, q.remainingCapacity()); |
1192 |
|
} |
1220 |
|
}}); |
1221 |
|
|
1222 |
|
await(pleaseInterrupt); |
1223 |
< |
assertThreadStaysAlive(t); |
1223 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1224 |
|
t.interrupt(); |
1225 |
|
awaitTermination(t); |
1226 |
|
assertEquals(SIZE, q.size()); |
1242 |
|
pleaseTake.countDown(); |
1243 |
|
q.putLast(86); |
1244 |
|
|
1245 |
+ |
Thread.currentThread().interrupt(); |
1246 |
+ |
try { |
1247 |
+ |
q.putLast(99); |
1248 |
+ |
shouldThrow(); |
1249 |
+ |
} catch (InterruptedException success) {} |
1250 |
+ |
assertFalse(Thread.interrupted()); |
1251 |
+ |
|
1252 |
|
pleaseInterrupt.countDown(); |
1253 |
|
try { |
1254 |
|
q.putLast(99); |
1262 |
|
assertEquals(0, q.take()); |
1263 |
|
|
1264 |
|
await(pleaseInterrupt); |
1265 |
< |
assertThreadStaysAlive(t); |
1265 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1266 |
|
t.interrupt(); |
1267 |
|
awaitTermination(t); |
1268 |
|
assertEquals(0, q.remainingCapacity()); |
1271 |
|
/** |
1272 |
|
* timed offerLast times out if full and elements not taken |
1273 |
|
*/ |
1274 |
< |
public void testTimedOfferLast() throws InterruptedException { |
1274 |
> |
public void testTimedOfferLast() { |
1275 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1276 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1277 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1279 |
|
q.putLast(new Object()); |
1280 |
|
q.putLast(new Object()); |
1281 |
|
long startTime = System.nanoTime(); |
1282 |
+ |
|
1283 |
|
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1284 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1285 |
+ |
|
1286 |
+ |
Thread.currentThread().interrupt(); |
1287 |
+ |
try { |
1288 |
+ |
q.offerLast(new Object(), randomTimeout(), randomTimeUnit()); |
1289 |
+ |
shouldThrow(); |
1290 |
+ |
} catch (InterruptedException success) {} |
1291 |
+ |
|
1292 |
|
pleaseInterrupt.countDown(); |
1293 |
|
try { |
1294 |
< |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1294 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1295 |
|
shouldThrow(); |
1296 |
|
} catch (InterruptedException success) {} |
1297 |
+ |
|
1298 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1299 |
|
}}); |
1300 |
|
|
1301 |
|
await(pleaseInterrupt); |
1302 |
< |
assertThreadStaysAlive(t); |
1302 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1303 |
|
t.interrupt(); |
1304 |
|
awaitTermination(t); |
1305 |
|
} |
1310 |
|
public void testTakeLast() throws InterruptedException { |
1311 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1312 |
|
for (int i = 0; i < SIZE; ++i) { |
1313 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1313 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1314 |
|
} |
1315 |
|
} |
1316 |
|
|
1322 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1323 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1324 |
|
public void realRun() throws InterruptedException { |
1325 |
< |
for (int i = 0; i < SIZE; ++i) { |
1326 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1260 |
< |
} |
1325 |
> |
for (int i = 0; i < SIZE; i++) |
1326 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1327 |
|
|
1328 |
|
Thread.currentThread().interrupt(); |
1329 |
|
try { |
1341 |
|
}}); |
1342 |
|
|
1343 |
|
await(pleaseInterrupt); |
1344 |
< |
assertThreadStaysAlive(t); |
1344 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1345 |
|
t.interrupt(); |
1346 |
|
awaitTermination(t); |
1347 |
|
} |
1352 |
|
public void testTimedPollLast0() throws InterruptedException { |
1353 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1354 |
|
for (int i = 0; i < SIZE; ++i) { |
1355 |
< |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1355 |
> |
assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); |
1356 |
|
} |
1357 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1358 |
|
} |
1364 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1365 |
|
for (int i = 0; i < SIZE; ++i) { |
1366 |
|
long startTime = System.nanoTime(); |
1367 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1367 |
> |
assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1368 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1369 |
|
} |
1370 |
|
long startTime = System.nanoTime(); |
1378 |
|
* returning timeout status |
1379 |
|
*/ |
1380 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1381 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1382 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1383 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1384 |
|
public void realRun() throws InterruptedException { |
1385 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1386 |
< |
for (int i = 0; i < SIZE; ++i) { |
1387 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1388 |
< |
} |
1385 |
> |
long startTime = System.nanoTime(); |
1386 |
> |
for (int i = 0; i < SIZE; i++) |
1387 |
> |
assertEquals(SIZE - i - 1, |
1388 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1389 |
|
|
1390 |
|
Thread.currentThread().interrupt(); |
1391 |
|
try { |
1392 |
< |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1392 |
> |
q.pollLast(randomTimeout(), randomTimeUnit()); |
1393 |
|
shouldThrow(); |
1394 |
|
} catch (InterruptedException success) {} |
1395 |
|
assertFalse(Thread.interrupted()); |
1400 |
|
shouldThrow(); |
1401 |
|
} catch (InterruptedException success) {} |
1402 |
|
assertFalse(Thread.interrupted()); |
1403 |
+ |
|
1404 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1405 |
|
}}); |
1406 |
|
|
1407 |
|
await(pleaseInterrupt); |
1408 |
< |
assertThreadStaysAlive(t); |
1408 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1409 |
|
t.interrupt(); |
1410 |
|
awaitTermination(t); |
1411 |
+ |
checkEmpty(q); |
1412 |
|
} |
1413 |
|
|
1414 |
|
/** |
1430 |
|
|
1431 |
|
Thread.currentThread().interrupt(); |
1432 |
|
try { |
1433 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1433 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
1434 |
|
shouldThrow(); |
1435 |
|
} catch (InterruptedException success) {} |
1436 |
|
assertFalse(Thread.interrupted()); |
1441 |
|
shouldThrow(); |
1442 |
|
} catch (InterruptedException success) {} |
1443 |
|
assertFalse(Thread.interrupted()); |
1444 |
+ |
|
1445 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1446 |
|
}}); |
1447 |
|
|
1448 |
|
barrier.await(); |
1451 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1452 |
|
|
1453 |
|
barrier.await(); |
1454 |
< |
assertThreadStaysAlive(t); |
1454 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1455 |
|
t.interrupt(); |
1456 |
|
awaitTermination(t); |
1457 |
|
} |
1527 |
|
assertTrue(changed); |
1528 |
|
|
1529 |
|
assertTrue(q.containsAll(p)); |
1530 |
< |
assertEquals(SIZE-i, q.size()); |
1530 |
> |
assertEquals(SIZE - i, q.size()); |
1531 |
|
p.remove(); |
1532 |
|
} |
1533 |
|
} |
1540 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1541 |
|
LinkedBlockingDeque p = populatedDeque(i); |
1542 |
|
assertTrue(q.removeAll(p)); |
1543 |
< |
assertEquals(SIZE-i, q.size()); |
1543 |
> |
assertEquals(SIZE - i, q.size()); |
1544 |
|
for (int j = 0; j < i; ++j) { |
1545 |
< |
Integer I = (Integer)(p.remove()); |
1546 |
< |
assertFalse(q.contains(I)); |
1545 |
> |
Integer x = (Integer)(p.remove()); |
1546 |
> |
assertFalse(q.contains(x)); |
1547 |
|
} |
1548 |
|
} |
1549 |
|
} |
1553 |
|
*/ |
1554 |
|
public void testToArray() throws InterruptedException { |
1555 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1556 |
< |
Object[] o = q.toArray(); |
1557 |
< |
for (int i = 0; i < o.length; i++) |
1558 |
< |
assertSame(o[i], q.poll()); |
1556 |
> |
Object[] a = q.toArray(); |
1557 |
> |
assertSame(Object[].class, a.getClass()); |
1558 |
> |
for (Object o : a) |
1559 |
> |
assertSame(o, q.poll()); |
1560 |
> |
assertTrue(q.isEmpty()); |
1561 |
|
} |
1562 |
|
|
1563 |
|
/** |
1568 |
|
Integer[] ints = new Integer[SIZE]; |
1569 |
|
Integer[] array = q.toArray(ints); |
1570 |
|
assertSame(ints, array); |
1571 |
< |
for (int i = 0; i < ints.length; i++) |
1572 |
< |
assertSame(ints[i], q.remove()); |
1571 |
> |
for (Integer o : ints) |
1572 |
> |
assertSame(o, q.remove()); |
1573 |
> |
assertTrue(q.isEmpty()); |
1574 |
|
} |
1575 |
|
|
1576 |
|
/** |
1590 |
|
public void testIterator() throws InterruptedException { |
1591 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1592 |
|
Iterator it = q.iterator(); |
1593 |
< |
while (it.hasNext()) { |
1593 |
> |
int i; |
1594 |
> |
for (i = 0; it.hasNext(); i++) |
1595 |
> |
assertTrue(q.contains(it.next())); |
1596 |
> |
assertEquals(i, SIZE); |
1597 |
> |
assertIteratorExhausted(it); |
1598 |
> |
|
1599 |
> |
it = q.iterator(); |
1600 |
> |
for (i = 0; it.hasNext(); i++) |
1601 |
|
assertEquals(it.next(), q.take()); |
1602 |
< |
} |
1602 |
> |
assertEquals(i, SIZE); |
1603 |
> |
assertIteratorExhausted(it); |
1604 |
> |
} |
1605 |
> |
|
1606 |
> |
/** |
1607 |
> |
* iterator of empty collection has no elements |
1608 |
> |
*/ |
1609 |
> |
public void testEmptyIterator() { |
1610 |
> |
Deque c = new LinkedBlockingDeque(); |
1611 |
> |
assertIteratorExhausted(c.iterator()); |
1612 |
> |
assertIteratorExhausted(c.descendingIterator()); |
1613 |
|
} |
1614 |
|
|
1615 |
|
/** |
1742 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1743 |
|
q.add(one); |
1744 |
|
q.add(two); |
1653 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1745 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1746 |
< |
executor.execute(new CheckedRunnable() { |
1747 |
< |
public void realRun() throws InterruptedException { |
1748 |
< |
assertFalse(q.offer(three)); |
1749 |
< |
threadsStarted.await(); |
1750 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1751 |
< |
assertEquals(0, q.remainingCapacity()); |
1752 |
< |
}}); |
1753 |
< |
|
1754 |
< |
executor.execute(new CheckedRunnable() { |
1755 |
< |
public void realRun() throws InterruptedException { |
1756 |
< |
threadsStarted.await(); |
1757 |
< |
assertSame(one, q.take()); |
1758 |
< |
}}); |
1759 |
< |
|
1760 |
< |
joinPool(executor); |
1746 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1747 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1748 |
> |
executor.execute(new CheckedRunnable() { |
1749 |
> |
public void realRun() throws InterruptedException { |
1750 |
> |
assertFalse(q.offer(three)); |
1751 |
> |
threadsStarted.await(); |
1752 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1753 |
> |
assertEquals(0, q.remainingCapacity()); |
1754 |
> |
}}); |
1755 |
> |
|
1756 |
> |
executor.execute(new CheckedRunnable() { |
1757 |
> |
public void realRun() throws InterruptedException { |
1758 |
> |
threadsStarted.await(); |
1759 |
> |
assertSame(one, q.take()); |
1760 |
> |
}}); |
1761 |
> |
} |
1762 |
|
} |
1763 |
|
|
1764 |
|
/** |
1767 |
|
public void testPollInExecutor() { |
1768 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1769 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1770 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1771 |
< |
executor.execute(new CheckedRunnable() { |
1772 |
< |
public void realRun() throws InterruptedException { |
1773 |
< |
assertNull(q.poll()); |
1774 |
< |
threadsStarted.await(); |
1775 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1776 |
< |
checkEmpty(q); |
1777 |
< |
}}); |
1778 |
< |
|
1779 |
< |
executor.execute(new CheckedRunnable() { |
1780 |
< |
public void realRun() throws InterruptedException { |
1781 |
< |
threadsStarted.await(); |
1782 |
< |
q.put(one); |
1783 |
< |
}}); |
1784 |
< |
|
1785 |
< |
joinPool(executor); |
1770 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1771 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1772 |
> |
executor.execute(new CheckedRunnable() { |
1773 |
> |
public void realRun() throws InterruptedException { |
1774 |
> |
assertNull(q.poll()); |
1775 |
> |
threadsStarted.await(); |
1776 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1777 |
> |
checkEmpty(q); |
1778 |
> |
}}); |
1779 |
> |
|
1780 |
> |
executor.execute(new CheckedRunnable() { |
1781 |
> |
public void realRun() throws InterruptedException { |
1782 |
> |
threadsStarted.await(); |
1783 |
> |
q.put(one); |
1784 |
> |
}}); |
1785 |
> |
} |
1786 |
|
} |
1787 |
|
|
1788 |
|
/** |
1789 |
< |
* A deserialized serialized deque has same elements in same order |
1789 |
> |
* A deserialized/reserialized deque has same elements in same order |
1790 |
|
*/ |
1791 |
|
public void testSerialization() throws Exception { |
1792 |
|
Queue x = populatedDeque(SIZE); |
1793 |
|
Queue y = serialClone(x); |
1794 |
|
|
1795 |
< |
assertTrue(x != y); |
1795 |
> |
assertNotSame(y, x); |
1796 |
|
assertEquals(x.size(), y.size()); |
1797 |
|
assertEquals(x.toString(), y.toString()); |
1798 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
1834 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1835 |
|
Thread t = new Thread(new CheckedRunnable() { |
1836 |
|
public void realRun() throws InterruptedException { |
1837 |
< |
q.put(new Integer(SIZE+1)); |
1837 |
> |
q.put(new Integer(SIZE + 1)); |
1838 |
|
}}); |
1839 |
|
|
1840 |
|
t.start(); |
1858 |
|
ArrayList l = new ArrayList(); |
1859 |
|
q.drainTo(l, i); |
1860 |
|
int k = (i < SIZE) ? i : SIZE; |
1861 |
< |
assertEquals(l.size(), k); |
1862 |
< |
assertEquals(q.size(), SIZE-k); |
1861 |
> |
assertEquals(k, l.size()); |
1862 |
> |
assertEquals(SIZE - k, q.size()); |
1863 |
|
for (int j = 0; j < k; ++j) |
1864 |
|
assertEquals(l.get(j), new Integer(j)); |
1865 |
< |
while (q.poll() != null) ; |
1865 |
> |
do {} while (q.poll() != null); |
1866 |
> |
} |
1867 |
> |
} |
1868 |
> |
|
1869 |
> |
/** |
1870 |
> |
* remove(null), contains(null) always return false |
1871 |
> |
*/ |
1872 |
> |
public void testNeverContainsNull() { |
1873 |
> |
Deque<?>[] qs = { |
1874 |
> |
new LinkedBlockingDeque<Object>(), |
1875 |
> |
populatedDeque(2), |
1876 |
> |
}; |
1877 |
> |
|
1878 |
> |
for (Deque<?> q : qs) { |
1879 |
> |
assertFalse(q.contains(null)); |
1880 |
> |
assertFalse(q.remove(null)); |
1881 |
> |
assertFalse(q.removeFirstOccurrence(null)); |
1882 |
> |
assertFalse(q.removeLastOccurrence(null)); |
1883 |
|
} |
1884 |
|
} |
1885 |
|
|