jdk/test/java/util/concurrent/FutureTask/Customized.java
author igerasim
Mon, 02 Jun 2014 19:49:57 +0400
changeset 24692 268fbc344d53
parent 7668 d4a77089c587
child 34347 4a17f9e90a0f
permissions -rw-r--r--
8037866: Replace the Fun class in tests with lambdas Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
     2
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    73
               () -> task.get(0L, TimeUnit.SECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static <V> void checkDone(final FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            check(task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            check(! task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            check(task.get() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static <V> void checkCancelled(final FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        check(task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        check(task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        THROWS(CancellationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    88
               () -> task.get(0L, TimeUnit.SECONDS),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    89
               () -> task.get());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static <V> void checkThrew(final FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        check(task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        check(! task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        THROWS(ExecutionException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    96
               () -> task.get(0L, TimeUnit.SECONDS),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    97
               () -> task.get());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        task.cancel(mayInterruptIfRunning);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        checkCancelled(task);
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 run(FutureTask<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        boolean isCancelled = task.isCancelled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        task.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        check(task.isDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        equal(isCancelled, task.isCancelled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        final Runnable nop = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                public void run() {}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        final Runnable bad = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                public void run() { throw new Error(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            equalCounts(0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            check(task.runAndReset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            equalCounts(0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            checkDone(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            equalCounts(1,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            equal(42L, task.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            checkDone(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            equalCounts(1,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            equal(42L, task.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            cancel(task, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            equalCounts(2,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            cancel(task, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            equalCounts(2,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            equalCounts(2,1,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            check(! task.runAndReset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            final MyFutureTask<Long> task = new MyFutureTask<Long>(bad, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            checkThrew(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            equalCounts(3,1,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            equalCounts(3,1,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            task.set(99L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            checkDone(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            equalCounts(4,2,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            equalCounts(4,2,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            task.setException(new Throwable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            checkDone(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            equalCounts(4,2,2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            checkReady(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            task.setException(new Throwable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            checkThrew(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            equalCounts(5,2,3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            run(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            equalCounts(5,2,3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            task.set(99L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            checkThrew(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            equalCounts(5,3,3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        System.out.printf("doneCount=%d%n", doneCount.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        System.out.printf("setCount=%d%n", setCount.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        System.out.printf("setExceptionCount=%d%n", setExceptionCount.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (failed > 0) throw new AssertionError("Some tests failed");}
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   201
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
}