1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
13 |
|
import java.io.ObjectOutputStream; |
14 |
|
import java.util.ArrayList; |
15 |
|
import java.util.Arrays; |
16 |
+ |
import java.util.Collection; |
17 |
|
import java.util.Iterator; |
18 |
|
import java.util.List; |
19 |
|
import java.util.NoSuchElementException; |
20 |
< |
import java.util.concurrent.*; |
20 |
> |
import java.util.concurrent.BlockingQueue; |
21 |
> |
import java.util.concurrent.CountDownLatch; |
22 |
> |
import java.util.concurrent.Executors; |
23 |
> |
import java.util.concurrent.ExecutorService; |
24 |
> |
import java.util.concurrent.LinkedTransferQueue; |
25 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
26 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
27 |
|
import junit.framework.Test; |
45 |
|
new Generic().testSuite()); |
46 |
|
} |
47 |
|
|
43 |
– |
void checkEmpty(BlockingQueue q) { |
44 |
– |
try { |
45 |
– |
assertTrue(q.isEmpty()); |
46 |
– |
assertEquals(0, q.size()); |
47 |
– |
assertNull(q.peek()); |
48 |
– |
assertNull(q.poll()); |
49 |
– |
assertNull(q.poll(0, MILLISECONDS)); |
50 |
– |
assertEquals(q.toString(), "[]"); |
51 |
– |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
52 |
– |
assertFalse(q.iterator().hasNext()); |
53 |
– |
try { |
54 |
– |
q.element(); |
55 |
– |
shouldThrow(); |
56 |
– |
} catch (NoSuchElementException success) {} |
57 |
– |
try { |
58 |
– |
q.iterator().next(); |
59 |
– |
shouldThrow(); |
60 |
– |
} catch (NoSuchElementException success) {} |
61 |
– |
try { |
62 |
– |
q.remove(); |
63 |
– |
shouldThrow(); |
64 |
– |
} catch (NoSuchElementException success) {} |
65 |
– |
} catch (InterruptedException ie) { |
66 |
– |
threadUnexpectedException(ie); |
67 |
– |
} |
68 |
– |
} |
69 |
– |
|
48 |
|
/** |
49 |
|
* Constructor builds new queue with size being zero and empty |
50 |
|
* being true |
70 |
|
* NullPointerException |
71 |
|
*/ |
72 |
|
public void testConstructor3() { |
73 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
74 |
|
try { |
75 |
< |
Integer[] ints = new Integer[SIZE]; |
97 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
75 |
> |
new LinkedTransferQueue(elements); |
76 |
|
shouldThrow(); |
77 |
|
} catch (NullPointerException success) {} |
78 |
|
} |
82 |
|
* throws NullPointerException |
83 |
|
*/ |
84 |
|
public void testConstructor4() { |
85 |
+ |
Integer[] ints = new Integer[SIZE]; |
86 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
87 |
+ |
ints[i] = i; |
88 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
89 |
|
try { |
90 |
< |
Integer[] ints = new Integer[SIZE]; |
109 |
< |
for (int i = 0; i < SIZE - 1; ++i) { |
110 |
< |
ints[i] = i; |
111 |
< |
} |
112 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
90 |
> |
new LinkedTransferQueue(elements); |
91 |
|
shouldThrow(); |
92 |
|
} catch (NullPointerException success) {} |
93 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
/** |
159 |
– |
* offer(null) throws NullPointerException |
160 |
– |
*/ |
161 |
– |
public void testOfferNull() { |
162 |
– |
try { |
163 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
164 |
– |
q.offer(null); |
165 |
– |
shouldThrow(); |
166 |
– |
} catch (NullPointerException success) {} |
167 |
– |
} |
168 |
– |
|
169 |
– |
/** |
170 |
– |
* add(null) throws NullPointerException |
171 |
– |
*/ |
172 |
– |
public void testAddNull() { |
173 |
– |
try { |
174 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
175 |
– |
q.add(null); |
176 |
– |
shouldThrow(); |
177 |
– |
} catch (NullPointerException success) {} |
178 |
– |
} |
179 |
– |
|
180 |
– |
/** |
181 |
– |
* addAll(null) throws NullPointerException |
182 |
– |
*/ |
183 |
– |
public void testAddAll1() { |
184 |
– |
try { |
185 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
186 |
– |
q.addAll(null); |
187 |
– |
shouldThrow(); |
188 |
– |
} catch (NullPointerException success) {} |
189 |
– |
} |
190 |
– |
|
191 |
– |
/** |
137 |
|
* addAll(this) throws IllegalArgumentException |
138 |
|
*/ |
139 |
|
public void testAddAllSelf() { |
145 |
|
} |
146 |
|
|
147 |
|
/** |
203 |
– |
* addAll of a collection with null elements throws NullPointerException |
204 |
– |
*/ |
205 |
– |
public void testAddAll2() { |
206 |
– |
try { |
207 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
208 |
– |
Integer[] ints = new Integer[SIZE]; |
209 |
– |
q.addAll(Arrays.asList(ints)); |
210 |
– |
shouldThrow(); |
211 |
– |
} catch (NullPointerException success) {} |
212 |
– |
} |
213 |
– |
|
214 |
– |
/** |
148 |
|
* addAll of a collection with any null elements throws |
149 |
|
* NullPointerException after possibly adding some elements |
150 |
|
*/ |
178 |
|
} |
179 |
|
|
180 |
|
/** |
248 |
– |
* put(null) throws NullPointerException |
249 |
– |
*/ |
250 |
– |
public void testPutNull() throws InterruptedException { |
251 |
– |
try { |
252 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
253 |
– |
q.put(null); |
254 |
– |
shouldThrow(); |
255 |
– |
} catch (NullPointerException success) {} |
256 |
– |
} |
257 |
– |
|
258 |
– |
/** |
181 |
|
* all elements successfully put are contained |
182 |
|
*/ |
183 |
|
public void testPut() { |
203 |
|
* take removes existing elements until empty, then blocks interruptibly |
204 |
|
*/ |
205 |
|
public void testBlockingTake() throws InterruptedException { |
206 |
< |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
207 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
206 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
207 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
208 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
209 |
|
public void realRun() throws InterruptedException { |
210 |
|
for (int i = 0; i < SIZE; ++i) { |
211 |
< |
assertEquals(i, (int) q.take()); |
211 |
> |
assertEquals(i, q.take()); |
212 |
|
} |
213 |
< |
aboutToWait.countDown(); |
213 |
> |
|
214 |
> |
Thread.currentThread().interrupt(); |
215 |
> |
try { |
216 |
> |
q.take(); |
217 |
> |
shouldThrow(); |
218 |
> |
} catch (InterruptedException success) {} |
219 |
> |
assertFalse(Thread.interrupted()); |
220 |
> |
|
221 |
> |
pleaseInterrupt.countDown(); |
222 |
|
try { |
223 |
|
q.take(); |
224 |
|
shouldThrow(); |
225 |
|
} catch (InterruptedException success) {} |
226 |
+ |
assertFalse(Thread.interrupted()); |
227 |
|
}}); |
228 |
|
|
229 |
< |
aboutToWait.await(); |
230 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
229 |
> |
await(pleaseInterrupt); |
230 |
> |
assertThreadStaysAlive(t); |
231 |
|
t.interrupt(); |
232 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
302 |
< |
checkEmpty(q); |
232 |
> |
awaitTermination(t); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
262 |
|
public void testTimedPoll() throws InterruptedException { |
263 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
264 |
|
for (int i = 0; i < SIZE; ++i) { |
265 |
< |
long t0 = System.nanoTime(); |
266 |
< |
assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
267 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
268 |
< |
} |
269 |
< |
long t0 = System.nanoTime(); |
270 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
271 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
265 |
> |
long startTime = System.nanoTime(); |
266 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
267 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
268 |
> |
} |
269 |
> |
long startTime = System.nanoTime(); |
270 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
271 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
272 |
|
checkEmpty(q); |
273 |
|
} |
274 |
|
|
378 |
|
*/ |
379 |
|
public void testRemoveElement() throws InterruptedException { |
380 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
381 |
< |
for (int i = 1; i < SIZE; i += 2) { |
381 |
> |
for (int i = 1; i < SIZE; i+=2) { |
382 |
> |
assertTrue(q.contains(i)); |
383 |
|
assertTrue(q.remove(i)); |
384 |
+ |
assertFalse(q.contains(i)); |
385 |
+ |
assertTrue(q.contains(i-1)); |
386 |
|
} |
387 |
< |
for (int i = 0; i < SIZE; i += 2) { |
387 |
> |
for (int i = 0; i < SIZE; i+=2) { |
388 |
> |
assertTrue(q.contains(i)); |
389 |
|
assertTrue(q.remove(i)); |
390 |
< |
assertFalse(q.remove(i + 1)); |
390 |
> |
assertFalse(q.contains(i)); |
391 |
> |
assertFalse(q.remove(i+1)); |
392 |
> |
assertFalse(q.contains(i+1)); |
393 |
|
} |
394 |
|
checkEmpty(q); |
395 |
|
} |
510 |
|
} |
511 |
|
|
512 |
|
/** |
577 |
– |
* toArray(null) throws NullPointerException |
578 |
– |
*/ |
579 |
– |
public void testToArray_NullArg() { |
580 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
581 |
– |
try { |
582 |
– |
q.toArray(null); |
583 |
– |
shouldThrow(); |
584 |
– |
} catch (NullPointerException success) {} |
585 |
– |
} |
586 |
– |
|
587 |
– |
/** |
513 |
|
* toArray(incompatible array type) throws ArrayStoreException |
514 |
|
*/ |
515 |
|
public void testToArray1_BadArg() { |
592 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
593 |
|
String s = q.toString(); |
594 |
|
for (int i = 0; i < SIZE; ++i) { |
595 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
595 |
> |
assertTrue(s.contains(String.valueOf(i))); |
596 |
|
} |
597 |
|
} |
598 |
|
|
601 |
|
*/ |
602 |
|
public void testOfferInExecutor() { |
603 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
604 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
604 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
605 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
606 |
|
|
607 |
|
executor.execute(new CheckedRunnable() { |
608 |
|
public void realRun() throws InterruptedException { |
684 |
– |
threadsStarted.countDown(); |
609 |
|
threadsStarted.await(); |
610 |
< |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
610 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
611 |
|
}}); |
612 |
|
|
613 |
|
executor.execute(new CheckedRunnable() { |
614 |
|
public void realRun() throws InterruptedException { |
691 |
– |
threadsStarted.countDown(); |
615 |
|
threadsStarted.await(); |
616 |
|
assertSame(one, q.take()); |
617 |
|
checkEmpty(q); |
625 |
|
*/ |
626 |
|
public void testPollInExecutor() { |
627 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
628 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
628 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
629 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
630 |
|
|
631 |
|
executor.execute(new CheckedRunnable() { |
632 |
|
public void realRun() throws InterruptedException { |
633 |
|
assertNull(q.poll()); |
711 |
– |
threadsStarted.countDown(); |
634 |
|
threadsStarted.await(); |
635 |
< |
assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
635 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
636 |
|
checkEmpty(q); |
637 |
|
}}); |
638 |
|
|
639 |
|
executor.execute(new CheckedRunnable() { |
640 |
|
public void realRun() throws InterruptedException { |
719 |
– |
threadsStarted.countDown(); |
641 |
|
threadsStarted.await(); |
642 |
|
q.put(one); |
643 |
|
}}); |
672 |
|
} |
673 |
|
|
674 |
|
/** |
754 |
– |
* drainTo(null) throws NullPointerException |
755 |
– |
*/ |
756 |
– |
public void testDrainToNull() { |
757 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
758 |
– |
try { |
759 |
– |
q.drainTo(null); |
760 |
– |
shouldThrow(); |
761 |
– |
} catch (NullPointerException success) {} |
762 |
– |
} |
763 |
– |
|
764 |
– |
/** |
765 |
– |
* drainTo(this) throws IllegalArgumentException |
766 |
– |
*/ |
767 |
– |
public void testDrainToSelf() { |
768 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
769 |
– |
try { |
770 |
– |
q.drainTo(q); |
771 |
– |
shouldThrow(); |
772 |
– |
} catch (IllegalArgumentException success) {} |
773 |
– |
} |
774 |
– |
|
775 |
– |
/** |
675 |
|
* drainTo(c) empties queue into another collection c |
676 |
|
*/ |
677 |
|
public void testDrainTo() { |
717 |
|
} |
718 |
|
|
719 |
|
/** |
821 |
– |
* drainTo(null, n) throws NullPointerException |
822 |
– |
*/ |
823 |
– |
public void testDrainToNullN() { |
824 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
825 |
– |
try { |
826 |
– |
q.drainTo(null, SIZE); |
827 |
– |
shouldThrow(); |
828 |
– |
} catch (NullPointerException success) {} |
829 |
– |
} |
830 |
– |
|
831 |
– |
/** |
832 |
– |
* drainTo(this, n) throws IllegalArgumentException |
833 |
– |
*/ |
834 |
– |
public void testDrainToSelfN() { |
835 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
836 |
– |
try { |
837 |
– |
q.drainTo(q, SIZE); |
838 |
– |
shouldThrow(); |
839 |
– |
} catch (IllegalArgumentException success) {} |
840 |
– |
} |
841 |
– |
|
842 |
– |
/** |
720 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
721 |
|
*/ |
722 |
|
public void testDrainToN() { |
949 |
|
} |
950 |
|
|
951 |
|
/** |
952 |
< |
* tryTransfer waits the amount given if interrupted, and |
1076 |
< |
* throws interrupted exception |
952 |
> |
* tryTransfer blocks interruptibly if no takers |
953 |
|
*/ |
954 |
|
public void testTryTransfer5() throws InterruptedException { |
955 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
956 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
956 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
957 |
> |
assertTrue(q.isEmpty()); |
958 |
|
|
959 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
960 |
|
public void realRun() throws InterruptedException { |
961 |
< |
long t0 = System.nanoTime(); |
1085 |
< |
threadStarted.countDown(); |
961 |
> |
Thread.currentThread().interrupt(); |
962 |
|
try { |
963 |
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
964 |
|
shouldThrow(); |
965 |
|
} catch (InterruptedException success) {} |
966 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
966 |
> |
assertFalse(Thread.interrupted()); |
967 |
> |
|
968 |
> |
pleaseInterrupt.countDown(); |
969 |
> |
try { |
970 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
971 |
> |
shouldThrow(); |
972 |
> |
} catch (InterruptedException success) {} |
973 |
> |
assertFalse(Thread.interrupted()); |
974 |
|
}}); |
975 |
|
|
976 |
< |
threadStarted.await(); |
977 |
< |
Thread.sleep(SHORT_DELAY_MS); |
976 |
> |
await(pleaseInterrupt); |
977 |
> |
assertThreadStaysAlive(t); |
978 |
|
t.interrupt(); |
979 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
979 |
> |
awaitTermination(t); |
980 |
|
checkEmpty(q); |
981 |
|
} |
982 |
|
|
990 |
|
public void realRun() throws InterruptedException { |
991 |
|
long t0 = System.nanoTime(); |
992 |
|
assertFalse(q.tryTransfer(new Object(), |
993 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
994 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
993 |
> |
timeoutMillis(), MILLISECONDS)); |
994 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
995 |
|
checkEmpty(q); |
996 |
|
}}); |
997 |
|
|
998 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
998 |
> |
awaitTermination(t); |
999 |
|
checkEmpty(q); |
1000 |
|
} |
1001 |
|
|
1023 |
|
} |
1024 |
|
|
1025 |
|
/** |
1026 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1027 |
< |
* false not enqueueing and the successive poll is null |
1026 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1027 |
> |
* returning false not enqueueing and the successive poll is null |
1028 |
|
*/ |
1029 |
|
public void testTryTransfer8() throws InterruptedException { |
1030 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1031 |
|
assertTrue(q.offer(four)); |
1032 |
|
assertEquals(1, q.size()); |
1033 |
|
long t0 = System.nanoTime(); |
1034 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1035 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1034 |
> |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1035 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1036 |
|
assertEquals(1, q.size()); |
1037 |
|
assertSame(four, q.poll()); |
1038 |
|
assertNull(q.poll()); |