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 package test.java.util.Timer;
31 
32 import java.util.*;
33 import java.util.concurrent.*;
34 import static java.util.concurrent.TimeUnit.*;
35 
36 public class Args {
37     static final long DELAY_MS = 30 * 1000L;
38 
schedule(final Timer t, final TimerTask task, final Date d)39     void schedule(final Timer t, final TimerTask task, final Date d) {
40         t.schedule(task, d);
41         assertThrows
42             (IllegalStateException.class,
43              () -> t.schedule(task, d));
44     }
45 
schedule(final Timer t, final TimerTask task, final Date d, final long period)46     void schedule(final Timer t, final TimerTask task, final Date d, final
47 long period) {
48         t.schedule(task, d, period);
49         assertThrows
50             (IllegalStateException.class,
51              () -> t.schedule(task, d, period));
52     }
53 
scheduleAtFixedRate(final Timer t, final TimerTask task, final Date d, final long period)54     void scheduleAtFixedRate(final Timer t, final TimerTask task, final
55 Date d, final long period) {
56         t.scheduleAtFixedRate(task, d, period);
57         assertThrows
58             (IllegalStateException.class,
59              () -> t.scheduleAtFixedRate(task, d, period));
60     }
61 
counter(final CountDownLatch latch)62     TimerTask counter(final CountDownLatch latch) {
63         return new TimerTask() { public void run() {
64             if (latch.getCount() == 0)
65                 fail(String.format("Latch counted down too many times: " +
66 latch));
67             latch.countDown();
68         }};
69     }
70 
71     TimerTask nop() {
72         return new TimerTask() { public void run() { }};
73     }
74 
75     void test(String[] args) throws Throwable {
76         final Timer t = new Timer();
77         try {
78             test(t);
79         } finally {
80             // Ensure this test doesn't interfere with subsequent
81             // tests even in case of failure.
82             t.cancel();
83         }
84 
85         // Attempts to schedule tasks on a cancelled Timer result in ISE.
86 
87         final Date past = new Date(System.currentTimeMillis() - DELAY_MS);
88         final Date future = new Date(System.currentTimeMillis() + DELAY_MS);
89         assertThrows
90             (IllegalStateException.class,
91              () -> t.schedule(nop(), 42),
92              () -> t.schedule(nop(), 42),
93              () -> t.schedule(nop(), past),
94              () -> t.schedule(nop(), 42, 42),
95              () -> t.schedule(nop(), past, 42),
96              () -> t.scheduleAtFixedRate(nop(), 42, 42),
97              () -> t.scheduleAtFixedRate(nop(), past, 42),
98              () -> t.scheduleAtFixedRate(nop(), future, 42));
99     }
100 
101     void test(Timer t) throws Throwable {
102         final TimerTask x = new TimerTask() { public void run() {}};
103         assertThrows
104             (IllegalArgumentException.class,
105              () -> t.schedule(x, -42),
106              () -> t.schedule(x, new Date(-42)),
107 
108              () -> t.schedule(x, Long.MAX_VALUE),
109              () -> t.schedule(x, -42, 42),
110              () -> t.schedule(x, new Date(-42), 42),
111              () -> t.schedule(x, Long.MAX_VALUE, 42),
112              () -> t.schedule(x, 42, 0),
113              () -> t.schedule(x, new Date(42), 0),
114              () -> t.schedule(x, 42, -42),
115              () -> t.schedule(x, new Date(42), -42),
116 
117              () -> t.scheduleAtFixedRate(x, -42, 42),
118              () -> t.scheduleAtFixedRate(x, new Date(-42), 42),
119              () -> t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42),
120              () -> t.scheduleAtFixedRate(x, 42, 0),
121              () -> t.scheduleAtFixedRate(x, new Date(42), 0),
122              () -> t.scheduleAtFixedRate(x, 42, -42),
123              () -> t.scheduleAtFixedRate(x, new Date(42), -42));
124 
125         assertThrows
126             (NullPointerException.class,
127              () -> t.schedule(null, 42),
128              () -> t.schedule(x, (Date)null),
129 
130              () -> t.schedule(null, 42, 42),
131              () -> t.schedule(x, (Date)null, 42),
132 
133              () -> t.scheduleAtFixedRate(null, 42, 42),
134              () -> t.scheduleAtFixedRate(x, (Date)null, 42));
135 
136         // Create local classes for clearer diagnostics in case of failure
137         class OneShotLatch extends CountDownLatch {
138             OneShotLatch() { super(1); }
139         }
140         class FixedDelayLatch extends CountDownLatch {
141             FixedDelayLatch() { super(1); }
142         }
143         class FixedRateLatch extends CountDownLatch {
144             FixedRateLatch() { super(11); }
145         }
146         final CountDownLatch y1 = new OneShotLatch();
147         final CountDownLatch y2 = new FixedDelayLatch();
148         final CountDownLatch y3 = new FixedRateLatch();
149 
150         final long start = System.currentTimeMillis();
151         final Date past = new Date(start - (10 * DELAY_MS + DELAY_MS / 2));
152 
153         schedule(           t, counter(y1), past);
154         schedule(           t, counter(y2), past, DELAY_MS);
155         scheduleAtFixedRate(t, counter(y3), past, DELAY_MS);
156 
157         check(y1.await(DELAY_MS / 4, MILLISECONDS));
158         check(y2.await(DELAY_MS / 4, MILLISECONDS));
159         check(y3.await(DELAY_MS / 4, MILLISECONDS));
160 
161         final long elapsed = System.currentTimeMillis() - start;
162         if (elapsed >= DELAY_MS / 2)
163             fail(String.format("Test took too long: elapsed=%d%n",
164 elapsed));
165     }
166 
167     //--------------------- Infrastructure ---------------------------
168     volatile int passed = 0, failed = 0;
169     void pass() {passed++;}
170     void fail() {failed++; Thread.dumpStack();}
171     void fail(String msg) {System.err.println(msg); fail();}
172     void unexpected(Throwable t) {failed++; t.printStackTrace();}
173     void check(boolean cond) {if (cond) pass(); else fail();}
174     void equal(Object x, Object y) {
175         if (x == null ? y == null : x.equals(y)) pass();
176         else fail(x + " not equal to " + y);}
177     public static void main(String[] args) throws Throwable {
178         new Args().instanceMain(args);}
179     void instanceMain(String[] args) throws Throwable {
180         try {test(args);} catch (Throwable t) {unexpected(t);}
181         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
182         if (failed > 0) throw new AssertionError("Some tests failed");}
183     interface F { void f() throws Throwable; }
184     void assertThrows(Class<? extends Throwable> k, F... fs) {
185         for (F f : fs)
186             try {f.f(); fail("Expected " + k.getName() + " not thrown");}
187             catch (Throwable t) {
188                 if (k.isAssignableFrom(t.getClass())) pass();
189                 else unexpected(t);}}
190 }
191 
192