Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Timer/DelayOverflow.java
38821 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 673050726* @summary java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times27* @author Chris Hegarty28* @author Martin Buchholz29* @key randomness30*/3132import java.util.Date;33import java.util.Timer;34import java.util.TimerTask;35import java.util.concurrent.*;36import java.util.concurrent.atomic.AtomicInteger;3738public class DelayOverflow39{40void scheduleNow(Timer timer, TimerTask task, int how) {41switch (how) {42case 0 :43timer.schedule(task, new Date(), Long.MAX_VALUE);44break;45case 1:46timer.schedule(task, 0L, Long.MAX_VALUE);47break;48case 2:49timer.scheduleAtFixedRate(task, new Date(), Long.MAX_VALUE);50break;51case 3:52timer.scheduleAtFixedRate(task, 0L, Long.MAX_VALUE);53break;54default:55fail(String.valueOf(how));56}57}5859void sleep(long millis) {60try { Thread.sleep(millis); }61catch (Throwable t) { unexpected(t); }62}6364/** Checks that scheduledExecutionTime returns a "recent" time. */65void checkScheduledExecutionTime(TimerTask task) {66long t = System.currentTimeMillis()67- task.scheduledExecutionTime();68check(t >= 0 && t < 1000 * 600);69}7071void test(String[] args) throws Throwable {72for (int how=0; how<4; how++) {73final CountDownLatch done = new CountDownLatch(1);74final AtomicInteger count = new AtomicInteger(0);75final Timer timer = new Timer();76final TimerTask task = new TimerTask() {77@Override78public void run() {79checkScheduledExecutionTime(this);80count.incrementAndGet();81done.countDown();82}};8384scheduleNow(timer, task, how);85done.await();86equal(count.get(), 1);87checkScheduledExecutionTime(task);88if (new java.util.Random().nextBoolean())89sleep(10);90check(task.cancel());91timer.cancel();92checkScheduledExecutionTime(task);93}94}9596//--------------------- Infrastructure ---------------------------97volatile int passed = 0, failed = 0;98void pass() {passed++;}99void fail() {failed++; Thread.dumpStack();}100void fail(String msg) {System.err.println(msg); fail();}101void unexpected(Throwable t) {failed++; t.printStackTrace();}102void check(boolean cond) {if (cond) pass(); else fail();}103void equal(Object x, Object y) {104if (x == null ? y == null : x.equals(y)) pass();105else fail(x + " not equal to " + y);}106public static void main(String[] args) throws Throwable {107Class<?> k = new Object(){}.getClass().getEnclosingClass();108try {k.getMethod("instanceMain",String[].class)109.invoke( k.newInstance(), (Object) args);}110catch (Throwable e) {throw e.getCause();}}111public void instanceMain(String[] args) throws Throwable {112try {test(args);} catch (Throwable t) {unexpected(t);}113System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);114if (failed > 0) throw new AssertionError("Some tests failed");}115}116117118