--- old/test/java/util/Timer/Args.java 2014-06-03 21:39:47.491818392 +0800 +++ new/test/java/util/Timer/Args.java 2014-06-03 21:39:46.742808895 +0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,102 +23,130 @@ /* * @test - * @bug 6571655 6571881 6574585 6571297 + * @bug 6571655 6571881 6574585 6571297 8004807 * @summary Test various args to task scheduling methods */ -import java.util.*; -import java.util.concurrent.*; +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.CountDownLatch; public class Args { + static final long DELAY_MS = 1000L; + void schedule(final Timer t, final TimerTask task, final Date d) { t.schedule(task, d); - THROWS(IllegalStateException.class, - new F(){void f(){ t.schedule(task, d); }}); + assertThrows + (IllegalStateException.class, + () -> t.schedule(task, d)); } void schedule(final Timer t, final TimerTask task, final Date d, final long period) { t.schedule(task, d, period); - THROWS(IllegalStateException.class, - new F(){void f(){ t.schedule(task, d, period); }}); + assertThrows + (IllegalStateException.class, + () -> t.schedule(task, d, period)); } void scheduleAtFixedRate(final Timer t, final TimerTask task, final Date d, final long period) { t.scheduleAtFixedRate(task, d, period); - THROWS(IllegalStateException.class, - new F(){void f(){ t.scheduleAtFixedRate(task, d, period); }}); + assertThrows + (IllegalStateException.class, + () -> t.scheduleAtFixedRate(task, d, period)); } TimerTask counter(final CountDownLatch latch) { return new TimerTask() { public void run() { check(latch.getCount() > 0); latch.countDown(); + if (latch.getCount() == 0) { + this.cancel(); + } }}; } + TimerTask nop() { + return new TimerTask() { public void run() { }}; + } + void test(String[] args) throws Throwable { final Timer t = new Timer(); + try { + test(t); + } finally { + // Ensure this test doesn't interfere with subsequent + // tests even in case of failure. + t.cancel(); + } + + // Attempts to schedule tasks on a cancelled Timer result in ISE. + + final Date past = new Date(System.currentTimeMillis() - DELAY_MS); + final Date future = new Date(System.currentTimeMillis() + DELAY_MS); + assertThrows + (IllegalStateException.class, + () -> t.schedule(nop(), 42), + () -> t.schedule(nop(), 42), + () -> t.schedule(nop(), past), + () -> t.schedule(nop(), 42, 42), + () -> t.schedule(nop(), past, 42), + () -> t.scheduleAtFixedRate(nop(), 42, 42), + () -> t.scheduleAtFixedRate(nop(), past, 42), + () -> t.scheduleAtFixedRate(nop(), future, 42)); + } + + void test(Timer t) throws Throwable { final TimerTask x = new TimerTask() { public void run() {}}; - THROWS(IllegalArgumentException.class, - new F(){void f(){ t.schedule(x, -42); }}, - new F(){void f(){ t.schedule(x, new Date(-42)); }}, - - new F(){void f(){ t.schedule(x, Long.MAX_VALUE); }}, - new F(){void f(){ t.schedule(x, -42, 42); }}, - new F(){void f(){ t.schedule(x, new Date(-42), 42); }}, - new F(){void f(){ t.schedule(x, Long.MAX_VALUE, 42); }}, - new F(){void f(){ t.schedule(x, 42, 0); }}, - new F(){void f(){ t.schedule(x, new Date(42), 0); }}, - new F(){void f(){ t.schedule(x, 42, -42); }}, - new F(){void f(){ t.schedule(x, new Date(42), -42); }}, - - new F(){void f(){ t.scheduleAtFixedRate(x, -42, 42); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, new Date(-42), 42); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, 42, 0); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), 0); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, 42, -42); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), -42); }} - ); - - THROWS(NullPointerException.class, - new F(){void f(){ t.schedule(null, 42); }}, - new F(){void f(){ t.schedule(x, (Date)null); }}, - - new F(){void f(){ t.schedule(null, 42, 42); }}, - new F(){void f(){ t.schedule(x, (Date)null, 42); }}, - - new F(){void f(){ t.scheduleAtFixedRate(null, 42, 42); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, (Date)null, 42); }} - ); + assertThrows + (IllegalArgumentException.class, + () -> t.schedule(x, -42), + () -> t.schedule(x, new Date(-42)), + + () -> t.schedule(x, Long.MAX_VALUE), + () -> t.schedule(x, -42, 42), + () -> t.schedule(x, new Date(-42), 42), + () -> t.schedule(x, Long.MAX_VALUE, 42), + () -> t.schedule(x, 42, 0), + () -> t.schedule(x, new Date(42), 0), + () -> t.schedule(x, 42, -42), + () -> t.schedule(x, new Date(42), -42), + + () -> t.scheduleAtFixedRate(x, -42, 42), + () -> t.scheduleAtFixedRate(x, new Date(-42), 42), + () -> t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42), + () -> t.scheduleAtFixedRate(x, 42, 0), + () -> t.scheduleAtFixedRate(x, new Date(42), 0), + () -> t.scheduleAtFixedRate(x, 42, -42), + () -> t.scheduleAtFixedRate(x, new Date(42), -42)); + + assertThrows + (NullPointerException.class, + () -> t.schedule(null, 42), + () -> t.schedule(x, (Date)null), + + () -> t.schedule(null, 42, 42), + () -> t.schedule(x, (Date)null, 42), + + () -> t.scheduleAtFixedRate(null, 42, 42), + () -> t.scheduleAtFixedRate(x, (Date)null, 42)); final CountDownLatch y1 = new CountDownLatch(1); final CountDownLatch y2 = new CountDownLatch(1); final CountDownLatch y3 = new CountDownLatch(11); final long start = System.currentTimeMillis(); - final Date past = new Date(start - 10500); + final Date past = new Date(start - (10 * DELAY_MS + DELAY_MS / 2)); schedule( t, counter(y1), past); - schedule( t, counter(y2), past, 1000); - scheduleAtFixedRate(t, counter(y3), past, 1000); + schedule( t, counter(y2), past, DELAY_MS); + scheduleAtFixedRate(t, counter(y3), past, DELAY_MS); y3.await(); y1.await(); y2.await(); final long elapsed = System.currentTimeMillis() - start; - System.out.printf("elapsed=%d%n", elapsed); - check(elapsed < 500); - - t.cancel(); - - THROWS(IllegalStateException.class, - new F(){void f(){ t.schedule(x, 42); }}, - new F(){void f(){ t.schedule(x, past); }}, - new F(){void f(){ t.schedule(x, 42, 42); }}, - new F(){void f(){ t.schedule(x, past, 42); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, 42, 42); }}, - new F(){void f(){ t.scheduleAtFixedRate(x, past, 42); }}); - + if (elapsed >= DELAY_MS / 2) + fail(String.format("Test took too long: elapsed=%d%n", elapsed)); } //--------------------- Infrastructure --------------------------- @@ -137,8 +165,8 @@ try {test(args);} catch (Throwable t) {unexpected(t);} System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); if (failed > 0) throw new AssertionError("Some tests failed");} - abstract class F {abstract void f() throws Throwable;} - void THROWS(Class k, F... fs) { + interface F { void f() throws Throwable; } + void assertThrows(Class k, F... fs) { for (F f : fs) try {f.f(); fail("Expected " + k.getName() + " not thrown");} catch (Throwable t) {