jdk/test/java/util/concurrent/FutureTask/Customized.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6464365
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test state transitions; check protected methods are called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.atomic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Customized {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static final AtomicLong doneCount = new AtomicLong(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static final AtomicLong setCount = new AtomicLong(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static final AtomicLong setExceptionCount = new AtomicLong(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static void equal(long expected, AtomicLong actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        equal(expected, actual.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void equalCounts(long x, long y, long z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        equal(x, doneCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        equal(y, setCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        equal(z, setExceptionCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static class MyFutureTask<V> extends FutureTask<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        MyFutureTask(Runnable r, V result) { super(r, result); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        protected void done() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            doneCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            super.done();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        protected void set(V v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            setCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            super.set(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        protected void setException(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            setExceptionCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            super.setException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public boolean runAndReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return super.runAndReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static <V> void checkReady(final FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        check(! task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        check(! task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        THROWS(TimeoutException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
               new Fun(){void f() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                       task.get(0L, TimeUnit.SECONDS); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static <V> void checkDone(final FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            check(task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            check(! task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            check(task.get() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static <V> void checkCancelled(final FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        check(task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        check(task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        THROWS(CancellationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
           new Fun(){void f() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
               task.get(0L, TimeUnit.SECONDS); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
           new Fun(){void f() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
               task.get(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static <V> void checkThrew(final FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        check(task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        check(! task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        THROWS(ExecutionException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
           new Fun(){void f() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
               task.get(0L, TimeUnit.SECONDS); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
           new Fun(){void f() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
               task.get(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        task.cancel(mayInterruptIfRunning);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        checkCancelled(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static <V> void run(FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        boolean isCancelled = task.isCancelled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        task.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        check(task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        equal(isCancelled, task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        final Runnable nop = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                public void run() {}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        final Runnable bad = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                public void run() { throw new Error(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            equalCounts(0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            check(task.runAndReset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            equalCounts(0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            checkDone(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            equalCounts(1,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            equal(42L, task.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            checkDone(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            equalCounts(1,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            equal(42L, task.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            cancel(task, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            equalCounts(2,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            cancel(task, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            equalCounts(2,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            equalCounts(2,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            check(! task.runAndReset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            final MyFutureTask<Long> task = new MyFutureTask<Long>(bad, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            checkThrew(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            equalCounts(3,1,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            equalCounts(3,1,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            task.set(99L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            checkDone(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            equalCounts(4,2,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            equalCounts(4,2,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            task.setException(new Throwable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            checkDone(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            equalCounts(4,2,2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            task.setException(new Throwable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            checkThrew(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            equalCounts(5,2,3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            equalCounts(5,2,3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            task.set(99L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            checkThrew(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            equalCounts(5,3,3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        System.out.printf("doneCount=%d%n", doneCount.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        System.out.printf("setCount=%d%n", setCount.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        System.out.printf("setExceptionCount=%d%n", setExceptionCount.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private static abstract class Fun {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
}