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