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