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 blocks interruptibly when empty |
278 |
*/ |
279 |
public void testTakeFromEmpty() throws InterruptedException { |
280 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
281 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
282 |
public void realRun() throws InterruptedException { |
283 |
q.take(); |
284 |
}}); |
285 |
Thread.sleep(SHORT_DELAY_MS); |
286 |
t.interrupt(); |
287 |
t.join(); |
288 |
} |
289 |
|
290 |
/** |
291 |
* Take removes existing elements until empty, then blocks interruptibly |
292 |
*/ |
293 |
public void testBlockingTake() throws InterruptedException { |
294 |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
295 |
Thread t = new Thread(new CheckedRunnable() { |
296 |
public void realRun() throws InterruptedException { |
297 |
for (int i = 0; i < SIZE; ++i) { |
298 |
assertEquals(i, (int) q.take()); |
299 |
} |
300 |
try { |
301 |
q.take(); |
302 |
shouldThrow(); |
303 |
} catch (InterruptedException success) {} |
304 |
}}); |
305 |
|
306 |
t.start(); |
307 |
Thread.sleep(SHORT_DELAY_MS); |
308 |
t.interrupt(); |
309 |
t.join(); |
310 |
checkEmpty(q); |
311 |
} |
312 |
|
313 |
/** |
314 |
* poll succeeds unless empty |
315 |
*/ |
316 |
public void testPoll() throws InterruptedException { |
317 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
318 |
for (int i = 0; i < SIZE; ++i) { |
319 |
assertEquals(i, (int) q.poll()); |
320 |
} |
321 |
assertNull(q.poll()); |
322 |
checkEmpty(q); |
323 |
} |
324 |
|
325 |
/** |
326 |
* timed pool with zero timeout succeeds when non-empty, else times out |
327 |
*/ |
328 |
public void testTimedPoll0() throws InterruptedException { |
329 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
330 |
for (int i = 0; i < SIZE; ++i) { |
331 |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
332 |
} |
333 |
assertNull(q.poll(0, MILLISECONDS)); |
334 |
checkEmpty(q); |
335 |
} |
336 |
|
337 |
/** |
338 |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
339 |
*/ |
340 |
public void testTimedPoll() throws InterruptedException { |
341 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
342 |
for (int i = 0; i < SIZE; ++i) { |
343 |
long t0 = System.nanoTime(); |
344 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
345 |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
346 |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
347 |
} |
348 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
349 |
checkEmpty(q); |
350 |
} |
351 |
|
352 |
/** |
353 |
* Interrupted timed poll throws InterruptedException instead of |
354 |
* returning timeout status |
355 |
*/ |
356 |
public void testInterruptedTimedPoll() throws InterruptedException { |
357 |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
358 |
Thread t = newStartedThread(new CheckedRunnable() { |
359 |
public void realRun() throws InterruptedException { |
360 |
for (int i = 0; i < SIZE; ++i) { |
361 |
long t0 = System.nanoTime(); |
362 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
363 |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
364 |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
365 |
} |
366 |
try { |
367 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
368 |
shouldThrow(); |
369 |
} catch (InterruptedException success) {} |
370 |
}}); |
371 |
|
372 |
Thread.sleep(SMALL_DELAY_MS); |
373 |
t.interrupt(); |
374 |
t.join(); |
375 |
checkEmpty(q); |
376 |
} |
377 |
|
378 |
/** |
379 |
* peek returns next element, or null if empty |
380 |
*/ |
381 |
public void testPeek() throws InterruptedException { |
382 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
383 |
for (int i = 0; i < SIZE; ++i) { |
384 |
assertEquals(i, (int) q.peek()); |
385 |
assertEquals(i, (int) q.poll()); |
386 |
assertTrue(q.peek() == null || |
387 |
i != (int) q.peek()); |
388 |
} |
389 |
assertNull(q.peek()); |
390 |
checkEmpty(q); |
391 |
} |
392 |
|
393 |
/** |
394 |
* element returns next element, or throws NoSuchElementException if empty |
395 |
*/ |
396 |
public void testElement() throws InterruptedException { |
397 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
398 |
for (int i = 0; i < SIZE; ++i) { |
399 |
assertEquals(i, (int) q.element()); |
400 |
assertEquals(i, (int) q.poll()); |
401 |
} |
402 |
try { |
403 |
q.element(); |
404 |
shouldThrow(); |
405 |
} catch (NoSuchElementException success) {} |
406 |
checkEmpty(q); |
407 |
} |
408 |
|
409 |
/** |
410 |
* remove removes next element, or throws NoSuchElementException if empty |
411 |
*/ |
412 |
public void testRemove() throws InterruptedException { |
413 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
414 |
for (int i = 0; i < SIZE; ++i) { |
415 |
assertEquals(i, (int) q.remove()); |
416 |
} |
417 |
try { |
418 |
q.remove(); |
419 |
shouldThrow(); |
420 |
} catch (NoSuchElementException success) {} |
421 |
checkEmpty(q); |
422 |
} |
423 |
|
424 |
/** |
425 |
* remove(x) removes x and returns true if present |
426 |
*/ |
427 |
public void testRemoveElement() throws InterruptedException { |
428 |
LinkedTransferQueue q = populatedQueue(SIZE); |
429 |
for (int i = 1; i < SIZE; i += 2) { |
430 |
assertTrue(q.remove(i)); |
431 |
} |
432 |
for (int i = 0; i < SIZE; i += 2) { |
433 |
assertTrue(q.remove(i)); |
434 |
assertFalse(q.remove(i + 1)); |
435 |
} |
436 |
checkEmpty(q); |
437 |
} |
438 |
|
439 |
/** |
440 |
* An add following remove(x) succeeds |
441 |
*/ |
442 |
public void testRemoveElementAndAdd() throws InterruptedException { |
443 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
444 |
assertTrue(q.add(one)); |
445 |
assertTrue(q.add(two)); |
446 |
assertTrue(q.remove(one)); |
447 |
assertTrue(q.remove(two)); |
448 |
assertTrue(q.add(three)); |
449 |
assertSame(q.take(), three); |
450 |
} |
451 |
|
452 |
/** |
453 |
* contains(x) reports true when elements added but not yet removed |
454 |
*/ |
455 |
public void testContains() { |
456 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
457 |
for (int i = 0; i < SIZE; ++i) { |
458 |
assertTrue(q.contains(i)); |
459 |
assertEquals(i, (int) q.poll()); |
460 |
assertFalse(q.contains(i)); |
461 |
} |
462 |
} |
463 |
|
464 |
/** |
465 |
* clear removes all elements |
466 |
*/ |
467 |
public void testClear() throws InterruptedException { |
468 |
LinkedTransferQueue q = populatedQueue(SIZE); |
469 |
q.clear(); |
470 |
checkEmpty(q); |
471 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
472 |
q.add(one); |
473 |
assertFalse(q.isEmpty()); |
474 |
assertEquals(1, q.size()); |
475 |
assertTrue(q.contains(one)); |
476 |
q.clear(); |
477 |
checkEmpty(q); |
478 |
} |
479 |
|
480 |
/** |
481 |
* containsAll(c) is true when c contains a subset of elements |
482 |
*/ |
483 |
public void testContainsAll() { |
484 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
485 |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
486 |
for (int i = 0; i < SIZE; ++i) { |
487 |
assertTrue(q.containsAll(p)); |
488 |
assertFalse(p.containsAll(q)); |
489 |
p.add(i); |
490 |
} |
491 |
assertTrue(p.containsAll(q)); |
492 |
} |
493 |
|
494 |
/** |
495 |
* retainAll(c) retains only those elements of c and reports true |
496 |
* if changed |
497 |
*/ |
498 |
public void testRetainAll() { |
499 |
LinkedTransferQueue q = populatedQueue(SIZE); |
500 |
LinkedTransferQueue p = populatedQueue(SIZE); |
501 |
for (int i = 0; i < SIZE; ++i) { |
502 |
boolean changed = q.retainAll(p); |
503 |
if (i == 0) { |
504 |
assertFalse(changed); |
505 |
} else { |
506 |
assertTrue(changed); |
507 |
} |
508 |
assertTrue(q.containsAll(p)); |
509 |
assertEquals(SIZE - i, q.size()); |
510 |
p.remove(); |
511 |
} |
512 |
} |
513 |
|
514 |
/** |
515 |
* removeAll(c) removes only those elements of c and reports true |
516 |
* if changed |
517 |
*/ |
518 |
public void testRemoveAll() { |
519 |
for (int i = 1; i < SIZE; ++i) { |
520 |
LinkedTransferQueue q = populatedQueue(SIZE); |
521 |
LinkedTransferQueue p = populatedQueue(i); |
522 |
assertTrue(q.removeAll(p)); |
523 |
assertEquals(SIZE - i, q.size()); |
524 |
for (int j = 0; j < i; ++j) { |
525 |
assertFalse(q.contains(p.remove())); |
526 |
} |
527 |
} |
528 |
} |
529 |
|
530 |
/** |
531 |
* toArray() contains all elements |
532 |
*/ |
533 |
public void testToArray() throws InterruptedException { |
534 |
LinkedTransferQueue q = populatedQueue(SIZE); |
535 |
Object[] o = q.toArray(); |
536 |
for (int i = 0; i < o.length; i++) { |
537 |
assertEquals(o[i], q.take()); |
538 |
} |
539 |
} |
540 |
|
541 |
/** |
542 |
* toArray(a) contains all elements |
543 |
*/ |
544 |
public void testToArray2() throws InterruptedException { |
545 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
546 |
Integer[] ints = new Integer[SIZE]; |
547 |
ints = q.toArray(ints); |
548 |
for (int i = 0; i < ints.length; i++) { |
549 |
assertEquals(ints[i], q.take()); |
550 |
} |
551 |
} |
552 |
|
553 |
/** |
554 |
* toArray(null) throws NullPointerException |
555 |
*/ |
556 |
public void testToArray_BadArg() { |
557 |
LinkedTransferQueue q = populatedQueue(SIZE); |
558 |
try { |
559 |
Object o[] = q.toArray(null); |
560 |
shouldThrow(); |
561 |
} catch (NullPointerException success) {} |
562 |
} |
563 |
|
564 |
/** |
565 |
* toArray(incompatible array type) throws CCE |
566 |
*/ |
567 |
public void testToArray1_BadArg() { |
568 |
LinkedTransferQueue q = populatedQueue(SIZE); |
569 |
try { |
570 |
Object o[] = q.toArray(new String[10]); |
571 |
shouldThrow(); |
572 |
} catch (ArrayStoreException success) {} |
573 |
} |
574 |
|
575 |
/** |
576 |
* iterator iterates through all elements |
577 |
*/ |
578 |
public void testIterator() throws InterruptedException { |
579 |
LinkedTransferQueue q = populatedQueue(SIZE); |
580 |
Iterator it = q.iterator(); |
581 |
int i = 0; |
582 |
while (it.hasNext()) { |
583 |
assertEquals(it.next(), i++); |
584 |
} |
585 |
assertEquals(i, SIZE); |
586 |
} |
587 |
|
588 |
/** |
589 |
* iterator.remove() removes current element |
590 |
*/ |
591 |
public void testIteratorRemove() { |
592 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
593 |
q.add(two); |
594 |
q.add(one); |
595 |
q.add(three); |
596 |
|
597 |
Iterator it = q.iterator(); |
598 |
it.next(); |
599 |
it.remove(); |
600 |
|
601 |
it = q.iterator(); |
602 |
assertSame(it.next(), one); |
603 |
assertSame(it.next(), three); |
604 |
assertFalse(it.hasNext()); |
605 |
} |
606 |
|
607 |
/** |
608 |
* iterator ordering is FIFO |
609 |
*/ |
610 |
public void testIteratorOrdering() { |
611 |
final LinkedTransferQueue<Integer> q |
612 |
= new LinkedTransferQueue<Integer>(); |
613 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
614 |
q.add(one); |
615 |
q.add(two); |
616 |
q.add(three); |
617 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
618 |
int k = 0; |
619 |
for (Integer n : q) { |
620 |
assertEquals(++k, (int) n); |
621 |
} |
622 |
assertEquals(3, k); |
623 |
} |
624 |
|
625 |
/** |
626 |
* Modifications do not cause iterators to fail |
627 |
*/ |
628 |
public void testWeaklyConsistentIteration() { |
629 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
630 |
q.add(one); |
631 |
q.add(two); |
632 |
q.add(three); |
633 |
for (Iterator it = q.iterator(); it.hasNext();) { |
634 |
q.remove(); |
635 |
it.next(); |
636 |
} |
637 |
assertEquals(0, q.size()); |
638 |
} |
639 |
|
640 |
/** |
641 |
* toString contains toStrings of elements |
642 |
*/ |
643 |
public void testToString() { |
644 |
LinkedTransferQueue q = populatedQueue(SIZE); |
645 |
String s = q.toString(); |
646 |
for (int i = 0; i < SIZE; ++i) { |
647 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
648 |
} |
649 |
} |
650 |
|
651 |
/** |
652 |
* offer transfers elements across Executor tasks |
653 |
*/ |
654 |
public void testOfferInExecutor() { |
655 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
656 |
q.add(one); |
657 |
q.add(two); |
658 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
659 |
|
660 |
executor.execute(new CheckedRunnable() { |
661 |
public void realRun() { |
662 |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
663 |
}}); |
664 |
|
665 |
executor.execute(new CheckedRunnable() { |
666 |
public void realRun() throws InterruptedException { |
667 |
Thread.sleep(SMALL_DELAY_MS); |
668 |
assertSame(one, q.take()); |
669 |
}}); |
670 |
|
671 |
joinPool(executor); |
672 |
} |
673 |
|
674 |
/** |
675 |
* timed poll retrieves elements across Executor threads |
676 |
*/ |
677 |
public void testPollInExecutor() { |
678 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
679 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
680 |
|
681 |
executor.execute(new CheckedRunnable() { |
682 |
public void realRun() throws InterruptedException { |
683 |
assertNull(q.poll()); |
684 |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
685 |
assertTrue(q.isEmpty()); |
686 |
}}); |
687 |
|
688 |
executor.execute(new CheckedRunnable() { |
689 |
public void realRun() throws InterruptedException { |
690 |
Thread.sleep(SMALL_DELAY_MS); |
691 |
q.put(one); |
692 |
}}); |
693 |
|
694 |
joinPool(executor); |
695 |
} |
696 |
|
697 |
/** |
698 |
* A deserialized serialized queue has same elements in same order |
699 |
*/ |
700 |
public void testSerialization() throws Exception { |
701 |
LinkedTransferQueue q = populatedQueue(SIZE); |
702 |
|
703 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
704 |
ObjectOutputStream out |
705 |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
706 |
out.writeObject(q); |
707 |
out.close(); |
708 |
|
709 |
ByteArrayInputStream bin |
710 |
= new ByteArrayInputStream(bout.toByteArray()); |
711 |
ObjectInputStream in |
712 |
= new ObjectInputStream(new BufferedInputStream(bin)); |
713 |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
714 |
|
715 |
assertEquals(q.size(), r.size()); |
716 |
while (!q.isEmpty()) { |
717 |
assertEquals(q.remove(), r.remove()); |
718 |
} |
719 |
} |
720 |
|
721 |
/** |
722 |
* drainTo(null) throws NullPointerException |
723 |
*/ |
724 |
public void testDrainToNull() { |
725 |
LinkedTransferQueue q = populatedQueue(SIZE); |
726 |
try { |
727 |
q.drainTo(null); |
728 |
shouldThrow(); |
729 |
} catch (NullPointerException success) {} |
730 |
} |
731 |
|
732 |
/** |
733 |
* drainTo(this) throws IllegalArgumentException |
734 |
*/ |
735 |
public void testDrainToSelf() { |
736 |
LinkedTransferQueue q = populatedQueue(SIZE); |
737 |
try { |
738 |
q.drainTo(q); |
739 |
shouldThrow(); |
740 |
} catch (IllegalArgumentException success) {} |
741 |
} |
742 |
|
743 |
/** |
744 |
* drainTo(c) empties queue into another collection c |
745 |
*/ |
746 |
public void testDrainTo() { |
747 |
LinkedTransferQueue q = populatedQueue(SIZE); |
748 |
ArrayList l = new ArrayList(); |
749 |
q.drainTo(l); |
750 |
assertEquals(q.size(), 0); |
751 |
assertEquals(l.size(), SIZE); |
752 |
for (int i = 0; i < SIZE; ++i) { |
753 |
assertEquals(l.get(i), i); |
754 |
} |
755 |
q.add(zero); |
756 |
q.add(one); |
757 |
assertFalse(q.isEmpty()); |
758 |
assertTrue(q.contains(zero)); |
759 |
assertTrue(q.contains(one)); |
760 |
l.clear(); |
761 |
q.drainTo(l); |
762 |
assertEquals(q.size(), 0); |
763 |
assertEquals(l.size(), 2); |
764 |
for (int i = 0; i < 2; ++i) { |
765 |
assertEquals(l.get(i), i); |
766 |
} |
767 |
} |
768 |
|
769 |
/** |
770 |
* drainTo(c) empties full queue, unblocking a waiting put. |
771 |
*/ |
772 |
public void testDrainToWithActivePut() throws InterruptedException { |
773 |
final LinkedTransferQueue q = populatedQueue(SIZE); |
774 |
Thread t = newStartedThread(new CheckedRunnable() { |
775 |
public void realRun() { |
776 |
q.put(SIZE + 1); |
777 |
}}); |
778 |
ArrayList l = new ArrayList(); |
779 |
q.drainTo(l); |
780 |
assertTrue(l.size() >= SIZE); |
781 |
for (int i = 0; i < SIZE; ++i) { |
782 |
assertEquals(l.get(i), i); |
783 |
} |
784 |
t.join(); |
785 |
assertTrue(q.size() + l.size() >= SIZE); |
786 |
} |
787 |
|
788 |
/** |
789 |
* drainTo(null, n) throws NullPointerException |
790 |
*/ |
791 |
public void testDrainToNullN() { |
792 |
LinkedTransferQueue q = populatedQueue(SIZE); |
793 |
try { |
794 |
q.drainTo(null, SIZE); |
795 |
shouldThrow(); |
796 |
} catch (NullPointerException success) {} |
797 |
} |
798 |
|
799 |
/** |
800 |
* drainTo(this, n) throws IllegalArgumentException |
801 |
*/ |
802 |
public void testDrainToSelfN() { |
803 |
LinkedTransferQueue q = populatedQueue(SIZE); |
804 |
try { |
805 |
q.drainTo(q, SIZE); |
806 |
shouldThrow(); |
807 |
} catch (IllegalArgumentException success) {} |
808 |
} |
809 |
|
810 |
/** |
811 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
812 |
*/ |
813 |
public void testDrainToN() { |
814 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
815 |
for (int i = 0; i < SIZE + 2; ++i) { |
816 |
for (int j = 0; j < SIZE; j++) { |
817 |
assertTrue(q.offer(j)); |
818 |
} |
819 |
ArrayList l = new ArrayList(); |
820 |
q.drainTo(l, i); |
821 |
int k = (i < SIZE) ? i : SIZE; |
822 |
assertEquals(l.size(), k); |
823 |
assertEquals(q.size(), SIZE - k); |
824 |
for (int j = 0; j < k; ++j) { |
825 |
assertEquals(l.get(j), j); |
826 |
} |
827 |
while (q.poll() != null) |
828 |
; |
829 |
} |
830 |
} |
831 |
|
832 |
/** |
833 |
* timed poll() or take() increments the waiting consumer count; |
834 |
* offer(e) decrements the waiting consumer count |
835 |
*/ |
836 |
public void testWaitingConsumer() throws InterruptedException { |
837 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
838 |
assertEquals(q.getWaitingConsumerCount(), 0); |
839 |
assertFalse(q.hasWaitingConsumer()); |
840 |
|
841 |
Thread t = newStartedThread(new CheckedRunnable() { |
842 |
public void realRun() throws InterruptedException { |
843 |
Thread.sleep(SMALL_DELAY_MS); |
844 |
assertTrue(q.hasWaitingConsumer()); |
845 |
assertEquals(q.getWaitingConsumerCount(), 1); |
846 |
assertTrue(q.offer(one)); |
847 |
assertFalse(q.hasWaitingConsumer()); |
848 |
assertEquals(q.getWaitingConsumerCount(), 0); |
849 |
}}); |
850 |
|
851 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
852 |
assertEquals(q.getWaitingConsumerCount(), 0); |
853 |
assertFalse(q.hasWaitingConsumer()); |
854 |
t.join(); |
855 |
} |
856 |
|
857 |
/** |
858 |
* transfer(null) throws NullPointerException |
859 |
*/ |
860 |
public void testTransfer1() throws InterruptedException { |
861 |
try { |
862 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
863 |
q.transfer(null); |
864 |
shouldThrow(); |
865 |
} catch (NullPointerException success) {} |
866 |
} |
867 |
|
868 |
/** |
869 |
* transfer waits until a poll occurs. The transfered element |
870 |
* is returned by this associated poll. |
871 |
*/ |
872 |
public void testTransfer2() throws InterruptedException { |
873 |
final LinkedTransferQueue<Integer> q |
874 |
= new LinkedTransferQueue<Integer>(); |
875 |
|
876 |
Thread t = newStartedThread(new CheckedRunnable() { |
877 |
public void realRun() throws InterruptedException { |
878 |
q.transfer(SIZE); |
879 |
assertTrue(q.isEmpty()); |
880 |
}}); |
881 |
|
882 |
Thread.sleep(SHORT_DELAY_MS); |
883 |
assertEquals(1, q.size()); |
884 |
assertEquals(SIZE, (int) q.poll()); |
885 |
assertTrue(q.isEmpty()); |
886 |
t.join(); |
887 |
} |
888 |
|
889 |
/** |
890 |
* transfer waits until a poll occurs, and then transfers in fifo order |
891 |
*/ |
892 |
public void testTransfer3() throws InterruptedException { |
893 |
final LinkedTransferQueue<Integer> q |
894 |
= new LinkedTransferQueue<Integer>(); |
895 |
|
896 |
Thread first = newStartedThread(new CheckedRunnable() { |
897 |
public void realRun() throws InterruptedException { |
898 |
Integer i = SIZE + 1; |
899 |
q.transfer(i); |
900 |
assertTrue(!q.contains(i)); |
901 |
assertEquals(1, q.size()); |
902 |
}}); |
903 |
|
904 |
Thread interruptedThread = newStartedThread( |
905 |
new CheckedInterruptedRunnable() { |
906 |
public void realRun() throws InterruptedException { |
907 |
while (q.size() == 0) |
908 |
Thread.yield(); |
909 |
q.transfer(SIZE); |
910 |
}}); |
911 |
|
912 |
while (q.size() < 2) |
913 |
Thread.yield(); |
914 |
assertEquals(2, q.size()); |
915 |
assertEquals(SIZE + 1, (int) q.poll()); |
916 |
first.join(); |
917 |
assertEquals(1, q.size()); |
918 |
interruptedThread.interrupt(); |
919 |
interruptedThread.join(); |
920 |
assertEquals(0, q.size()); |
921 |
assertTrue(q.isEmpty()); |
922 |
} |
923 |
|
924 |
/** |
925 |
* transfer waits until a poll occurs, at which point the polling |
926 |
* thread returns the element |
927 |
*/ |
928 |
public void testTransfer4() throws InterruptedException { |
929 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
930 |
|
931 |
Thread t = newStartedThread(new CheckedRunnable() { |
932 |
public void realRun() throws InterruptedException { |
933 |
q.transfer(four); |
934 |
assertFalse(q.contains(four)); |
935 |
assertSame(three, q.poll()); |
936 |
}}); |
937 |
|
938 |
Thread.sleep(SHORT_DELAY_MS); |
939 |
assertTrue(q.offer(three)); |
940 |
assertSame(four, q.poll()); |
941 |
t.join(); |
942 |
} |
943 |
|
944 |
/** |
945 |
* transfer waits until a take occurs. The transfered element |
946 |
* is returned by this associated take. |
947 |
*/ |
948 |
public void testTransfer5() throws InterruptedException { |
949 |
final LinkedTransferQueue<Integer> q |
950 |
= new LinkedTransferQueue<Integer>(); |
951 |
|
952 |
Thread t = newStartedThread(new CheckedRunnable() { |
953 |
public void realRun() throws InterruptedException { |
954 |
q.transfer(SIZE); |
955 |
checkEmpty(q); |
956 |
}}); |
957 |
|
958 |
Thread.sleep(SHORT_DELAY_MS); |
959 |
assertEquals(SIZE, (int) q.take()); |
960 |
checkEmpty(q); |
961 |
t.join(); |
962 |
} |
963 |
|
964 |
/** |
965 |
* tryTransfer(null) throws NullPointerException |
966 |
*/ |
967 |
public void testTryTransfer1() { |
968 |
try { |
969 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
970 |
q.tryTransfer(null); |
971 |
shouldThrow(); |
972 |
} catch (NullPointerException success) {} |
973 |
} |
974 |
|
975 |
/** |
976 |
* tryTransfer returns false and does not enqueue if there are no |
977 |
* consumers waiting to poll or take. |
978 |
*/ |
979 |
public void testTryTransfer2() throws InterruptedException { |
980 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
981 |
assertFalse(q.tryTransfer(new Object())); |
982 |
assertFalse(q.hasWaitingConsumer()); |
983 |
checkEmpty(q); |
984 |
} |
985 |
|
986 |
/** |
987 |
* If there is a consumer waiting in timed poll, tryTransfer |
988 |
* returns true while successfully transfering object. |
989 |
*/ |
990 |
public void testTryTransfer3() throws InterruptedException { |
991 |
final Object hotPotato = new Object(); |
992 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
993 |
|
994 |
Thread t = newStartedThread(new CheckedRunnable() { |
995 |
public void realRun() { |
996 |
while (! q.hasWaitingConsumer()) |
997 |
Thread.yield(); |
998 |
assertTrue(q.hasWaitingConsumer()); |
999 |
assertTrue(q.isEmpty()); |
1000 |
assertEquals(q.size(), 0); |
1001 |
assertTrue(q.tryTransfer(hotPotato)); |
1002 |
}}); |
1003 |
|
1004 |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1005 |
checkEmpty(q); |
1006 |
t.join(); |
1007 |
} |
1008 |
|
1009 |
/** |
1010 |
* If there is a consumer waiting in take, tryTransfer returns |
1011 |
* true while successfully transfering object. |
1012 |
*/ |
1013 |
public void testTryTransfer4() throws InterruptedException { |
1014 |
final Object hotPotato = new Object(); |
1015 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1016 |
|
1017 |
Thread t = newStartedThread(new CheckedRunnable() { |
1018 |
public void realRun() { |
1019 |
while (! q.hasWaitingConsumer()) |
1020 |
Thread.yield(); |
1021 |
assertTrue(q.hasWaitingConsumer()); |
1022 |
assertTrue(q.isEmpty()); |
1023 |
assertEquals(q.size(), 0); |
1024 |
assertTrue(q.tryTransfer(hotPotato)); |
1025 |
}}); |
1026 |
|
1027 |
assertSame(q.take(), hotPotato); |
1028 |
checkEmpty(q); |
1029 |
t.join(); |
1030 |
} |
1031 |
|
1032 |
/** |
1033 |
* tryTransfer waits the amount given if interrupted, and |
1034 |
* throws interrupted exception |
1035 |
*/ |
1036 |
public void testTryTransfer5() throws InterruptedException { |
1037 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1038 |
|
1039 |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1040 |
public void realRun() throws InterruptedException { |
1041 |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1042 |
}}); |
1043 |
|
1044 |
Thread.sleep(SMALL_DELAY_MS); |
1045 |
toInterrupt.interrupt(); |
1046 |
toInterrupt.join(); |
1047 |
} |
1048 |
|
1049 |
/** |
1050 |
* tryTransfer gives up after the timeout and return false |
1051 |
*/ |
1052 |
public void testTryTransfer6() throws InterruptedException { |
1053 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1054 |
|
1055 |
Thread t = newStartedThread(new CheckedRunnable() { |
1056 |
public void realRun() throws InterruptedException { |
1057 |
long t0 = System.nanoTime(); |
1058 |
assertFalse(q.tryTransfer(new Object(), |
1059 |
SHORT_DELAY_MS, MILLISECONDS)); |
1060 |
long elapsed = NANOSECONDS.toMillis(System.nanoTime() - t0); |
1061 |
assertTrue(elapsed >= SHORT_DELAY_MS); |
1062 |
}}); |
1063 |
|
1064 |
checkEmpty(q); |
1065 |
awaitTermination(t, MEDIUM_DELAY_MS); |
1066 |
checkEmpty(q); |
1067 |
} |
1068 |
|
1069 |
/** |
1070 |
* tryTransfer waits for any elements previously in to be removed |
1071 |
* before transfering to a poll or take |
1072 |
*/ |
1073 |
public void testTryTransfer7() throws InterruptedException { |
1074 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1075 |
assertTrue(q.offer(four)); |
1076 |
|
1077 |
Thread t = newStartedThread(new CheckedRunnable() { |
1078 |
public void realRun() throws InterruptedException { |
1079 |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1080 |
assertTrue(q.isEmpty()); |
1081 |
}}); |
1082 |
|
1083 |
Thread.sleep(SHORT_DELAY_MS); |
1084 |
assertEquals(2, q.size()); |
1085 |
assertSame(four, q.poll()); |
1086 |
assertSame(five, q.poll()); |
1087 |
checkEmpty(q); |
1088 |
t.join(); |
1089 |
} |
1090 |
|
1091 |
/** |
1092 |
* tryTransfer attempts to enqueue into the q and fails returning |
1093 |
* false not enqueueing and the successive poll is null |
1094 |
*/ |
1095 |
public void testTryTransfer8() throws InterruptedException { |
1096 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1097 |
assertTrue(q.offer(four)); |
1098 |
assertEquals(1, q.size()); |
1099 |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1100 |
assertEquals(1, q.size()); |
1101 |
assertSame(four, q.poll()); |
1102 |
assertNull(q.poll()); |
1103 |
checkEmpty(q); |
1104 |
} |
1105 |
|
1106 |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1107 |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1108 |
assertTrue(q.isEmpty()); |
1109 |
for (int i = 0; i < n; i++) { |
1110 |
assertEquals(i, q.size()); |
1111 |
assertTrue(q.offer(i)); |
1112 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1113 |
} |
1114 |
assertFalse(q.isEmpty()); |
1115 |
return q; |
1116 |
} |
1117 |
} |