479N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
479N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
479N/A *
479N/A * This code is free software; you can redistribute it and/or modify it
479N/A * under the terms of the GNU General Public License version 2 only, as
479N/A * published by the Free Software Foundation.
479N/A *
479N/A * This code is distributed in the hope that it will be useful, but WITHOUT
479N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
479N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
479N/A * version 2 for more details (a copy is included in the LICENSE file that
479N/A * accompanied this code).
479N/A *
479N/A * You should have received a copy of the GNU General Public License version
479N/A * 2 along with this work; if not, write to the Free Software Foundation,
479N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
479N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
479N/A */
479N/A
479N/A/*
479N/A * @test
479N/A * @bug 6730507
479N/A * @summary java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
479N/A * @author Chris Hegarty
479N/A * @author Martin Buchholz
479N/A */
479N/A
479N/Aimport java.util.Date;
479N/Aimport java.util.Timer;
479N/Aimport java.util.TimerTask;
479N/Aimport java.util.concurrent.*;
479N/Aimport java.util.concurrent.atomic.AtomicInteger;
479N/A
479N/Apublic class DelayOverflow
479N/A{
479N/A void scheduleNow(Timer timer, TimerTask task, int how) {
479N/A switch (how) {
479N/A case 0 :
479N/A timer.schedule(task, new Date(), Long.MAX_VALUE);
479N/A break;
479N/A case 1:
479N/A timer.schedule(task, 0L, Long.MAX_VALUE);
479N/A break;
479N/A case 2:
479N/A timer.scheduleAtFixedRate(task, new Date(), Long.MAX_VALUE);
479N/A break;
479N/A case 3:
479N/A timer.scheduleAtFixedRate(task, 0L, Long.MAX_VALUE);
479N/A break;
479N/A default:
479N/A fail(String.valueOf(how));
479N/A }
479N/A }
479N/A
479N/A void sleep(long millis) {
479N/A try { Thread.sleep(millis); }
479N/A catch (Throwable t) { unexpected(t); }
479N/A }
479N/A
479N/A /** Checks that scheduledExecutionTime returns a "recent" time. */
479N/A void checkScheduledExecutionTime(TimerTask task) {
479N/A long t = System.currentTimeMillis()
479N/A - task.scheduledExecutionTime();
479N/A check(t >= 0 && t < 1000 * 600);
479N/A }
479N/A
479N/A void test(String[] args) throws Throwable {
479N/A for (int how=0; how<4; how++) {
479N/A final CountDownLatch done = new CountDownLatch(1);
479N/A final AtomicInteger count = new AtomicInteger(0);
479N/A final Timer timer = new Timer();
479N/A final TimerTask task = new TimerTask() {
479N/A @Override
479N/A public void run() {
479N/A checkScheduledExecutionTime(this);
479N/A count.incrementAndGet();
479N/A done.countDown();
479N/A }};
479N/A
479N/A scheduleNow(timer, task, how);
479N/A done.await();
479N/A equal(count.get(), 1);
479N/A checkScheduledExecutionTime(task);
479N/A if (new java.util.Random().nextBoolean())
479N/A sleep(10);
479N/A check(task.cancel());
479N/A timer.cancel();
479N/A checkScheduledExecutionTime(task);
479N/A }
479N/A }
479N/A
479N/A //--------------------- Infrastructure ---------------------------
479N/A volatile int passed = 0, failed = 0;
479N/A void pass() {passed++;}
479N/A void fail() {failed++; Thread.dumpStack();}
479N/A void fail(String msg) {System.err.println(msg); fail();}
479N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
479N/A void check(boolean cond) {if (cond) pass(); else fail();}
479N/A void equal(Object x, Object y) {
479N/A if (x == null ? y == null : x.equals(y)) pass();
479N/A else fail(x + " not equal to " + y);}
479N/A public static void main(String[] args) throws Throwable {
479N/A Class<?> k = new Object(){}.getClass().getEnclosingClass();
479N/A try {k.getMethod("instanceMain",String[].class)
479N/A .invoke( k.newInstance(), (Object) args);}
479N/A catch (Throwable e) {throw e.getCause();}}
479N/A public void instanceMain(String[] args) throws Throwable {
479N/A try {test(args);} catch (Throwable t) {unexpected(t);}
479N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
479N/A if (failed > 0) throw new AssertionError("Some tests failed");}
479N/A}