test/java/util/Timer/Args.java

Print this page


   1 /*
   2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6571655 6571881 6574585 6571297
  27  * @summary Test various args to task scheduling methods
  28  */
  29 
  30 import java.util.*;
  31 import java.util.concurrent.*;


  32 
  33 public class Args {


  34     void schedule(final Timer t, final TimerTask task, final Date d) {
  35         t.schedule(task, d);
  36         THROWS(IllegalStateException.class,
  37                new F(){void f(){ t.schedule(task, d); }});

  38     }
  39 
  40     void schedule(final Timer t, final TimerTask task, final Date d, final long period) {
  41         t.schedule(task, d, period);
  42         THROWS(IllegalStateException.class,
  43                new F(){void f(){ t.schedule(task, d, period); }});

  44     }
  45 
  46     void scheduleAtFixedRate(final Timer t, final TimerTask task, final Date d, final long period) {
  47         t.scheduleAtFixedRate(task, d, period);
  48         THROWS(IllegalStateException.class,
  49                new F(){void f(){ t.scheduleAtFixedRate(task, d, period); }});

  50     }
  51 
  52     TimerTask counter(final CountDownLatch latch) {
  53         return new TimerTask() { public void run() {
  54             check(latch.getCount() > 0);
  55             latch.countDown();



  56         }};
  57     }
  58 




  59     void test(String[] args) throws Throwable {
  60         final Timer t = new Timer();

























  61         final TimerTask x = new TimerTask() { public void run() {}};
  62         THROWS(IllegalArgumentException.class,
  63                new F(){void f(){ t.schedule(x, -42); }},
  64                new F(){void f(){ t.schedule(x, new Date(-42)); }},
  65 
  66                new F(){void f(){ t.schedule(x, Long.MAX_VALUE); }},
  67                new F(){void f(){ t.schedule(x, -42, 42); }},
  68                new F(){void f(){ t.schedule(x, new Date(-42), 42); }},
  69                new F(){void f(){ t.schedule(x, Long.MAX_VALUE, 42); }},
  70                new F(){void f(){ t.schedule(x, 42, 0); }},
  71                new F(){void f(){ t.schedule(x, new Date(42), 0); }},
  72                new F(){void f(){ t.schedule(x, 42, -42); }},
  73                new F(){void f(){ t.schedule(x, new Date(42), -42); }},
  74 
  75                new F(){void f(){ t.scheduleAtFixedRate(x, -42, 42); }},
  76                new F(){void f(){ t.scheduleAtFixedRate(x, new Date(-42), 42); }},
  77                new F(){void f(){ t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42); }},
  78                new F(){void f(){ t.scheduleAtFixedRate(x, 42, 0); }},
  79                new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), 0); }},
  80                new F(){void f(){ t.scheduleAtFixedRate(x, 42, -42); }},
  81                new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), -42); }}
  82                );
  83 
  84         THROWS(NullPointerException.class,
  85                new F(){void f(){ t.schedule(null, 42); }},
  86                new F(){void f(){ t.schedule(x, (Date)null); }},
  87 
  88                new F(){void f(){ t.schedule(null, 42, 42); }},
  89                new F(){void f(){ t.schedule(x, (Date)null, 42); }},
  90 
  91                new F(){void f(){ t.scheduleAtFixedRate(null, 42, 42); }},
  92                new F(){void f(){ t.scheduleAtFixedRate(x, (Date)null, 42); }}
  93                );
  94 
  95         final CountDownLatch y1 = new CountDownLatch(1);
  96         final CountDownLatch y2 = new CountDownLatch(1);
  97         final CountDownLatch y3 = new CountDownLatch(11);
  98         final long start = System.currentTimeMillis();
  99         final Date past = new Date(start - 10500);
 100 
 101         schedule(           t, counter(y1), past);
 102         schedule(           t, counter(y2), past, 1000);
 103         scheduleAtFixedRate(t, counter(y3), past, 1000);
 104         y3.await();
 105         y1.await();
 106         y2.await();
 107 
 108         final long elapsed = System.currentTimeMillis() - start;
 109         System.out.printf("elapsed=%d%n", elapsed);
 110         check(elapsed < 500);
 111 
 112         t.cancel();
 113 
 114         THROWS(IllegalStateException.class,
 115                new F(){void f(){ t.schedule(x, 42); }},
 116                new F(){void f(){ t.schedule(x, past); }},
 117                new F(){void f(){ t.schedule(x, 42, 42); }},
 118                new F(){void f(){ t.schedule(x, past, 42); }},
 119                new F(){void f(){ t.scheduleAtFixedRate(x, 42, 42); }},
 120                new F(){void f(){ t.scheduleAtFixedRate(x, past, 42); }});
 121 
 122     }
 123 
 124     //--------------------- Infrastructure ---------------------------
 125     volatile int passed = 0, failed = 0;
 126     void pass() {passed++;}
 127     void fail() {failed++; Thread.dumpStack();}
 128     void fail(String msg) {System.err.println(msg); fail();}
 129     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 130     void check(boolean cond) {if (cond) pass(); else fail();}
 131     void equal(Object x, Object y) {
 132         if (x == null ? y == null : x.equals(y)) pass();
 133         else fail(x + " not equal to " + y);}
 134     public static void main(String[] args) throws Throwable {
 135         new Args().instanceMain(args);}
 136     void instanceMain(String[] args) throws Throwable {
 137         try {test(args);} catch (Throwable t) {unexpected(t);}
 138         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 139         if (failed > 0) throw new AssertionError("Some tests failed");}
 140     abstract class F {abstract void f() throws Throwable;}
 141     void THROWS(Class<? extends Throwable> k, F... fs) {
 142         for (F f : fs)
 143             try {f.f(); fail("Expected " + k.getName() + " not thrown");}
 144             catch (Throwable t) {
 145                 if (k.isAssignableFrom(t.getClass())) pass();
 146                 else unexpected(t);}}
 147 }
   1 /*
   2  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6571655 6571881 6574585 6571297 8004807
  27  * @summary Test various args to task scheduling methods
  28  */
  29 
  30 import java.util.Date;
  31 import java.util.Timer;
  32 import java.util.TimerTask;
  33 import java.util.concurrent.CountDownLatch;
  34 
  35 public class Args {
  36     static final long DELAY_MS = 1000L;
  37 
  38     void schedule(final Timer t, final TimerTask task, final Date d) {
  39         t.schedule(task, d);
  40         assertThrows
  41             (IllegalStateException.class,
  42              () -> t.schedule(task, d));
  43     }
  44 
  45     void schedule(final Timer t, final TimerTask task, final Date d, final long period) {
  46         t.schedule(task, d, period);
  47         assertThrows
  48             (IllegalStateException.class,
  49              () -> t.schedule(task, d, period));
  50     }
  51 
  52     void scheduleAtFixedRate(final Timer t, final TimerTask task, final Date d, final long period) {
  53         t.scheduleAtFixedRate(task, d, period);
  54         assertThrows
  55             (IllegalStateException.class,
  56              () -> t.scheduleAtFixedRate(task, d, period));
  57     }
  58 
  59     TimerTask counter(final CountDownLatch latch) {
  60         return new TimerTask() { public void run() {
  61             check(latch.getCount() > 0);
  62             latch.countDown();
  63             if (latch.getCount() == 0) {
  64                 this.cancel();
  65             }
  66         }};
  67     }
  68 
  69     TimerTask nop() {
  70         return new TimerTask() { public void run() { }};
  71     }
  72 
  73     void test(String[] args) throws Throwable {
  74         final Timer t = new Timer();
  75         try {
  76             test(t);
  77         } finally {
  78             // Ensure this test doesn't interfere with subsequent
  79             // tests even in case of failure.
  80             t.cancel();
  81         }
  82 
  83         // Attempts to schedule tasks on a cancelled Timer result in ISE.
  84 
  85         final Date past = new Date(System.currentTimeMillis() - DELAY_MS);
  86         final Date future = new Date(System.currentTimeMillis() + DELAY_MS);
  87         assertThrows
  88             (IllegalStateException.class,
  89              () -> t.schedule(nop(), 42),
  90              () -> t.schedule(nop(), 42),
  91              () -> t.schedule(nop(), past),
  92              () -> t.schedule(nop(), 42, 42),
  93              () -> t.schedule(nop(), past, 42),
  94              () -> t.scheduleAtFixedRate(nop(), 42, 42),
  95              () -> t.scheduleAtFixedRate(nop(), past, 42),
  96              () -> t.scheduleAtFixedRate(nop(), future, 42));
  97     }
  98 
  99     void test(Timer t) throws Throwable {
 100         final TimerTask x = new TimerTask() { public void run() {}};
 101         assertThrows
 102             (IllegalArgumentException.class,
 103              () -> t.schedule(x, -42),
 104              () -> t.schedule(x, new Date(-42)),
 105 
 106              () -> t.schedule(x, Long.MAX_VALUE),
 107              () -> t.schedule(x, -42, 42),
 108              () -> t.schedule(x, new Date(-42), 42),
 109              () -> t.schedule(x, Long.MAX_VALUE, 42),
 110              () -> t.schedule(x, 42, 0),
 111              () -> t.schedule(x, new Date(42), 0),
 112              () -> t.schedule(x, 42, -42),
 113              () -> t.schedule(x, new Date(42), -42),
 114 
 115              () -> t.scheduleAtFixedRate(x, -42, 42),
 116              () -> t.scheduleAtFixedRate(x, new Date(-42), 42),
 117              () -> t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42),
 118              () -> t.scheduleAtFixedRate(x, 42, 0),
 119              () -> t.scheduleAtFixedRate(x, new Date(42), 0),
 120              () -> t.scheduleAtFixedRate(x, 42, -42),
 121              () -> t.scheduleAtFixedRate(x, new Date(42), -42));
 122 
 123         assertThrows
 124             (NullPointerException.class,
 125              () -> t.schedule(null, 42),
 126              () -> t.schedule(x, (Date)null),
 127 
 128              () -> t.schedule(null, 42, 42),
 129              () -> t.schedule(x, (Date)null, 42),
 130 
 131              () -> t.scheduleAtFixedRate(null, 42, 42),
 132              () -> t.scheduleAtFixedRate(x, (Date)null, 42));
 133 
 134         final CountDownLatch y1 = new CountDownLatch(1);
 135         final CountDownLatch y2 = new CountDownLatch(1);
 136         final CountDownLatch y3 = new CountDownLatch(11);
 137         final long start = System.currentTimeMillis();
 138         final Date past = new Date(start - (10 * DELAY_MS + DELAY_MS / 2));
 139 
 140         schedule(           t, counter(y1), past);
 141         schedule(           t, counter(y2), past, DELAY_MS);
 142         scheduleAtFixedRate(t, counter(y3), past, DELAY_MS);
 143         y3.await();
 144         y1.await();
 145         y2.await();
 146 
 147         final long elapsed = System.currentTimeMillis() - start;
 148         if (elapsed >= DELAY_MS / 2)
 149             fail(String.format("Test took too long: elapsed=%d%n", elapsed));











 150     }
 151 
 152     //--------------------- Infrastructure ---------------------------
 153     volatile int passed = 0, failed = 0;
 154     void pass() {passed++;}
 155     void fail() {failed++; Thread.dumpStack();}
 156     void fail(String msg) {System.err.println(msg); fail();}
 157     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 158     void check(boolean cond) {if (cond) pass(); else fail();}
 159     void equal(Object x, Object y) {
 160         if (x == null ? y == null : x.equals(y)) pass();
 161         else fail(x + " not equal to " + y);}
 162     public static void main(String[] args) throws Throwable {
 163         new Args().instanceMain(args);}
 164     void instanceMain(String[] args) throws Throwable {
 165         try {test(args);} catch (Throwable t) {unexpected(t);}
 166         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 167         if (failed > 0) throw new AssertionError("Some tests failed");}
 168     interface F { void f() throws Throwable; }
 169     void assertThrows(Class<? extends Throwable> k, F... fs) {
 170         for (F f : fs)
 171             try {f.f(); fail("Expected " + k.getName() + " not thrown");}
 172             catch (Throwable t) {
 173                 if (k.isAssignableFrom(t.getClass())) pass();
 174                 else unexpected(t);}}
 175 }