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 |
5 |
* Other contributors include John Vint |
6 |
*/ |
7 |
|
8 |
import java.io.BufferedInputStream; |
9 |
import java.io.BufferedOutputStream; |
10 |
import java.io.ByteArrayInputStream; |
11 |
import java.io.ByteArrayOutputStream; |
12 |
import java.io.ObjectInputStream; |
13 |
import java.io.ObjectOutputStream; |
14 |
import java.util.ArrayList; |
15 |
import java.util.Arrays; |
16 |
import java.util.Iterator; |
17 |
import java.util.List; |
18 |
import java.util.NoSuchElementException; |
19 |
import java.util.concurrent.*; |
20 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
22 |
import junit.framework.Test; |
23 |
import junit.framework.TestSuite; |
24 |
|
25 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
|
28 |
public static class Generic extends BlockingQueueTest { |
29 |
protected BlockingQueue emptyCollection() { |
30 |
return new LinkedTransferQueue(); |
31 |
} |
32 |
} |
33 |
|
34 |
public static void main(String[] args) { |
35 |
junit.textui.TestRunner.run(suite()); |
36 |
} |
37 |
|
38 |
public static Test suite() { |
39 |
return newTestSuite(LinkedTransferQueueTest.class, |
40 |
new Generic().testSuite()); |
41 |
} |
42 |
|
43 |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
44 |
assertTrue(q.isEmpty()); |
45 |
assertEquals(0, q.size()); |
46 |
assertNull(q.peek()); |
47 |
assertNull(q.poll()); |
48 |
assertNull(q.poll(0, MILLISECONDS)); |
49 |
assertEquals(q.toString(), "[]"); |
50 |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
51 |
assertFalse(q.iterator().hasNext()); |
52 |
try { |
53 |
q.element(); |
54 |
shouldThrow(); |
55 |
} catch (NoSuchElementException success) {} |
56 |
try { |
57 |
q.iterator().next(); |
58 |
shouldThrow(); |
59 |
} catch (NoSuchElementException success) {} |
60 |
try { |
61 |
q.remove(); |
62 |
shouldThrow(); |
63 |
} catch (NoSuchElementException success) {} |
64 |
} |
65 |
|
66 |
/** |
67 |
* Constructor builds new queue with size being zero and empty |
68 |
* being true |
69 |
*/ |
70 |
public void testConstructor1() { |
71 |
assertEquals(0, new LinkedTransferQueue().size()); |
72 |
assertTrue(new LinkedTransferQueue().isEmpty()); |
73 |
} |
74 |
|
75 |
/** |
76 |
* Initializing constructor with null collection throws |
77 |
* NullPointerException |
78 |
*/ |
79 |
public void testConstructor2() { |
80 |
try { |
81 |
new LinkedTransferQueue(null); |
82 |
shouldThrow(); |
83 |
} catch (NullPointerException success) {} |
84 |
} |
85 |
|
86 |
/** |
87 |
* Initializing from Collection of null elements throws |
88 |
* NullPointerException |
89 |
*/ |
90 |
public void testConstructor3() { |
91 |
try { |
92 |
Integer[] ints = new Integer[SIZE]; |
93 |
new LinkedTransferQueue(Arrays.asList(ints)); |
94 |
shouldThrow(); |
95 |
} catch (NullPointerException success) {} |
96 |
} |
97 |
|
98 |
/** |
99 |
* Initializing constructor with a collection containing some null elements |
100 |
* throws NullPointerException |
101 |
*/ |
102 |
public void testConstructor4() { |
103 |
try { |
104 |
Integer[] ints = new Integer[SIZE]; |
105 |
for (int i = 0; i < SIZE - 1; ++i) { |
106 |
ints[i] = i; |
107 |
} |
108 |
new LinkedTransferQueue(Arrays.asList(ints)); |
109 |
shouldThrow(); |
110 |
} catch (NullPointerException success) {} |
111 |
} |
112 |
|
113 |
/** |
114 |
* Queue contains all elements of the collection it is initialized by |
115 |
*/ |
116 |
public void testConstructor5() { |
117 |
Integer[] ints = new Integer[SIZE]; |
118 |
for (int i = 0; i < SIZE; ++i) { |
119 |
ints[i] = i; |
120 |
} |
121 |
List intList = Arrays.asList(ints); |
122 |
LinkedTransferQueue q |
123 |
= new LinkedTransferQueue(intList); |
124 |
assertEquals(q.size(), intList.size()); |
125 |
assertEquals(q.toString(), intList.toString()); |
126 |
assertTrue(Arrays.equals(q.toArray(), |
127 |
intList.toArray())); |
128 |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
129 |
intList.toArray(new Object[0]))); |
130 |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
131 |
intList.toArray(new Object[SIZE]))); |
132 |
for (int i = 0; i < SIZE; ++i) { |
133 |
assertEquals(ints[i], q.poll()); |
134 |
} |
135 |
} |
136 |
|
137 |
/** |
138 |
* remainingCapacity() always returns Integer.MAX_VALUE |
139 |
*/ |
140 |
public void testRemainingCapacity() { |
141 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
142 |
for (int i = 0; i < SIZE; ++i) { |
143 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
144 |
assertEquals(SIZE - i, q.size()); |
145 |
q.remove(); |
146 |
} |
147 |
for (int i = 0; i < SIZE; ++i) { |
148 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
149 |
assertEquals(i, q.size()); |
150 |
q.add(i); |
151 |
} |
152 |
} |
153 |
|
154 |
/** |
155 |
* offer(null) throws NullPointerException |
156 |
*/ |
157 |
public void testOfferNull() { |
158 |
try { |
159 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
160 |
q.offer(null); |
161 |
shouldThrow(); |
162 |
} catch (NullPointerException success) {} |
163 |
} |
164 |
|
165 |
/** |
166 |
* add(null) throws NullPointerException |
167 |
*/ |
168 |
public void testAddNull() { |
169 |
try { |
170 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
171 |
q.add(null); |
172 |
shouldThrow(); |
173 |
} catch (NullPointerException success) {} |
174 |
} |
175 |
|
176 |
/** |
177 |
* addAll(null) throws NullPointerException |
178 |
*/ |
179 |
public void testAddAll1() { |
180 |
try { |
181 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
182 |
q.addAll(null); |
183 |
shouldThrow(); |
184 |
} catch (NullPointerException success) {} |
185 |
} |
186 |
|
187 |
/** |
188 |
* addAll(this) throws IllegalArgumentException |
189 |
*/ |
190 |
public void testAddAllSelf() { |
191 |
try { |
192 |
LinkedTransferQueue q = populatedQueue(SIZE); |
193 |
q.addAll(q); |
194 |
shouldThrow(); |
195 |
} catch (IllegalArgumentException success) {} |
196 |
} |
197 |
|
198 |
/** |
199 |
* addAll of a collection with null elements throws NullPointerException |
200 |
*/ |
201 |
public void testAddAll2() { |
202 |
try { |
203 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
204 |
Integer[] ints = new Integer[SIZE]; |
205 |
q.addAll(Arrays.asList(ints)); |
206 |
shouldThrow(); |
207 |
} catch (NullPointerException success) {} |
208 |
} |
209 |
|
210 |
/** |
211 |
* addAll of a collection with any null elements throws |
212 |
* NullPointerException after possibly adding some elements |
213 |
*/ |
214 |
public void testAddAll3() { |
215 |
try { |
216 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
217 |
Integer[] ints = new Integer[SIZE]; |
218 |
for (int i = 0; i < SIZE - 1; ++i) { |
219 |
ints[i] = i; |
220 |
} |
221 |
q.addAll(Arrays.asList(ints)); |
222 |
shouldThrow(); |
223 |
} catch (NullPointerException success) {} |
224 |
} |
225 |
|
226 |
/** |
227 |
* Queue contains all elements, in traversal order, of successful addAll |
228 |
*/ |
229 |
public void testAddAll5() { |
230 |
Integer[] empty = new Integer[0]; |
231 |
Integer[] ints = new Integer[SIZE]; |
232 |
for (int i = 0; i < SIZE; ++i) { |
233 |
ints[i] = i; |
234 |
} |
235 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
236 |
assertFalse(q.addAll(Arrays.asList(empty))); |
237 |
assertTrue(q.addAll(Arrays.asList(ints))); |
238 |
for (int i = 0; i < SIZE; ++i) { |
239 |
assertEquals(ints[i], q.poll()); |
240 |
} |
241 |
} |
242 |
|
243 |
/** |
244 |
* put(null) throws NullPointerException |
245 |
*/ |
246 |
public void testPutNull() throws InterruptedException { |
247 |
try { |
248 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
249 |
q.put(null); |
250 |
shouldThrow(); |
251 |
} catch (NullPointerException success) {} |
252 |
} |
253 |
|
254 |
/** |
255 |
* all elements successfully put are contained |
256 |
*/ |
257 |
public void testPut() { |
258 |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
259 |
for (int i = 0; i < SIZE; ++i) { |
260 |
assertEquals(q.size(), i); |
261 |
q.put(i); |
262 |
assertTrue(q.contains(i)); |
263 |
} |
264 |
} |
265 |
|
266 |
/** |
267 |
* take retrieves elements in FIFO order |
268 |
*/ |
269 |
public void testTake() throws InterruptedException { |
270 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
271 |
for (int i = 0; i < SIZE; ++i) { |
272 |
assertEquals(i, (int) q.take()); |
273 |
} |
274 |
} |
275 |
|
276 |
/** |
277 |
* take removes existing elements until empty, then blocks interruptibly |
278 |
*/ |
279 |
public void testBlockingTake() throws InterruptedException { |
280 |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
281 |
Thread t = new Thread(new CheckedRunnable() { |
282 |
public void realRun() throws InterruptedException { |
283 |
for (int i = 0; i < SIZE; ++i) { |
284 |
assertEquals(i, (int) q.take()); |
285 |
} |
286 |
try { |
287 |
q.take(); |
288 |
shouldThrow(); |
289 |
} catch (InterruptedException success) {} |
290 |
}}); |
291 |
|
292 |
t.start(); |
293 |
Thread.sleep(SHORT_DELAY_MS); |
294 |
t.interrupt(); |
295 |
awaitTermination(t, MEDIUM_DELAY_MS); |
296 |
checkEmpty(q); |
297 |
} |
298 |
|
299 |
/** |
300 |
* poll succeeds unless empty |
301 |
*/ |
302 |
public void testPoll() throws InterruptedException { |
303 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
304 |
for (int i = 0; i < SIZE; ++i) { |
305 |
assertEquals(i, (int) q.poll()); |
306 |
} |
307 |
assertNull(q.poll()); |
308 |
checkEmpty(q); |
309 |
} |
310 |
|
311 |
/** |
312 |
* timed poll with zero timeout succeeds when non-empty, else times out |
313 |
*/ |
314 |
public void testTimedPoll0() throws InterruptedException { |
315 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
316 |
for (int i = 0; i < SIZE; ++i) { |
317 |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
318 |
} |
319 |
assertNull(q.poll(0, MILLISECONDS)); |
320 |
checkEmpty(q); |
321 |
} |
322 |
|
323 |
/** |
324 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
325 |
*/ |
326 |
public void testTimedPoll() throws InterruptedException { |
327 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
328 |
for (int i = 0; i < SIZE; ++i) { |
329 |
long t0 = System.nanoTime(); |
330 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
331 |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
332 |
} |
333 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
334 |
checkEmpty(q); |
335 |
} |
336 |
|
337 |
/** |
338 |
* Interrupted timed poll throws InterruptedException instead of |
339 |
* returning timeout status |
340 |
*/ |
341 |
public void testInterruptedTimedPoll() throws InterruptedException { |
342 |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
343 |
Thread t = newStartedThread(new CheckedRunnable() { |
344 |
public void realRun() throws InterruptedException { |
345 |
for (int i = 0; i < SIZE; ++i) { |
346 |
long t0 = System.nanoTime(); |
347 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
348 |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
349 |
} |
350 |
try { |
351 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
352 |
shouldThrow(); |
353 |
} catch (InterruptedException success) {} |
354 |
}}); |
355 |
|
356 |
Thread.sleep(SMALL_DELAY_MS); |
357 |
t.interrupt(); |
358 |
awaitTermination(t, MEDIUM_DELAY_MS); |
359 |
checkEmpty(q); |
360 |
} |
361 |
|
362 |
/** |
363 |
* peek returns next element, or null if empty |
364 |
*/ |
365 |
public void testPeek() throws InterruptedException { |
366 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
367 |
for (int i = 0; i < SIZE; ++i) { |
368 |
assertEquals(i, (int) q.peek()); |
369 |
assertEquals(i, (int) q.poll()); |
370 |
assertTrue(q.peek() == null || |
371 |
i != (int) q.peek()); |
372 |
} |
373 |
assertNull(q.peek()); |
374 |
checkEmpty(q); |
375 |
} |
376 |
|
377 |
/** |
378 |
* element returns next element, or throws NoSuchElementException if empty |
379 |
*/ |
380 |
public void testElement() throws InterruptedException { |
381 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
382 |
for (int i = 0; i < SIZE; ++i) { |
383 |
assertEquals(i, (int) q.element()); |
384 |
assertEquals(i, (int) q.poll()); |
385 |
} |
386 |
try { |
387 |
q.element(); |
388 |
shouldThrow(); |
389 |
} catch (NoSuchElementException success) {} |
390 |
checkEmpty(q); |
391 |
} |
392 |
|
393 |
/** |
394 |
* remove removes next element, or throws NoSuchElementException if empty |
395 |
*/ |
396 |
public void testRemove() throws InterruptedException { |
397 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
398 |
for (int i = 0; i < SIZE; ++i) { |
399 |
assertEquals(i, (int) q.remove()); |
400 |
} |
401 |
try { |
402 |
q.remove(); |
403 |
shouldThrow(); |
404 |
} catch (NoSuchElementException success) {} |
405 |
checkEmpty(q); |
406 |
} |
407 |
|
408 |
/** |
409 |
* remove(x) removes x and returns true if present |
410 |
*/ |
411 |
public void testRemoveElement() throws InterruptedException { |
412 |
LinkedTransferQueue q = populatedQueue(SIZE); |
413 |
for (int i = 1; i < SIZE; i += 2) { |
414 |
assertTrue(q.remove(i)); |
415 |
} |
416 |
for (int i = 0; i < SIZE; i += 2) { |
417 |
assertTrue(q.remove(i)); |
418 |
assertFalse(q.remove(i + 1)); |
419 |
} |
420 |
checkEmpty(q); |
421 |
} |
422 |
|
423 |
/** |
424 |
* An add following remove(x) succeeds |
425 |
*/ |
426 |
public void testRemoveElementAndAdd() throws InterruptedException { |
427 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
428 |
assertTrue(q.add(one)); |
429 |
assertTrue(q.add(two)); |
430 |
assertTrue(q.remove(one)); |
431 |
assertTrue(q.remove(two)); |
432 |
assertTrue(q.add(three)); |
433 |
assertSame(q.take(), three); |
434 |
} |
435 |
|
436 |
/** |
437 |
* contains(x) reports true when elements added but not yet removed |
438 |
*/ |
439 |
public void testContains() { |
440 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
441 |
for (int i = 0; i < SIZE; ++i) { |
442 |
assertTrue(q.contains(i)); |
443 |
assertEquals(i, (int) q.poll()); |
444 |
assertFalse(q.contains(i)); |
445 |
} |
446 |
} |
447 |
|
448 |
/** |
449 |
* clear removes all elements |
450 |
*/ |
451 |
public void testClear() throws InterruptedException { |
452 |
LinkedTransferQueue q = populatedQueue(SIZE); |
453 |
q.clear(); |
454 |
checkEmpty(q); |
455 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
456 |
q.add(one); |
457 |
assertFalse(q.isEmpty()); |
458 |
assertEquals(1, q.size()); |
459 |
assertTrue(q.contains(one)); |
460 |
q.clear(); |
461 |
checkEmpty(q); |
462 |
} |
463 |
|
464 |
/** |
465 |
* containsAll(c) is true when c contains a subset of elements |
466 |
*/ |
467 |
public void testContainsAll() { |
468 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
469 |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
470 |
for (int i = 0; i < SIZE; ++i) { |
471 |
assertTrue(q.containsAll(p)); |
472 |
assertFalse(p.containsAll(q)); |
473 |
p.add(i); |
474 |
} |
475 |
assertTrue(p.containsAll(q)); |
476 |
} |
477 |
|
478 |
/** |
479 |
* retainAll(c) retains only those elements of c and reports true |
480 |
* if changed |
481 |
*/ |
482 |
public void testRetainAll() { |
483 |
LinkedTransferQueue q = populatedQueue(SIZE); |
484 |
LinkedTransferQueue p = populatedQueue(SIZE); |
485 |
for (int i = 0; i < SIZE; ++i) { |
486 |
boolean changed = q.retainAll(p); |
487 |
if (i == 0) { |
488 |
assertFalse(changed); |
489 |
} else { |
490 |
assertTrue(changed); |
491 |
} |
492 |
assertTrue(q.containsAll(p)); |
493 |
assertEquals(SIZE - i, q.size()); |
494 |
p.remove(); |
495 |
} |
496 |
} |
497 |
|
498 |
/** |
499 |
* removeAll(c) removes only those elements of c and reports true |
500 |
* if changed |
501 |
*/ |
502 |
public void testRemoveAll() { |
503 |
for (int i = 1; i < SIZE; ++i) { |
504 |
LinkedTransferQueue q = populatedQueue(SIZE); |
505 |
LinkedTransferQueue p = populatedQueue(i); |
506 |
assertTrue(q.removeAll(p)); |
507 |
assertEquals(SIZE - i, q.size()); |
508 |
for (int j = 0; j < i; ++j) { |
509 |
assertFalse(q.contains(p.remove())); |
510 |
} |
511 |
} |
512 |
} |
513 |
|
514 |
/** |
515 |
* toArray() contains all elements |
516 |
*/ |
517 |
public void testToArray() throws InterruptedException { |
518 |
LinkedTransferQueue q = populatedQueue(SIZE); |
519 |
Object[] o = q.toArray(); |
520 |
for (int i = 0; i < o.length; i++) { |
521 |
assertEquals(o[i], q.take()); |
522 |
} |
523 |
} |
524 |
|
525 |
/** |
526 |
* toArray(a) contains all elements |
527 |
*/ |
528 |
public void testToArray2() throws InterruptedException { |
529 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
530 |
Integer[] ints = new Integer[SIZE]; |
531 |
ints = q.toArray(ints); |
532 |
for (int i = 0; i < ints.length; i++) { |
533 |
assertEquals(ints[i], q.take()); |
534 |
} |
535 |
} |
536 |
|
537 |
/** |
538 |
* toArray(null) throws NullPointerException |
539 |
*/ |
540 |
public void testToArray_BadArg() { |
541 |
LinkedTransferQueue q = populatedQueue(SIZE); |
542 |
try { |
543 |
Object o[] = q.toArray(null); |
544 |
shouldThrow(); |
545 |
} catch (NullPointerException success) {} |
546 |
} |
547 |
|
548 |
/** |
549 |
* toArray(incompatible array type) throws CCE |
550 |
*/ |
551 |
public void testToArray1_BadArg() { |
552 |
LinkedTransferQueue q = populatedQueue(SIZE); |
553 |
try { |
554 |
Object o[] = q.toArray(new String[10]); |
555 |
shouldThrow(); |
556 |
} catch (ArrayStoreException success) {} |
557 |
} |
558 |
|
559 |
/** |
560 |
* iterator iterates through all elements |
561 |
*/ |
562 |
public void testIterator() throws InterruptedException { |
563 |
LinkedTransferQueue q = populatedQueue(SIZE); |
564 |
Iterator it = q.iterator(); |
565 |
int i = 0; |
566 |
while (it.hasNext()) { |
567 |
assertEquals(it.next(), i++); |
568 |
} |
569 |
assertEquals(i, SIZE); |
570 |
} |
571 |
|
572 |
/** |
573 |
* iterator.remove() removes current element |
574 |
*/ |
575 |
public void testIteratorRemove() { |
576 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
577 |
q.add(two); |
578 |
q.add(one); |
579 |
q.add(three); |
580 |
|
581 |
Iterator it = q.iterator(); |
582 |
it.next(); |
583 |
it.remove(); |
584 |
|
585 |
it = q.iterator(); |
586 |
assertSame(it.next(), one); |
587 |
assertSame(it.next(), three); |
588 |
assertFalse(it.hasNext()); |
589 |
} |
590 |
|
591 |
/** |
592 |
* iterator ordering is FIFO |
593 |
*/ |
594 |
public void testIteratorOrdering() { |
595 |
final LinkedTransferQueue<Integer> q |
596 |
= new LinkedTransferQueue<Integer>(); |
597 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
598 |
q.add(one); |
599 |
q.add(two); |
600 |
q.add(three); |
601 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
602 |
int k = 0; |
603 |
for (Integer n : q) { |
604 |
assertEquals(++k, (int) n); |
605 |
} |
606 |
assertEquals(3, k); |
607 |
} |
608 |
|
609 |
/** |
610 |
* Modifications do not cause iterators to fail |
611 |
*/ |
612 |
public void testWeaklyConsistentIteration() { |
613 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
614 |
q.add(one); |
615 |
q.add(two); |
616 |
q.add(three); |
617 |
for (Iterator it = q.iterator(); it.hasNext();) { |
618 |
q.remove(); |
619 |
it.next(); |
620 |
} |
621 |
assertEquals(0, q.size()); |
622 |
} |
623 |
|
624 |
/** |
625 |
* toString contains toStrings of elements |
626 |
*/ |
627 |
public void testToString() { |
628 |
LinkedTransferQueue q = populatedQueue(SIZE); |
629 |
String s = q.toString(); |
630 |
for (int i = 0; i < SIZE; ++i) { |
631 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
632 |
} |
633 |
} |
634 |
|
635 |
/** |
636 |
* offer transfers elements across Executor tasks |
637 |
*/ |
638 |
public void testOfferInExecutor() { |
639 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
640 |
q.add(one); |
641 |
q.add(two); |
642 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
643 |
|
644 |
executor.execute(new CheckedRunnable() { |
645 |
public void realRun() { |
646 |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
647 |
}}); |
648 |
|
649 |
executor.execute(new CheckedRunnable() { |
650 |
public void realRun() throws InterruptedException { |
651 |
Thread.sleep(SMALL_DELAY_MS); |
652 |
assertSame(one, q.take()); |
653 |
}}); |
654 |
|
655 |
joinPool(executor); |
656 |
} |
657 |
|
658 |
/** |
659 |
* timed poll retrieves elements across Executor threads |
660 |
*/ |
661 |
public void testPollInExecutor() { |
662 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
663 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
664 |
|
665 |
executor.execute(new CheckedRunnable() { |
666 |
public void realRun() throws InterruptedException { |
667 |
assertNull(q.poll()); |
668 |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
669 |
assertTrue(q.isEmpty()); |
670 |
}}); |
671 |
|
672 |
executor.execute(new CheckedRunnable() { |
673 |
public void realRun() throws InterruptedException { |
674 |
Thread.sleep(SMALL_DELAY_MS); |
675 |
q.put(one); |
676 |
}}); |
677 |
|
678 |
joinPool(executor); |
679 |
} |
680 |
|
681 |
/** |
682 |
* A deserialized serialized queue has same elements in same order |
683 |
*/ |
684 |
public void testSerialization() throws Exception { |
685 |
LinkedTransferQueue q = populatedQueue(SIZE); |
686 |
|
687 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
688 |
ObjectOutputStream out |
689 |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
690 |
out.writeObject(q); |
691 |
out.close(); |
692 |
|
693 |
ByteArrayInputStream bin |
694 |
= new ByteArrayInputStream(bout.toByteArray()); |
695 |
ObjectInputStream in |
696 |
= new ObjectInputStream(new BufferedInputStream(bin)); |
697 |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
698 |
|
699 |
assertEquals(q.size(), r.size()); |
700 |
while (!q.isEmpty()) { |
701 |
assertEquals(q.remove(), r.remove()); |
702 |
} |
703 |
} |
704 |
|
705 |
/** |
706 |
* drainTo(null) throws NullPointerException |
707 |
*/ |
708 |
public void testDrainToNull() { |
709 |
LinkedTransferQueue q = populatedQueue(SIZE); |
710 |
try { |
711 |
q.drainTo(null); |
712 |
shouldThrow(); |
713 |
} catch (NullPointerException success) {} |
714 |
} |
715 |
|
716 |
/** |
717 |
* drainTo(this) throws IllegalArgumentException |
718 |
*/ |
719 |
public void testDrainToSelf() { |
720 |
LinkedTransferQueue q = populatedQueue(SIZE); |
721 |
try { |
722 |
q.drainTo(q); |
723 |
shouldThrow(); |
724 |
} catch (IllegalArgumentException success) {} |
725 |
} |
726 |
|
727 |
/** |
728 |
* drainTo(c) empties queue into another collection c |
729 |
*/ |
730 |
public void testDrainTo() { |
731 |
LinkedTransferQueue q = populatedQueue(SIZE); |
732 |
ArrayList l = new ArrayList(); |
733 |
q.drainTo(l); |
734 |
assertEquals(q.size(), 0); |
735 |
assertEquals(l.size(), SIZE); |
736 |
for (int i = 0; i < SIZE; ++i) { |
737 |
assertEquals(l.get(i), i); |
738 |
} |
739 |
q.add(zero); |
740 |
q.add(one); |
741 |
assertFalse(q.isEmpty()); |
742 |
assertTrue(q.contains(zero)); |
743 |
assertTrue(q.contains(one)); |
744 |
l.clear(); |
745 |
q.drainTo(l); |
746 |
assertEquals(q.size(), 0); |
747 |
assertEquals(l.size(), 2); |
748 |
for (int i = 0; i < 2; ++i) { |
749 |
assertEquals(l.get(i), i); |
750 |
} |
751 |
} |
752 |
|
753 |
/** |
754 |
* drainTo(c) empties full queue, unblocking a waiting put. |
755 |
*/ |
756 |
public void testDrainToWithActivePut() throws InterruptedException { |
757 |
final LinkedTransferQueue q = populatedQueue(SIZE); |
758 |
Thread t = newStartedThread(new CheckedRunnable() { |
759 |
public void realRun() { |
760 |
q.put(SIZE + 1); |
761 |
}}); |
762 |
ArrayList l = new ArrayList(); |
763 |
q.drainTo(l); |
764 |
assertTrue(l.size() >= SIZE); |
765 |
for (int i = 0; i < SIZE; ++i) { |
766 |
assertEquals(l.get(i), i); |
767 |
} |
768 |
awaitTermination(t, MEDIUM_DELAY_MS); |
769 |
assertTrue(q.size() + l.size() >= SIZE); |
770 |
} |
771 |
|
772 |
/** |
773 |
* drainTo(null, n) throws NullPointerException |
774 |
*/ |
775 |
public void testDrainToNullN() { |
776 |
LinkedTransferQueue q = populatedQueue(SIZE); |
777 |
try { |
778 |
q.drainTo(null, SIZE); |
779 |
shouldThrow(); |
780 |
} catch (NullPointerException success) {} |
781 |
} |
782 |
|
783 |
/** |
784 |
* drainTo(this, n) throws IllegalArgumentException |
785 |
*/ |
786 |
public void testDrainToSelfN() { |
787 |
LinkedTransferQueue q = populatedQueue(SIZE); |
788 |
try { |
789 |
q.drainTo(q, SIZE); |
790 |
shouldThrow(); |
791 |
} catch (IllegalArgumentException success) {} |
792 |
} |
793 |
|
794 |
/** |
795 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
796 |
*/ |
797 |
public void testDrainToN() { |
798 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
799 |
for (int i = 0; i < SIZE + 2; ++i) { |
800 |
for (int j = 0; j < SIZE; j++) { |
801 |
assertTrue(q.offer(j)); |
802 |
} |
803 |
ArrayList l = new ArrayList(); |
804 |
q.drainTo(l, i); |
805 |
int k = (i < SIZE) ? i : SIZE; |
806 |
assertEquals(l.size(), k); |
807 |
assertEquals(q.size(), SIZE - k); |
808 |
for (int j = 0; j < k; ++j) { |
809 |
assertEquals(l.get(j), j); |
810 |
} |
811 |
while (q.poll() != null) |
812 |
; |
813 |
} |
814 |
} |
815 |
|
816 |
/** |
817 |
* timed poll() or take() increments the waiting consumer count; |
818 |
* offer(e) decrements the waiting consumer count |
819 |
*/ |
820 |
public void testWaitingConsumer() throws InterruptedException { |
821 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
822 |
assertEquals(q.getWaitingConsumerCount(), 0); |
823 |
assertFalse(q.hasWaitingConsumer()); |
824 |
|
825 |
Thread t = newStartedThread(new CheckedRunnable() { |
826 |
public void realRun() throws InterruptedException { |
827 |
Thread.sleep(SMALL_DELAY_MS); |
828 |
assertTrue(q.hasWaitingConsumer()); |
829 |
assertEquals(q.getWaitingConsumerCount(), 1); |
830 |
assertTrue(q.offer(one)); |
831 |
assertFalse(q.hasWaitingConsumer()); |
832 |
assertEquals(q.getWaitingConsumerCount(), 0); |
833 |
}}); |
834 |
|
835 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
836 |
assertEquals(q.getWaitingConsumerCount(), 0); |
837 |
assertFalse(q.hasWaitingConsumer()); |
838 |
awaitTermination(t, MEDIUM_DELAY_MS); |
839 |
} |
840 |
|
841 |
/** |
842 |
* transfer(null) throws NullPointerException |
843 |
*/ |
844 |
public void testTransfer1() throws InterruptedException { |
845 |
try { |
846 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
847 |
q.transfer(null); |
848 |
shouldThrow(); |
849 |
} catch (NullPointerException success) {} |
850 |
} |
851 |
|
852 |
/** |
853 |
* transfer waits until a poll occurs. The transfered element |
854 |
* is returned by this associated poll. |
855 |
*/ |
856 |
public void testTransfer2() throws InterruptedException { |
857 |
final LinkedTransferQueue<Integer> q |
858 |
= new LinkedTransferQueue<Integer>(); |
859 |
|
860 |
Thread t = newStartedThread(new CheckedRunnable() { |
861 |
public void realRun() throws InterruptedException { |
862 |
q.transfer(SIZE); |
863 |
assertTrue(q.isEmpty()); |
864 |
}}); |
865 |
|
866 |
Thread.sleep(SHORT_DELAY_MS); |
867 |
assertEquals(1, q.size()); |
868 |
assertEquals(SIZE, (int) q.poll()); |
869 |
assertTrue(q.isEmpty()); |
870 |
awaitTermination(t, MEDIUM_DELAY_MS); |
871 |
} |
872 |
|
873 |
/** |
874 |
* transfer waits until a poll occurs, and then transfers in fifo order |
875 |
*/ |
876 |
public void testTransfer3() throws InterruptedException { |
877 |
final LinkedTransferQueue<Integer> q |
878 |
= new LinkedTransferQueue<Integer>(); |
879 |
|
880 |
Thread first = newStartedThread(new CheckedRunnable() { |
881 |
public void realRun() throws InterruptedException { |
882 |
Integer i = SIZE + 1; |
883 |
q.transfer(i); |
884 |
assertTrue(!q.contains(i)); |
885 |
assertEquals(1, q.size()); |
886 |
}}); |
887 |
|
888 |
Thread interruptedThread = newStartedThread( |
889 |
new CheckedInterruptedRunnable() { |
890 |
public void realRun() throws InterruptedException { |
891 |
while (q.size() == 0) |
892 |
Thread.yield(); |
893 |
q.transfer(SIZE); |
894 |
}}); |
895 |
|
896 |
while (q.size() < 2) |
897 |
Thread.yield(); |
898 |
assertEquals(2, q.size()); |
899 |
assertEquals(SIZE + 1, (int) q.poll()); |
900 |
first.join(); |
901 |
assertEquals(1, q.size()); |
902 |
interruptedThread.interrupt(); |
903 |
interruptedThread.join(); |
904 |
assertEquals(0, q.size()); |
905 |
assertTrue(q.isEmpty()); |
906 |
} |
907 |
|
908 |
/** |
909 |
* transfer waits until a poll occurs, at which point the polling |
910 |
* thread returns the element |
911 |
*/ |
912 |
public void testTransfer4() throws InterruptedException { |
913 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
914 |
|
915 |
Thread t = newStartedThread(new CheckedRunnable() { |
916 |
public void realRun() throws InterruptedException { |
917 |
q.transfer(four); |
918 |
assertFalse(q.contains(four)); |
919 |
assertSame(three, q.poll()); |
920 |
}}); |
921 |
|
922 |
Thread.sleep(SHORT_DELAY_MS); |
923 |
assertTrue(q.offer(three)); |
924 |
assertSame(four, q.poll()); |
925 |
awaitTermination(t, MEDIUM_DELAY_MS); |
926 |
} |
927 |
|
928 |
/** |
929 |
* transfer waits until a take occurs. The transfered element |
930 |
* is returned by this associated take. |
931 |
*/ |
932 |
public void testTransfer5() throws InterruptedException { |
933 |
final LinkedTransferQueue<Integer> q |
934 |
= new LinkedTransferQueue<Integer>(); |
935 |
|
936 |
Thread t = newStartedThread(new CheckedRunnable() { |
937 |
public void realRun() throws InterruptedException { |
938 |
q.transfer(SIZE); |
939 |
checkEmpty(q); |
940 |
}}); |
941 |
|
942 |
Thread.sleep(SHORT_DELAY_MS); |
943 |
assertEquals(SIZE, (int) q.take()); |
944 |
checkEmpty(q); |
945 |
awaitTermination(t, MEDIUM_DELAY_MS); |
946 |
} |
947 |
|
948 |
/** |
949 |
* tryTransfer(null) throws NullPointerException |
950 |
*/ |
951 |
public void testTryTransfer1() { |
952 |
try { |
953 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
954 |
q.tryTransfer(null); |
955 |
shouldThrow(); |
956 |
} catch (NullPointerException success) {} |
957 |
} |
958 |
|
959 |
/** |
960 |
* tryTransfer returns false and does not enqueue if there are no |
961 |
* consumers waiting to poll or take. |
962 |
*/ |
963 |
public void testTryTransfer2() throws InterruptedException { |
964 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
965 |
assertFalse(q.tryTransfer(new Object())); |
966 |
assertFalse(q.hasWaitingConsumer()); |
967 |
checkEmpty(q); |
968 |
} |
969 |
|
970 |
/** |
971 |
* If there is a consumer waiting in timed poll, tryTransfer |
972 |
* returns true while successfully transfering object. |
973 |
*/ |
974 |
public void testTryTransfer3() throws InterruptedException { |
975 |
final Object hotPotato = new Object(); |
976 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
977 |
|
978 |
Thread t = newStartedThread(new CheckedRunnable() { |
979 |
public void realRun() { |
980 |
while (! q.hasWaitingConsumer()) |
981 |
Thread.yield(); |
982 |
assertTrue(q.hasWaitingConsumer()); |
983 |
assertTrue(q.isEmpty()); |
984 |
assertEquals(q.size(), 0); |
985 |
assertTrue(q.tryTransfer(hotPotato)); |
986 |
}}); |
987 |
|
988 |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
989 |
checkEmpty(q); |
990 |
awaitTermination(t, MEDIUM_DELAY_MS); |
991 |
} |
992 |
|
993 |
/** |
994 |
* If there is a consumer waiting in take, tryTransfer returns |
995 |
* true while successfully transfering object. |
996 |
*/ |
997 |
public void testTryTransfer4() throws InterruptedException { |
998 |
final Object hotPotato = new Object(); |
999 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1000 |
|
1001 |
Thread t = newStartedThread(new CheckedRunnable() { |
1002 |
public void realRun() { |
1003 |
while (! q.hasWaitingConsumer()) |
1004 |
Thread.yield(); |
1005 |
assertTrue(q.hasWaitingConsumer()); |
1006 |
assertTrue(q.isEmpty()); |
1007 |
assertEquals(q.size(), 0); |
1008 |
assertTrue(q.tryTransfer(hotPotato)); |
1009 |
}}); |
1010 |
|
1011 |
assertSame(q.take(), hotPotato); |
1012 |
checkEmpty(q); |
1013 |
awaitTermination(t, MEDIUM_DELAY_MS); |
1014 |
} |
1015 |
|
1016 |
/** |
1017 |
* tryTransfer waits the amount given if interrupted, and |
1018 |
* throws interrupted exception |
1019 |
*/ |
1020 |
public void testTryTransfer5() throws InterruptedException { |
1021 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1022 |
|
1023 |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1024 |
public void realRun() throws InterruptedException { |
1025 |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1026 |
}}); |
1027 |
|
1028 |
Thread.sleep(SMALL_DELAY_MS); |
1029 |
toInterrupt.interrupt(); |
1030 |
toInterrupt.join(); |
1031 |
} |
1032 |
|
1033 |
/** |
1034 |
* tryTransfer gives up after the timeout and return false |
1035 |
*/ |
1036 |
public void testTryTransfer6() throws InterruptedException { |
1037 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1038 |
|
1039 |
Thread t = newStartedThread(new CheckedRunnable() { |
1040 |
public void realRun() throws InterruptedException { |
1041 |
long t0 = System.nanoTime(); |
1042 |
assertFalse(q.tryTransfer(new Object(), |
1043 |
SHORT_DELAY_MS, MILLISECONDS)); |
1044 |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1045 |
}}); |
1046 |
|
1047 |
checkEmpty(q); |
1048 |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
checkEmpty(q); |
1050 |
} |
1051 |
|
1052 |
/** |
1053 |
* tryTransfer waits for any elements previously in to be removed |
1054 |
* before transfering to a poll or take |
1055 |
*/ |
1056 |
public void testTryTransfer7() throws InterruptedException { |
1057 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1058 |
assertTrue(q.offer(four)); |
1059 |
|
1060 |
Thread t = newStartedThread(new CheckedRunnable() { |
1061 |
public void realRun() throws InterruptedException { |
1062 |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1063 |
assertTrue(q.isEmpty()); |
1064 |
}}); |
1065 |
|
1066 |
Thread.sleep(SHORT_DELAY_MS); |
1067 |
assertEquals(2, q.size()); |
1068 |
assertSame(four, q.poll()); |
1069 |
assertSame(five, q.poll()); |
1070 |
checkEmpty(q); |
1071 |
awaitTermination(t, MEDIUM_DELAY_MS); |
1072 |
} |
1073 |
|
1074 |
/** |
1075 |
* tryTransfer attempts to enqueue into the q and fails returning |
1076 |
* false not enqueueing and the successive poll is null |
1077 |
*/ |
1078 |
public void testTryTransfer8() throws InterruptedException { |
1079 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1080 |
assertTrue(q.offer(four)); |
1081 |
assertEquals(1, q.size()); |
1082 |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1083 |
assertEquals(1, q.size()); |
1084 |
assertSame(four, q.poll()); |
1085 |
assertNull(q.poll()); |
1086 |
checkEmpty(q); |
1087 |
} |
1088 |
|
1089 |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1090 |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1091 |
assertTrue(q.isEmpty()); |
1092 |
for (int i = 0; i < n; i++) { |
1093 |
assertEquals(i, q.size()); |
1094 |
assertTrue(q.offer(i)); |
1095 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1096 |
} |
1097 |
assertFalse(q.isEmpty()); |
1098 |
return q; |
1099 |
} |
1100 |
} |