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