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