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