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

Comparing jsr166/src/test/tck/DelayQueueTest.java (file contents):
Revision 1.14 by dl, Sun Sep 25 13:10:59 2005 UTC vs.
Revision 1.15 by jsr166, Mon Nov 2 20:28:31 2009 UTC

# Line 2 | Line 2
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.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 12 | Line 12 | import java.util.concurrent.*;
12  
13   public class DelayQueueTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());  
15 >        junit.textui.TestRunner.run (suite());
16      }
17  
18      public static Test suite() {
# Line 25 | Line 25 | public class DelayQueueTest extends JSR1
25       * A delayed implementation for testing.
26       * Most  tests use Pseudodelays, where delays are all elapsed
27       * (so, no blocking solely for delays) but are still ordered
28 <     */
29 <    static class PDelay implements Delayed {
28 >     */
29 >    static class PDelay implements Delayed {
30          int pseudodelay;
31          PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
32          public int compareTo(PDelay y) {
# Line 69 | Line 69 | public class DelayQueueTest extends JSR1
69      /**
70       * Delayed implementation that actually delays
71       */
72 <    static class NanoDelay implements Delayed {
72 >    static class NanoDelay implements Delayed {
73          long trigger;
74 <        NanoDelay(long i) {
74 >        NanoDelay(long i) {
75              trigger = System.nanoTime() + i;
76          }
77          public int compareTo(NanoDelay y) {
# Line 128 | Line 128 | public class DelayQueueTest extends JSR1
128          assertEquals(n, q.size());
129          return q;
130      }
131 <
131 >
132      /**
133       * A new queue has unbounded capacity
134       */
# Line 229 | Line 229 | public class DelayQueueTest extends JSR1
229              DelayQueue q = new DelayQueue();
230              q.offer(null);
231              shouldThrow();
232 <        } catch (NullPointerException success) { }  
232 >        } catch (NullPointerException success) { }
233      }
234  
235      /**
# Line 240 | Line 240 | public class DelayQueueTest extends JSR1
240              DelayQueue q = new DelayQueue();
241              q.add(null);
242              shouldThrow();
243 <        } catch (NullPointerException success) { }  
243 >        } catch (NullPointerException success) { }
244      }
245  
246      /**
# Line 342 | Line 342 | public class DelayQueueTest extends JSR1
342              DelayQueue q = new DelayQueue();
343              q.put(null);
344              shouldThrow();
345 <        }
345 >        }
346          catch (NullPointerException success){
347 <        }  
347 >        }
348       }
349  
350      /**
# Line 412 | Line 412 | public class DelayQueueTest extends JSR1
412                      } finally { }
413                  }
414              });
415 <        
415 >
416          try {
417              t.start();
418              Thread.sleep(SMALL_DELAY_MS);
# Line 434 | Line 434 | public class DelayQueueTest extends JSR1
434              }
435          } catch (InterruptedException e){
436              unexpectedException();
437 <        }  
437 >        }
438      }
439  
440      /**
# Line 447 | Line 447 | public class DelayQueueTest extends JSR1
447                      try {
448                          q.take();
449                          threadShouldThrow();
450 <                    } catch (InterruptedException success){ }                
450 >                    } catch (InterruptedException success){ }
451                  }
452              });
453          try {
# Line 474 | Line 474 | public class DelayQueueTest extends JSR1
474                          q.take();
475                          threadShouldThrow();
476                      } catch (InterruptedException success){
477 <                    }  
477 >                    }
478                  }});
479          t.start();
480 <        try {
481 <           Thread.sleep(SHORT_DELAY_MS);
480 >        try {
481 >           Thread.sleep(SHORT_DELAY_MS);
482             t.interrupt();
483             t.join();
484          }
# Line 511 | Line 511 | public class DelayQueueTest extends JSR1
511              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
512          } catch (InterruptedException e){
513              unexpectedException();
514 <        }  
514 >        }
515      }
516  
517      /**
# Line 526 | Line 526 | public class DelayQueueTest extends JSR1
526              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
527          } catch (InterruptedException e){
528              unexpectedException();
529 <        }  
529 >        }
530      }
531  
532      /**
# Line 543 | Line 543 | public class DelayQueueTest extends JSR1
543                          }
544                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
545                      } catch (InterruptedException success){
546 <                    }  
546 >                    }
547                  }});
548          t.start();
549 <        try {
550 <           Thread.sleep(SHORT_DELAY_MS);
549 >        try {
550 >           Thread.sleep(SHORT_DELAY_MS);
551             t.interrupt();
552             t.join();
553          }
# Line 569 | Line 569 | public class DelayQueueTest extends JSR1
569                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
570                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
571                          threadFail("Should block");
572 <                    } catch (InterruptedException success) { }                
572 >                    } catch (InterruptedException success) { }
573                  }
574              });
575          try {
# Line 581 | Line 581 | public class DelayQueueTest extends JSR1
581          } catch (Exception e){
582              unexpectedException();
583          }
584 <    }  
584 >    }
585  
586  
587      /**
# Line 628 | Line 628 | public class DelayQueueTest extends JSR1
628              q.remove();
629              shouldThrow();
630          } catch (NoSuchElementException success){
631 <        }  
631 >        }
632      }
633  
634      /**
# Line 645 | Line 645 | public class DelayQueueTest extends JSR1
645          }
646          assertTrue(q.isEmpty());
647      }
648 <        
648 >
649      /**
650       * contains(x) reports true when elements added but not yet removed
651       */
# Line 736 | Line 736 | public class DelayQueueTest extends JSR1
736              assertEquals(o[i], q.take());
737          } catch (InterruptedException e){
738              unexpectedException();
739 <        }    
739 >        }
740      }
741  
742      /**
# Line 752 | Line 752 | public class DelayQueueTest extends JSR1
752                  assertEquals(ints[i], q.take());
753          } catch (InterruptedException e){
754              unexpectedException();
755 <        }    
755 >        }
756      }
757  
758  
# Line 777 | Line 777 | public class DelayQueueTest extends JSR1
777              shouldThrow();
778          } catch(ArrayStoreException  success){}
779      }
780 <    
780 >
781      /**
782       * iterator iterates through all elements
783       */
# Line 819 | Line 819 | public class DelayQueueTest extends JSR1
819          for (int i = 0; i < SIZE; ++i) {
820              assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
821          }
822 <    }        
822 >    }
823  
824      /**
825       * offer transfers elements across Executor tasks
# Line 875 | Line 875 | public class DelayQueueTest extends JSR1
875                  NanoDelay e = (NanoDelay)(q.take());
876                  long tt = e.getTriggerTime();
877                  assertTrue(tt <= System.nanoTime());
878 <                if (i != 0)
878 >                if (i != 0)
879                      assertTrue(tt >= last);
880                  last = tt;
881              }
# Line 919 | Line 919 | public class DelayQueueTest extends JSR1
919  
920      /**
921       * drainTo(null) throws NPE
922 <     */
922 >     */
923      public void testDrainToNull() {
924          DelayQueue q = populatedQueue(SIZE);
925          try {
# Line 931 | Line 931 | public class DelayQueueTest extends JSR1
931  
932      /**
933       * drainTo(this) throws IAE
934 <     */
934 >     */
935      public void testDrainToSelf() {
936          DelayQueue q = populatedQueue(SIZE);
937          try {
# Line 943 | Line 943 | public class DelayQueueTest extends JSR1
943  
944      /**
945       * drainTo(c) empties queue into another collection c
946 <     */
946 >     */
947      public void testDrainTo() {
948          DelayQueue q = new DelayQueue();
949          PDelay[] elems = new PDelay[SIZE];
# Line 954 | Line 954 | public class DelayQueueTest extends JSR1
954          ArrayList l = new ArrayList();
955          q.drainTo(l);
956          assertEquals(q.size(), 0);
957 <        for (int i = 0; i < SIZE; ++i)
957 >        for (int i = 0; i < SIZE; ++i)
958              assertEquals(l.get(i), elems[i]);
959          q.add(elems[0]);
960          q.add(elems[1]);
# Line 965 | Line 965 | public class DelayQueueTest extends JSR1
965          q.drainTo(l);
966          assertEquals(q.size(), 0);
967          assertEquals(l.size(), 2);
968 <        for (int i = 0; i < 2; ++i)
968 >        for (int i = 0; i < 2; ++i)
969              assertEquals(l.get(i), elems[i]);
970      }
971  
972      /**
973       * drainTo empties queue
974 <     */
974 >     */
975      public void testDrainToWithActivePut() {
976          final DelayQueue q = populatedQueue(SIZE);
977          Thread t = new Thread(new Runnable() {
# Line 993 | Line 993 | public class DelayQueueTest extends JSR1
993  
994      /**
995       * drainTo(null, n) throws NPE
996 <     */
996 >     */
997      public void testDrainToNullN() {
998          DelayQueue q = populatedQueue(SIZE);
999          try {
# Line 1005 | Line 1005 | public class DelayQueueTest extends JSR1
1005  
1006      /**
1007       * drainTo(this, n) throws IAE
1008 <     */
1008 >     */
1009      public void testDrainToSelfN() {
1010          DelayQueue q = populatedQueue(SIZE);
1011          try {
# Line 1017 | Line 1017 | public class DelayQueueTest extends JSR1
1017  
1018      /**
1019       * drainTo(c, n) empties first max {n, size} elements of queue into c
1020 <     */
1020 >     */
1021      public void testDrainToN() {
1022          for (int i = 0; i < SIZE + 2; ++i) {
1023              DelayQueue q = populatedQueue(SIZE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines