martin | 662c80d | 2008-08-07 06:36:41 -0700 | [diff] [blame] | 1 | /* |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
martin | 662c80d | 2008-08-07 06:36:41 -0700 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 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. |
martin | 662c80d | 2008-08-07 06:36:41 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6730507 |
| 27 | * @summary java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times |
| 28 | * @author Chris Hegarty |
| 29 | * @author Martin Buchholz |
| 30 | */ |
| 31 | |
| 32 | import java.util.Date; |
| 33 | import java.util.Timer; |
| 34 | import java.util.TimerTask; |
| 35 | import java.util.concurrent.*; |
| 36 | import java.util.concurrent.atomic.AtomicInteger; |
| 37 | |
| 38 | public class DelayOverflow |
| 39 | { |
| 40 | void scheduleNow(Timer timer, TimerTask task, int how) { |
| 41 | switch (how) { |
| 42 | case 0 : |
| 43 | timer.schedule(task, new Date(), Long.MAX_VALUE); |
| 44 | break; |
| 45 | case 1: |
| 46 | timer.schedule(task, 0L, Long.MAX_VALUE); |
| 47 | break; |
| 48 | case 2: |
| 49 | timer.scheduleAtFixedRate(task, new Date(), Long.MAX_VALUE); |
| 50 | break; |
| 51 | case 3: |
| 52 | timer.scheduleAtFixedRate(task, 0L, Long.MAX_VALUE); |
| 53 | break; |
| 54 | default: |
| 55 | fail(String.valueOf(how)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void sleep(long millis) { |
| 60 | try { Thread.sleep(millis); } |
| 61 | catch (Throwable t) { unexpected(t); } |
| 62 | } |
| 63 | |
| 64 | /** Checks that scheduledExecutionTime returns a "recent" time. */ |
| 65 | void checkScheduledExecutionTime(TimerTask task) { |
| 66 | long t = System.currentTimeMillis() |
| 67 | - task.scheduledExecutionTime(); |
| 68 | check(t >= 0 && t < 1000 * 600); |
| 69 | } |
| 70 | |
| 71 | void test(String[] args) throws Throwable { |
| 72 | for (int how=0; how<4; how++) { |
| 73 | final CountDownLatch done = new CountDownLatch(1); |
| 74 | final AtomicInteger count = new AtomicInteger(0); |
| 75 | final Timer timer = new Timer(); |
| 76 | final TimerTask task = new TimerTask() { |
| 77 | @Override |
| 78 | public void run() { |
| 79 | checkScheduledExecutionTime(this); |
| 80 | count.incrementAndGet(); |
| 81 | done.countDown(); |
| 82 | }}; |
| 83 | |
| 84 | scheduleNow(timer, task, how); |
| 85 | done.await(); |
| 86 | equal(count.get(), 1); |
| 87 | checkScheduledExecutionTime(task); |
| 88 | if (new java.util.Random().nextBoolean()) |
| 89 | sleep(10); |
| 90 | check(task.cancel()); |
| 91 | timer.cancel(); |
| 92 | checkScheduledExecutionTime(task); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | //--------------------- Infrastructure --------------------------- |
| 97 | volatile int passed = 0, failed = 0; |
| 98 | void pass() {passed++;} |
| 99 | void fail() {failed++; Thread.dumpStack();} |
| 100 | void fail(String msg) {System.err.println(msg); fail();} |
| 101 | void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 102 | void check(boolean cond) {if (cond) pass(); else fail();} |
| 103 | void equal(Object x, Object y) { |
| 104 | if (x == null ? y == null : x.equals(y)) pass(); |
| 105 | else fail(x + " not equal to " + y);} |
| 106 | public static void main(String[] args) throws Throwable { |
| 107 | Class<?> k = new Object(){}.getClass().getEnclosingClass(); |
| 108 | try {k.getMethod("instanceMain",String[].class) |
| 109 | .invoke( k.newInstance(), (Object) args);} |
| 110 | catch (Throwable e) {throw e.getCause();}} |
| 111 | public void instanceMain(String[] args) throws Throwable { |
| 112 | try {test(args);} catch (Throwable t) {unexpected(t);} |
| 113 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 114 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 115 | } |