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