jdk/test/java/util/Timer/DelayOverflow.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 5506 202f599c92aa
child 30046 cf2c86e1819e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1014
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1014
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1014
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
     4
 *
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
     8
 *
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    13
 * accompanied this code).
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    14
 *
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1014
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1014
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1014
diff changeset
    21
 * questions.
1014
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    22
 */
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    23
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    24
/*
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    25
 * @test
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    26
 * @bug 6730507
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    27
 * @summary java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    28
 * @author Chris Hegarty
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    29
 * @author Martin Buchholz
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    30
 */
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    31
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    32
import java.util.Date;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    33
import java.util.Timer;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    34
import java.util.TimerTask;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    35
import java.util.concurrent.*;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    36
import java.util.concurrent.atomic.AtomicInteger;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    37
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    38
public class DelayOverflow
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    39
{
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    40
    void scheduleNow(Timer timer, TimerTask task, int how) {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    41
        switch (how) {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    42
            case 0 :
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    43
                timer.schedule(task, new Date(), Long.MAX_VALUE);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    44
                break;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    45
            case 1:
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    46
                timer.schedule(task, 0L, Long.MAX_VALUE);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    47
                break;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    48
            case 2:
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    49
                timer.scheduleAtFixedRate(task, new Date(), Long.MAX_VALUE);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    50
                break;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    51
            case 3:
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    52
                timer.scheduleAtFixedRate(task, 0L, Long.MAX_VALUE);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    53
                break;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    54
            default:
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    55
                fail(String.valueOf(how));
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    56
        }
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    57
    }
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    58
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    59
    void sleep(long millis) {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    60
        try { Thread.sleep(millis); }
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    61
        catch (Throwable t) { unexpected(t); }
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    62
    }
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    63
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    64
    /** Checks that scheduledExecutionTime returns a "recent" time. */
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    65
    void checkScheduledExecutionTime(TimerTask task) {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    66
        long t = System.currentTimeMillis()
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    67
            - task.scheduledExecutionTime();
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    68
        check(t >= 0 && t < 1000 * 600);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    69
    }
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    70
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    71
    void test(String[] args) throws Throwable {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    72
        for (int how=0; how<4; how++) {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    73
            final CountDownLatch done = new CountDownLatch(1);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    74
            final AtomicInteger count = new AtomicInteger(0);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    75
            final Timer timer = new Timer();
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    76
            final TimerTask task = new TimerTask() {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    77
                @Override
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    78
                public void run() {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    79
                    checkScheduledExecutionTime(this);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    80
                    count.incrementAndGet();
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    81
                    done.countDown();
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    82
                }};
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    83
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    84
            scheduleNow(timer, task, how);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    85
            done.await();
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    86
            equal(count.get(), 1);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    87
            checkScheduledExecutionTime(task);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    88
            if (new java.util.Random().nextBoolean())
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    89
                sleep(10);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    90
            check(task.cancel());
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    91
            timer.cancel();
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    92
            checkScheduledExecutionTime(task);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    93
        }
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    94
    }
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    95
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    96
    //--------------------- Infrastructure ---------------------------
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    97
    volatile int passed = 0, failed = 0;
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    98
    void pass() {passed++;}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
    99
    void fail() {failed++; Thread.dumpStack();}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   100
    void fail(String msg) {System.err.println(msg); fail();}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   101
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   102
    void check(boolean cond) {if (cond) pass(); else fail();}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   103
    void equal(Object x, Object y) {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   104
        if (x == null ? y == null : x.equals(y)) pass();
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   105
        else fail(x + " not equal to " + y);}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   106
    public static void main(String[] args) throws Throwable {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   107
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   108
        try {k.getMethod("instanceMain",String[].class)
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   109
                .invoke( k.newInstance(), (Object) args);}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   110
        catch (Throwable e) {throw e.getCause();}}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   111
    public void instanceMain(String[] args) throws Throwable {
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   112
        try {test(args);} catch (Throwable t) {unexpected(t);}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   113
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   114
        if (failed > 0) throw new AssertionError("Some tests failed");}
335a6ba0adba 6730507: java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
martin
parents:
diff changeset
   115
}