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.3 by dl, Sun Sep 14 20:42:40 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 20 | Line 21 | public class SynchronousQueueTest extend
21          return new TestSuite(SynchronousQueueTest.class);
22      }
23  
24 +    /**
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 <    public void testOfferNull(){
35 >    /**
36 >     * offer(null) throws NPE
37 >     */
38 >    public void testOfferNull() {
39          try {
40              SynchronousQueue q = new SynchronousQueue();
41              q.offer(null);
42 <            fail("should throw NPE");
42 >            shouldThrow();
43          } catch (NullPointerException success) { }  
44      }
45  
46 <    public void testOffer(){
47 <        SynchronousQueue q = new SynchronousQueue();
48 <        assertFalse(q.offer(new Integer(1)));
46 >    /**
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(one));
63      }
64  
65 <    public void testAdd(){
65 >    /**
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 <    public void testAddAll1(){
78 >    /**
79 >     * addAll(null) throws NPE
80 >     */
81 >    public void testAddAll1() {
82          try {
83              SynchronousQueue q = new SynchronousQueue();
84              q.addAll(null);
85 <            fail("Cannot add null collection");
85 >            shouldThrow();
86          }
87          catch (NullPointerException success) {}
88      }
89 <    public void testAddAll2(){
89 >
90 >    /**
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 {
107              SynchronousQueue q = new SynchronousQueue();
108              Integer[] ints = new Integer[1];
109              q.addAll(Arrays.asList(ints));
110 <            fail("Cannot add null elements");
110 >            shouldThrow();
111          }
112          catch (NullPointerException success) {}
113      }
114 <    public void testAddAll4(){
114 >    /**
115 >     * addAll throws ISE if no active taker
116 >     */
117 >    public void testAddAll4() {
118          try {
119              SynchronousQueue q = new SynchronousQueue();
120              Integer[] ints = new Integer[1];
121              for (int i = 0; i < 1; ++i)
122                  ints[i] = new Integer(i);
123              q.addAll(Arrays.asList(ints));
124 <            fail("Cannot add with insufficient capacity");
124 >            shouldThrow();
125          }
126          catch (IllegalStateException success) {}
127      }
128  
129 +    /**
130 +     * put(null) throws NPE
131 +     */
132      public void testPutNull() {
133          try {
134              SynchronousQueue q = new SynchronousQueue();
135              q.put(null);
136 <            fail("put should throw NPE");
136 >            shouldThrow();
137          }
138          catch (NullPointerException success){
139          }  
140          catch (InterruptedException ie) {
141 <            fail("Unexpected exception");
141 >            unexpectedException();
142          }
143       }
144  
145 <    public void testBlockingPut(){
145 >    /**
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));
154 <                        threadFail("put should block");
153 >                        q.put(zero);
154 >                        threadShouldThrow();
155                      } catch (InterruptedException ie){
156                      }  
157                  }});
# Line 109 | Line 162 | public class SynchronousQueueTest extend
162             t.join();
163          }
164          catch (InterruptedException ie) {
165 <            fail("Unexpected exception");
165 >            unexpectedException();
166          }
167      }
168  
169 +    /**
170 +     * put blocks waiting for take
171 +     */
172      public void testPutWithTake() {
173          final SynchronousQueue q = new SynchronousQueue();
174          Thread t = new Thread(new Runnable() {
175 <                public void run(){
175 >                public void run() {
176                      int added = 0;
177                      try {
178                          q.put(new Object());
# Line 127 | Line 183 | public class SynchronousQueueTest extend
183                          ++added;
184                          q.put(new Object());
185                          ++added;
186 <                        threadFail("Should block");
186 >                        threadShouldThrow();
187                      } catch (InterruptedException e){
188                          assertTrue(added >= 1);
189                      }
# Line 141 | Line 197 | public class SynchronousQueueTest extend
197              t.interrupt();
198              t.join();
199          } catch (Exception e){
200 <            fail("Unexpected exception");
200 >            unexpectedException();
201          }
202      }
203  
204 +    /**
205 +     * timed offer times out if elements not taken
206 +     */
207      public void testTimedOffer() {
208          final SynchronousQueue q = new SynchronousQueue();
209          Thread t = new Thread(new Runnable() {
210 <                public void run(){
210 >                public void run() {
211                      try {
212  
213                          threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
214                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
215 <                        threadFail("Should block");
215 >                        threadShouldThrow();
216                      } catch (InterruptedException success){}
217                  }
218              });
# Line 164 | Line 223 | public class SynchronousQueueTest extend
223              t.interrupt();
224              t.join();
225          } catch (Exception e){
226 <            fail("Unexpected exception");
226 >            unexpectedException();
227          }
228      }
229  
230  
231 +    /**
232 +     * take blocks interruptibly when empty
233 +     */
234      public void testTakeFromEmpty() {
235          final SynchronousQueue q = new SynchronousQueue();
236          Thread t = new Thread(new Runnable() {
237 <                public void run(){
237 >                public void run() {
238                      try {
239                          q.take();
240 <                        threadFail("Should block");
240 >                        threadShouldThrow();
241                      } catch (InterruptedException success){ }                
242                  }
243              });
# Line 185 | Line 247 | public class SynchronousQueueTest extend
247              t.interrupt();
248              t.join();
249          } catch (Exception e){
250 <            fail("Unexpected exception");
250 >            unexpectedException();
251          }
252      }
253  
254 <    public void testPoll(){
254 >    /**
255 >     * poll fails unless active taker
256 >     */
257 >    public void testPoll() {
258          SynchronousQueue q = new SynchronousQueue();
259          assertNull(q.poll());
260      }
261  
262 +    /**
263 +     * timed pool with zero timeout times out if no active taker
264 +     */
265      public void testTimedPoll0() {
266          try {
267              SynchronousQueue q = new SynchronousQueue();
268              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
269          } catch (InterruptedException e){
270 <            fail("Unexpected exception");
270 >            unexpectedException();
271          }  
272      }
273  
274 +    /**
275 +     * timed pool with nonzero timeout times out if no active taker
276 +     */
277      public void testTimedPoll() {
278          try {
279              SynchronousQueue q = new SynchronousQueue();
280              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
281          } catch (InterruptedException e){
282 <            fail("Unexpected exception");
282 >            unexpectedException();
283          }  
284      }
285  
286 <    public void testInterruptedTimedPoll(){
286 >    /**
287 >     * Interrupted timed poll throws InterruptedException instead of
288 >     * returning timeout status
289 >     */
290 >    public void testInterruptedTimedPoll() {
291          Thread t = new Thread(new Runnable() {
292                  public void run() {
293                      try {
# Line 228 | Line 303 | public class SynchronousQueueTest extend
303             t.join();
304          }
305          catch (InterruptedException ie) {
306 <            fail("Unexpected exception");
306 >            unexpectedException();
307          }
308      }
309  
310 <    public void testTimedPollWithOffer(){
310 >    /**
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();
316          Thread t = new Thread(new Runnable() {
317 <                public void run(){
317 >                public void run() {
318                      try {
319                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
320                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
321                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
322 <                        threadFail("Should block");
322 >                        threadShouldThrow();
323                      } catch (InterruptedException success) { }                
324                  }
325              });
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){
333 <            fail("Unexpected exception");
333 >            unexpectedException();
334          }
335      }  
336  
337  
338 <    public void testPeek(){
338 >    /**
339 >     * peek returns null
340 >     */
341 >    public void testPeek() {
342          SynchronousQueue q = new SynchronousQueue();
343          assertNull(q.peek());
344      }
345  
346 <    public void testElement(){
346 >    /**
347 >     * element throws NSEE
348 >     */
349 >    public void testElement() {
350          SynchronousQueue q = new SynchronousQueue();
351          try {
352              q.element();
353 <            fail("no such element");
353 >            shouldThrow();
354          }
355          catch (NoSuchElementException success) {}
356      }
357  
358 <    public void testRemove(){
358 >    /**
359 >     * remove throws NSEE if no active taker
360 >     */
361 >    public void testRemove() {
362          SynchronousQueue q = new SynchronousQueue();
363          try {
364              q.remove();
365 <            fail("remove should throw");
365 >            shouldThrow();
366          } catch (NoSuchElementException success){
367          }  
368      }
369  
370 <    public void testRemoveElement(){
370 >    /**
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 <    public void testContains(){
380 <        SynchronousQueue q = new SynchronousQueue();
381 <        assertFalse(q.contains(new Integer(0)));
379 >    /**
380 >     * contains returns false
381 >     */
382 >    public void testContains() {
383 >        SynchronousQueue q = new SynchronousQueue();
384 >        assertFalse(q.contains(zero));
385      }
386  
387 <    public void testClear(){
387 >    /**
388 >     * clear ensures isEmpty
389 >     */
390 >    public void testClear() {
391          SynchronousQueue q = new SynchronousQueue();
392          q.clear();
393          assertTrue(q.isEmpty());
394      }
395  
396 <    public void testContainsAll(){
396 >    /**
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);
403 <        //        assertTrue(q.containsAll(Arrays.asList(empty)));
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 <    public void testRetainAll(){
407 >    /**
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 <        //        assertTrue(q.containsAll(Arrays.asList(empty)));
313 <        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 <    public void testRemoveAll(){
418 >    /**
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));
321 <        //        assertTrue(q.containsAll(Arrays.asList(empty)));
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 <    public void testToArray(){
430 >    /**
431 >     * toArray is empty
432 >     */
433 >    public void testToArray() {
434          SynchronousQueue q = new SynchronousQueue();
435          Object[] o = q.toArray();
436          assertEquals(o.length, 0);
437      }
438  
439 <    public void testToArray2(){
439 >    /**
440 >     * toArray(a) is nulled at position 0
441 >     */
442 >    public void testToArray2() {
443          SynchronousQueue q = new SynchronousQueue();
444          Integer[] ints = new Integer[1];
445          assertNull(ints[0]);
446      }
447      
448 <    public void testIterator(){
448 >    /**
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();
465          Iterator it = q.iterator();
466          assertFalse(it.hasNext());
467          try {
468              Object x = it.next();
469 <            fail("should throw");
469 >            shouldThrow();
470          }
471          catch (NoSuchElementException success) {}
472      }
473  
474 <    public void testIteratorRemove(){
474 >    /**
475 >     * iterator remove throws ISE
476 >     */
477 >    public void testIteratorRemove() {
478          SynchronousQueue q = new SynchronousQueue();
479          Iterator it = q.iterator();
480          try {
481              it.remove();
482 <            fail("should throw");
482 >            shouldThrow();
483          }
484          catch (IllegalStateException success) {}
485      }
486  
487 <    public void testToString(){
487 >    /**
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 +     * offer transfers elements across Executor tasks
499 +     */
500      public void testOfferInExecutor() {
501          final SynchronousQueue q = new SynchronousQueue();
502          ExecutorService executor = Executors.newFixedThreadPool(2);
# Line 376 | Line 510 | public class SynchronousQueueTest extend
510                      threadAssertEquals(0, q.remainingCapacity());
511                  }
512                  catch (InterruptedException e) {
513 <                    threadFail("should not be interrupted");
513 >                    threadUnexpectedException();
514                  }
515              }
516          });
# Line 388 | Line 522 | public class SynchronousQueueTest extend
522                      threadAssertEquals(one, q.take());
523                  }
524                  catch (InterruptedException e) {
525 <                    fail("should not be interrupted");
525 >                    threadUnexpectedException();
526                  }
527              }
528          });
# Line 397 | Line 531 | public class SynchronousQueueTest extend
531  
532      }
533  
534 +    /**
535 +     * poll retrieves elements across Executor threads
536 +     */
537      public void testPollInExecutor() {
401
538          final SynchronousQueue q = new SynchronousQueue();
403
539          ExecutorService executor = Executors.newFixedThreadPool(2);
405
540          executor.execute(new Runnable() {
541              public void run() {
542                  threadAssertNull(q.poll());
# Line 411 | Line 545 | public class SynchronousQueueTest extend
545                      threadAssertTrue(q.isEmpty());
546                  }
547                  catch (InterruptedException e) {
548 <                    threadFail("should not be interrupted");
548 >                    threadUnexpectedException();
549                  }
550              }
551          });
# Line 423 | Line 557 | public class SynchronousQueueTest extend
557                      q.put(new Integer(1));
558                  }
559                  catch (InterruptedException e) {
560 <                    threadFail("should not be interrupted");
560 >                    threadUnexpectedException();
561                  }
562              }
563          });
564          
565          joinPool(executor);
432
566      }
567  
568 +    /**
569 +     * a deserialized serialized queue is usable
570 +     */
571      public void testSerialization() {
572          SynchronousQueue q = new SynchronousQueue();
573          try {
# Line 447 | Line 583 | public class SynchronousQueueTest extend
583              while (!q.isEmpty())
584                  assertEquals(q.remove(), r.remove());
585          } catch(Exception e){
586 <            fail("unexpected exception");
586 >            unexpectedException();
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