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