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.6 by dl, Sun Oct 5 23:00:40 2003 UTC

# Line 20 | Line 20 | public class SynchronousQueueTest extend
20          return new TestSuite(SynchronousQueueTest.class);
21      }
22  
23 +    /**
24 +     * A SynchronousQueue is both empty and full
25 +     */
26      public void testEmptyFull() {
27          SynchronousQueue q = new SynchronousQueue();
28          assertTrue(q.isEmpty());
29          assertEquals(0, q.size());
30          assertEquals(0, q.remainingCapacity());
31 <        assertFalse(q.offer(new Integer(3)));
31 >        assertFalse(q.offer(zero));
32      }
33  
34 <    public void testOfferNull(){
34 >    /**
35 >     * offer(null) throws NPE
36 >     */
37 >    public void testOfferNull() {
38          try {
39              SynchronousQueue q = new SynchronousQueue();
40              q.offer(null);
41 <            fail("should throw NPE");
41 >            shouldThrow();
42          } catch (NullPointerException success) { }  
43      }
44  
45 <    public void testOffer(){
46 <        SynchronousQueue q = new SynchronousQueue();
47 <        assertFalse(q.offer(new Integer(1)));
45 >    /**
46 >     * add(null) throws NPE
47 >     */
48 >    public void testAddNull() {
49 >        try {
50 >            SynchronousQueue q = new SynchronousQueue();
51 >            q.add(null);
52 >            shouldThrow();
53 >        } catch (NullPointerException success) { }  
54 >    }
55 >
56 >    /**
57 >     * offer fails if no active taker
58 >     */
59 >    public void testOffer() {
60 >        SynchronousQueue q = new SynchronousQueue();
61 >        assertFalse(q.offer(one));
62      }
63  
64 <    public void testAdd(){
64 >    /**
65 >     * add throws ISE if no active taker
66 >     */
67 >    public void testAdd() {
68          try {
69              SynchronousQueue q = new SynchronousQueue();
70              assertEquals(0, q.remainingCapacity());
71 <            q.add(new Integer(0));
71 >            q.add(one);
72 >            shouldThrow();
73          } catch (IllegalStateException success){
74          }  
75      }
76  
77 <    public void testAddAll1(){
77 >    /**
78 >     * addAll(null) throws NPE
79 >     */
80 >    public void testAddAll1() {
81          try {
82              SynchronousQueue q = new SynchronousQueue();
83              q.addAll(null);
84 <            fail("Cannot add null collection");
84 >            shouldThrow();
85          }
86          catch (NullPointerException success) {}
87      }
88 <    public void testAddAll2(){
88 >
89 >    /**
90 >     * addAll(this) throws IAE
91 >     */
92 >    public void testAddAllSelf() {
93 >        try {
94 >            SynchronousQueue q = new SynchronousQueue();
95 >            q.addAll(q);
96 >            shouldThrow();
97 >        }
98 >        catch (IllegalArgumentException success) {}
99 >    }
100 >
101 >    /**
102 >     * addAll of a collection with null elements throws NPE
103 >     */
104 >    public void testAddAll2() {
105          try {
106              SynchronousQueue q = new SynchronousQueue();
107              Integer[] ints = new Integer[1];
108              q.addAll(Arrays.asList(ints));
109 <            fail("Cannot add null elements");
109 >            shouldThrow();
110          }
111          catch (NullPointerException success) {}
112      }
113 <    public void testAddAll4(){
113 >    /**
114 >     * addAll throws ISE if no active taker
115 >     */
116 >    public void testAddAll4() {
117          try {
118              SynchronousQueue q = new SynchronousQueue();
119              Integer[] ints = new Integer[1];
120              for (int i = 0; i < 1; ++i)
121                  ints[i] = new Integer(i);
122              q.addAll(Arrays.asList(ints));
123 <            fail("Cannot add with insufficient capacity");
123 >            shouldThrow();
124          }
125          catch (IllegalStateException success) {}
126      }
127  
128 +    /**
129 +     * put(null) throws NPE
130 +     */
131      public void testPutNull() {
132          try {
133              SynchronousQueue q = new SynchronousQueue();
134              q.put(null);
135 <            fail("put should throw NPE");
135 >            shouldThrow();
136          }
137          catch (NullPointerException success){
138          }  
139          catch (InterruptedException ie) {
140 <            fail("Unexpected exception");
140 >            unexpectedException();
141          }
142       }
143  
144 <    public void testBlockingPut(){
144 >    /**
145 >     * put blocks interruptibly if no active taker
146 >     */
147 >    public void testBlockingPut() {
148          Thread t = new Thread(new Runnable() {
149                  public void run() {
150                      try {
151                          SynchronousQueue q = new SynchronousQueue();
152 <                        q.put(new Integer(0));
153 <                        threadFail("put should block");
152 >                        q.put(zero);
153 >                        threadShouldThrow();
154                      } catch (InterruptedException ie){
155                      }  
156                  }});
# Line 109 | Line 161 | public class SynchronousQueueTest extend
161             t.join();
162          }
163          catch (InterruptedException ie) {
164 <            fail("Unexpected exception");
164 >            unexpectedException();
165          }
166      }
167  
168 +    /**
169 +     * put blocks waiting for take
170 +     */
171      public void testPutWithTake() {
172          final SynchronousQueue q = new SynchronousQueue();
173          Thread t = new Thread(new Runnable() {
174 <                public void run(){
174 >                public void run() {
175                      int added = 0;
176                      try {
177                          q.put(new Object());
# Line 127 | Line 182 | public class SynchronousQueueTest extend
182                          ++added;
183                          q.put(new Object());
184                          ++added;
185 <                        threadFail("Should block");
185 >                        threadShouldThrow();
186                      } catch (InterruptedException e){
187                          assertTrue(added >= 1);
188                      }
# Line 141 | Line 196 | public class SynchronousQueueTest extend
196              t.interrupt();
197              t.join();
198          } catch (Exception e){
199 <            fail("Unexpected exception");
199 >            unexpectedException();
200          }
201      }
202  
203 +    /**
204 +     * timed offer times out if elements not taken
205 +     */
206      public void testTimedOffer() {
207          final SynchronousQueue q = new SynchronousQueue();
208          Thread t = new Thread(new Runnable() {
209 <                public void run(){
209 >                public void run() {
210                      try {
211  
212                          threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
213                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
214 <                        threadFail("Should block");
214 >                        threadShouldThrow();
215                      } catch (InterruptedException success){}
216                  }
217              });
# Line 164 | Line 222 | public class SynchronousQueueTest extend
222              t.interrupt();
223              t.join();
224          } catch (Exception e){
225 <            fail("Unexpected exception");
225 >            unexpectedException();
226          }
227      }
228  
229  
230 +    /**
231 +     * take blocks interruptibly when empty
232 +     */
233      public void testTakeFromEmpty() {
234          final SynchronousQueue q = new SynchronousQueue();
235          Thread t = new Thread(new Runnable() {
236 <                public void run(){
236 >                public void run() {
237                      try {
238                          q.take();
239 <                        threadFail("Should block");
239 >                        threadShouldThrow();
240                      } catch (InterruptedException success){ }                
241                  }
242              });
# Line 185 | Line 246 | public class SynchronousQueueTest extend
246              t.interrupt();
247              t.join();
248          } catch (Exception e){
249 <            fail("Unexpected exception");
249 >            unexpectedException();
250          }
251      }
252  
253 <    public void testPoll(){
253 >    /**
254 >     * poll fails unless active taker
255 >     */
256 >    public void testPoll() {
257          SynchronousQueue q = new SynchronousQueue();
258          assertNull(q.poll());
259      }
260  
261 +    /**
262 +     * timed pool with zero timeout times out if no active taker
263 +     */
264      public void testTimedPoll0() {
265          try {
266              SynchronousQueue q = new SynchronousQueue();
267              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
268          } catch (InterruptedException e){
269 <            fail("Unexpected exception");
269 >            unexpectedException();
270          }  
271      }
272  
273 +    /**
274 +     * timed pool with nonzero timeout times out if no active taker
275 +     */
276      public void testTimedPoll() {
277          try {
278              SynchronousQueue q = new SynchronousQueue();
279              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
280          } catch (InterruptedException e){
281 <            fail("Unexpected exception");
281 >            unexpectedException();
282          }  
283      }
284  
285 <    public void testInterruptedTimedPoll(){
285 >    /**
286 >     * Interrupted timed poll throws InterruptedException instead of
287 >     * returning timeout status
288 >     */
289 >    public void testInterruptedTimedPoll() {
290          Thread t = new Thread(new Runnable() {
291                  public void run() {
292                      try {
# Line 228 | Line 302 | public class SynchronousQueueTest extend
302             t.join();
303          }
304          catch (InterruptedException ie) {
305 <            fail("Unexpected exception");
305 >            unexpectedException();
306          }
307      }
308  
309 <    public void testTimedPollWithOffer(){
309 >    /**
310 >     *  timed poll before a delayed offer fails; after offer succeeds;
311 >     *  on interruption throws
312 >     */
313 >    public void testTimedPollWithOffer() {
314          final SynchronousQueue q = new SynchronousQueue();
315          Thread t = new Thread(new Runnable() {
316 <                public void run(){
316 >                public void run() {
317                      try {
318                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
319                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
320                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
321 <                        threadFail("Should block");
321 >                        threadShouldThrow();
322                      } catch (InterruptedException success) { }                
323                  }
324              });
325          try {
326              t.start();
327              Thread.sleep(SMALL_DELAY_MS);
328 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
328 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
329              t.interrupt();
330              t.join();
331          } catch (Exception e){
332 <            fail("Unexpected exception");
332 >            unexpectedException();
333          }
334      }  
335  
336  
337 <    public void testPeek(){
337 >    /**
338 >     * peek returns null
339 >     */
340 >    public void testPeek() {
341          SynchronousQueue q = new SynchronousQueue();
342          assertNull(q.peek());
343      }
344  
345 <    public void testElement(){
345 >    /**
346 >     * element throws NSEE
347 >     */
348 >    public void testElement() {
349          SynchronousQueue q = new SynchronousQueue();
350          try {
351              q.element();
352 <            fail("no such element");
352 >            shouldThrow();
353          }
354          catch (NoSuchElementException success) {}
355      }
356  
357 <    public void testRemove(){
357 >    /**
358 >     * remove throws NSEE if no active taker
359 >     */
360 >    public void testRemove() {
361          SynchronousQueue q = new SynchronousQueue();
362          try {
363              q.remove();
364 <            fail("remove should throw");
364 >            shouldThrow();
365          } catch (NoSuchElementException success){
366          }  
367      }
368  
369 <    public void testRemoveElement(){
369 >    /**
370 >     * remove(x) returns false
371 >     */
372 >    public void testRemoveElement() {
373          SynchronousQueue q = new SynchronousQueue();
374 <        assertFalse(q.remove(new Integer(0)));
374 >        assertFalse(q.remove(zero));
375          assertTrue(q.isEmpty());
376      }
377          
378 <    public void testContains(){
379 <        SynchronousQueue q = new SynchronousQueue();
380 <        assertFalse(q.contains(new Integer(0)));
378 >    /**
379 >     * contains returns false
380 >     */
381 >    public void testContains() {
382 >        SynchronousQueue q = new SynchronousQueue();
383 >        assertFalse(q.contains(zero));
384      }
385  
386 <    public void testClear(){
386 >    /**
387 >     * clear ensures isEmpty
388 >     */
389 >    public void testClear() {
390          SynchronousQueue q = new SynchronousQueue();
391          q.clear();
392          assertTrue(q.isEmpty());
393      }
394  
395 <    public void testContainsAll(){
395 >    /**
396 >     * containsAll returns false unless empty
397 >     */
398 >    public void testContainsAll() {
399          SynchronousQueue q = new SynchronousQueue();
400          Integer[] empty = new Integer[0];
401 <        Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
402 <        //        assertTrue(q.containsAll(Arrays.asList(empty)));
401 >        assertTrue(q.containsAll(Arrays.asList(empty)));
402 >        Integer[] ints = new Integer[1]; ints[0] = zero;
403          assertFalse(q.containsAll(Arrays.asList(ints)));
404      }
405  
406 <    public void testRetainAll(){
406 >    /**
407 >     * retainAll returns false
408 >     */
409 >    public void testRetainAll() {
410          SynchronousQueue q = new SynchronousQueue();
411          Integer[] empty = new Integer[0];
412 <        Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
413 <        q.retainAll(Arrays.asList(ints));
414 <        //        assertTrue(q.containsAll(Arrays.asList(empty)));
313 <        assertFalse(q.containsAll(Arrays.asList(ints)));
412 >        assertFalse(q.retainAll(Arrays.asList(empty)));
413 >        Integer[] ints = new Integer[1]; ints[0] = zero;
414 >        assertFalse(q.retainAll(Arrays.asList(ints)));
415      }
416  
417 <    public void testRemoveAll(){
417 >    /**
418 >     * removeAll returns false
419 >     */
420 >    public void testRemoveAll() {
421          SynchronousQueue q = new SynchronousQueue();
422          Integer[] empty = new Integer[0];
423 <        Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
424 <        q.removeAll(Arrays.asList(ints));
321 <        //        assertTrue(q.containsAll(Arrays.asList(empty)));
423 >        assertFalse(q.removeAll(Arrays.asList(empty)));
424 >        Integer[] ints = new Integer[1]; ints[0] = zero;
425          assertFalse(q.containsAll(Arrays.asList(ints)));
426      }
427  
428  
429 <    public void testToArray(){
429 >    /**
430 >     * toArray is empty
431 >     */
432 >    public void testToArray() {
433          SynchronousQueue q = new SynchronousQueue();
434          Object[] o = q.toArray();
435          assertEquals(o.length, 0);
436      }
437  
438 <    public void testToArray2(){
438 >    /**
439 >     * toArray(a) is nulled at position 0
440 >     */
441 >    public void testToArray2() {
442          SynchronousQueue q = new SynchronousQueue();
443          Integer[] ints = new Integer[1];
444          assertNull(ints[0]);
445      }
446      
447 <    public void testIterator(){
447 >    /**
448 >     * toArray(null) throws NPE
449 >     */
450 >    public void testToArray_BadArg() {
451 >        try {
452 >            SynchronousQueue q = new SynchronousQueue();
453 >            Object o[] = q.toArray(null);
454 >            shouldThrow();
455 >        } catch(NullPointerException success){}
456 >    }
457 >
458 >
459 >    /**
460 >     * iterator does not traverse any elements
461 >     */
462 >    public void testIterator() {
463          SynchronousQueue q = new SynchronousQueue();
464          Iterator it = q.iterator();
465          assertFalse(it.hasNext());
466          try {
467              Object x = it.next();
468 <            fail("should throw");
468 >            shouldThrow();
469          }
470          catch (NoSuchElementException success) {}
471      }
472  
473 <    public void testIteratorRemove(){
473 >    /**
474 >     * iterator remove throws ISE
475 >     */
476 >    public void testIteratorRemove() {
477          SynchronousQueue q = new SynchronousQueue();
478          Iterator it = q.iterator();
479          try {
480              it.remove();
481 <            fail("should throw");
481 >            shouldThrow();
482          }
483          catch (IllegalStateException success) {}
484      }
485  
486 <    public void testToString(){
486 >    /**
487 >     * toString returns a non-null string
488 >     */
489 >    public void testToString() {
490          SynchronousQueue q = new SynchronousQueue();
491          String s = q.toString();
492 <        assertTrue(s != null);
492 >        assertNotNull(s);
493      }        
494  
495  
496 +    /**
497 +     * offer transfers elements across Executor tasks
498 +     */
499      public void testOfferInExecutor() {
500          final SynchronousQueue q = new SynchronousQueue();
501          ExecutorService executor = Executors.newFixedThreadPool(2);
# Line 376 | Line 509 | public class SynchronousQueueTest extend
509                      threadAssertEquals(0, q.remainingCapacity());
510                  }
511                  catch (InterruptedException e) {
512 <                    threadFail("should not be interrupted");
512 >                    threadUnexpectedException();
513                  }
514              }
515          });
# Line 388 | Line 521 | public class SynchronousQueueTest extend
521                      threadAssertEquals(one, q.take());
522                  }
523                  catch (InterruptedException e) {
524 <                    fail("should not be interrupted");
524 >                    threadUnexpectedException();
525                  }
526              }
527          });
# Line 397 | Line 530 | public class SynchronousQueueTest extend
530  
531      }
532  
533 +    /**
534 +     * poll retrieves elements across Executor threads
535 +     */
536      public void testPollInExecutor() {
401
537          final SynchronousQueue q = new SynchronousQueue();
403
538          ExecutorService executor = Executors.newFixedThreadPool(2);
405
539          executor.execute(new Runnable() {
540              public void run() {
541                  threadAssertNull(q.poll());
# Line 411 | Line 544 | public class SynchronousQueueTest extend
544                      threadAssertTrue(q.isEmpty());
545                  }
546                  catch (InterruptedException e) {
547 <                    threadFail("should not be interrupted");
547 >                    threadUnexpectedException();
548                  }
549              }
550          });
# Line 423 | Line 556 | public class SynchronousQueueTest extend
556                      q.put(new Integer(1));
557                  }
558                  catch (InterruptedException e) {
559 <                    threadFail("should not be interrupted");
559 >                    threadUnexpectedException();
560                  }
561              }
562          });
563          
564          joinPool(executor);
432
565      }
566  
567 +    /**
568 +     * a deserialized serialized queue is usable
569 +     */
570      public void testSerialization() {
571          SynchronousQueue q = new SynchronousQueue();
572          try {
# Line 447 | Line 582 | public class SynchronousQueueTest extend
582              while (!q.isEmpty())
583                  assertEquals(q.remove(), r.remove());
584          } catch(Exception e){
585 <            fail("unexpected exception");
585 >            unexpectedException();
586 >        }
587 >    }
588 >
589 >    /**
590 >     * drainTo(null) throws NPE
591 >     */
592 >    public void testDrainToNull() {
593 >        SynchronousQueue q = new SynchronousQueue();
594 >        try {
595 >            q.drainTo(null);
596 >            shouldThrow();
597 >        } catch(NullPointerException success) {
598 >        }
599 >    }
600 >
601 >    /**
602 >     * drainTo(this) throws IAE
603 >     */
604 >    public void testDrainToSelf() {
605 >        SynchronousQueue q = new SynchronousQueue();
606 >        try {
607 >            q.drainTo(q);
608 >            shouldThrow();
609 >        } catch(IllegalArgumentException success) {
610 >        }
611 >    }
612 >
613 >    /**
614 >     * drainTo(c) of empty queue doesn't transfer elements
615 >     */
616 >    public void testDrainTo() {
617 >        SynchronousQueue q = new SynchronousQueue();
618 >        ArrayList l = new ArrayList();
619 >        q.drainTo(l);
620 >        assertEquals(q.size(), 0);
621 >        assertEquals(l.size(), 0);
622 >    }
623 >
624 >    /**
625 >     * drainTo empties queue, unblocking a waiting put.
626 >     */
627 >    public void testDrainToWithActivePut() {
628 >        final SynchronousQueue q = new SynchronousQueue();
629 >        Thread t = new Thread(new Runnable() {
630 >                public void run() {
631 >                    try {
632 >                        q.put(new Integer(1));
633 >                    } catch (InterruptedException ie){
634 >                        threadUnexpectedException();
635 >                    }
636 >                }
637 >            });
638 >        try {
639 >            t.start();
640 >            ArrayList l = new ArrayList();
641 >            Thread.sleep(SHORT_DELAY_MS);
642 >            q.drainTo(l);
643 >            assertTrue(l.size() <= 1);
644 >            if (l.size() > 0)
645 >                assertEquals(l.get(0), new Integer(1));
646 >            t.join();
647 >            assertTrue(l.size() <= 1);
648 >        } catch(Exception e){
649 >            unexpectedException();
650 >        }
651 >    }
652 >
653 >    /**
654 >     * drainTo(null, n) throws NPE
655 >     */
656 >    public void testDrainToNullN() {
657 >        SynchronousQueue q = new SynchronousQueue();
658 >        try {
659 >            q.drainTo(null, 0);
660 >            shouldThrow();
661 >        } catch(NullPointerException success) {
662 >        }
663 >    }
664 >
665 >    /**
666 >     * drainTo(this, n) throws IAE
667 >     */
668 >    public void testDrainToSelfN() {
669 >        SynchronousQueue q = new SynchronousQueue();
670 >        try {
671 >            q.drainTo(q, 0);
672 >            shouldThrow();
673 >        } catch(IllegalArgumentException success) {
674 >        }
675 >    }
676 >
677 >    /**
678 >     * drainTo(c, n) empties up to n elements of queue into c
679 >     */
680 >    public void testDrainToN() {
681 >        final SynchronousQueue q = new SynchronousQueue();
682 >        Thread t1 = new Thread(new Runnable() {
683 >                public void run() {
684 >                    try {
685 >                        q.put(one);
686 >                    } catch (InterruptedException ie){
687 >                        threadUnexpectedException();
688 >                    }
689 >                }
690 >            });
691 >        Thread t2 = new Thread(new Runnable() {
692 >                public void run() {
693 >                    try {
694 >                        q.put(two);
695 >                    } catch (InterruptedException ie){
696 >                        threadUnexpectedException();
697 >                    }
698 >                }
699 >            });
700 >
701 >        try {
702 >            t1.start();
703 >            t2.start();
704 >            ArrayList l = new ArrayList();
705 >            Thread.sleep(SHORT_DELAY_MS);
706 >            q.drainTo(l, 1);
707 >            assertTrue(l.size() == 1);
708 >            q.drainTo(l, 1);
709 >            assertTrue(l.size() == 2);
710 >            assertTrue(l.contains(one));
711 >            assertTrue(l.contains(two));
712 >            t1.join();
713 >            t2.join();
714 >        } catch(Exception e){
715 >            unexpectedException();
716          }
717      }
718  
719 +
720   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines