ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SynchronousQueueTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 18:20:08 2003 UTC vs.
Revision 1.7 by dl, Sat Dec 27 19:26:44 2003 UTC

# Line 1 | Line 1
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.
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 Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 21 | Line 22 | public class SynchronousQueueTest extend
22      }
23  
24      /**
25 <     *
25 >     * A SynchronousQueue is both empty and full
26       */
27      public void testEmptyFull() {
28          SynchronousQueue q = new SynchronousQueue();
29          assertTrue(q.isEmpty());
30          assertEquals(0, q.size());
31          assertEquals(0, q.remainingCapacity());
32 <        assertFalse(q.offer(new Integer(3)));
32 >        assertFalse(q.offer(zero));
33      }
34  
35      /**
36 <     *
36 >     * offer(null) throws NPE
37       */
38      public void testOfferNull() {
39          try {
# Line 43 | Line 44 | public class SynchronousQueueTest extend
44      }
45  
46      /**
47 <     *
47 >     * add(null) throws NPE
48 >     */
49 >    public void testAddNull() {
50 >        try {
51 >            SynchronousQueue q = new SynchronousQueue();
52 >            q.add(null);
53 >            shouldThrow();
54 >        } catch (NullPointerException success) { }  
55 >    }
56 >
57 >    /**
58 >     * offer fails if no active taker
59       */
60      public void testOffer() {
61          SynchronousQueue q = new SynchronousQueue();
62 <        assertFalse(q.offer(new Integer(1)));
62 >        assertFalse(q.offer(one));
63      }
64  
65      /**
66 <     *
66 >     * add throws ISE if no active taker
67       */
68      public void testAdd() {
69          try {
70              SynchronousQueue q = new SynchronousQueue();
71              assertEquals(0, q.remainingCapacity());
72 <            q.add(new Integer(0));
72 >            q.add(one);
73 >            shouldThrow();
74          } catch (IllegalStateException success){
75          }  
76      }
77  
78      /**
79 <     *
79 >     * addAll(null) throws NPE
80       */
81      public void testAddAll1() {
82          try {
# Line 73 | Line 86 | public class SynchronousQueueTest extend
86          }
87          catch (NullPointerException success) {}
88      }
89 +
90      /**
91 <     *
91 >     * addAll(this) throws IAE
92 >     */
93 >    public void testAddAllSelf() {
94 >        try {
95 >            SynchronousQueue q = new SynchronousQueue();
96 >            q.addAll(q);
97 >            shouldThrow();
98 >        }
99 >        catch (IllegalArgumentException success) {}
100 >    }
101 >
102 >    /**
103 >     * addAll of a collection with null elements throws NPE
104       */
105      public void testAddAll2() {
106          try {
# Line 86 | Line 112 | public class SynchronousQueueTest extend
112          catch (NullPointerException success) {}
113      }
114      /**
115 <     *
115 >     * addAll throws ISE if no active taker
116       */
117      public void testAddAll4() {
118          try {
# Line 101 | Line 127 | public class SynchronousQueueTest extend
127      }
128  
129      /**
130 <     *
130 >     * put(null) throws NPE
131       */
132      public void testPutNull() {
133          try {
# Line 117 | Line 143 | public class SynchronousQueueTest extend
143       }
144  
145      /**
146 <     *
146 >     * put blocks interruptibly if no active taker
147       */
148      public void testBlockingPut() {
149          Thread t = new Thread(new Runnable() {
150                  public void run() {
151                      try {
152                          SynchronousQueue q = new SynchronousQueue();
153 <                        q.put(new Integer(0));
153 >                        q.put(zero);
154                          threadShouldThrow();
155                      } catch (InterruptedException ie){
156                      }  
# Line 141 | Line 167 | public class SynchronousQueueTest extend
167      }
168  
169      /**
170 <     *
170 >     * put blocks waiting for take
171       */
172      public void testPutWithTake() {
173          final SynchronousQueue q = new SynchronousQueue();
# Line 176 | Line 202 | public class SynchronousQueueTest extend
202      }
203  
204      /**
205 <     *
205 >     * timed offer times out if elements not taken
206       */
207      public void testTimedOffer() {
208          final SynchronousQueue q = new SynchronousQueue();
# Line 203 | Line 229 | public class SynchronousQueueTest extend
229  
230  
231      /**
232 <     *
232 >     * take blocks interruptibly when empty
233       */
234      public void testTakeFromEmpty() {
235          final SynchronousQueue q = new SynchronousQueue();
# Line 226 | Line 252 | public class SynchronousQueueTest extend
252      }
253  
254      /**
255 <     *
255 >     * poll fails unless active taker
256       */
257      public void testPoll() {
258          SynchronousQueue q = new SynchronousQueue();
# Line 234 | Line 260 | public class SynchronousQueueTest extend
260      }
261  
262      /**
263 <     *
263 >     * timed pool with zero timeout times out if no active taker
264       */
265      public void testTimedPoll0() {
266          try {
# Line 246 | Line 272 | public class SynchronousQueueTest extend
272      }
273  
274      /**
275 <     *
275 >     * timed pool with nonzero timeout times out if no active taker
276       */
277      public void testTimedPoll() {
278          try {
# Line 258 | Line 284 | public class SynchronousQueueTest extend
284      }
285  
286      /**
287 <     *
287 >     * Interrupted timed poll throws InterruptedException instead of
288 >     * returning timeout status
289       */
290      public void testInterruptedTimedPoll() {
291          Thread t = new Thread(new Runnable() {
# Line 281 | Line 308 | public class SynchronousQueueTest extend
308      }
309  
310      /**
311 <     *
311 >     *  timed poll before a delayed offer fails; after offer succeeds;
312 >     *  on interruption throws
313       */
314      public void testTimedPollWithOffer() {
315          final SynchronousQueue q = new SynchronousQueue();
# Line 298 | Line 326 | public class SynchronousQueueTest extend
326          try {
327              t.start();
328              Thread.sleep(SMALL_DELAY_MS);
329 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
329 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
330              t.interrupt();
331              t.join();
332          } catch (Exception e){
# Line 308 | Line 336 | public class SynchronousQueueTest extend
336  
337  
338      /**
339 <     *
339 >     * peek returns null
340       */
341      public void testPeek() {
342          SynchronousQueue q = new SynchronousQueue();
# Line 316 | Line 344 | public class SynchronousQueueTest extend
344      }
345  
346      /**
347 <     *
347 >     * element throws NSEE
348       */
349      public void testElement() {
350          SynchronousQueue q = new SynchronousQueue();
# Line 328 | Line 356 | public class SynchronousQueueTest extend
356      }
357  
358      /**
359 <     *
359 >     * remove throws NSEE if no active taker
360       */
361      public void testRemove() {
362          SynchronousQueue q = new SynchronousQueue();
# Line 340 | Line 368 | public class SynchronousQueueTest extend
368      }
369  
370      /**
371 <     *
371 >     * remove(x) returns false
372       */
373      public void testRemoveElement() {
374          SynchronousQueue q = new SynchronousQueue();
375 <        assertFalse(q.remove(new Integer(0)));
375 >        assertFalse(q.remove(zero));
376          assertTrue(q.isEmpty());
377      }
378          
379      /**
380 <     *
380 >     * contains returns false
381       */
382      public void testContains() {
383          SynchronousQueue q = new SynchronousQueue();
384 <        assertFalse(q.contains(new Integer(0)));
384 >        assertFalse(q.contains(zero));
385      }
386  
387      /**
388 <     *
388 >     * clear ensures isEmpty
389       */
390      public void testClear() {
391          SynchronousQueue q = new SynchronousQueue();
# Line 366 | Line 394 | public class SynchronousQueueTest extend
394      }
395  
396      /**
397 <     *
397 >     * containsAll returns false unless empty
398       */
399      public void testContainsAll() {
400          SynchronousQueue q = new SynchronousQueue();
401          Integer[] empty = new Integer[0];
402 <        Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
402 >        assertTrue(q.containsAll(Arrays.asList(empty)));
403 >        Integer[] ints = new Integer[1]; ints[0] = zero;
404          assertFalse(q.containsAll(Arrays.asList(ints)));
405      }
406  
407      /**
408 <     *
408 >     * retainAll returns false
409       */
410      public void testRetainAll() {
411          SynchronousQueue q = new SynchronousQueue();
412          Integer[] empty = new Integer[0];
413 <        Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
414 <        q.retainAll(Arrays.asList(ints));
415 <        assertFalse(q.containsAll(Arrays.asList(ints)));
413 >        assertFalse(q.retainAll(Arrays.asList(empty)));
414 >        Integer[] ints = new Integer[1]; ints[0] = zero;
415 >        assertFalse(q.retainAll(Arrays.asList(ints)));
416      }
417  
418      /**
419 <     *
419 >     * removeAll returns false
420       */
421      public void testRemoveAll() {
422          SynchronousQueue q = new SynchronousQueue();
423          Integer[] empty = new Integer[0];
424 <        Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
425 <        q.removeAll(Arrays.asList(ints));
424 >        assertFalse(q.removeAll(Arrays.asList(empty)));
425 >        Integer[] ints = new Integer[1]; ints[0] = zero;
426          assertFalse(q.containsAll(Arrays.asList(ints)));
427      }
428  
429  
430      /**
431 <     *
431 >     * toArray is empty
432       */
433      public void testToArray() {
434          SynchronousQueue q = new SynchronousQueue();
# Line 408 | Line 437 | public class SynchronousQueueTest extend
437      }
438  
439      /**
440 <     *
440 >     * toArray(a) is nulled at position 0
441       */
442      public void testToArray2() {
443          SynchronousQueue q = new SynchronousQueue();
# Line 417 | Line 446 | public class SynchronousQueueTest extend
446      }
447      
448      /**
449 <     *
449 >     * toArray(null) throws NPE
450 >     */
451 >    public void testToArray_BadArg() {
452 >        try {
453 >            SynchronousQueue q = new SynchronousQueue();
454 >            Object o[] = q.toArray(null);
455 >            shouldThrow();
456 >        } catch(NullPointerException success){}
457 >    }
458 >
459 >
460 >    /**
461 >     * iterator does not traverse any elements
462       */
463      public void testIterator() {
464          SynchronousQueue q = new SynchronousQueue();
# Line 431 | Line 472 | public class SynchronousQueueTest extend
472      }
473  
474      /**
475 <     *
475 >     * iterator remove throws ISE
476       */
477      public void testIteratorRemove() {
478          SynchronousQueue q = new SynchronousQueue();
# Line 444 | Line 485 | public class SynchronousQueueTest extend
485      }
486  
487      /**
488 <     *
488 >     * toString returns a non-null string
489       */
490      public void testToString() {
491          SynchronousQueue q = new SynchronousQueue();
492          String s = q.toString();
493 <        assertTrue(s != null);
493 >        assertNotNull(s);
494      }        
495  
496  
497      /**
498 <     *
498 >     * offer transfers elements across Executor tasks
499       */
500      public void testOfferInExecutor() {
501          final SynchronousQueue q = new SynchronousQueue();
# Line 491 | Line 532 | public class SynchronousQueueTest extend
532      }
533  
534      /**
535 <     *
535 >     * poll retrieves elements across Executor threads
536       */
537      public void testPollInExecutor() {
497
538          final SynchronousQueue q = new SynchronousQueue();
499
539          ExecutorService executor = Executors.newFixedThreadPool(2);
501
540          executor.execute(new Runnable() {
541              public void run() {
542                  threadAssertNull(q.poll());
# Line 525 | Line 563 | public class SynchronousQueueTest extend
563          });
564          
565          joinPool(executor);
528
566      }
567  
568      /**
569 <     *
569 >     * a deserialized serialized queue is usable
570       */
571      public void testSerialization() {
572          SynchronousQueue q = new SynchronousQueue();
# Line 550 | Line 587 | public class SynchronousQueueTest extend
587          }
588      }
589  
590 +    /**
591 +     * drainTo(null) throws NPE
592 +     */
593 +    public void testDrainToNull() {
594 +        SynchronousQueue q = new SynchronousQueue();
595 +        try {
596 +            q.drainTo(null);
597 +            shouldThrow();
598 +        } catch(NullPointerException success) {
599 +        }
600 +    }
601 +
602 +    /**
603 +     * drainTo(this) throws IAE
604 +     */
605 +    public void testDrainToSelf() {
606 +        SynchronousQueue q = new SynchronousQueue();
607 +        try {
608 +            q.drainTo(q);
609 +            shouldThrow();
610 +        } catch(IllegalArgumentException success) {
611 +        }
612 +    }
613 +
614 +    /**
615 +     * drainTo(c) of empty queue doesn't transfer elements
616 +     */
617 +    public void testDrainTo() {
618 +        SynchronousQueue q = new SynchronousQueue();
619 +        ArrayList l = new ArrayList();
620 +        q.drainTo(l);
621 +        assertEquals(q.size(), 0);
622 +        assertEquals(l.size(), 0);
623 +    }
624 +
625 +    /**
626 +     * drainTo empties queue, unblocking a waiting put.
627 +     */
628 +    public void testDrainToWithActivePut() {
629 +        final SynchronousQueue q = new SynchronousQueue();
630 +        Thread t = new Thread(new Runnable() {
631 +                public void run() {
632 +                    try {
633 +                        q.put(new Integer(1));
634 +                    } catch (InterruptedException ie){
635 +                        threadUnexpectedException();
636 +                    }
637 +                }
638 +            });
639 +        try {
640 +            t.start();
641 +            ArrayList l = new ArrayList();
642 +            Thread.sleep(SHORT_DELAY_MS);
643 +            q.drainTo(l);
644 +            assertTrue(l.size() <= 1);
645 +            if (l.size() > 0)
646 +                assertEquals(l.get(0), new Integer(1));
647 +            t.join();
648 +            assertTrue(l.size() <= 1);
649 +        } catch(Exception e){
650 +            unexpectedException();
651 +        }
652 +    }
653 +
654 +    /**
655 +     * drainTo(null, n) throws NPE
656 +     */
657 +    public void testDrainToNullN() {
658 +        SynchronousQueue q = new SynchronousQueue();
659 +        try {
660 +            q.drainTo(null, 0);
661 +            shouldThrow();
662 +        } catch(NullPointerException success) {
663 +        }
664 +    }
665 +
666 +    /**
667 +     * drainTo(this, n) throws IAE
668 +     */
669 +    public void testDrainToSelfN() {
670 +        SynchronousQueue q = new SynchronousQueue();
671 +        try {
672 +            q.drainTo(q, 0);
673 +            shouldThrow();
674 +        } catch(IllegalArgumentException success) {
675 +        }
676 +    }
677 +
678 +    /**
679 +     * drainTo(c, n) empties up to n elements of queue into c
680 +     */
681 +    public void testDrainToN() {
682 +        final SynchronousQueue q = new SynchronousQueue();
683 +        Thread t1 = new Thread(new Runnable() {
684 +                public void run() {
685 +                    try {
686 +                        q.put(one);
687 +                    } catch (InterruptedException ie){
688 +                        threadUnexpectedException();
689 +                    }
690 +                }
691 +            });
692 +        Thread t2 = new Thread(new Runnable() {
693 +                public void run() {
694 +                    try {
695 +                        q.put(two);
696 +                    } catch (InterruptedException ie){
697 +                        threadUnexpectedException();
698 +                    }
699 +                }
700 +            });
701 +
702 +        try {
703 +            t1.start();
704 +            t2.start();
705 +            ArrayList l = new ArrayList();
706 +            Thread.sleep(SHORT_DELAY_MS);
707 +            q.drainTo(l, 1);
708 +            assertTrue(l.size() == 1);
709 +            q.drainTo(l, 1);
710 +            assertTrue(l.size() == 2);
711 +            assertTrue(l.contains(one));
712 +            assertTrue(l.contains(two));
713 +            t1.join();
714 +            t2.join();
715 +        } catch(Exception e){
716 +            unexpectedException();
717 +        }
718 +    }
719 +
720 +
721   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines