ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
Revision: 1.25
Committed: Wed Oct 6 07:49:22 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.24: +8 -24 lines
Log Message:
start of a big refactoring, with only one test refactored: testTimedPollWithOffer

File Contents

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