585 |
|
* put blocks interruptibly if full |
586 |
|
*/ |
587 |
|
public void testBlockingPut() throws InterruptedException { |
588 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
589 |
|
Thread t = new Thread(new CheckedRunnable() { |
590 |
< |
public void realRun() { |
591 |
< |
int added = 0; |
590 |
> |
public void realRun() throws InterruptedException { |
591 |
> |
for (int i = 0; i < SIZE; ++i) |
592 |
> |
q.put(i); |
593 |
> |
assertEquals(SIZE, q.size()); |
594 |
> |
assertEquals(0, q.remainingCapacity()); |
595 |
|
try { |
596 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
597 |
< |
for (int i = 0; i < SIZE; ++i) { |
598 |
< |
q.put(new Integer(i)); |
595 |
< |
++added; |
596 |
< |
} |
597 |
< |
q.put(new Integer(SIZE)); |
598 |
< |
threadShouldThrow(); |
599 |
< |
} catch (InterruptedException success) { |
600 |
< |
threadAssertEquals(added, SIZE); |
601 |
< |
} |
596 |
> |
q.put(99); |
597 |
> |
shouldThrow(); |
598 |
> |
} catch (InterruptedException success) {} |
599 |
|
}}); |
600 |
|
|
601 |
|
t.start(); |
602 |
|
Thread.sleep(SHORT_DELAY_MS); |
603 |
|
t.interrupt(); |
604 |
|
t.join(); |
605 |
+ |
assertEquals(SIZE, q.size()); |
606 |
+ |
assertEquals(0, q.remainingCapacity()); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
|
* put blocks waiting for take when full |
611 |
|
*/ |
612 |
|
public void testPutWithTake() throws InterruptedException { |
613 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
613 |
> |
final int capacity = 2; |
614 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
615 |
|
Thread t = new Thread(new CheckedRunnable() { |
616 |
< |
public void realRun() { |
617 |
< |
int added = 0; |
616 |
> |
public void realRun() throws InterruptedException { |
617 |
> |
for (int i = 0; i < capacity + 1; i++) |
618 |
> |
q.put(i); |
619 |
|
try { |
620 |
< |
q.put(new Object()); |
621 |
< |
++added; |
622 |
< |
q.put(new Object()); |
622 |
< |
++added; |
623 |
< |
q.put(new Object()); |
624 |
< |
++added; |
625 |
< |
q.put(new Object()); |
626 |
< |
++added; |
627 |
< |
threadShouldThrow(); |
628 |
< |
} catch (InterruptedException success) { |
629 |
< |
threadAssertTrue(added >= 2); |
630 |
< |
} |
620 |
> |
q.put(99); |
621 |
> |
shouldThrow(); |
622 |
> |
} catch (InterruptedException success) {} |
623 |
|
}}); |
624 |
|
|
625 |
|
t.start(); |
626 |
|
Thread.sleep(SHORT_DELAY_MS); |
627 |
< |
q.take(); |
627 |
> |
assertEquals(q.remainingCapacity(), 0); |
628 |
> |
assertEquals(0, q.take()); |
629 |
> |
Thread.sleep(SHORT_DELAY_MS); |
630 |
|
t.interrupt(); |
631 |
|
t.join(); |
632 |
+ |
assertEquals(q.remainingCapacity(), 0); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
684 |
|
* Take removes existing elements until empty, then blocks interruptibly |
685 |
|
*/ |
686 |
|
public void testBlockingTake() throws InterruptedException { |
687 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
687 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
688 |
> |
Thread t = new Thread(new CheckedRunnable() { |
689 |
|
public void realRun() throws InterruptedException { |
694 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
690 |
|
for (int i = 0; i < SIZE; ++i) { |
691 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
692 |
|
} |
693 |
< |
q.take(); |
694 |
< |
}}; |
693 |
> |
try { |
694 |
> |
q.take(); |
695 |
> |
shouldThrow(); |
696 |
> |
} catch (InterruptedException success) {} |
697 |
> |
}}); |
698 |
|
|
699 |
|
t.start(); |
700 |
|
Thread.sleep(SHORT_DELAY_MS); |
811 |
|
* putFirst blocks interruptibly if full |
812 |
|
*/ |
813 |
|
public void testBlockingPutFirst() throws InterruptedException { |
814 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
815 |
|
Thread t = new Thread(new CheckedRunnable() { |
816 |
< |
public void realRun() { |
817 |
< |
int added = 0; |
816 |
> |
public void realRun() throws InterruptedException { |
817 |
> |
for (int i = 0; i < SIZE; ++i) |
818 |
> |
q.putFirst(i); |
819 |
> |
assertEquals(SIZE, q.size()); |
820 |
> |
assertEquals(0, q.remainingCapacity()); |
821 |
|
try { |
822 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
823 |
< |
for (int i = 0; i < SIZE; ++i) { |
824 |
< |
q.putFirst(new Integer(i)); |
823 |
< |
++added; |
824 |
< |
} |
825 |
< |
q.putFirst(new Integer(SIZE)); |
826 |
< |
threadShouldThrow(); |
827 |
< |
} catch (InterruptedException success) { |
828 |
< |
threadAssertEquals(added, SIZE); |
829 |
< |
} |
822 |
> |
q.putFirst(99); |
823 |
> |
shouldThrow(); |
824 |
> |
} catch (InterruptedException success) {} |
825 |
|
}}); |
826 |
|
|
827 |
|
t.start(); |
828 |
|
Thread.sleep(SHORT_DELAY_MS); |
829 |
|
t.interrupt(); |
830 |
|
t.join(); |
831 |
+ |
assertEquals(SIZE, q.size()); |
832 |
+ |
assertEquals(0, q.remainingCapacity()); |
833 |
|
} |
834 |
|
|
835 |
|
/** |
836 |
|
* putFirst blocks waiting for take when full |
837 |
|
*/ |
838 |
|
public void testPutFirstWithTake() throws InterruptedException { |
839 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
839 |
> |
final int capacity = 2; |
840 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
841 |
|
Thread t = new Thread(new CheckedRunnable() { |
842 |
< |
public void realRun() { |
843 |
< |
int added = 0; |
842 |
> |
public void realRun() throws InterruptedException { |
843 |
> |
for (int i = 0; i < capacity + 1; i++) |
844 |
> |
q.putFirst(i); |
845 |
|
try { |
846 |
< |
q.putFirst(new Object()); |
847 |
< |
++added; |
848 |
< |
q.putFirst(new Object()); |
850 |
< |
++added; |
851 |
< |
q.putFirst(new Object()); |
852 |
< |
++added; |
853 |
< |
q.putFirst(new Object()); |
854 |
< |
++added; |
855 |
< |
threadShouldThrow(); |
856 |
< |
} catch (InterruptedException success) { |
857 |
< |
threadAssertTrue(added >= 2); |
858 |
< |
} |
846 |
> |
q.putFirst(99); |
847 |
> |
shouldThrow(); |
848 |
> |
} catch (InterruptedException success) {} |
849 |
|
}}); |
850 |
|
|
851 |
|
t.start(); |
852 |
|
Thread.sleep(SHORT_DELAY_MS); |
853 |
< |
q.take(); |
853 |
> |
assertEquals(q.remainingCapacity(), 0); |
854 |
> |
assertEquals(capacity - 1, q.take()); |
855 |
> |
Thread.sleep(SHORT_DELAY_MS); |
856 |
|
t.interrupt(); |
857 |
|
t.join(); |
858 |
+ |
assertEquals(q.remainingCapacity(), 0); |
859 |
|
} |
860 |
|
|
861 |
|
/** |
910 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
911 |
|
*/ |
912 |
|
public void testBlockingTakeFirst() throws InterruptedException { |
913 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
913 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
914 |
> |
Thread t = new Thread(new CheckedRunnable() { |
915 |
|
public void realRun() throws InterruptedException { |
916 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
917 |
< |
for (int i = 0; i < SIZE; ++i) { |
918 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
919 |
< |
} |
920 |
< |
q.takeFirst(); |
921 |
< |
}}; |
916 |
> |
for (int i = 0; i < SIZE; ++i) |
917 |
> |
assertEquals(i, q.takeFirst()); |
918 |
> |
try { |
919 |
> |
q.takeFirst(); |
920 |
> |
shouldThrow(); |
921 |
> |
} catch (InterruptedException success) {} |
922 |
> |
}}); |
923 |
|
|
924 |
|
t.start(); |
925 |
|
Thread.sleep(SHORT_DELAY_MS); |
1024 |
|
* putLast blocks interruptibly if full |
1025 |
|
*/ |
1026 |
|
public void testBlockingPutLast() throws InterruptedException { |
1027 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1028 |
|
Thread t = new Thread(new CheckedRunnable() { |
1029 |
< |
public void realRun() { |
1030 |
< |
int added = 0; |
1029 |
> |
public void realRun() throws InterruptedException { |
1030 |
> |
for (int i = 0; i < SIZE; ++i) |
1031 |
> |
q.putLast(i); |
1032 |
> |
assertEquals(SIZE, q.size()); |
1033 |
> |
assertEquals(0, q.remainingCapacity()); |
1034 |
|
try { |
1035 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1036 |
< |
for (int i = 0; i < SIZE; ++i) { |
1037 |
< |
q.putLast(new Integer(i)); |
1039 |
< |
++added; |
1040 |
< |
} |
1041 |
< |
q.putLast(new Integer(SIZE)); |
1042 |
< |
threadShouldThrow(); |
1043 |
< |
} catch (InterruptedException success) { |
1044 |
< |
threadAssertEquals(added, SIZE); |
1045 |
< |
} |
1035 |
> |
q.putLast(99); |
1036 |
> |
shouldThrow(); |
1037 |
> |
} catch (InterruptedException success) {} |
1038 |
|
}}); |
1039 |
|
|
1040 |
|
t.start(); |
1041 |
|
Thread.sleep(SHORT_DELAY_MS); |
1042 |
|
t.interrupt(); |
1043 |
|
t.join(); |
1044 |
+ |
assertEquals(SIZE, q.size()); |
1045 |
+ |
assertEquals(0, q.remainingCapacity()); |
1046 |
|
} |
1047 |
|
|
1048 |
|
/** |
1049 |
|
* putLast blocks waiting for take when full |
1050 |
|
*/ |
1051 |
|
public void testPutLastWithTake() throws InterruptedException { |
1052 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1052 |
> |
final int capacity = 2; |
1053 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1054 |
|
Thread t = new Thread(new CheckedRunnable() { |
1055 |
< |
public void realRun() { |
1056 |
< |
int added = 0; |
1055 |
> |
public void realRun() throws InterruptedException { |
1056 |
> |
for (int i = 0; i < capacity + 1; i++) |
1057 |
> |
q.putLast(i); |
1058 |
|
try { |
1059 |
< |
q.putLast(new Object()); |
1060 |
< |
++added; |
1061 |
< |
q.putLast(new Object()); |
1066 |
< |
++added; |
1067 |
< |
q.putLast(new Object()); |
1068 |
< |
++added; |
1069 |
< |
q.putLast(new Object()); |
1070 |
< |
++added; |
1071 |
< |
threadShouldThrow(); |
1072 |
< |
} catch (InterruptedException success) { |
1073 |
< |
threadAssertTrue(added >= 2); |
1074 |
< |
} |
1059 |
> |
q.putLast(99); |
1060 |
> |
shouldThrow(); |
1061 |
> |
} catch (InterruptedException success) {} |
1062 |
|
}}); |
1063 |
|
|
1064 |
|
t.start(); |
1065 |
|
Thread.sleep(SHORT_DELAY_MS); |
1066 |
< |
q.take(); |
1066 |
> |
assertEquals(q.remainingCapacity(), 0); |
1067 |
> |
assertEquals(0, q.take()); |
1068 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1069 |
|
t.interrupt(); |
1070 |
|
t.join(); |
1071 |
+ |
assertEquals(q.remainingCapacity(), 0); |
1072 |
|
} |
1073 |
|
|
1074 |
|
/** |
1123 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1124 |
|
*/ |
1125 |
|
public void testBlockingTakeLast() throws InterruptedException { |
1126 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1126 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1127 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1128 |
|
public void realRun() throws InterruptedException { |
1129 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1130 |
< |
for (int i = 0; i < SIZE; ++i) { |
1131 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1132 |
< |
} |
1133 |
< |
q.takeLast(); |
1134 |
< |
}}; |
1129 |
> |
for (int i = 0; i < SIZE; ++i) |
1130 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1131 |
> |
try { |
1132 |
> |
q.takeLast(); |
1133 |
> |
shouldThrow(); |
1134 |
> |
} catch (InterruptedException success) {} |
1135 |
> |
}}); |
1136 |
|
|
1137 |
|
t.start(); |
1138 |
|
Thread.sleep(SHORT_DELAY_MS); |
1140 |
|
t.join(); |
1141 |
|
} |
1142 |
|
|
1151 |
– |
|
1143 |
|
/** |
1144 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1145 |
|
*/ |