ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +224 -84 lines
Log Message:
Documentation scaffolding

File Contents

# Content
1 /*
2 * Written by members of JCP JSR-166 Expert Group and released to the
3 * public domain. Use, modify, and redistribute this code in any way
4 * without acknowledgement. Other contributors include Andrew Wright,
5 * Jeffrey Hayes, Pat Fischer, Mike Judd.
6 */
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11 import java.io.*;
12
13 public class LinkedBlockingQueueTest extends JSR166TestCase {
14
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18
19 public static Test suite() {
20 return new TestSuite(LinkedBlockingQueueTest.class);
21 }
22
23
24 /**
25 * Create a queue of given size containing consecutive
26 * Integers 0 ... n.
27 */
28 private LinkedBlockingQueue populatedQueue(int n) {
29 LinkedBlockingQueue q = new LinkedBlockingQueue(n);
30 assertTrue(q.isEmpty());
31 for(int i = 0; i < n; i++)
32 assertTrue(q.offer(new Integer(i)));
33 assertFalse(q.isEmpty());
34 assertEquals(0, q.remainingCapacity());
35 assertEquals(n, q.size());
36 return q;
37 }
38
39 /**
40 *
41 */
42 public void testConstructor1() {
43 assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
44 }
45
46 /**
47 *
48 */
49 public void testConstructor2() {
50 try {
51 LinkedBlockingQueue q = new LinkedBlockingQueue(0);
52 shouldThrow();
53 }
54 catch (IllegalArgumentException success) {}
55 }
56
57 /**
58 *
59 */
60 public void testConstructor3() {
61
62 try {
63 LinkedBlockingQueue q = new LinkedBlockingQueue(null);
64 shouldThrow();
65 }
66 catch (NullPointerException success) {}
67 }
68
69 /**
70 *
71 */
72 public void testConstructor4() {
73 try {
74 Integer[] ints = new Integer[SIZE];
75 LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
76 shouldThrow();
77 }
78 catch (NullPointerException success) {}
79 }
80
81 /**
82 *
83 */
84 public void testConstructor5() {
85 try {
86 Integer[] ints = new Integer[SIZE];
87 for (int i = 0; i < SIZE-1; ++i)
88 ints[i] = new Integer(i);
89 LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
90 shouldThrow();
91 }
92 catch (NullPointerException success) {}
93 }
94
95 /**
96 *
97 */
98 public void testConstructor6() {
99 try {
100 Integer[] ints = new Integer[SIZE];
101 for (int i = 0; i < SIZE; ++i)
102 ints[i] = new Integer(i);
103 LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
104 for (int i = 0; i < SIZE; ++i)
105 assertEquals(ints[i], q.poll());
106 }
107 finally {}
108 }
109
110 /**
111 *
112 */
113 public void testEmptyFull() {
114 LinkedBlockingQueue q = new LinkedBlockingQueue(2);
115 assertTrue(q.isEmpty());
116 assertEquals("should have room for 2", 2, q.remainingCapacity());
117 q.add(one);
118 assertFalse(q.isEmpty());
119 q.add(two);
120 assertFalse(q.isEmpty());
121 assertEquals(0, q.remainingCapacity());
122 assertFalse(q.offer(three));
123 }
124
125 /**
126 *
127 */
128 public void testRemainingCapacity() {
129 LinkedBlockingQueue q = populatedQueue(SIZE);
130 for (int i = 0; i < SIZE; ++i) {
131 assertEquals(i, q.remainingCapacity());
132 assertEquals(SIZE-i, q.size());
133 q.remove();
134 }
135 for (int i = 0; i < SIZE; ++i) {
136 assertEquals(SIZE-i, q.remainingCapacity());
137 assertEquals(i, q.size());
138 q.add(new Integer(i));
139 }
140 }
141
142 /**
143 *
144 */
145 public void testOfferNull() {
146 try {
147 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
148 q.offer(null);
149 shouldThrow();
150 } catch (NullPointerException success) { }
151 }
152
153 /**
154 *
155 */
156 public void testOffer() {
157 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
158 assertTrue(q.offer(zero));
159 assertFalse(q.offer(one));
160 }
161
162 /**
163 *
164 */
165 public void testAdd() {
166 try {
167 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
168 for (int i = 0; i < SIZE; ++i) {
169 assertTrue(q.add(new Integer(i)));
170 }
171 assertEquals(0, q.remainingCapacity());
172 q.add(new Integer(SIZE));
173 } catch (IllegalStateException success){
174 }
175 }
176
177 /**
178 *
179 */
180 public void testAddAll1() {
181 try {
182 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
183 q.addAll(null);
184 shouldThrow();
185 }
186 catch (NullPointerException success) {}
187 }
188 /**
189 *
190 */
191 public void testAddAll2() {
192 try {
193 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
194 Integer[] ints = new Integer[SIZE];
195 q.addAll(Arrays.asList(ints));
196 shouldThrow();
197 }
198 catch (NullPointerException success) {}
199 }
200 /**
201 *
202 */
203 public void testAddAll3() {
204 try {
205 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
206 Integer[] ints = new Integer[SIZE];
207 for (int i = 0; i < SIZE-1; ++i)
208 ints[i] = new Integer(i);
209 q.addAll(Arrays.asList(ints));
210 shouldThrow();
211 }
212 catch (NullPointerException success) {}
213 }
214 /**
215 *
216 */
217 public void testAddAll4() {
218 try {
219 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
220 Integer[] ints = new Integer[SIZE];
221 for (int i = 0; i < SIZE; ++i)
222 ints[i] = new Integer(i);
223 q.addAll(Arrays.asList(ints));
224 shouldThrow();
225 }
226 catch (IllegalStateException success) {}
227 }
228 /**
229 *
230 */
231 public void testAddAll5() {
232 try {
233 Integer[] empty = new Integer[0];
234 Integer[] ints = new Integer[SIZE];
235 for (int i = 0; i < SIZE; ++i)
236 ints[i] = new Integer(i);
237 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
238 assertFalse(q.addAll(Arrays.asList(empty)));
239 assertTrue(q.addAll(Arrays.asList(ints)));
240 for (int i = 0; i < SIZE; ++i)
241 assertEquals(ints[i], q.poll());
242 }
243 finally {}
244 }
245
246 /**
247 *
248 */
249 public void testPutNull() {
250 try {
251 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
252 q.put(null);
253 shouldThrow();
254 }
255 catch (NullPointerException success){
256 }
257 catch (InterruptedException ie) {
258 unexpectedException();
259 }
260 }
261
262 /**
263 *
264 */
265 public void testPut() {
266 try {
267 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
268 for (int i = 0; i < SIZE; ++i) {
269 Integer I = new Integer(i);
270 q.put(I);
271 assertTrue(q.contains(I));
272 }
273 assertEquals(0, q.remainingCapacity());
274 }
275 catch (InterruptedException ie) {
276 unexpectedException();
277 }
278 }
279
280 /**
281 *
282 */
283 public void testBlockingPut() {
284 Thread t = new Thread(new Runnable() {
285 public void run() {
286 int added = 0;
287 try {
288 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
289 for (int i = 0; i < SIZE; ++i) {
290 q.put(new Integer(i));
291 ++added;
292 }
293 q.put(new Integer(SIZE));
294 threadShouldThrow();
295 } catch (InterruptedException ie){
296 threadAssertEquals(added, SIZE);
297 }
298 }});
299 t.start();
300 try {
301 Thread.sleep(SHORT_DELAY_MS);
302 t.interrupt();
303 t.join();
304 }
305 catch (InterruptedException ie) {
306 unexpectedException();
307 }
308 }
309
310 /**
311 *
312 */
313 public void testPutWithTake() {
314 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
315 Thread t = new Thread(new Runnable() {
316 public void run() {
317 int added = 0;
318 try {
319 q.put(new Object());
320 ++added;
321 q.put(new Object());
322 ++added;
323 q.put(new Object());
324 ++added;
325 q.put(new Object());
326 ++added;
327 threadShouldThrow();
328 } catch (InterruptedException e){
329 threadAssertTrue(added >= 2);
330 }
331 }
332 });
333 try {
334 t.start();
335 Thread.sleep(SHORT_DELAY_MS);
336 q.take();
337 t.interrupt();
338 t.join();
339 } catch (Exception e){
340 unexpectedException();
341 }
342 }
343
344 /**
345 *
346 */
347 public void testTimedOffer() {
348 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
349 Thread t = new Thread(new Runnable() {
350 public void run() {
351 try {
352 q.put(new Object());
353 q.put(new Object());
354 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
355 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
356 threadShouldThrow();
357 } catch (InterruptedException success){}
358 }
359 });
360
361 try {
362 t.start();
363 Thread.sleep(SMALL_DELAY_MS);
364 t.interrupt();
365 t.join();
366 } catch (Exception e){
367 unexpectedException();
368 }
369 }
370
371 /**
372 *
373 */
374 public void testTake() {
375 try {
376 LinkedBlockingQueue q = populatedQueue(SIZE);
377 for (int i = 0; i < SIZE; ++i) {
378 assertEquals(i, ((Integer)q.take()).intValue());
379 }
380 } catch (InterruptedException e){
381 unexpectedException();
382 }
383 }
384
385 /**
386 *
387 */
388 public void testTakeFromEmpty() {
389 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
390 Thread t = new Thread(new Runnable() {
391 public void run() {
392 try {
393 q.take();
394 threadShouldThrow();
395 } catch (InterruptedException success){ }
396 }
397 });
398 try {
399 t.start();
400 Thread.sleep(SHORT_DELAY_MS);
401 t.interrupt();
402 t.join();
403 } catch (Exception e){
404 unexpectedException();
405 }
406 }
407
408 /**
409 *
410 */
411 public void testBlockingTake() {
412 Thread t = new Thread(new Runnable() {
413 public void run() {
414 try {
415 LinkedBlockingQueue q = populatedQueue(SIZE);
416 for (int i = 0; i < SIZE; ++i) {
417 assertEquals(i, ((Integer)q.take()).intValue());
418 }
419 q.take();
420 threadShouldThrow();
421 } catch (InterruptedException success){
422 }
423 }});
424 t.start();
425 try {
426 Thread.sleep(SHORT_DELAY_MS);
427 t.interrupt();
428 t.join();
429 }
430 catch (InterruptedException ie) {
431 unexpectedException();
432 }
433 }
434
435
436 /**
437 *
438 */
439 public void testPoll() {
440 LinkedBlockingQueue q = populatedQueue(SIZE);
441 for (int i = 0; i < SIZE; ++i) {
442 assertEquals(i, ((Integer)q.poll()).intValue());
443 }
444 assertNull(q.poll());
445 }
446
447 /**
448 *
449 */
450 public void testTimedPoll0() {
451 try {
452 LinkedBlockingQueue q = populatedQueue(SIZE);
453 for (int i = 0; i < SIZE; ++i) {
454 assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
455 }
456 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
457 } catch (InterruptedException e){
458 unexpectedException();
459 }
460 }
461
462 /**
463 *
464 */
465 public void testTimedPoll() {
466 try {
467 LinkedBlockingQueue q = populatedQueue(SIZE);
468 for (int i = 0; i < SIZE; ++i) {
469 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
470 }
471 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
472 } catch (InterruptedException e){
473 unexpectedException();
474 }
475 }
476
477 /**
478 *
479 */
480 public void testInterruptedTimedPoll() {
481 Thread t = new Thread(new Runnable() {
482 public void run() {
483 try {
484 LinkedBlockingQueue q = populatedQueue(SIZE);
485 for (int i = 0; i < SIZE; ++i) {
486 threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
487 }
488 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
489 } catch (InterruptedException success){
490 }
491 }});
492 t.start();
493 try {
494 Thread.sleep(SHORT_DELAY_MS);
495 t.interrupt();
496 t.join();
497 }
498 catch (InterruptedException ie) {
499 unexpectedException();
500 }
501 }
502
503 /**
504 *
505 */
506 public void testTimedPollWithOffer() {
507 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
508 Thread t = new Thread(new Runnable() {
509 public void run() {
510 try {
511 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
512 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
513 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
514 threadShouldThrow();
515 } catch (InterruptedException success) { }
516 }
517 });
518 try {
519 t.start();
520 Thread.sleep(SMALL_DELAY_MS);
521 assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
522 t.interrupt();
523 t.join();
524 } catch (Exception e){
525 unexpectedException();
526 }
527 }
528
529
530 /**
531 *
532 */
533 public void testPeek() {
534 LinkedBlockingQueue q = populatedQueue(SIZE);
535 for (int i = 0; i < SIZE; ++i) {
536 assertEquals(i, ((Integer)q.peek()).intValue());
537 q.poll();
538 assertTrue(q.peek() == null ||
539 i != ((Integer)q.peek()).intValue());
540 }
541 assertNull(q.peek());
542 }
543
544 /**
545 *
546 */
547 public void testElement() {
548 LinkedBlockingQueue q = populatedQueue(SIZE);
549 for (int i = 0; i < SIZE; ++i) {
550 assertEquals(i, ((Integer)q.element()).intValue());
551 q.poll();
552 }
553 try {
554 q.element();
555 shouldThrow();
556 }
557 catch (NoSuchElementException success) {}
558 }
559
560 /**
561 *
562 */
563 public void testRemove() {
564 LinkedBlockingQueue q = populatedQueue(SIZE);
565 for (int i = 0; i < SIZE; ++i) {
566 assertEquals(i, ((Integer)q.remove()).intValue());
567 }
568 try {
569 q.remove();
570 shouldThrow();
571 } catch (NoSuchElementException success){
572 }
573 }
574
575 /**
576 *
577 */
578 public void testRemoveElement() {
579 LinkedBlockingQueue q = populatedQueue(SIZE);
580 for (int i = 1; i < SIZE; i+=2) {
581 assertTrue(q.remove(new Integer(i)));
582 }
583 for (int i = 0; i < SIZE; i+=2) {
584 assertTrue(q.remove(new Integer(i)));
585 assertFalse(q.remove(new Integer(i+1)));
586 }
587 assertTrue(q.isEmpty());
588 }
589
590 /**
591 *
592 */
593 public void testContains() {
594 LinkedBlockingQueue q = populatedQueue(SIZE);
595 for (int i = 0; i < SIZE; ++i) {
596 assertTrue(q.contains(new Integer(i)));
597 q.poll();
598 assertFalse(q.contains(new Integer(i)));
599 }
600 }
601
602 /**
603 *
604 */
605 public void testClear() {
606 LinkedBlockingQueue q = populatedQueue(SIZE);
607 q.clear();
608 assertTrue(q.isEmpty());
609 assertEquals(0, q.size());
610 assertEquals(SIZE, q.remainingCapacity());
611 q.add(one);
612 assertFalse(q.isEmpty());
613 q.clear();
614 assertTrue(q.isEmpty());
615 }
616
617 /**
618 *
619 */
620 public void testContainsAll() {
621 LinkedBlockingQueue q = populatedQueue(SIZE);
622 LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
623 for (int i = 0; i < SIZE; ++i) {
624 assertTrue(q.containsAll(p));
625 assertFalse(p.containsAll(q));
626 p.add(new Integer(i));
627 }
628 assertTrue(p.containsAll(q));
629 }
630
631 /**
632 *
633 */
634 public void testRetainAll() {
635 LinkedBlockingQueue q = populatedQueue(SIZE);
636 LinkedBlockingQueue p = populatedQueue(SIZE);
637 for (int i = 0; i < SIZE; ++i) {
638 boolean changed = q.retainAll(p);
639 if (i == 0)
640 assertFalse(changed);
641 else
642 assertTrue(changed);
643
644 assertTrue(q.containsAll(p));
645 assertEquals(SIZE-i, q.size());
646 p.remove();
647 }
648 }
649
650 /**
651 *
652 */
653 public void testRemoveAll() {
654 for (int i = 1; i < SIZE; ++i) {
655 LinkedBlockingQueue q = populatedQueue(SIZE);
656 LinkedBlockingQueue p = populatedQueue(i);
657 assertTrue(q.removeAll(p));
658 assertEquals(SIZE-i, q.size());
659 for (int j = 0; j < i; ++j) {
660 Integer I = (Integer)(p.remove());
661 assertFalse(q.contains(I));
662 }
663 }
664 }
665
666
667 /**
668 *
669 */
670 public void testToArray() {
671 LinkedBlockingQueue q = populatedQueue(SIZE);
672 Object[] o = q.toArray();
673 try {
674 for(int i = 0; i < o.length; i++)
675 assertEquals(o[i], q.take());
676 } catch (InterruptedException e){
677 unexpectedException();
678 }
679 }
680
681 /**
682 *
683 */
684 public void testToArray2() {
685 LinkedBlockingQueue q = populatedQueue(SIZE);
686 Integer[] ints = new Integer[SIZE];
687 ints = (Integer[])q.toArray(ints);
688 try {
689 for(int i = 0; i < ints.length; i++)
690 assertEquals(ints[i], q.take());
691 } catch (InterruptedException e){
692 unexpectedException();
693 }
694 }
695
696 /**
697 *
698 */
699 public void testIterator() {
700 LinkedBlockingQueue q = populatedQueue(SIZE);
701 Iterator it = q.iterator();
702 try {
703 while(it.hasNext()){
704 assertEquals(it.next(), q.take());
705 }
706 } catch (InterruptedException e){
707 unexpectedException();
708 }
709 }
710
711 /**
712 *
713 */
714 public void testIteratorOrdering() {
715
716 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
717
718 q.add(one);
719 q.add(two);
720 q.add(three);
721
722 assertEquals(0, q.remainingCapacity());
723
724 int k = 0;
725 for (Iterator it = q.iterator(); it.hasNext();) {
726 int i = ((Integer)(it.next())).intValue();
727 assertEquals(++k, i);
728 }
729
730 assertEquals(3, k);
731 }
732
733 /**
734 *
735 */
736 public void testWeaklyConsistentIteration () {
737
738 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
739
740 q.add(one);
741 q.add(two);
742 q.add(three);
743
744 try {
745 for (Iterator it = q.iterator(); it.hasNext();) {
746 q.remove();
747 it.next();
748 }
749 }
750 catch (ConcurrentModificationException e) {
751 unexpectedException();
752 }
753
754 assertEquals(0, q.size());
755 }
756
757
758 /**
759 *
760 */
761 public void testToString() {
762 LinkedBlockingQueue q = populatedQueue(SIZE);
763 String s = q.toString();
764 for (int i = 0; i < SIZE; ++i) {
765 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
766 }
767 }
768
769
770 /**
771 *
772 */
773 public void testOfferInExecutor() {
774
775 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
776
777 q.add(one);
778 q.add(two);
779
780 ExecutorService executor = Executors.newFixedThreadPool(2);
781
782 executor.execute(new Runnable() {
783 public void run() {
784 threadAssertFalse(q.offer(three));
785 try {
786 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
787 threadAssertEquals(0, q.remainingCapacity());
788 }
789 catch (InterruptedException e) {
790 threadUnexpectedException();
791 }
792 }
793 });
794
795 executor.execute(new Runnable() {
796 public void run() {
797 try {
798 Thread.sleep(SMALL_DELAY_MS);
799 threadAssertEquals(one, q.take());
800 }
801 catch (InterruptedException e) {
802 threadUnexpectedException();
803 }
804 }
805 });
806
807 joinPool(executor);
808
809 }
810
811 /**
812 *
813 */
814 public void testPollInExecutor() {
815
816 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
817
818 ExecutorService executor = Executors.newFixedThreadPool(2);
819
820 executor.execute(new Runnable() {
821 public void run() {
822 threadAssertNull(q.poll());
823 try {
824 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
825 threadAssertTrue(q.isEmpty());
826 }
827 catch (InterruptedException e) {
828 threadUnexpectedException();
829 }
830 }
831 });
832
833 executor.execute(new Runnable() {
834 public void run() {
835 try {
836 Thread.sleep(SMALL_DELAY_MS);
837 q.put(one);
838 }
839 catch (InterruptedException e) {
840 threadUnexpectedException();
841 }
842 }
843 });
844
845 joinPool(executor);
846 }
847
848 /**
849 *
850 */
851 public void testSerialization() {
852 LinkedBlockingQueue q = populatedQueue(SIZE);
853
854 try {
855 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
856 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
857 out.writeObject(q);
858 out.close();
859
860 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
861 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
862 LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
863 assertEquals(q.size(), r.size());
864 while (!q.isEmpty())
865 assertEquals(q.remove(), r.remove());
866 } catch(Exception e){
867 unexpectedException();
868 }
869 }
870
871 }