test/jdk/java/util/concurrent/tck/FutureTaskTest.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 51951 1239bfca87f8
permissions -rw-r--r--
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09 Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     1
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     3
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     7
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    12
 * accompanied this code).
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    13
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    17
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    20
 * questions.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    21
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    22
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    23
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    27
 * file:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    28
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    32
 * Other contributors include Andrew Wright, Jeffrey Hayes,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    33
 * Pat Fisher, Mike Judd.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    34
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    35
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    36
import static java.util.concurrent.TimeUnit.MILLISECONDS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    37
import static java.util.concurrent.TimeUnit.NANOSECONDS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    38
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    39
import java.util.ArrayList;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    40
import java.util.List;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    41
import java.util.NoSuchElementException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    42
import java.util.concurrent.Callable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    43
import java.util.concurrent.CancellationException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    44
import java.util.concurrent.CountDownLatch;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    45
import java.util.concurrent.ExecutionException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    46
import java.util.concurrent.Executors;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    47
import java.util.concurrent.ExecutorService;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    48
import java.util.concurrent.Future;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    49
import java.util.concurrent.FutureTask;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    50
import java.util.concurrent.TimeoutException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    51
import java.util.concurrent.atomic.AtomicInteger;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    52
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    53
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    54
import junit.framework.TestSuite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    55
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    56
public class FutureTaskTest extends JSR166TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    57
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    58
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    59
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    60
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    61
    public static Test suite() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    62
        return new TestSuite(FutureTaskTest.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    63
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    64
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    65
    void checkIsDone(Future<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    66
        assertTrue(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    67
        assertFalse(f.cancel(false));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    68
        assertFalse(f.cancel(true));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    69
        if (f instanceof PublicFutureTask) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    70
            PublicFutureTask pf = (PublicFutureTask) f;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    71
            assertEquals(1, pf.doneCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    72
            assertFalse(pf.runAndReset());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    73
            assertEquals(1, pf.doneCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    74
            Object r = null; Object exInfo = null;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    75
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    76
                r = f.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    77
            } catch (CancellationException t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    78
                exInfo = CancellationException.class;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    79
            } catch (ExecutionException t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    80
                exInfo = t.getCause();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    81
            } catch (Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    82
                threadUnexpectedException(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    83
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    84
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    85
            // Check that run and runAndReset have no effect.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    86
            int savedRunCount = pf.runCount();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    87
            pf.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    88
            pf.runAndReset();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    89
            assertEquals(savedRunCount, pf.runCount());
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
    90
            Object r2 = null;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    91
            try {
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
    92
                r2 = f.get();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    93
            } catch (CancellationException t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    94
                assertSame(exInfo, CancellationException.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    95
            } catch (ExecutionException t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    96
                assertSame(exInfo, t.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    97
            } catch (Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    98
                threadUnexpectedException(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    99
            }
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   100
            if (exInfo == null)
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   101
                assertSame(r, r2);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   102
            assertTrue(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   103
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   104
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   105
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   106
    void checkNotDone(Future<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   107
        assertFalse(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   108
        assertFalse(f.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   109
        if (f instanceof PublicFutureTask) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   110
            PublicFutureTask pf = (PublicFutureTask) f;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   111
            assertEquals(0, pf.doneCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   112
            assertEquals(0, pf.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
            assertEquals(0, pf.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   114
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   115
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   116
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   117
    void checkIsRunning(Future<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   118
        checkNotDone(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   119
        if (f instanceof FutureTask) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   120
            FutureTask ft = (FutureTask<?>) f;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   121
            // Check that run methods do nothing
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   122
            ft.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   123
            if (f instanceof PublicFutureTask) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   124
                PublicFutureTask pf = (PublicFutureTask) f;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   125
                int savedRunCount = pf.runCount();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   126
                pf.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   127
                assertFalse(pf.runAndReset());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   128
                assertEquals(savedRunCount, pf.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   129
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   130
            checkNotDone(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   131
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   132
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   133
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   134
    <T> void checkCompletedNormally(Future<T> f, T expectedValue) {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   135
        checkIsDone(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   136
        assertFalse(f.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   137
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   138
        T v1 = null, v2 = null;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   139
        try {
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   140
            v1 = f.get();
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   141
            v2 = f.get(randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   142
        } catch (Throwable fail) { threadUnexpectedException(fail); }
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   143
        assertSame(expectedValue, v1);
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   144
        assertSame(expectedValue, v2);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   145
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   146
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   147
    void checkCancelled(Future<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   148
        checkIsDone(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   149
        assertTrue(f.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   150
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   151
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   152
            f.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   153
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   154
        } catch (CancellationException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   155
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   156
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   157
        try {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   158
            f.get(randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   159
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   160
        } catch (CancellationException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   161
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   162
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   163
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   164
    void tryToConfuseDoneTask(PublicFutureTask pf) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   165
        pf.set(new Object());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   166
        pf.setException(new Error());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   167
        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   168
            pf.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   169
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   170
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   171
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   172
    void checkCompletedAbnormally(Future<?> f, Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   173
        checkIsDone(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   174
        assertFalse(f.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   175
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   176
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   177
            f.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   178
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   179
        } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   180
            assertSame(t, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   181
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   182
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   183
        try {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   184
            f.get(randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   185
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   186
        } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   187
            assertSame(t, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   188
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   189
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   190
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   191
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   192
     * Subclass to expose protected methods
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   193
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   194
    static class PublicFutureTask extends FutureTask {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   195
        private final AtomicInteger runCount;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   196
        private final AtomicInteger doneCount = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   197
        private final AtomicInteger runAndResetCount = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   198
        private final AtomicInteger setCount = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   199
        private final AtomicInteger setExceptionCount = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   200
        public int runCount() { return runCount.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   201
        public int doneCount() { return doneCount.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   202
        public int runAndResetCount() { return runAndResetCount.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   203
        public int setCount() { return setCount.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   204
        public int setExceptionCount() { return setExceptionCount.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   205
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   206
        PublicFutureTask(Runnable runnable) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   207
            this(runnable, seven);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   208
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   209
        PublicFutureTask(Runnable runnable, Object result) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   210
            this(runnable, result, new AtomicInteger(0));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   211
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   212
        private PublicFutureTask(final Runnable runnable, Object result,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   213
                                 final AtomicInteger runCount) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   214
            super(new Runnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   215
                public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   216
                    runCount.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   217
                    runnable.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   218
                }}, result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   219
            this.runCount = runCount;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   220
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   221
        PublicFutureTask(Callable callable) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   222
            this(callable, new AtomicInteger(0));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   223
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   224
        private PublicFutureTask(final Callable callable,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   225
                                 final AtomicInteger runCount) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   226
            super(new Callable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   227
                public Object call() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   228
                    runCount.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   229
                    return callable.call();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   230
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   231
            this.runCount = runCount;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   232
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   233
        @Override public void done() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   234
            assertTrue(isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   235
            doneCount.incrementAndGet();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   236
            super.done();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   237
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   238
        @Override public boolean runAndReset() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   239
            runAndResetCount.incrementAndGet();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   240
            return super.runAndReset();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   241
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   242
        @Override public void set(Object x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   243
            setCount.incrementAndGet();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   244
            super.set(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   245
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   246
        @Override public void setException(Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   247
            setExceptionCount.incrementAndGet();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   248
            super.setException(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   249
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   250
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   251
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   252
    class Counter extends CheckedRunnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   253
        final AtomicInteger count = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   254
        public int get() { return count.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   255
        public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   256
            count.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   257
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   258
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   259
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   260
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   261
     * creating a future with a null callable throws NullPointerException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   262
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   263
    public void testConstructor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   264
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   265
            new FutureTask(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   266
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   267
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   268
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   269
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   270
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   271
     * creating a future with null runnable throws NullPointerException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   272
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   273
    public void testConstructor2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   274
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   275
            new FutureTask(null, Boolean.TRUE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   276
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   277
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   278
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   279
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   280
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   281
     * isDone is true when a task completes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   282
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   283
    public void testIsDone() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   284
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   285
        assertFalse(task.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   286
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   287
        assertTrue(task.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   288
        checkCompletedNormally(task, Boolean.TRUE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   289
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   290
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   291
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   292
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   293
     * runAndReset of a non-cancelled task succeeds
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   294
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   295
    public void testRunAndReset() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   296
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   297
        for (int i = 0; i < 3; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   298
            assertTrue(task.runAndReset());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   299
            checkNotDone(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   300
            assertEquals(i + 1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   301
            assertEquals(i + 1, task.runAndResetCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   302
            assertEquals(0, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   303
            assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   304
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   305
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   306
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   307
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   308
     * runAndReset after cancellation fails
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   309
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   310
    public void testRunAndResetAfterCancel() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   311
        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   312
            PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   313
            assertTrue(task.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   314
            for (int i = 0; i < 3; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   315
                assertFalse(task.runAndReset());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   316
                assertEquals(0, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   317
                assertEquals(i + 1, task.runAndResetCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   318
                assertEquals(0, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   319
                assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   320
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   321
            tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   322
            checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   323
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   324
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   325
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   326
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   327
     * setting value causes get to return it
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   328
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   329
    public void testSet() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   330
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   331
        task.set(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   332
        for (int i = 0; i < 3; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   333
            assertSame(one, task.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   334
            assertSame(one, task.get(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   335
            assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   336
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   337
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   338
        checkCompletedNormally(task, one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   339
        assertEquals(0, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   340
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   341
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   342
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   343
     * setException causes get to throw ExecutionException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   344
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   345
    public void testSetException_get() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   346
        Exception nse = new NoSuchElementException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   347
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   348
        task.setException(nse);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   349
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   350
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   351
            task.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   352
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   353
        } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   354
            assertSame(nse, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   355
            checkCompletedAbnormally(task, nse);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   356
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   357
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   358
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   359
            task.get(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   360
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   361
        } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   362
            assertSame(nse, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   363
            checkCompletedAbnormally(task, nse);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   364
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   365
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   366
        assertEquals(1, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   367
        assertEquals(0, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   368
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   369
        checkCompletedAbnormally(task, nse);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   370
        assertEquals(0, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   371
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   372
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   373
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   374
     * cancel(false) before run succeeds
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   375
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   376
    public void testCancelBeforeRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   377
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   378
        assertTrue(task.cancel(false));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   379
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   380
        assertEquals(0, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   381
        assertEquals(0, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   382
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   383
        assertTrue(task.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   384
        assertTrue(task.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   385
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   386
        assertEquals(0, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   387
        checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   388
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   389
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   390
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   391
     * cancel(true) before run succeeds
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   392
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   393
    public void testCancelBeforeRun2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   394
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   395
        assertTrue(task.cancel(true));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   396
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   397
        assertEquals(0, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   398
        assertEquals(0, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   399
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   400
        assertTrue(task.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   401
        assertTrue(task.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   402
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   403
        assertEquals(0, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   404
        checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   405
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   406
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   407
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   408
     * cancel(false) of a completed task fails
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   409
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   410
    public void testCancelAfterRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   411
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   412
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   413
        assertFalse(task.cancel(false));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   414
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   415
        assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   416
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   417
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   418
        checkCompletedNormally(task, Boolean.TRUE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   419
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   420
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   421
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   422
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   423
     * cancel(true) of a completed task fails
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   424
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   425
    public void testCancelAfterRun2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   426
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   427
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   428
        assertFalse(task.cancel(true));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   429
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   430
        assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   431
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   432
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   433
        checkCompletedNormally(task, Boolean.TRUE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   434
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   435
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   436
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   437
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   438
     * cancel(true) interrupts a running task that subsequently succeeds
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   439
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   440
    public void testCancelInterrupt() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   441
        final CountDownLatch pleaseCancel = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   442
        final PublicFutureTask task =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   443
            new PublicFutureTask(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   444
                public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   445
                    pleaseCancel.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   446
                    try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   447
                        delay(LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   448
                        shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   449
                    } catch (InterruptedException success) {}
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   450
                    assertFalse(Thread.interrupted());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   451
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   452
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   453
        Thread t = newStartedThread(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   454
        await(pleaseCancel);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   455
        assertTrue(task.cancel(true));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   456
        assertTrue(task.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   457
        assertTrue(task.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   458
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   459
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   460
        assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   461
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   462
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   463
        checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   464
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   465
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   466
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   467
     * cancel(true) tries to interrupt a running task, but
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   468
     * Thread.interrupt throws (simulating a restrictive security
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   469
     * manager)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   470
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   471
    public void testCancelInterrupt_ThrowsSecurityException() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   472
        final CountDownLatch pleaseCancel = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   473
        final CountDownLatch cancelled = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   474
        final PublicFutureTask task =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   475
            new PublicFutureTask(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   476
                public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   477
                    pleaseCancel.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   478
                    await(cancelled);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   479
                    assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   480
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   481
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   482
        final Thread t = new Thread(task) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   483
            // Simulate a restrictive security manager.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   484
            @Override public void interrupt() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   485
                throw new SecurityException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   486
            }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   487
        t.setDaemon(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   488
        t.start();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   489
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   490
        await(pleaseCancel);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   491
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   492
            task.cancel(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   493
            shouldThrow();
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 48541
diff changeset
   494
        } catch (SecurityException success) {}
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   495
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   496
        // We failed to deliver the interrupt, but the world retains
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   497
        // its sanity, as if we had done task.cancel(false)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   498
        assertTrue(task.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   499
        assertTrue(task.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   500
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   501
        assertEquals(1, task.doneCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   502
        assertEquals(0, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   503
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   504
        cancelled.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   505
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   506
        assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   507
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   508
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   509
        checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   510
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   511
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   512
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   513
     * cancel(true) interrupts a running task that subsequently throws
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   514
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   515
    public void testCancelInterrupt_taskFails() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   516
        final CountDownLatch pleaseCancel = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   517
        final PublicFutureTask task =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   518
            new PublicFutureTask(new Runnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   519
                public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   520
                    pleaseCancel.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   521
                    try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   522
                        delay(LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   523
                        threadShouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   524
                    } catch (InterruptedException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   525
                    } catch (Throwable t) { threadUnexpectedException(t); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   526
                    throw new RuntimeException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   527
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   528
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   529
        Thread t = newStartedThread(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   530
        await(pleaseCancel);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   531
        assertTrue(task.cancel(true));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   532
        assertTrue(task.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   533
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   534
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   535
        assertEquals(0, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   536
        assertEquals(1, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   537
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   538
        checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   539
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   540
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   541
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   542
     * cancel(false) does not interrupt a running task
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   543
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   544
    public void testCancelNoInterrupt() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   545
        final CountDownLatch pleaseCancel = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   546
        final CountDownLatch cancelled = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   547
        final PublicFutureTask task =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   548
            new PublicFutureTask(new CheckedCallable<Boolean>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   549
                public Boolean realCall() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   550
                    pleaseCancel.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   551
                    await(cancelled);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   552
                    assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   553
                    return Boolean.TRUE;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   554
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   555
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   556
        Thread t = newStartedThread(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   557
        await(pleaseCancel);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   558
        assertTrue(task.cancel(false));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   559
        assertTrue(task.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   560
        cancelled.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   561
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   562
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   563
        assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   564
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   565
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   566
        checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   567
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   568
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   569
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   570
     * run in one thread causes get in another thread to retrieve value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   571
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   572
    public void testGetRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   573
        final CountDownLatch pleaseRun = new CountDownLatch(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   574
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   575
        final PublicFutureTask task =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   576
            new PublicFutureTask(new CheckedCallable<Object>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   577
                public Object realCall() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   578
                    return two;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   579
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   580
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   581
        Thread t1 = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   582
            public void realRun() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   583
                pleaseRun.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   584
                assertSame(two, task.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   585
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   586
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   587
        Thread t2 = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   588
            public void realRun() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   589
                pleaseRun.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   590
                assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   591
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   592
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   593
        await(pleaseRun);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   594
        checkNotDone(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   595
        assertTrue(t1.isAlive());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   596
        assertTrue(t2.isAlive());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   597
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   598
        checkCompletedNormally(task, two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   599
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   600
        assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   601
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   602
        awaitTermination(t1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   603
        awaitTermination(t2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   604
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   605
        checkCompletedNormally(task, two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   606
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   607
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   608
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   609
     * set in one thread causes get in another thread to retrieve value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   610
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   611
    public void testGetSet() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   612
        final CountDownLatch pleaseSet = new CountDownLatch(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   613
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   614
        final PublicFutureTask task =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   615
            new PublicFutureTask(new CheckedCallable<Object>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   616
                public Object realCall() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   617
                    return two;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   618
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   619
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   620
        Thread t1 = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   621
            public void realRun() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   622
                pleaseSet.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   623
                assertSame(two, task.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   624
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   625
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   626
        Thread t2 = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   627
            public void realRun() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   628
                pleaseSet.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   629
                assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   630
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   631
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   632
        await(pleaseSet);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   633
        checkNotDone(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   634
        assertTrue(t1.isAlive());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   635
        assertTrue(t2.isAlive());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   636
        task.set(two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   637
        assertEquals(0, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   638
        assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   639
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   640
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   641
        checkCompletedNormally(task, two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   642
        awaitTermination(t1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   643
        awaitTermination(t2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   644
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   645
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   646
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   647
     * Cancelling a task causes timed get in another thread to throw
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   648
     * CancellationException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   649
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   650
    public void testTimedGet_Cancellation() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   651
        testTimedGet_Cancellation(false);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   652
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   653
    public void testTimedGet_Cancellation_interrupt() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   654
        testTimedGet_Cancellation(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   655
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   656
    public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   657
        final CountDownLatch pleaseCancel = new CountDownLatch(3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   658
        final CountDownLatch cancelled = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   659
        final Callable<Object> callable =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   660
            new CheckedCallable<Object>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   661
            public Object realCall() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   662
                pleaseCancel.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   663
                if (mayInterruptIfRunning) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   664
                    try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   665
                        delay(2*LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   666
                    } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   667
                } else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   668
                    await(cancelled);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   669
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   670
                return two;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   671
            }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   672
        final PublicFutureTask task = new PublicFutureTask(callable);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   673
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   674
        Thread t1 = new ThreadShouldThrow(CancellationException.class) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   675
                public void realRun() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   676
                    pleaseCancel.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   677
                    task.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   678
                }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   679
        Thread t2 = new ThreadShouldThrow(CancellationException.class) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   680
                public void realRun() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   681
                    pleaseCancel.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   682
                    task.get(2*LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   683
                }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   684
        t1.start();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   685
        t2.start();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   686
        Thread t3 = newStartedThread(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   687
        await(pleaseCancel);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   688
        checkIsRunning(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   689
        task.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   690
        checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   691
        awaitTermination(t1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   692
        awaitTermination(t2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   693
        cancelled.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   694
        awaitTermination(t3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   695
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   696
        assertEquals(1, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   697
        assertEquals(0, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   698
        tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   699
        checkCancelled(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   700
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   701
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   702
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   703
     * A runtime exception in task causes get to throw ExecutionException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   704
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   705
    public void testGet_ExecutionException() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   706
        final ArithmeticException e = new ArithmeticException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   707
        final PublicFutureTask task = new PublicFutureTask(new Callable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   708
            public Object call() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   709
                throw e;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   710
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   711
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   712
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   713
        assertEquals(1, task.runCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   714
        assertEquals(0, task.setCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   715
        assertEquals(1, task.setExceptionCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   716
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   717
            task.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   718
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   719
        } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   720
            assertSame(e, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   721
            tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   722
            checkCompletedAbnormally(task, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   723
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   724
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   725
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   726
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   727
     * A runtime exception in task causes timed get to throw ExecutionException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   728
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   729
    public void testTimedGet_ExecutionException2() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   730
        final ArithmeticException e = new ArithmeticException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   731
        final PublicFutureTask task = new PublicFutureTask(new Callable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   732
            public Object call() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   733
                throw e;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   734
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   735
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   736
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   737
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   738
            task.get(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   739
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   740
        } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   741
            assertSame(e, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   742
            tryToConfuseDoneTask(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   743
            checkCompletedAbnormally(task, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   744
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   745
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   746
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   747
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   748
     * get is interruptible
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   749
     */
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 51951
diff changeset
   750
    public void testGet_Interruptible() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   751
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   752
        final FutureTask task = new FutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   753
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   754
            public void realRun() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   755
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   756
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   757
                    task.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   758
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   759
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   760
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   761
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   762
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   763
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   764
                    task.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   765
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   766
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   767
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   768
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   769
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   770
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   771
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   772
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   773
        checkNotDone(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   774
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   775
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   776
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   777
     * timed get is interruptible
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   778
     */
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 51951
diff changeset
   779
    public void testTimedGet_Interruptible() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   780
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   781
        final FutureTask task = new FutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   782
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   783
            public void realRun() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   784
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   785
                try {
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 51951
diff changeset
   786
                    task.get(randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   787
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   788
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   789
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   790
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   791
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   792
                try {
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 51951
diff changeset
   793
                    task.get(LONGER_DELAY_MS, MILLISECONDS);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   794
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   795
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   796
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   797
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   798
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   799
        await(pleaseInterrupt);
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 51951
diff changeset
   800
        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   801
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   802
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   803
        checkNotDone(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   804
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   805
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   806
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   807
     * A timed out timed get throws TimeoutException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   808
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   809
    public void testGet_TimeoutException() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   810
        FutureTask task = new FutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   811
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   812
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   813
            task.get(timeoutMillis(), MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   814
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   815
        } catch (TimeoutException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   816
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   817
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   818
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   819
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   820
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   821
     * timed get with null TimeUnit throws NullPointerException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   822
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   823
    public void testGet_NullTimeUnit() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   824
        FutureTask task = new FutureTask(new NoOpCallable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   825
        long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   826
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   827
        for (long timeout : timeouts) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   828
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   829
                task.get(timeout, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   830
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   831
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   832
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   833
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   834
        task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   835
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   836
        for (long timeout : timeouts) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   837
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   838
                task.get(timeout, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   839
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   840
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   841
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   842
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   843
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   844
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   845
     * timed get with most negative timeout works correctly (i.e. no
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   846
     * underflow bug)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   847
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   848
    public void testGet_NegativeInfinityTimeout() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   849
        final ExecutorService pool = Executors.newFixedThreadPool(10);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   850
        final Runnable nop = new Runnable() { public void run() {}};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   851
        final FutureTask<Void> task = new FutureTask<>(nop, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   852
        final List<Future<?>> futures = new ArrayList<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   853
        Runnable r = new Runnable() { public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   854
            for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   855
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   856
                    task.get(timeout, NANOSECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   857
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   858
                } catch (TimeoutException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   859
                } catch (Throwable fail) {threadUnexpectedException(fail);}}}};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   860
        for (int i = 0; i < 10; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   861
            futures.add(pool.submit(r));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   862
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   863
            joinPool(pool);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   864
            for (Future<?> future : futures)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   865
                checkCompletedNormally(future, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   866
        } finally {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   867
            task.run();         // last resort to help terminate
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   868
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   869
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   870
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   871
    /**
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   872
     * toString indicates current completion state
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   873
     */
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   874
    public void testToString_incomplete() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   875
        FutureTask<String> f = new FutureTask<>(() -> "");
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   876
        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   877
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   878
            assertTrue(f.toString().startsWith(
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   879
                               identityString(f) + "[Not completed, task ="));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   880
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   881
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   882
    public void testToString_normal() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   883
        FutureTask<String> f = new FutureTask<>(() -> "");
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   884
        f.run();
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   885
        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   886
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   887
            assertEquals(identityString(f) + "[Completed normally]",
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   888
                         f.toString());
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   889
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   890
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   891
    public void testToString_exception() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   892
        FutureTask<String> f = new FutureTask<>(
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   893
                () -> { throw new ArithmeticException(); });
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   894
        f.run();
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   895
        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   896
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   897
            assertTrue(f.toString().startsWith(
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   898
                               identityString(f) + "[Completed exceptionally: "));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   899
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   900
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   901
    public void testToString_cancelled() {
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   902
        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   903
            FutureTask<String> f = new FutureTask<>(() -> "");
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   904
            assertTrue(f.cancel(mayInterruptIfRunning));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   905
            assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   906
            if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   907
                assertEquals(identityString(f) + "[Cancelled]",
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   908
                             f.toString());
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   909
        }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   910
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   911
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   912
}