test/jdk/java/util/concurrent/tck/CompletableFutureTest.java
author dl
Sat, 10 Feb 2018 09:23:41 -0800
changeset 48843 21efc1774302
parent 48541 946e34c2dec9
child 50764 5637aca18f1d
permissions -rw-r--r--
8195590: Miscellaneous changes imported from jsr166 CVS 2018-02 Reviewed-by: martin, psandoz, dholmes
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 and Martin Buchholz with assistance from
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    30
 * members of JCP JSR-166 Expert Group and released to the public
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    31
 * domain, as explained at
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    32
 * http://creativecommons.org/publicdomain/zero/1.0/
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    33
 */
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
import static java.util.concurrent.TimeUnit.MILLISECONDS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    36
import static java.util.concurrent.TimeUnit.SECONDS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    37
import static java.util.concurrent.CompletableFuture.completedFuture;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    38
import static java.util.concurrent.CompletableFuture.failedFuture;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    39
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    40
import java.lang.reflect.Method;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    41
import java.lang.reflect.Modifier;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    42
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    43
import java.util.stream.Collectors;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    44
import java.util.stream.Stream;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    45
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    46
import java.util.ArrayList;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    47
import java.util.Arrays;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    48
import java.util.List;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    49
import java.util.Objects;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    50
import java.util.Set;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    51
import java.util.concurrent.Callable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    52
import java.util.concurrent.CancellationException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    53
import java.util.concurrent.CompletableFuture;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    54
import java.util.concurrent.CompletionException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    55
import java.util.concurrent.CompletionStage;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    56
import java.util.concurrent.ExecutionException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    57
import java.util.concurrent.Executor;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    58
import java.util.concurrent.ForkJoinPool;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    59
import java.util.concurrent.ForkJoinTask;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
    60
import java.util.concurrent.RejectedExecutionException;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    61
import java.util.concurrent.TimeoutException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    62
import java.util.concurrent.atomic.AtomicInteger;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    63
import java.util.concurrent.atomic.AtomicReference;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    64
import java.util.function.BiConsumer;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    65
import java.util.function.BiFunction;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    66
import java.util.function.Consumer;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    67
import java.util.function.Function;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    68
import java.util.function.Predicate;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    69
import java.util.function.Supplier;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    70
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    71
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    72
import junit.framework.TestSuite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    73
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    74
public class CompletableFutureTest extends JSR166TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    75
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    76
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    77
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    78
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    79
    public static Test suite() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    80
        return new TestSuite(CompletableFutureTest.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    81
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    82
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    83
    static class CFException extends RuntimeException {}
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
    void checkIncomplete(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    86
        assertFalse(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    87
        assertFalse(f.isCancelled());
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
    88
        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    89
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    90
            assertNull(f.getNow(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    91
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    92
        try {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
    93
            f.get(randomExpiredTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    94
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    95
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    96
        catch (TimeoutException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    97
        catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    98
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    99
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   100
    <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   101
        checkTimedGet(f, value);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   102
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   103
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   104
            assertEquals(value, f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   105
            assertEquals(value, f.getNow(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   106
            assertEquals(value, f.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   107
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   108
        assertTrue(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   109
        assertFalse(f.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   110
        assertFalse(f.isCompletedExceptionally());
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   111
        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   112
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
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
     * Returns the "raw" internal exceptional completion of f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   116
     * without any additional wrapping with CompletionException.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   117
     */
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
   118
    Throwable exceptionalCompletion(CompletableFuture<?> f) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
   119
        // handle (and whenComplete and exceptionally) can distinguish
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
   120
        // between "direct" and "wrapped" exceptional completion
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
   121
        return f.handle((u, t) -> t).join();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   122
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   123
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   124
    void checkCompletedExceptionally(CompletableFuture<?> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   125
                                     boolean wrapped,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   126
                                     Consumer<Throwable> checker) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   127
        Throwable cause = exceptionalCompletion(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   128
        if (wrapped) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   129
            assertTrue(cause instanceof CompletionException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   130
            cause = cause.getCause();
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
        checker.accept(cause);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   133
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   134
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   135
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   136
            f.get(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   137
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   138
        } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   139
            assertSame(cause, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   140
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   141
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   142
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   143
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   144
            f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   145
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   146
        } catch (CompletionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   147
            assertSame(cause, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   148
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   149
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   150
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   151
            f.getNow(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   152
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   153
        } catch (CompletionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   154
            assertSame(cause, success.getCause());
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 {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   158
            f.get();
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 (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   161
            assertSame(cause, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   162
        } catch (Throwable fail) { threadUnexpectedException(fail); }
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
        assertFalse(f.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   165
        assertTrue(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   166
        assertTrue(f.isCompletedExceptionally());
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   167
        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   168
    }
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
    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   171
        checkCompletedExceptionally(f, true,
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   172
            t -> assertTrue(t instanceof CFException));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   173
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   174
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   175
    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   176
        checkCompletedExceptionally(f, true,
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   177
            t -> assertTrue(t instanceof CancellationException));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   178
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   179
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   180
    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   181
        checkCompletedExceptionally(f, false,
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   182
            t -> assertTrue(t instanceof TimeoutException));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   183
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   184
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   185
    void checkCompletedWithWrappedException(CompletableFuture<?> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   186
                                            Throwable ex) {
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   187
        checkCompletedExceptionally(f, true, t -> assertSame(t, ex));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   188
    }
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
    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   191
        checkCompletedExceptionally(f, false, t -> assertSame(t, ex));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   192
    }
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
    void checkCancelled(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   195
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   196
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   197
            f.get(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   198
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   199
        } catch (CancellationException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   200
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   201
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   202
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   203
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   204
            f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   205
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   206
        } catch (CancellationException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   207
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   208
            f.getNow(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   209
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   210
        } catch (CancellationException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   211
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   212
            f.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   213
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   214
        } catch (CancellationException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   215
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   216
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   217
        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   218
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   219
        assertTrue(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   220
        assertTrue(f.isCompletedExceptionally());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   221
        assertTrue(f.isCancelled());
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   222
        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
35394
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   225
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   226
     * A newly constructed CompletableFuture is incomplete, as indicated
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   227
     * by methods isDone, isCancelled, and getNow
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   228
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   229
    public void testConstructor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   230
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   231
        checkIncomplete(f);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   234
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   235
     * complete completes normally, as indicated by methods isDone,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   236
     * isCancelled, join, get, and getNow
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
    public void testComplete() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   239
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   240
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   241
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   242
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   243
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   244
        assertFalse(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   245
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   246
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   247
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   248
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   249
     * completeExceptionally completes exceptionally, as indicated by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   250
     * methods isDone, isCancelled, join, get, and getNow
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
    public void testCompleteExceptionally() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   253
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   254
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   255
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   256
        f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   257
        checkCompletedExceptionally(f, ex);
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
     * cancel completes exceptionally and reports cancelled, as indicated by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   262
     * methods isDone, isCancelled, join, get, and getNow
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   263
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   264
    public void testCancel() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   265
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   266
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   267
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   268
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   269
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   270
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   271
        assertTrue(f.cancel(!mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   272
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   273
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   274
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   275
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   276
     * obtrudeValue forces completion with given value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   277
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   278
    public void testObtrudeValue() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   279
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   280
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   281
        assertTrue(f.complete(one));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   282
        checkCompletedNormally(f, one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   283
        f.obtrudeValue(three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   284
        checkCompletedNormally(f, three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   285
        f.obtrudeValue(two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   286
        checkCompletedNormally(f, two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   287
        f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   288
        f.obtrudeValue(three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   289
        checkCompletedNormally(f, three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   290
        f.obtrudeValue(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   291
        checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   292
        f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   293
        f.completeExceptionally(new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   294
        f.obtrudeValue(four);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   295
        checkCompletedNormally(f, four);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   296
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   297
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   298
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   299
     * obtrudeException forces completion with given exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   300
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   301
    public void testObtrudeException() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   302
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   303
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   304
        CFException ex;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   305
        CompletableFuture<Integer> f;
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
        f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   308
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   309
        for (int i = 0; i < 2; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   310
            f.obtrudeException(ex = new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   311
            checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   312
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   313
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   314
        f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   315
        for (int i = 0; i < 2; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   316
            f.obtrudeException(ex = new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   317
            checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   318
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   319
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   320
        f = new CompletableFuture<>();
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   321
        f.completeExceptionally(new CFException());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   322
        f.obtrudeValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   323
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   324
        f.obtrudeException(ex = new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   325
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   326
        f.completeExceptionally(new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   327
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   328
        assertFalse(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   329
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   330
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   331
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   332
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   333
     * getNumberOfDependents returns number of dependent tasks
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   334
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   335
    public void testGetNumberOfDependents() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   336
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   337
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   338
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   339
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   340
        assertEquals(0, f.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   341
        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   342
        assertEquals(1, f.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   343
        assertEquals(0, g.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   344
        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   345
        assertEquals(2, f.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   346
        assertEquals(0, h.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   347
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   348
        checkCompletedNormally(g, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   349
        checkCompletedNormally(h, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   350
        assertEquals(0, f.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   351
        assertEquals(0, g.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   352
        assertEquals(0, h.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   353
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   354
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   355
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   356
     * toString indicates current completion state
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   357
     */
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   358
    public void testToString_incomplete() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   359
        CompletableFuture<String> f = new CompletableFuture<>();
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   360
        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   361
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   362
            assertEquals(identityString(f) + "[Not completed]",
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   363
                         f.toString());
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   364
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   365
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   366
    public void testToString_normal() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   367
        CompletableFuture<String> f = new CompletableFuture<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   368
        assertTrue(f.complete("foo"));
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   369
        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   370
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   371
            assertEquals(identityString(f) + "[Completed normally]",
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   372
                         f.toString());
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   373
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   374
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   375
    public void testToString_exception() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   376
        CompletableFuture<String> f = new CompletableFuture<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   377
        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   378
        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   379
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   380
            assertTrue(f.toString().startsWith(
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   381
                               identityString(f) + "[Completed exceptionally: "));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   382
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   383
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   384
    public void testToString_cancelled() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   385
        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   386
            CompletableFuture<String> f = new CompletableFuture<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   387
            assertTrue(f.cancel(mayInterruptIfRunning));
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   388
            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   389
            if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   390
                assertTrue(f.toString().startsWith(
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   391
                                   identityString(f) + "[Completed exceptionally: "));
35394
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   395
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   396
     * completedFuture returns a completed CompletableFuture with given value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   397
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   398
    public void testCompletedFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   399
        CompletableFuture<String> f = CompletableFuture.completedFuture("test");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   400
        checkCompletedNormally(f, "test");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   401
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   402
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   403
    abstract static class CheckedAction {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   404
        int invocationCount = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   405
        final ExecutionMode m;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   406
        CheckedAction(ExecutionMode m) { this.m = m; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   407
        void invoked() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   408
            m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   409
            assertEquals(0, invocationCount++);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   410
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   411
        void assertNotInvoked() { assertEquals(0, invocationCount); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   412
        void assertInvoked() { assertEquals(1, invocationCount); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   413
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   414
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   415
    abstract static class CheckedIntegerAction extends CheckedAction {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   416
        Integer value;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   417
        CheckedIntegerAction(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   418
        void assertValue(Integer expected) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   419
            assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   420
            assertEquals(expected, value);
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
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   424
    static class IntegerSupplier extends CheckedAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   425
        implements Supplier<Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   426
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   427
        final Integer value;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   428
        IntegerSupplier(ExecutionMode m, Integer value) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   429
            super(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   430
            this.value = value;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   431
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   432
        public Integer get() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   433
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   434
            return value;
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
    // A function that handles and produces null values as well.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   439
    static Integer inc(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   440
        return (x == null) ? null : x + 1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   441
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   442
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   443
    static class NoopConsumer extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   444
        implements Consumer<Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   445
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   446
        NoopConsumer(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   447
        public void accept(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   448
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   449
            value = x;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   450
        }
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
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   453
    static class IncFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   454
        implements Function<Integer,Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   455
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   456
        IncFunction(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   457
        public Integer apply(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   458
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   459
            return value = inc(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   460
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   461
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   462
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   463
    // Choose non-commutative actions for better coverage
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   464
    // A non-commutative function that handles and produces null values as well.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   465
    static Integer subtract(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   466
        return (x == null && y == null) ? null :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   467
            ((x == null) ? 42 : x.intValue())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   468
            - ((y == null) ? 99 : y.intValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   469
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   470
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   471
    static class SubtractAction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   472
        implements BiConsumer<Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   473
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   474
        SubtractAction(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   475
        public void accept(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   476
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   477
            value = subtract(x, y);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   478
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   479
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   480
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   481
    static class SubtractFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   482
        implements BiFunction<Integer, Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   483
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   484
        SubtractFunction(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   485
        public Integer apply(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   486
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   487
            return value = subtract(x, y);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   488
        }
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
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   491
    static class Noop extends CheckedAction implements Runnable {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   492
        Noop(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   493
        public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   494
            invoked();
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   497
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   498
    static class FailingSupplier extends CheckedAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   499
        implements Supplier<Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   500
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   501
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   502
        FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   503
        public Integer get() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   504
            invoked();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   505
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   506
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   507
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   508
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   509
    static class FailingConsumer extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   510
        implements Consumer<Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   511
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   512
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   513
        FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   514
        public void accept(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   515
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   516
            value = x;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   517
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   518
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   519
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   520
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   521
    static class FailingBiConsumer extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   522
        implements BiConsumer<Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   523
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   524
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   525
        FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   526
        public void accept(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   527
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   528
            value = subtract(x, y);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   529
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   530
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   531
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   532
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   533
    static class FailingFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   534
        implements Function<Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   535
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   536
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   537
        FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   538
        public Integer apply(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   539
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   540
            value = x;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   541
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   542
        }
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
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   545
    static class FailingBiFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   546
        implements BiFunction<Integer, Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   547
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   548
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   549
        FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   550
        public Integer apply(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   551
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   552
            value = subtract(x, y);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   553
            throw ex;
35394
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
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   557
    static class FailingRunnable extends CheckedAction implements Runnable {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   558
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   559
        FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   560
        public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   561
            invoked();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   562
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   563
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   564
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   565
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   566
    static class CompletableFutureInc extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   567
        implements Function<Integer, CompletableFuture<Integer>>
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
        CompletableFutureInc(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   570
        public CompletableFuture<Integer> apply(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   571
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   572
            value = x;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   573
            CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   574
            assertTrue(f.complete(inc(x)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   575
            return f;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   576
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   577
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   578
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   579
    static class FailingCompletableFutureFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   580
        implements Function<Integer, CompletableFuture<Integer>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   581
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   582
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   583
        FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   584
        public CompletableFuture<Integer> apply(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   585
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   586
            value = x;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   587
            throw ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   588
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   589
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   590
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   591
    static class CountingRejectingExecutor implements Executor {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   592
        final RejectedExecutionException ex = new RejectedExecutionException();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   593
        final AtomicInteger count = new AtomicInteger(0);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   594
        public void execute(Runnable r) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   595
            count.getAndIncrement();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   596
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   597
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   598
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   599
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   600
    // Used for explicit executor tests
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   601
    static final class ThreadExecutor implements Executor {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   602
        final AtomicInteger count = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   603
        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   604
        static boolean startedCurrentThread() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   605
            return Thread.currentThread().getThreadGroup() == tg;
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
        public void execute(Runnable r) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   609
            count.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   610
            new Thread(tg, r).start();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   611
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   612
    }
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
    static final boolean defaultExecutorIsCommonPool
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   615
        = ForkJoinPool.getCommonPoolParallelism() > 1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   616
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   617
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   618
     * Permits the testing of parallel code for the 3 different
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   619
     * execution modes without copy/pasting all the test methods.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   620
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   621
    enum ExecutionMode {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   622
        SYNC {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   623
            public void checkExecutionMode() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   624
                assertFalse(ThreadExecutor.startedCurrentThread());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   625
                assertNull(ForkJoinTask.getPool());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   626
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   627
            public CompletableFuture<Void> runAsync(Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   628
                throw new UnsupportedOperationException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   629
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   630
            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   631
                throw new UnsupportedOperationException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   632
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   633
            public <T> CompletableFuture<Void> thenRun
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   634
                (CompletableFuture<T> f, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   635
                return f.thenRun(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   636
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   637
            public <T> CompletableFuture<Void> thenAccept
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   638
                (CompletableFuture<T> f, Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   639
                return f.thenAccept(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   640
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   641
            public <T,U> CompletableFuture<U> thenApply
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   642
                (CompletableFuture<T> f, Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   643
                return f.thenApply(a);
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
            public <T,U> CompletableFuture<U> thenCompose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   646
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   647
                 Function<? super T,? extends CompletionStage<U>> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   648
                return f.thenCompose(a);
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 <T,U> CompletableFuture<U> handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   651
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   652
                 BiFunction<? super T,Throwable,? extends U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   653
                return f.handle(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   654
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   655
            public <T> CompletableFuture<T> whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   656
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   657
                 BiConsumer<? super T,? super Throwable> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   658
                return f.whenComplete(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   659
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   660
            public <T,U> CompletableFuture<Void> runAfterBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   661
                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   662
                return f.runAfterBoth(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   663
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   664
            public <T,U> CompletableFuture<Void> thenAcceptBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   665
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   666
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   667
                 BiConsumer<? super T,? super U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   668
                return f.thenAcceptBoth(g, a);
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
            public <T,U,V> CompletableFuture<V> thenCombine
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   671
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   672
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   673
                 BiFunction<? super T,? super U,? extends V> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   674
                return f.thenCombine(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   675
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   676
            public <T> CompletableFuture<Void> runAfterEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   677
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   678
                 CompletionStage<?> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   679
                 java.lang.Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   680
                return f.runAfterEither(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   681
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   682
            public <T> CompletableFuture<Void> acceptEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   683
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   684
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   685
                 Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   686
                return f.acceptEither(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   687
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   688
            public <T,U> CompletableFuture<U> applyToEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   689
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   690
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   691
                 Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   692
                return f.applyToEither(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   693
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   694
        },
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   695
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   696
        ASYNC {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   697
            public void checkExecutionMode() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   698
                assertEquals(defaultExecutorIsCommonPool,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   699
                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
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
            public CompletableFuture<Void> runAsync(Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   702
                return CompletableFuture.runAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   703
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   704
            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   705
                return CompletableFuture.supplyAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   706
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   707
            public <T> CompletableFuture<Void> thenRun
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   708
                (CompletableFuture<T> f, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   709
                return f.thenRunAsync(a);
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
            public <T> CompletableFuture<Void> thenAccept
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   712
                (CompletableFuture<T> f, Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   713
                return f.thenAcceptAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   714
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   715
            public <T,U> CompletableFuture<U> thenApply
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   716
                (CompletableFuture<T> f, Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   717
                return f.thenApplyAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   718
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   719
            public <T,U> CompletableFuture<U> thenCompose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   720
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   721
                 Function<? super T,? extends CompletionStage<U>> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   722
                return f.thenComposeAsync(a);
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
            public <T,U> CompletableFuture<U> handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   725
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   726
                 BiFunction<? super T,Throwable,? extends U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   727
                return f.handleAsync(a);
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 <T> CompletableFuture<T> whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   730
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   731
                 BiConsumer<? super T,? super Throwable> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   732
                return f.whenCompleteAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   733
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   734
            public <T,U> CompletableFuture<Void> runAfterBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   735
                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   736
                return f.runAfterBothAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   737
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   738
            public <T,U> CompletableFuture<Void> thenAcceptBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   739
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   740
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   741
                 BiConsumer<? super T,? super U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   742
                return f.thenAcceptBothAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   743
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   744
            public <T,U,V> CompletableFuture<V> thenCombine
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   745
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   746
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   747
                 BiFunction<? super T,? super U,? extends V> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   748
                return f.thenCombineAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   749
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   750
            public <T> CompletableFuture<Void> runAfterEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   751
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   752
                 CompletionStage<?> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   753
                 java.lang.Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   754
                return f.runAfterEitherAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   755
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   756
            public <T> CompletableFuture<Void> acceptEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   757
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   758
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   759
                 Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   760
                return f.acceptEitherAsync(g, a);
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
            public <T,U> CompletableFuture<U> applyToEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   763
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   764
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   765
                 Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   766
                return f.applyToEitherAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   767
            }
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
        EXECUTOR {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   771
            public void checkExecutionMode() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   772
                assertTrue(ThreadExecutor.startedCurrentThread());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   773
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   774
            public CompletableFuture<Void> runAsync(Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   775
                return CompletableFuture.runAsync(a, new ThreadExecutor());
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
            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   778
                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   779
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   780
            public <T> CompletableFuture<Void> thenRun
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   781
                (CompletableFuture<T> f, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   782
                return f.thenRunAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   783
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   784
            public <T> CompletableFuture<Void> thenAccept
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   785
                (CompletableFuture<T> f, Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   786
                return f.thenAcceptAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   787
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   788
            public <T,U> CompletableFuture<U> thenApply
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   789
                (CompletableFuture<T> f, Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   790
                return f.thenApplyAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   791
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   792
            public <T,U> CompletableFuture<U> thenCompose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   793
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   794
                 Function<? super T,? extends CompletionStage<U>> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   795
                return f.thenComposeAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   796
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   797
            public <T,U> CompletableFuture<U> handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   798
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   799
                 BiFunction<? super T,Throwable,? extends U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   800
                return f.handleAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   801
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   802
            public <T> CompletableFuture<T> whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   803
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   804
                 BiConsumer<? super T,? super Throwable> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   805
                return f.whenCompleteAsync(a, new ThreadExecutor());
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
            public <T,U> CompletableFuture<Void> runAfterBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   808
                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   809
                return f.runAfterBothAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   810
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   811
            public <T,U> CompletableFuture<Void> thenAcceptBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   812
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   813
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   814
                 BiConsumer<? super T,? super U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   815
                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   816
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   817
            public <T,U,V> CompletableFuture<V> thenCombine
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   818
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   819
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   820
                 BiFunction<? super T,? super U,? extends V> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   821
                return f.thenCombineAsync(g, a, new ThreadExecutor());
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 <T> CompletableFuture<Void> runAfterEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   824
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   825
                 CompletionStage<?> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   826
                 java.lang.Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   827
                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   828
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   829
            public <T> CompletableFuture<Void> acceptEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   830
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   831
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   832
                 Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   833
                return f.acceptEitherAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   834
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   835
            public <T,U> CompletableFuture<U> applyToEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   836
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   837
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   838
                 Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   839
                return f.applyToEitherAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   840
            }
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
        public abstract void checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   844
        public abstract CompletableFuture<Void> runAsync(Runnable a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   845
        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   846
        public abstract <T> CompletableFuture<Void> thenRun
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   847
            (CompletableFuture<T> f, Runnable a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   848
        public abstract <T> CompletableFuture<Void> thenAccept
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   849
            (CompletableFuture<T> f, Consumer<? super T> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   850
        public abstract <T,U> CompletableFuture<U> thenApply
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   851
            (CompletableFuture<T> f, Function<? super T,U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   852
        public abstract <T,U> CompletableFuture<U> thenCompose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   853
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   854
             Function<? super T,? extends CompletionStage<U>> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   855
        public abstract <T,U> CompletableFuture<U> handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   856
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   857
             BiFunction<? super T,Throwable,? extends U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   858
        public abstract <T> CompletableFuture<T> whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   859
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   860
             BiConsumer<? super T,? super Throwable> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   861
        public abstract <T,U> CompletableFuture<Void> runAfterBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   862
            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   863
        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   864
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   865
             CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   866
             BiConsumer<? super T,? super U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   867
        public abstract <T,U,V> CompletableFuture<V> thenCombine
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   868
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   869
             CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   870
             BiFunction<? super T,? super U,? extends V> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   871
        public abstract <T> CompletableFuture<Void> runAfterEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   872
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   873
             CompletionStage<?> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   874
             java.lang.Runnable a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   875
        public abstract <T> CompletableFuture<Void> acceptEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   876
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   877
             CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   878
             Consumer<? super T> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   879
        public abstract <T,U> CompletableFuture<U> applyToEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   880
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   881
             CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   882
             Function<? super T,U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   883
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   884
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   885
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   886
     * exceptionally action is not invoked when source completes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   887
     * normally, and source result is propagated
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   888
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   889
    public void testExceptionally_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   890
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   891
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   892
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   893
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   894
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   895
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   896
        final CompletableFuture<Integer> g = f.exceptionally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   897
            ((Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   898
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   899
                threadFail("should not be called");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   900
                return null;            // unreached
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   901
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   902
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   903
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   904
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   905
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   906
        assertEquals(0, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   907
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   908
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   909
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   910
     * exceptionally action completes with function value on source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   911
     * exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   912
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   913
    public void testExceptionally_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   914
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   915
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   916
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   917
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   918
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   919
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   920
        if (!createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   921
        final CompletableFuture<Integer> g = f.exceptionally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   922
            ((Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   923
                ExecutionMode.SYNC.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   924
                threadAssertSame(t, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   925
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   926
                return v1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   927
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   928
        if (createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   929
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   930
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   931
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   932
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   933
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   934
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   935
     * If an "exceptionally action" throws an exception, it completes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   936
     * exceptionally with that exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   937
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   938
    public void testExceptionally_exceptionalCompletionActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   939
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   940
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   941
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   942
        final CFException ex1 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   943
        final CFException ex2 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   944
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   945
        if (!createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   946
        final CompletableFuture<Integer> g = f.exceptionally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   947
            ((Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   948
                ExecutionMode.SYNC.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   949
                threadAssertSame(t, ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   950
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   951
                throw ex2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   952
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   953
        if (createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   954
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   955
        checkCompletedWithWrappedException(g, ex2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   956
        checkCompletedExceptionally(f, ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   957
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   958
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   959
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   960
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   961
     * whenComplete action executes on normal completion, propagating
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   962
     * source result.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   963
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   964
    public void testWhenComplete_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   965
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   966
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   967
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   968
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   969
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   970
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   971
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   972
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   973
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   974
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   975
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   976
                threadAssertSame(result, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   977
                threadAssertNull(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   978
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   979
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   980
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   981
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   982
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   983
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   984
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   985
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   986
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   987
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   988
     * whenComplete action executes on exceptional completion, propagating
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   989
     * source result.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   990
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   991
    public void testWhenComplete_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   992
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   993
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   994
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   995
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   996
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   997
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   998
        if (!createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   999
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1000
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1001
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1002
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1003
                threadAssertNull(result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1004
                threadAssertSame(t, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1005
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1006
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1007
        if (createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1008
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1009
        checkCompletedWithWrappedException(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1010
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1011
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1012
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1013
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1014
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1015
     * whenComplete action executes on cancelled source, propagating
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1016
     * CancellationException.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1017
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1018
    public void testWhenComplete_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1019
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1020
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1021
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1022
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1023
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1024
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1025
        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1026
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1027
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1028
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1029
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1030
                threadAssertNull(result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1031
                threadAssertTrue(t instanceof CancellationException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1032
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1033
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1034
        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1035
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1036
        checkCompletedWithWrappedCancellationException(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1037
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1038
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1039
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1040
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1041
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1042
     * If a whenComplete action throws an exception when triggered by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1043
     * a normal completion, it completes exceptionally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1044
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1045
    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1046
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1047
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1048
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1049
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1050
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1051
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1052
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1053
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1054
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1055
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1056
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1057
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1058
                threadAssertSame(result, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1059
                threadAssertNull(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1060
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1061
                throw ex;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1062
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1063
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1064
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1065
        checkCompletedWithWrappedException(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1066
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1067
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1068
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1069
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1070
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1071
     * If a whenComplete action throws an exception when triggered by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1072
     * a source completion that also throws an exception, the source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1073
     * exception takes precedence (unlike handle)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1074
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1075
    public void testWhenComplete_sourceFailedActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1076
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1077
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1078
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1079
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1080
        final CFException ex1 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1081
        final CFException ex2 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1082
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1083
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1084
        if (!createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1085
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1086
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1087
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1088
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1089
                threadAssertSame(t, ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1090
                threadAssertNull(result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1091
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1092
                throw ex2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1093
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1094
        if (createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1095
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1096
        checkCompletedWithWrappedException(g, ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1097
        checkCompletedExceptionally(f, ex1);
35980
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35397
diff changeset
  1098
        if (testImplementationDetails) {
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35397
diff changeset
  1099
            assertEquals(1, ex1.getSuppressed().length);
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35397
diff changeset
  1100
            assertSame(ex2, ex1.getSuppressed()[0]);
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35397
diff changeset
  1101
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1102
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1103
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1104
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1105
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1106
     * handle action completes normally with function value on normal
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1107
     * completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1108
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1109
    public void testHandle_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1110
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1111
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1112
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1113
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1114
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1115
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1116
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1117
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1118
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1119
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1120
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1121
                threadAssertSame(result, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1122
                threadAssertNull(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1123
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1124
                return inc(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1125
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1126
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1127
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1128
        checkCompletedNormally(g, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1129
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1130
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1131
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1132
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1133
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1134
     * handle action completes normally with function value on
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1135
     * exceptional completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1136
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1137
    public void testHandle_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1138
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1139
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1140
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1141
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1142
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1143
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1144
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1145
        if (!createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1146
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1147
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1148
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1149
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1150
                threadAssertNull(result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1151
                threadAssertSame(t, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1152
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1153
                return v1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1154
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1155
        if (createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1156
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1157
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1158
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1159
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1160
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1161
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1162
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1163
     * handle action completes normally with function value on
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1164
     * cancelled source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1165
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1166
    public void testHandle_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1167
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1168
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1169
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1170
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1171
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1172
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1173
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1174
        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1175
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1176
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1177
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1178
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1179
                threadAssertNull(result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1180
                threadAssertTrue(t instanceof CancellationException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1181
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1182
                return v1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1183
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1184
        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1185
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1186
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1187
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1188
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1189
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1190
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1191
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1192
     * If a "handle action" throws an exception when triggered by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1193
     * a normal completion, it completes exceptionally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1194
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1195
    public void testHandle_sourceCompletedNormallyActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1196
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1197
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1198
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1199
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1200
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1201
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1202
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1203
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1204
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1205
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1206
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1207
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1208
                threadAssertSame(result, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1209
                threadAssertNull(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1210
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1211
                throw ex;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1212
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1213
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1214
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1215
        checkCompletedWithWrappedException(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1216
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1217
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1218
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1219
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1220
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1221
     * If a "handle action" throws an exception when triggered by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1222
     * a source completion that also throws an exception, the action
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1223
     * exception takes precedence (unlike whenComplete)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1224
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1225
    public void testHandle_sourceFailedActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1226
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1227
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1228
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1229
        final AtomicInteger a = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1230
        final CFException ex1 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1231
        final CFException ex2 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1232
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1233
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1234
        if (!createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1235
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1236
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1237
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1238
                m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1239
                threadAssertNull(result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1240
                threadAssertSame(ex1, t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1241
                a.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1242
                throw ex2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1243
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1244
        if (createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1245
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1246
        checkCompletedWithWrappedException(g, ex2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1247
        checkCompletedExceptionally(f, ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1248
        assertEquals(1, a.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1249
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1250
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1251
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1252
     * runAsync completes after running Runnable
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1253
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1254
    public void testRunAsync_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1255
        ExecutionMode[] executionModes = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1256
            ExecutionMode.ASYNC,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1257
            ExecutionMode.EXECUTOR,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1258
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1259
        for (ExecutionMode m : executionModes)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1260
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1261
        final Noop r = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1262
        final CompletableFuture<Void> f = m.runAsync(r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1263
        assertNull(f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1264
        checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1265
        r.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1266
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1267
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1268
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1269
     * failing runAsync completes exceptionally after running Runnable
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1270
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1271
    public void testRunAsync_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1272
        ExecutionMode[] executionModes = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1273
            ExecutionMode.ASYNC,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1274
            ExecutionMode.EXECUTOR,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1275
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1276
        for (ExecutionMode m : executionModes)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1277
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1278
        final FailingRunnable r = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1279
        final CompletableFuture<Void> f = m.runAsync(r);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1280
        checkCompletedWithWrappedException(f, r.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1281
        r.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1282
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1283
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  1284
    @SuppressWarnings("FutureReturnValueIgnored")
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1285
    public void testRunAsync_rejectingExecutor() {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1286
        CountingRejectingExecutor e = new CountingRejectingExecutor();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1287
        try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1288
            CompletableFuture.runAsync(() -> {}, e);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1289
            shouldThrow();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1290
        } catch (Throwable t) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1291
            assertSame(e.ex, t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1292
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1293
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1294
        assertEquals(1, e.count.get());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1295
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1296
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1297
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1298
     * supplyAsync completes with result of supplier
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1299
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1300
    public void testSupplyAsync_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1301
        ExecutionMode[] executionModes = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1302
            ExecutionMode.ASYNC,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1303
            ExecutionMode.EXECUTOR,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1304
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1305
        for (ExecutionMode m : executionModes)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1306
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1307
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1308
        final IntegerSupplier r = new IntegerSupplier(m, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1309
        final CompletableFuture<Integer> f = m.supplyAsync(r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1310
        assertSame(v1, f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1311
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1312
        r.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1313
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1314
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1315
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1316
     * Failing supplyAsync completes exceptionally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1317
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1318
    public void testSupplyAsync_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1319
        ExecutionMode[] executionModes = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1320
            ExecutionMode.ASYNC,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1321
            ExecutionMode.EXECUTOR,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1322
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1323
        for (ExecutionMode m : executionModes)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1324
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1325
        FailingSupplier r = new FailingSupplier(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1326
        CompletableFuture<Integer> f = m.supplyAsync(r);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1327
        checkCompletedWithWrappedException(f, r.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1328
        r.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1329
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1330
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  1331
    @SuppressWarnings("FutureReturnValueIgnored")
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1332
    public void testSupplyAsync_rejectingExecutor() {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1333
        CountingRejectingExecutor e = new CountingRejectingExecutor();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1334
        try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1335
            CompletableFuture.supplyAsync(() -> null, e);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1336
            shouldThrow();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1337
        } catch (Throwable t) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1338
            assertSame(e.ex, t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1339
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1340
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1341
        assertEquals(1, e.count.get());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1342
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1343
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1344
    // seq completion methods
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1345
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1346
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1347
     * thenRun result completes normally after normal completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1348
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1349
    public void testThenRun_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1350
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1351
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1352
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1353
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1354
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1355
        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1356
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1357
        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1358
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1359
        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1360
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1361
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1362
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1363
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1364
        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1365
        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1366
        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1367
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1368
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1369
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1370
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1371
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1372
        checkCompletedNormally(h4, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1373
        checkCompletedNormally(h5, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1374
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1375
        for (Noop r : rs) r.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1376
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1377
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1378
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1379
     * thenRun result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1380
     * completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1381
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1382
    public void testThenRun_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1383
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1384
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1385
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1386
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1387
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1388
        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1389
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1390
        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1391
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1392
        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1393
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1394
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1395
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1396
        assertTrue(f.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1397
        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1398
        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1399
        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1400
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1401
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1402
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1403
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1404
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1405
        checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1406
        checkCompletedWithWrappedException(h5, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1407
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1408
        for (Noop r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1409
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1410
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1411
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1412
     * thenRun result completes exceptionally if source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1413
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1414
    public void testThenRun_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1415
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1416
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1417
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1418
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1419
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1420
        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1421
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1422
        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1423
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1424
        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1425
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1426
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1427
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1428
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1429
        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1430
        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1431
        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1432
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1433
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1434
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1435
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1436
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1437
        checkCompletedWithWrappedCancellationException(h4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1438
        checkCompletedWithWrappedCancellationException(h5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1439
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1440
        for (Noop r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1441
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1442
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1443
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1444
     * thenRun result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1445
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1446
    public void testThenRun_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1447
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1448
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1449
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1450
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1451
        final FailingRunnable[] rs = new FailingRunnable[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1452
        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1453
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1454
        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1455
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1456
        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1457
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1458
        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1459
        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1460
        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1461
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1462
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1463
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1464
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1465
        checkCompletedWithWrappedException(h3, rs[3].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1466
        checkCompletedWithWrappedException(h4, rs[4].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1467
        checkCompletedWithWrappedException(h5, rs[5].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1468
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1469
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1470
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1471
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1472
     * thenApply result completes normally after normal completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1473
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1474
    public void testThenApply_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1475
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1476
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1477
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1478
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1479
        final IncFunction[] rs = new IncFunction[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1480
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1481
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1482
        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1483
        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1484
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1485
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1486
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1487
        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1488
        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1489
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1490
        checkCompletedNormally(h0, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1491
        checkCompletedNormally(h1, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1492
        checkCompletedNormally(h2, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1493
        checkCompletedNormally(h3, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1494
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1495
        for (IncFunction r : rs) r.assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1496
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1497
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1498
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1499
     * thenApply result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1500
     * completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1501
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1502
    public void testThenApply_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1503
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1504
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1505
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1506
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1507
        final IncFunction[] rs = new IncFunction[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1508
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1509
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1510
        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1511
        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1512
        assertTrue(f.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1513
        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1514
        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1515
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1516
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1517
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1518
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1519
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1520
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1521
        for (IncFunction r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1522
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1523
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1524
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1525
     * thenApply result completes exceptionally if source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1526
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1527
    public void testThenApply_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1528
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1529
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1530
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1531
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1532
        final IncFunction[] rs = new IncFunction[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1533
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1534
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1535
        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1536
        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1537
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1538
        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1539
        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1540
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1541
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1542
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1543
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1544
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1545
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1546
        for (IncFunction r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1547
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1548
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1549
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1550
     * thenApply result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1551
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1552
    public void testThenApply_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1553
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1554
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1555
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1556
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1557
        final FailingFunction[] rs = new FailingFunction[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1558
        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1559
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1560
        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1561
        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1562
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1563
        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1564
        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1565
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1566
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1567
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1568
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1569
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1570
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1571
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1572
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1573
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1574
     * thenAccept result completes normally after normal completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1575
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1576
    public void testThenAccept_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1577
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1578
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1579
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1580
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1581
        final NoopConsumer[] rs = new NoopConsumer[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1582
        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1583
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1584
        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1585
        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1586
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1587
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1588
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1589
        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1590
        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1591
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1592
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1593
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1594
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1595
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1596
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1597
        for (NoopConsumer r : rs) r.assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1598
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1599
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1600
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1601
     * thenAccept result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1602
     * completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1603
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1604
    public void testThenAccept_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1605
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1606
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1607
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1608
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1609
        final NoopConsumer[] rs = new NoopConsumer[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1610
        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1611
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1612
        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1613
        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1614
        assertTrue(f.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1615
        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1616
        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1617
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1618
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1619
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1620
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1621
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1622
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1623
        for (NoopConsumer r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1624
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1625
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1626
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1627
     * thenAccept result completes exceptionally if source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1628
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1629
    public void testThenAccept_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1630
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1631
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1632
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1633
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1634
        final NoopConsumer[] rs = new NoopConsumer[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1635
        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1636
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1637
        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1638
        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1639
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1640
        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1641
        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1642
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1643
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1644
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1645
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1646
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1647
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1648
        for (NoopConsumer r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1649
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1650
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1651
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1652
     * thenAccept result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1653
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1654
    public void testThenAccept_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1655
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1656
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1657
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1658
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1659
        final FailingConsumer[] rs = new FailingConsumer[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1660
        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1661
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1662
        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1663
        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1664
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1665
        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1666
        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1667
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1668
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1669
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1670
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1671
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1672
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1673
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1674
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1675
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1676
     * thenCombine result completes normally after normal completion
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1677
     * of sources
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1678
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1679
    public void testThenCombine_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1680
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1681
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1682
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1683
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1684
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1685
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1686
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1687
        final SubtractFunction[] rs = new SubtractFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1688
        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1689
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1690
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1691
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1692
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1693
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1694
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1695
        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1696
        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1697
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1698
        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1699
        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1700
        checkIncomplete(h0); rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1701
        checkIncomplete(h2); rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1702
        checkCompletedNormally(h1, subtract(w1, w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1703
        checkCompletedNormally(h3, subtract(w1, w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1704
        rs[1].assertValue(subtract(w1, w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1705
        rs[3].assertValue(subtract(w1, w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1706
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1707
        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1708
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1709
        checkCompletedNormally(h0, subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1710
        checkCompletedNormally(h2, subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1711
        checkCompletedNormally(h4, subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1712
        rs[0].assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1713
        rs[2].assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1714
        rs[4].assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1715
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1716
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1717
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1718
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1719
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1720
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1721
     * thenCombine result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1722
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1723
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1724
    public void testThenCombine_exceptionalCompletion() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1725
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1726
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1727
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1728
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1729
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1730
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1731
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1732
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1733
        final SubtractFunction r1 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1734
        final SubtractFunction r2 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1735
        final SubtractFunction r3 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1736
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1737
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1738
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1739
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1740
            () -> fst.completeExceptionally(ex) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1741
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1742
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1743
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1744
            () -> snd.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1745
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1746
        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1747
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1748
        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1749
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1750
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1751
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1752
        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1753
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1754
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1755
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1756
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1757
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1758
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1759
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1760
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1761
        checkCompletedExceptionally(failFirst ? fst : snd, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1762
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1763
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1764
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1765
     * thenCombine result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1766
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1767
    public void testThenCombine_sourceCancelled() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1768
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1769
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1770
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1771
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1772
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1773
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1774
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1775
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1776
        final SubtractFunction r1 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1777
        final SubtractFunction r2 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1778
        final SubtractFunction r3 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1779
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1780
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1781
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1782
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1783
            () -> fst.cancel(mayInterruptIfRunning) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1784
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1785
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1786
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1787
            () -> snd.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1788
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1789
        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1790
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1791
        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1792
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1793
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1794
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1795
        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1796
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1797
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1798
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1799
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1800
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1801
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1802
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1803
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1804
        checkCancelled(failFirst ? fst : snd);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1805
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1806
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1807
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1808
     * thenCombine result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1809
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1810
    public void testThenCombine_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1811
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1812
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1813
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1814
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1815
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1816
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1817
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1818
        final FailingBiFunction r1 = new FailingBiFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1819
        final FailingBiFunction r2 = new FailingBiFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1820
        final FailingBiFunction r3 = new FailingBiFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1821
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1822
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1823
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1824
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1825
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1826
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1827
        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1828
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1829
        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1830
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1831
        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1832
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1833
        checkCompletedWithWrappedException(h1, r1.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1834
        checkCompletedWithWrappedException(h2, r2.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1835
        checkCompletedWithWrappedException(h3, r3.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1836
        r1.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1837
        r2.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1838
        r3.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1839
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1840
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1841
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1842
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1843
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1844
     * thenAcceptBoth result completes normally after normal
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1845
     * completion of sources
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1846
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1847
    public void testThenAcceptBoth_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1848
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1849
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1850
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1851
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1852
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1853
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1854
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1855
        final SubtractAction r1 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1856
        final SubtractAction r2 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1857
        final SubtractAction r3 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1858
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1859
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1860
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1861
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1862
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1863
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1864
        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1865
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1866
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1867
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1868
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1869
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1870
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1871
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1872
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1873
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1874
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1875
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1876
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1877
        r1.assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1878
        r2.assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1879
        r3.assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1880
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1881
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1882
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1883
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1884
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1885
     * thenAcceptBoth result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1886
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1887
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1888
    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1889
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1890
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1891
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1892
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1893
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1894
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1895
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1896
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1897
        final SubtractAction r1 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1898
        final SubtractAction r2 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1899
        final SubtractAction r3 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1900
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1901
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1902
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1903
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1904
            () -> fst.completeExceptionally(ex) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1905
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1906
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1907
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1908
            () -> snd.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1909
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1910
        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1911
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1912
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1913
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1914
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1915
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1916
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1917
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1918
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1919
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1920
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1921
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1922
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1923
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1924
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1925
        checkCompletedExceptionally(failFirst ? fst : snd, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1926
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1927
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1928
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1929
     * thenAcceptBoth result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1930
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1931
    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1932
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1933
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1934
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1935
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1936
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1937
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1938
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1939
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1940
        final SubtractAction r1 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1941
        final SubtractAction r2 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1942
        final SubtractAction r3 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1943
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1944
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1945
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1946
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1947
            () -> fst.cancel(mayInterruptIfRunning) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1948
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1949
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1950
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1951
            () -> snd.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1952
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1953
        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1954
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1955
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1956
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1957
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1958
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1959
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1960
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1961
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1962
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1963
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1964
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1965
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1966
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1967
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1968
        checkCancelled(failFirst ? fst : snd);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1969
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1970
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1971
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1972
     * thenAcceptBoth result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1973
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1974
    public void testThenAcceptBoth_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1975
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1976
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1977
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1978
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1979
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1980
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1981
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1982
        final FailingBiConsumer r1 = new FailingBiConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1983
        final FailingBiConsumer r2 = new FailingBiConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1984
        final FailingBiConsumer r3 = new FailingBiConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1985
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1986
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1987
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1988
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1989
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1990
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1991
        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1992
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1993
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1994
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1995
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1996
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1997
        checkCompletedWithWrappedException(h1, r1.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1998
        checkCompletedWithWrappedException(h2, r2.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1999
        checkCompletedWithWrappedException(h3, r3.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2000
        r1.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2001
        r2.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2002
        r3.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2003
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2004
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2005
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2006
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2007
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2008
     * runAfterBoth result completes normally after normal
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2009
     * completion of sources
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2010
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2011
    public void testRunAfterBoth_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2012
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2013
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2014
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2015
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2016
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2017
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2018
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2019
        final Noop r1 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2020
        final Noop r2 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2021
        final Noop r3 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2022
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2023
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2024
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2025
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2026
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2027
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2028
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2029
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2030
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2031
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2032
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2033
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2034
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2035
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2036
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2037
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2038
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2039
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2040
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2041
        r1.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2042
        r2.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2043
        r3.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2044
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2045
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2046
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2047
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2048
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2049
     * runAfterBoth result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2050
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2051
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2052
    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2053
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2054
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2055
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2056
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2057
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2058
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2059
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2060
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2061
        final Noop r1 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2062
        final Noop r2 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2063
        final Noop r3 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2064
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2065
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2066
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2067
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2068
            () -> fst.completeExceptionally(ex) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2069
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2070
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2071
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2072
            () -> snd.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2073
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2074
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2075
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2076
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2077
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2078
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2079
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2080
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2081
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2082
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2083
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2084
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2085
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2086
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2087
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2088
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2089
        checkCompletedExceptionally(failFirst ? fst : snd, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2090
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2091
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2092
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2093
     * runAfterBoth result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2094
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2095
    public void testRunAfterBoth_sourceCancelled() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2096
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2097
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2098
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2099
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2100
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2101
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2102
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2103
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2104
        final Noop r1 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2105
        final Noop r2 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2106
        final Noop r3 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2107
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2108
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2109
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2110
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2111
            () -> fst.cancel(mayInterruptIfRunning) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2112
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2113
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2114
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2115
            () -> snd.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2116
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2117
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2118
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2119
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2120
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2121
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2122
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2123
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2124
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2125
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2126
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2127
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2128
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2129
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2130
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2131
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2132
        checkCancelled(failFirst ? fst : snd);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2133
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2134
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2135
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2136
     * runAfterBoth result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2137
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2138
    public void testRunAfterBoth_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2139
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2140
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2141
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2142
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2143
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2144
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2145
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2146
        final FailingRunnable r1 = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2147
        final FailingRunnable r2 = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2148
        final FailingRunnable r3 = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2149
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2150
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2151
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2152
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2153
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2154
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2155
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2156
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2157
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2158
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2159
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2160
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2161
        checkCompletedWithWrappedException(h1, r1.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2162
        checkCompletedWithWrappedException(h2, r2.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2163
        checkCompletedWithWrappedException(h3, r3.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2164
        r1.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2165
        r2.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2166
        r3.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2167
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2168
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2169
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2170
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2171
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2172
     * applyToEither result completes normally after normal completion
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2173
     * of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2174
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2175
    public void testApplyToEither_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2176
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2177
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2178
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2179
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2180
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2181
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2182
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2183
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2184
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2185
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2186
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2187
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2188
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2189
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2190
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2191
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2192
        checkCompletedNormally(h0, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2193
        checkCompletedNormally(h1, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2194
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2195
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2196
        checkCompletedNormally(h2, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2197
        checkCompletedNormally(h3, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2198
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2199
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2200
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2201
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2202
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2203
        rs[4].assertValue(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2204
        rs[5].assertValue(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2205
        assertTrue(Objects.equals(inc(v1), h4.join()) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2206
                   Objects.equals(inc(v2), h4.join()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2207
        assertTrue(Objects.equals(inc(v1), h5.join()) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2208
                   Objects.equals(inc(v2), h5.join()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2209
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2210
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2211
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2212
        checkCompletedNormally(h0, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2213
        checkCompletedNormally(h1, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2214
        checkCompletedNormally(h2, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2215
        checkCompletedNormally(h3, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2216
        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2217
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2218
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2219
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2220
     * applyToEither result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2221
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2222
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2223
    public void testApplyToEither_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2224
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2225
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2226
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2227
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2228
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2229
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2230
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2231
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2232
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2233
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2234
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2235
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2236
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2237
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2238
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2239
        f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2240
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2241
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2242
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2243
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2244
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2245
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2246
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2247
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2248
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2249
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2250
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2251
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2252
            assertEquals(inc(v1), h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2253
            rs[4].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2254
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2255
            checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2256
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2257
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2258
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2259
            assertEquals(inc(v1), h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2260
            rs[5].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2261
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2262
            checkCompletedWithWrappedException(h5, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2263
            rs[5].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2264
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2265
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2266
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2267
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2268
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2269
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2270
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2271
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2272
        checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2273
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2274
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2275
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2276
    public void testApplyToEither_exceptionalCompletion2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2277
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2278
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2279
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2280
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2281
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2282
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2283
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2284
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2285
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2286
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2287
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2288
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2289
        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2290
        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2291
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2292
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2293
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2294
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2295
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2296
            assertEquals(inc(v1), h0.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2297
            rs[0].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2298
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2299
            checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2300
            rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2301
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2302
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2303
            assertEquals(inc(v1), h1.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2304
            rs[1].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2305
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2306
            checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2307
            rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2308
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2309
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2310
            assertEquals(inc(v1), h2.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2311
            rs[2].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2312
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2313
            checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2314
            rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2315
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2316
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2317
            assertEquals(inc(v1), h3.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2318
            rs[3].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2319
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2320
            checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2321
            rs[3].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2322
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2323
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2324
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2325
        checkCompletedExceptionally(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2326
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2327
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2328
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2329
     * applyToEither result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2330
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2331
    public void testApplyToEither_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2332
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2333
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2334
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2335
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2336
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2337
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2338
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2339
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2340
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2341
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2342
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2343
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2344
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2345
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2346
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2347
        f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2348
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2349
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2350
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2351
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2352
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2353
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2354
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2355
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2356
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2357
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2358
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2359
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2360
            assertEquals(inc(v1), h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2361
            rs[4].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2362
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2363
            checkCompletedWithWrappedCancellationException(h4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2364
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2365
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2366
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2367
            assertEquals(inc(v1), h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2368
            rs[5].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2369
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2370
            checkCompletedWithWrappedCancellationException(h5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2371
            rs[5].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2372
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2373
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2374
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2375
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2376
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2377
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2378
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2379
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2380
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2381
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2382
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2383
    public void testApplyToEither_sourceCancelled2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2384
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2385
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2386
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2387
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2388
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2389
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2390
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2391
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2392
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2393
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2394
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2395
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2396
        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2397
        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2398
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2399
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2400
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2401
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2402
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2403
            assertEquals(inc(v1), h0.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2404
            rs[0].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2405
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2406
            checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2407
            rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2408
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2409
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2410
            assertEquals(inc(v1), h1.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2411
            rs[1].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2412
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2413
            checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2414
            rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2415
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2416
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2417
            assertEquals(inc(v1), h2.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2418
            rs[2].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2419
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2420
            checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2421
            rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2422
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2423
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2424
            assertEquals(inc(v1), h3.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2425
            rs[3].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2426
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2427
            checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2428
            rs[3].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2429
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2430
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2431
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2432
        checkCancelled(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2433
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2434
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2435
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2436
     * applyToEither result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2437
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2438
    public void testApplyToEither_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2439
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2440
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2441
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2442
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2443
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2444
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2445
        final FailingFunction[] rs = new FailingFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2446
        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2447
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2448
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2449
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2450
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2451
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2452
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2453
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2454
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2455
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2456
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2457
        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2458
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2459
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2460
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2461
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2462
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2463
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2464
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2465
        checkCompletedWithWrappedException(h4, rs[4].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2466
        assertTrue(Objects.equals(v1, rs[4].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2467
                   Objects.equals(v2, rs[4].value));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2468
        checkCompletedWithWrappedException(h5, rs[5].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2469
        assertTrue(Objects.equals(v1, rs[5].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2470
                   Objects.equals(v2, rs[5].value));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2471
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2472
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2473
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2474
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2475
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2476
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2477
     * acceptEither result completes normally after normal completion
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2478
     * of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2479
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2480
    public void testAcceptEither_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2481
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2482
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2483
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2484
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2485
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2486
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2487
        final NoopConsumer[] rs = new NoopConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2488
        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2489
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2490
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2491
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2492
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2493
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2494
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2495
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2496
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2497
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2498
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2499
        rs[0].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2500
        rs[1].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2501
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2502
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2503
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2504
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2505
        rs[2].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2506
        rs[3].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2507
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2508
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2509
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2510
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2511
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2512
        checkCompletedNormally(h4, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2513
        checkCompletedNormally(h5, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2514
        assertTrue(Objects.equals(v1, rs[4].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2515
                   Objects.equals(v2, rs[4].value));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2516
        assertTrue(Objects.equals(v1, rs[5].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2517
                   Objects.equals(v2, rs[5].value));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2518
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2519
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2520
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2521
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2522
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2523
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2524
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2525
        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2526
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2527
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2528
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2529
     * acceptEither result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2530
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2531
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2532
    public void testAcceptEither_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2533
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2534
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2535
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2536
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2537
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2538
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2539
        final NoopConsumer[] rs = new NoopConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2540
        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2541
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2542
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2543
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2544
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2545
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2546
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2547
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2548
        f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2549
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2550
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2551
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2552
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2553
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2554
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2555
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2556
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2557
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2558
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2559
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2560
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2561
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2562
            assertNull(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2563
            rs[4].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2564
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2565
            checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2566
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2567
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2568
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2569
            assertNull(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2570
            rs[5].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2571
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2572
            checkCompletedWithWrappedException(h5, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2573
            rs[5].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2574
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2575
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2576
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2577
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2578
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2579
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2580
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2581
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2582
        checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2583
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2584
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2585
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2586
    public void testAcceptEither_exceptionalCompletion2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2587
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2588
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2589
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2590
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2591
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2592
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2593
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2594
        final NoopConsumer[] rs = new NoopConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2595
        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2596
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2597
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2598
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2599
        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2600
        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2601
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2602
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2603
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2604
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2605
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2606
            assertNull(h0.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2607
            rs[0].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2608
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2609
            checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2610
            rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2611
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2612
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2613
            assertNull(h1.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2614
            rs[1].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2615
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2616
            checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2617
            rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2618
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2619
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2620
            assertNull(h2.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2621
            rs[2].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2622
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2623
            checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2624
            rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2625
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2626
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2627
            assertNull(h3.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2628
            rs[3].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2629
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2630
            checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2631
            rs[3].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2632
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2633
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2634
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2635
        checkCompletedExceptionally(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2636
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2637
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2638
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2639
     * acceptEither result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2640
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2641
    public void testAcceptEither_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2642
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2643
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2644
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2645
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2646
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2647
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2648
        final NoopConsumer[] rs = new NoopConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2649
        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2650
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2651
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2652
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2653
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2654
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2655
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2656
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2657
        f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2658
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2659
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2660
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2661
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2662
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2663
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2664
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2665
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2666
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2667
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2668
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2669
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2670
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2671
            assertNull(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2672
            rs[4].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2673
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2674
            checkCompletedWithWrappedCancellationException(h4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2675
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2676
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2677
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2678
            assertNull(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2679
            rs[5].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2680
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2681
            checkCompletedWithWrappedCancellationException(h5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2682
            rs[5].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2683
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2684
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2685
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2686
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2687
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2688
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2689
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2690
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2691
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2692
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2693
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2694
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2695
     * acceptEither result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2696
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2697
    public void testAcceptEither_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2698
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2699
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2700
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2701
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2702
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2703
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2704
        final FailingConsumer[] rs = new FailingConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2705
        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2706
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2707
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2708
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2709
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2710
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2711
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2712
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2713
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2714
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2715
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2716
        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2717
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2718
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2719
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2720
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2721
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2722
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2723
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2724
        checkCompletedWithWrappedException(h4, rs[4].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2725
        assertTrue(Objects.equals(v1, rs[4].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2726
                   Objects.equals(v2, rs[4].value));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2727
        checkCompletedWithWrappedException(h5, rs[5].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2728
        assertTrue(Objects.equals(v1, rs[5].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2729
                   Objects.equals(v2, rs[5].value));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2730
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2731
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2732
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2733
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2734
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2735
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2736
     * runAfterEither result completes normally after normal completion
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2737
     * of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2738
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2739
    public void testRunAfterEither_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2740
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2741
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2742
        for (Integer v2 : new Integer[] { 2, null })
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2743
        for (boolean pushNop : new boolean[] { true, false })
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2744
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2745
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2746
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2747
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2748
        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2749
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2750
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2751
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2752
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2753
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2754
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2755
        rs[1].assertNotInvoked();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2756
        if (pushNop) {          // ad hoc test of intra-completion interference
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2757
            m.thenRun(f, () -> {});
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2758
            m.thenRun(g, () -> {});
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2759
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2760
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2761
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2762
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2763
        rs[0].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2764
        rs[1].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2765
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2766
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2767
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2768
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2769
        rs[2].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2770
        rs[3].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2771
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2772
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2773
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2774
        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2775
        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2776
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2777
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2778
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2779
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2780
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2781
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2782
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2783
        checkCompletedNormally(h4, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2784
        checkCompletedNormally(h5, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2785
        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2786
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2787
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2788
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2789
     * runAfterEither result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2790
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2791
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2792
    public void testRunAfterEither_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2793
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2794
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2795
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2796
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2797
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2798
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2799
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2800
        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2801
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2802
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2803
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2804
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2805
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2806
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2807
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2808
        assertTrue(f.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2809
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2810
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2811
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2812
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2813
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2814
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2815
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2816
        assertTrue(g.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2817
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2818
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2819
        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2820
        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2821
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2822
            assertNull(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2823
            rs[4].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2824
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2825
            checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2826
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2827
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2828
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2829
            assertNull(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2830
            rs[5].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2831
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2832
            checkCompletedWithWrappedException(h5, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2833
            rs[5].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2834
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2835
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2836
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2837
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2838
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2839
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2840
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2841
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2842
        checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2843
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2844
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2845
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2846
    public void testRunAfterEither_exceptionalCompletion2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2847
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2848
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2849
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2850
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2851
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2852
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2853
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2854
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2855
        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2856
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2857
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2858
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2859
        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2860
        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2861
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2862
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2863
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2864
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2865
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2866
            assertNull(h0.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2867
            rs[0].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2868
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2869
            checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2870
            rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2871
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2872
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2873
            assertNull(h1.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2874
            rs[1].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2875
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2876
            checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2877
            rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2878
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2879
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2880
            assertNull(h2.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2881
            rs[2].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2882
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2883
            checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2884
            rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2885
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2886
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2887
            assertNull(h3.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2888
            rs[3].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2889
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2890
            checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2891
            rs[3].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2892
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2893
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2894
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2895
        checkCompletedExceptionally(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2896
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2897
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2898
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2899
     * runAfterEither result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2900
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2901
    public void testRunAfterEither_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2902
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2903
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2904
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2905
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2906
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2907
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2908
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2909
        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2910
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2911
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2912
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2913
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2914
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2915
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2916
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2917
        f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2918
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2919
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2920
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2921
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2922
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2923
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2924
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2925
        assertTrue(g.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2926
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2927
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2928
        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2929
        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2930
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2931
            assertNull(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2932
            rs[4].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2933
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2934
            checkCompletedWithWrappedCancellationException(h4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2935
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2936
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2937
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2938
            assertNull(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2939
            rs[5].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2940
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2941
            checkCompletedWithWrappedCancellationException(h5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2942
            rs[5].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2943
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2944
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2945
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2946
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2947
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2948
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2949
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2950
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2951
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2952
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2953
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2954
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2955
     * runAfterEither result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2956
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2957
    public void testRunAfterEither_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2958
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2959
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2960
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2961
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2962
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2963
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2964
        final FailingRunnable[] rs = new FailingRunnable[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2965
        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2966
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2967
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2968
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2969
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2970
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2971
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2972
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2973
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2974
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2975
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2976
        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2977
        assertTrue(g.complete(v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2978
        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2979
        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2980
        checkCompletedWithWrappedException(h4, rs[4].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2981
        checkCompletedWithWrappedException(h5, rs[5].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2982
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2983
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2984
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2985
        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2986
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2987
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2988
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2989
     * thenCompose result completes normally after normal completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2990
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2991
    public void testThenCompose_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2992
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2993
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2994
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2995
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2996
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2997
        final CompletableFutureInc r = new CompletableFutureInc(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2998
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2999
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3000
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3001
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3002
        checkCompletedNormally(g, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3003
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3004
        r.assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3005
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3006
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3007
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3008
     * thenCompose result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3009
     * completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3010
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3011
    public void testThenCompose_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3012
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3013
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3014
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3015
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3016
        final CompletableFutureInc r = new CompletableFutureInc(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3017
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3018
        if (!createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3019
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3020
        if (createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3021
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3022
        checkCompletedWithWrappedException(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3023
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3024
        r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3025
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3026
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3027
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3028
     * thenCompose result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3029
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3030
    public void testThenCompose_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3031
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3032
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3033
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3034
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3035
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3036
        final FailingCompletableFutureFunction r
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3037
            = new FailingCompletableFutureFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3038
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3039
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3040
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3041
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3042
        checkCompletedWithWrappedException(g, r.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3043
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3044
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3045
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3046
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3047
     * thenCompose result completes exceptionally if source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3048
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3049
    public void testThenCompose_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3050
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3051
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3052
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3053
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3054
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3055
        final CompletableFutureInc r = new CompletableFutureInc(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3056
        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3057
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3058
        if (createIncomplete) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3059
            checkIncomplete(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3060
            assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3061
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3062
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3063
        checkCompletedWithWrappedCancellationException(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3064
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3065
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3066
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3067
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3068
     * thenCompose result completes exceptionally if the result of the action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3069
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3070
    public void testThenCompose_actionReturnsFailingFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3071
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3072
        for (int order = 0; order < 6; order++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3073
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3074
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3075
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3076
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3077
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3078
        final CompletableFuture<Integer> h;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3079
        // Test all permutations of orders
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3080
        switch (order) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3081
        case 0:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3082
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3083
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3084
            h = m.thenCompose(f, (x -> g));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3085
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3086
        case 1:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3087
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3088
            h = m.thenCompose(f, (x -> g));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3089
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3090
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3091
        case 2:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3092
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3093
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3094
            h = m.thenCompose(f, (x -> g));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3095
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3096
        case 3:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3097
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3098
            h = m.thenCompose(f, (x -> g));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3099
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3100
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3101
        case 4:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3102
            h = m.thenCompose(f, (x -> g));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3103
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3104
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3105
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3106
        case 5:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3107
            h = m.thenCompose(f, (x -> g));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3108
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3109
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3110
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3111
        default: throw new AssertionError();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3112
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3113
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3114
        checkCompletedExceptionally(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3115
        checkCompletedWithWrappedException(h, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3116
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3117
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3118
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3119
    // other static methods
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3120
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3121
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3122
     * allOf(no component futures) returns a future completed normally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3123
     * with the value null
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3124
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3125
    public void testAllOf_empty() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3126
        CompletableFuture<Void> f = CompletableFuture.allOf();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3127
        checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3128
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3129
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3130
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3131
     * allOf returns a future completed normally with the value null
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3132
     * when all components complete normally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3133
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3134
    public void testAllOf_normal() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3135
        for (int k = 1; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3136
            CompletableFuture<Integer>[] fs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3137
                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3138
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3139
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3140
            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3141
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3142
                checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3143
                checkIncomplete(CompletableFuture.allOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3144
                fs[i].complete(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3145
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3146
            checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3147
            checkCompletedNormally(CompletableFuture.allOf(fs), null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3148
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3149
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3150
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3151
    public void testAllOf_normal_backwards() throws Exception {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3152
        for (int k = 1; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3153
            CompletableFuture<Integer>[] fs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3154
                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3155
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3156
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3157
            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3158
            for (int i = k - 1; i >= 0; i--) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3159
                checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3160
                checkIncomplete(CompletableFuture.allOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3161
                fs[i].complete(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3162
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3163
            checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3164
            checkCompletedNormally(CompletableFuture.allOf(fs), null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3165
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3166
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3167
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3168
    public void testAllOf_exceptional() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3169
        for (int k = 1; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3170
            CompletableFuture<Integer>[] fs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3171
                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3172
            CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3173
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3174
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3175
            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3176
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3177
                checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3178
                checkIncomplete(CompletableFuture.allOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3179
                if (i != k / 2) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3180
                    fs[i].complete(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3181
                    checkCompletedNormally(fs[i], i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3182
                } else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3183
                    fs[i].completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3184
                    checkCompletedExceptionally(fs[i], ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3185
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3186
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3187
            checkCompletedWithWrappedException(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3188
            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3189
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3190
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3191
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3192
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3193
     * anyOf(no component futures) returns an incomplete future
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3194
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3195
    public void testAnyOf_empty() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3196
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3197
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3198
        CompletableFuture<Object> f = CompletableFuture.anyOf();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3199
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3200
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3201
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3202
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3203
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3204
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3205
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3206
     * anyOf returns a future completed normally with a value when
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3207
     * a component future does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3208
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3209
    public void testAnyOf_normal() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3210
        for (int k = 0; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3211
            CompletableFuture[] fs = new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3212
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3213
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3214
            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3215
            checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3216
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3217
                fs[i].complete(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3218
                checkCompletedNormally(f, 0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3219
                int x = (int) CompletableFuture.anyOf(fs).join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3220
                assertTrue(0 <= x && x <= i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3221
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3222
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3223
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3224
    public void testAnyOf_normal_backwards() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3225
        for (int k = 0; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3226
            CompletableFuture[] fs = new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3227
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3228
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3229
            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3230
            checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3231
            for (int i = k - 1; i >= 0; i--) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3232
                fs[i].complete(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3233
                checkCompletedNormally(f, k - 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3234
                int x = (int) CompletableFuture.anyOf(fs).join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3235
                assertTrue(i <= x && x <= k - 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3236
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3237
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3238
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3239
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3240
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3241
     * anyOf result completes exceptionally when any component does.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3242
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3243
    public void testAnyOf_exceptional() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3244
        for (int k = 0; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3245
            CompletableFuture[] fs = new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3246
            CFException[] exs = new CFException[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3247
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3248
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3249
                exs[i] = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3250
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3251
            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3252
            checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3253
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3254
                fs[i].completeExceptionally(exs[i]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3255
                checkCompletedWithWrappedException(f, exs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3256
                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3257
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3258
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3259
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3260
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3261
    public void testAnyOf_exceptional_backwards() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3262
        for (int k = 0; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3263
            CompletableFuture[] fs = new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3264
            CFException[] exs = new CFException[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3265
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3266
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3267
                exs[i] = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3268
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3269
            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3270
            checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3271
            for (int i = k - 1; i >= 0; i--) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3272
                fs[i].completeExceptionally(exs[i]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3273
                checkCompletedWithWrappedException(f, exs[k - 1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3274
                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3275
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3276
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3277
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3278
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3279
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3280
     * Completion methods throw NullPointerException with null arguments
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3281
     */
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  3282
    @SuppressWarnings("FutureReturnValueIgnored")
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3283
    public void testNPE() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3284
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3285
        CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3286
        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3287
        ThreadExecutor exec = new ThreadExecutor();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3288
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3289
        Runnable[] throwingActions = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3290
            () -> CompletableFuture.supplyAsync(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3291
            () -> CompletableFuture.supplyAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3292
            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3293
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3294
            () -> CompletableFuture.runAsync(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3295
            () -> CompletableFuture.runAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3296
            () -> CompletableFuture.runAsync(() -> {}, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3297
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3298
            () -> f.completeExceptionally(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3299
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3300
            () -> f.thenApply(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3301
            () -> f.thenApplyAsync(null),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3302
            () -> f.thenApplyAsync(x -> x, null),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3303
            () -> f.thenApplyAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3304
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3305
            () -> f.thenAccept(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3306
            () -> f.thenAcceptAsync(null),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3307
            () -> f.thenAcceptAsync(x -> {} , null),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3308
            () -> f.thenAcceptAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3309
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3310
            () -> f.thenRun(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3311
            () -> f.thenRunAsync(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3312
            () -> f.thenRunAsync(() -> {} , null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3313
            () -> f.thenRunAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3314
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3315
            () -> f.thenCombine(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3316
            () -> f.thenCombineAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3317
            () -> f.thenCombineAsync(g, null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3318
            () -> f.thenCombine(nullFuture, (x, y) -> x),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3319
            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3320
            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3321
            () -> f.thenCombineAsync(g, (x, y) -> x, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3322
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3323
            () -> f.thenAcceptBoth(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3324
            () -> f.thenAcceptBothAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3325
            () -> f.thenAcceptBothAsync(g, null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3326
            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3327
            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3328
            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3329
            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3330
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3331
            () -> f.runAfterBoth(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3332
            () -> f.runAfterBothAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3333
            () -> f.runAfterBothAsync(g, null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3334
            () -> f.runAfterBoth(nullFuture, () -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3335
            () -> f.runAfterBothAsync(nullFuture, () -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3336
            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3337
            () -> f.runAfterBothAsync(g, () -> {}, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3338
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3339
            () -> f.applyToEither(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3340
            () -> f.applyToEitherAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3341
            () -> f.applyToEitherAsync(g, null, exec),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3342
            () -> f.applyToEither(nullFuture, x -> x),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3343
            () -> f.applyToEitherAsync(nullFuture, x -> x),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3344
            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3345
            () -> f.applyToEitherAsync(g, x -> x, null),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3346
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3347
            () -> f.acceptEither(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3348
            () -> f.acceptEitherAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3349
            () -> f.acceptEitherAsync(g, null, exec),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3350
            () -> f.acceptEither(nullFuture, x -> {}),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3351
            () -> f.acceptEitherAsync(nullFuture, x -> {}),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3352
            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3353
            () -> f.acceptEitherAsync(g, x -> {}, null),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3354
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3355
            () -> f.runAfterEither(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3356
            () -> f.runAfterEitherAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3357
            () -> f.runAfterEitherAsync(g, null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3358
            () -> f.runAfterEither(nullFuture, () -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3359
            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3360
            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3361
            () -> f.runAfterEitherAsync(g, () -> {}, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3362
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3363
            () -> f.thenCompose(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3364
            () -> f.thenComposeAsync(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3365
            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3366
            () -> f.thenComposeAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3367
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3368
            () -> f.exceptionally(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3369
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3370
            () -> f.handle(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3371
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3372
            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3373
            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3374
            () -> CompletableFuture.allOf(f, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3375
            () -> CompletableFuture.allOf(null, f),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3376
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3377
            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3378
            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3379
            () -> CompletableFuture.anyOf(f, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3380
            () -> CompletableFuture.anyOf(null, f),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3381
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3382
            () -> f.obtrudeException(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3383
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3384
            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3385
            () -> CompletableFuture.delayedExecutor(1L, null, exec),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3386
            () -> CompletableFuture.delayedExecutor(1L, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3387
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3388
            () -> f.orTimeout(1L, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3389
            () -> f.completeOnTimeout(42, 1L, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3390
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3391
            () -> CompletableFuture.failedFuture(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3392
            () -> CompletableFuture.failedStage(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3393
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3395
        assertThrows(NullPointerException.class, throwingActions);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3396
        assertEquals(0, exec.count.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3397
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3398
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3399
    /**
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3400
     * Test submissions to an executor that rejects all tasks.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3401
     */
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3402
    public void testRejectingExecutor() {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3403
        for (Integer v : new Integer[] { 1, null })
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3404
    {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3405
        final CountingRejectingExecutor e = new CountingRejectingExecutor();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3406
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3407
        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3408
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3409
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3410
        List<CompletableFuture<?>> futures = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3411
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3412
        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3413
        srcs.add(complete);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3414
        srcs.add(incomplete);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3415
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3416
        for (CompletableFuture<Integer> src : srcs) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3417
            List<CompletableFuture<?>> fs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3418
            fs.add(src.thenRunAsync(() -> {}, e));
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3419
            fs.add(src.thenAcceptAsync(z -> {}, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3420
            fs.add(src.thenApplyAsync(z -> z, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3421
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3422
            fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3423
            fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3424
            fs.add(src.runAfterBothAsync(src, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3425
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3426
            fs.add(src.applyToEitherAsync(src, z -> z, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3427
            fs.add(src.acceptEitherAsync(src, z -> {}, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3428
            fs.add(src.runAfterEitherAsync(src, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3429
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3430
            fs.add(src.thenComposeAsync(z -> null, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3431
            fs.add(src.whenCompleteAsync((z, t) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3432
            fs.add(src.handleAsync((z, t) -> null, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3433
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3434
            for (CompletableFuture<?> future : fs) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3435
                if (src.isDone())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3436
                    checkCompletedWithWrappedException(future, e.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3437
                else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3438
                    checkIncomplete(future);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3439
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3440
            futures.addAll(fs);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3441
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3442
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3443
        {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3444
            List<CompletableFuture<?>> fs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3445
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3446
            fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3447
            fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3448
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3449
            fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3450
            fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3451
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3452
            fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3453
            fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3454
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3455
            for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3456
                checkIncomplete(future);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3457
            futures.addAll(fs);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3458
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3459
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3460
        {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3461
            List<CompletableFuture<?>> fs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3462
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3463
            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3464
            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3465
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3466
            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3467
            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3468
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3469
            fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3470
            fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3471
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3472
            for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3473
                checkCompletedWithWrappedException(future, e.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3474
            futures.addAll(fs);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3475
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3476
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3477
        incomplete.complete(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3478
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3479
        for (CompletableFuture<?> future : futures)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3480
            checkCompletedWithWrappedException(future, e.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3481
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3482
        assertEquals(futures.size(), e.count.get());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3483
    }}
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3484
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3485
    /**
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3486
     * Test submissions to an executor that rejects all tasks, but
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3487
     * should never be invoked because the dependent future is
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3488
     * explicitly completed.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3489
     */
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3490
    public void testRejectingExecutorNeverInvoked() {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3491
        for (Integer v : new Integer[] { 1, null })
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3492
    {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3493
        final CountingRejectingExecutor e = new CountingRejectingExecutor();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3494
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3495
        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3496
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3497
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3498
        List<CompletableFuture<?>> futures = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3499
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3500
        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3501
        srcs.add(complete);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3502
        srcs.add(incomplete);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3503
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3504
        List<CompletableFuture<?>> fs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3505
        fs.add(incomplete.thenRunAsync(() -> {}, e));
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3506
        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3507
        fs.add(incomplete.thenApplyAsync(z -> z, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3508
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3509
        fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3510
        fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3511
        fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3512
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3513
        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3514
        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3515
        fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3516
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3517
        fs.add(incomplete.thenComposeAsync(z -> null, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3518
        fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3519
        fs.add(incomplete.handleAsync((z, t) -> null, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3520
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3521
        fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3522
        fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3523
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3524
        fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3525
        fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3526
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3527
        fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3528
        fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3529
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3530
        for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3531
            checkIncomplete(future);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3532
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3533
        for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3534
            future.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3535
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3536
        incomplete.complete(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3537
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3538
        for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3539
            checkCompletedNormally(future, null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3540
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3541
        assertEquals(0, e.count.get());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3542
    }}
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3543
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3544
    /**
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3545
     * toCompletableFuture returns this CompletableFuture.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3546
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3547
    public void testToCompletableFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3548
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3549
        assertSame(f, f.toCompletableFuture());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3550
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3551
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3552
    // jdk9
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3553
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3554
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3555
     * newIncompleteFuture returns an incomplete CompletableFuture
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3556
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3557
    public void testNewIncompleteFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3558
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3559
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3560
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3561
        CompletableFuture<Integer> g = f.newIncompleteFuture();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3562
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3563
        checkIncomplete(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3564
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3565
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3566
        checkIncomplete(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3567
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3568
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3569
        assertSame(g.getClass(), CompletableFuture.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3570
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3571
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3572
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3573
     * completedStage returns a completed CompletionStage
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3574
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3575
    public void testCompletedStage() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3576
        AtomicInteger x = new AtomicInteger(0);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
  3577
        AtomicReference<Throwable> r = new AtomicReference<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3578
        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3579
        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3580
        assertEquals(x.get(), 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3581
        assertNull(r.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3582
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3583
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3584
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3585
     * defaultExecutor by default returns the commonPool if
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3586
     * it supports more than one thread.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3587
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3588
    public void testDefaultExecutor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3589
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3590
        Executor e = f.defaultExecutor();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3591
        Executor c = ForkJoinPool.commonPool();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3592
        if (ForkJoinPool.getCommonPoolParallelism() > 1)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3593
            assertSame(e, c);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3594
        else
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3595
            assertNotSame(e, c);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3596
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3597
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3598
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3599
     * failedFuture returns a CompletableFuture completed
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3600
     * exceptionally with the given Exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3601
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3602
    public void testFailedFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3603
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3604
        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3605
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3606
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3607
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3608
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3609
     * failedFuture(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3610
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3611
    public void testFailedFuture_null() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3612
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3613
            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3614
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3615
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3616
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3617
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3618
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3619
     * copy returns a CompletableFuture that is completed normally,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3620
     * with the same value, when source is.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3621
     */
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3622
    public void testCopy_normalCompletion() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3623
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3624
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3625
    {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3626
        CompletableFuture<Integer> f = new CompletableFuture<>();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3627
        if (!createIncomplete) assertTrue(f.complete(v1));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3628
        CompletableFuture<Integer> g = f.copy();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3629
        if (createIncomplete) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3630
            checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3631
            checkIncomplete(g);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3632
            assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3633
        }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3634
        checkCompletedNormally(f, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3635
        checkCompletedNormally(g, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3636
    }}
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3637
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3638
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3639
     * copy returns a CompletableFuture that is completed exceptionally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3640
     * when source is.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3641
     */
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3642
    public void testCopy_exceptionalCompletion() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3643
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3644
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3645
        CFException ex = new CFException();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3646
        CompletableFuture<Integer> f = new CompletableFuture<>();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3647
        if (!createIncomplete) f.completeExceptionally(ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3648
        CompletableFuture<Integer> g = f.copy();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3649
        if (createIncomplete) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3650
            checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3651
            checkIncomplete(g);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3652
            f.completeExceptionally(ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3653
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3654
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3655
        checkCompletedWithWrappedException(g, ex);
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3656
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3657
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3658
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3659
     * Completion of a copy does not complete its source.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3660
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3661
    public void testCopy_oneWayPropagation() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3662
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3663
        assertTrue(f.copy().complete(1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3664
        assertTrue(f.copy().complete(null));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3665
        assertTrue(f.copy().cancel(true));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3666
        assertTrue(f.copy().cancel(false));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3667
        assertTrue(f.copy().completeExceptionally(new CFException()));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3668
        checkIncomplete(f);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3669
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3670
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3671
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3672
     * minimalCompletionStage returns a CompletableFuture that is
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3673
     * completed normally, with the same value, when source is.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3674
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3675
    public void testMinimalCompletionStage() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3676
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3677
        CompletionStage<Integer> g = f.minimalCompletionStage();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3678
        AtomicInteger x = new AtomicInteger(0);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
  3679
        AtomicReference<Throwable> r = new AtomicReference<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3680
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3681
        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3682
        f.complete(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3683
        checkCompletedNormally(f, 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3684
        assertEquals(x.get(), 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3685
        assertNull(r.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3686
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3687
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3688
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3689
     * minimalCompletionStage returns a CompletableFuture that is
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3690
     * completed exceptionally when source is.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3691
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3692
    public void testMinimalCompletionStage2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3693
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3694
        CompletionStage<Integer> g = f.minimalCompletionStage();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3695
        AtomicInteger x = new AtomicInteger(0);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
  3696
        AtomicReference<Throwable> r = new AtomicReference<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3697
        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3698
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3699
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3700
        f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3701
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3702
        assertEquals(x.get(), 0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3703
        assertEquals(r.get().getCause(), ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3704
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3705
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3706
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3707
     * failedStage returns a CompletionStage completed
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3708
     * exceptionally with the given Exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3709
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3710
    public void testFailedStage() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3711
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3712
        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3713
        AtomicInteger x = new AtomicInteger(0);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
  3714
        AtomicReference<Throwable> r = new AtomicReference<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3715
        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3716
        assertEquals(x.get(), 0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3717
        assertEquals(r.get(), ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3718
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3719
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3720
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3721
     * completeAsync completes with value of given supplier
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3722
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3723
    public void testCompleteAsync() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3724
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3725
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3726
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3727
        f.completeAsync(() -> v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3728
        f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3729
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3730
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3731
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3732
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3733
     * completeAsync completes exceptionally if given supplier throws
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3734
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3735
    public void testCompleteAsync2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3736
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3737
        CFException ex = new CFException();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3738
        f.completeAsync(() -> { throw ex; });
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3739
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3740
            f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3741
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3742
        } catch (CompletionException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3743
        checkCompletedWithWrappedException(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3744
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3745
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3746
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3747
     * completeAsync with given executor completes with value of given supplier
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3748
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3749
    public void testCompleteAsync3() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3750
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3751
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3752
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3753
        ThreadExecutor executor = new ThreadExecutor();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3754
        f.completeAsync(() -> v1, executor);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3755
        assertSame(v1, f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3756
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3757
        assertEquals(1, executor.count.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3758
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3759
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3760
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3761
     * completeAsync with given executor completes exceptionally if
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3762
     * given supplier throws
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3763
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3764
    public void testCompleteAsync4() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3765
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3766
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3767
        ThreadExecutor executor = new ThreadExecutor();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3768
        f.completeAsync(() -> { throw ex; }, executor);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3769
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3770
            f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3771
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3772
        } catch (CompletionException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3773
        checkCompletedWithWrappedException(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3774
        assertEquals(1, executor.count.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3775
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3776
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3777
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3778
     * orTimeout completes with TimeoutException if not complete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3779
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3780
    public void testOrTimeout_timesOut() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3781
        long timeoutMillis = timeoutMillis();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3782
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3783
        long startTime = System.nanoTime();
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3784
        assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3785
        checkCompletedWithTimeoutException(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3786
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3787
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3788
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3789
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3790
     * orTimeout completes normally if completed before timeout
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3791
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3792
    public void testOrTimeout_completed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3793
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3794
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3795
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3796
        CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3797
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3798
        f.complete(v1);
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3799
        assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS));
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3800
        assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3801
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3802
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3803
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3804
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3805
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3806
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3807
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3808
     * completeOnTimeout completes with given value if not complete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3809
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3810
    public void testCompleteOnTimeout_timesOut() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3811
        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3812
                       () -> testCompleteOnTimeout_timesOut(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3813
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3814
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3815
    /**
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3816
     * completeOnTimeout completes with given value if not complete
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3817
     */
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3818
    public void testCompleteOnTimeout_timesOut(Integer v) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3819
        long timeoutMillis = timeoutMillis();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3820
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3821
        long startTime = System.nanoTime();
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3822
        assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3823
        assertSame(v, f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3824
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3825
        f.complete(99);         // should have no effect
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3826
        checkCompletedNormally(f, v);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3827
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3828
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3829
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3830
     * completeOnTimeout has no effect if completed within timeout
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3831
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3832
    public void testCompleteOnTimeout_completed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3833
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3834
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3835
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3836
        CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3837
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3838
        f.complete(v1);
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3839
        assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3840
        assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3841
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3842
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3843
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3844
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3845
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3846
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3847
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3848
     * delayedExecutor returns an executor that delays submission
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3849
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3850
    public void testDelayedExecutor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3851
        testInParallel(() -> testDelayedExecutor(null, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3852
                       () -> testDelayedExecutor(null, 1),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3853
                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3854
                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3855
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3856
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3857
    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3858
        long timeoutMillis = timeoutMillis();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3859
        // Use an "unreasonably long" long timeout to catch lingering threads
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3860
        long longTimeoutMillis = 1000 * 60 * 60 * 24;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3861
        final Executor delayer, longDelayer;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3862
        if (executor == null) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3863
            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3864
            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3865
        } else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3866
            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3867
            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3868
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3869
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3870
        CompletableFuture<Integer> f =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3871
            CompletableFuture.supplyAsync(() -> v, delayer);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3872
        CompletableFuture<Integer> g =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3873
            CompletableFuture.supplyAsync(() -> v, longDelayer);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3874
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3875
        assertNull(g.getNow(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3876
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3877
        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3878
        long millisElapsed = millisElapsedSince(startTime);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3879
        assertTrue(millisElapsed >= timeoutMillis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3880
        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3881
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3882
        checkCompletedNormally(f, v);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3883
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3884
        checkIncomplete(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3885
        assertTrue(g.cancel(true));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3886
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3887
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3888
    //--- tests of implementation details; not part of official tck ---
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3889
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3890
    Object resultOf(CompletableFuture<?> f) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3891
        SecurityManager sm = System.getSecurityManager();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3892
        if (sm != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3893
            try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3894
                System.setSecurityManager(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3895
            } catch (SecurityException giveUp) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3896
                return "Reflection not available";
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3897
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3898
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3899
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3900
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3901
            java.lang.reflect.Field resultField
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3902
                = CompletableFuture.class.getDeclaredField("result");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3903
            resultField.setAccessible(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3904
            return resultField.get(f);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3905
        } catch (Throwable t) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3906
            throw new AssertionError(t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3907
        } finally {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3908
            if (sm != null) System.setSecurityManager(sm);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3909
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3910
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3911
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3912
    public void testExceptionPropagationReusesResultObject() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3913
        if (!testImplementationDetails) return;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3914
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3915
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3916
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3917
        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3918
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3919
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3920
        final Runnable noopRunnable = new Noop(m);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3921
        final Consumer<Integer> noopConsumer = new NoopConsumer(m);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3922
        final Function<Integer, Integer> incFunction = new IncFunction(m);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3923
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3924
        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3925
            = new ArrayList<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3926
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3927
        funs.add(y -> m.thenRun(y, noopRunnable));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3928
        funs.add(y -> m.thenAccept(y, noopConsumer));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3929
        funs.add(y -> m.thenApply(y, incFunction));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3930
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3931
        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3932
        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3933
        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3934
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3935
        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3936
        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3937
        funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3938
        funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3939
        funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m)));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3940
        funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m)));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3941
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3942
        funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3943
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3944
        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3945
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3946
        funs.add(y -> CompletableFuture.allOf(y));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3947
        funs.add(y -> CompletableFuture.allOf(y, v42));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3948
        funs.add(y -> CompletableFuture.allOf(v42, y));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3949
        funs.add(y -> CompletableFuture.anyOf(y));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3950
        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3951
        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3952
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3953
        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3954
                 fun : funs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3955
            CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3956
            f.completeExceptionally(ex);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3957
            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3958
            checkCompletedWithWrappedException(src, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3959
            CompletableFuture<?> dep = fun.apply(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3960
            checkCompletedWithWrappedException(dep, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3961
            assertSame(resultOf(src), resultOf(dep));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3962
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3963
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3964
        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3965
                 fun : funs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3966
            CompletableFuture<Integer> f = new CompletableFuture<>();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3967
            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3968
            CompletableFuture<?> dep = fun.apply(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3969
            f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3970
            checkCompletedWithWrappedException(src, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3971
            checkCompletedWithWrappedException(dep, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3972
            assertSame(resultOf(src), resultOf(dep));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3973
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3974
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3975
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3976
        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3977
                 fun : funs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3978
            CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3979
            f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3980
            checkCancelled(f);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3981
            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3982
            checkCompletedWithWrappedCancellationException(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3983
            CompletableFuture<?> dep = fun.apply(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3984
            checkCompletedWithWrappedCancellationException(dep);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3985
            assertSame(resultOf(src), resultOf(dep));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3986
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3987
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3988
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3989
        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3990
                 fun : funs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3991
            CompletableFuture<Integer> f = new CompletableFuture<>();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3992
            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3993
            CompletableFuture<?> dep = fun.apply(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3994
            f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3995
            checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3996
            checkCompletedWithWrappedCancellationException(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3997
            checkCompletedWithWrappedCancellationException(dep);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3998
            assertSame(resultOf(src), resultOf(dep));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3999
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4000
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4001
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4002
    /**
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4003
     * Minimal completion stages throw UOE for most non-CompletionStage methods
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4004
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4005
    public void testMinimalCompletionStage_minimality() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4006
        if (!testImplementationDetails) return;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4007
        Function<Method, String> toSignature =
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4008
            method -> method.getName() + Arrays.toString(method.getParameterTypes());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4009
        Predicate<Method> isNotStatic =
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4010
            method -> (method.getModifiers() & Modifier.STATIC) == 0;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4011
        List<Method> minimalMethods =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4012
            Stream.of(Object.class, CompletionStage.class)
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4013
            .flatMap(klazz -> Stream.of(klazz.getMethods()))
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4014
            .filter(isNotStatic)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4015
            .collect(Collectors.toList());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4016
        // Methods from CompletableFuture permitted NOT to throw UOE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4017
        String[] signatureWhitelist = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4018
            "newIncompleteFuture[]",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4019
            "defaultExecutor[]",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4020
            "minimalCompletionStage[]",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4021
            "copy[]",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4022
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4023
        Set<String> permittedMethodSignatures =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4024
            Stream.concat(minimalMethods.stream().map(toSignature),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4025
                          Stream.of(signatureWhitelist))
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4026
            .collect(Collectors.toSet());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4027
        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4028
            .filter(isNotStatic)
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4029
            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4030
            .collect(Collectors.toList());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4031
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4032
        List<CompletionStage<Integer>> stages = new ArrayList<>();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4033
        CompletionStage<Integer> min =
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4034
            new CompletableFuture<Integer>().minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4035
        stages.add(min);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4036
        stages.add(min.thenApply(x -> x));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4037
        stages.add(CompletableFuture.completedStage(1));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4038
        stages.add(CompletableFuture.failedStage(new CFException()));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4039
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4040
        List<Method> bugs = new ArrayList<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4041
        for (Method method : allMethods) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4042
            Class<?>[] parameterTypes = method.getParameterTypes();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4043
            Object[] args = new Object[parameterTypes.length];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4044
            // Manufacture boxed primitives for primitive params
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4045
            for (int i = 0; i < args.length; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4046
                Class<?> type = parameterTypes[i];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4047
                if (parameterTypes[i] == boolean.class)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4048
                    args[i] = false;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4049
                else if (parameterTypes[i] == int.class)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4050
                    args[i] = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4051
                else if (parameterTypes[i] == long.class)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4052
                    args[i] = 0L;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4053
            }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4054
            for (CompletionStage<Integer> stage : stages) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4055
                try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4056
                    method.invoke(stage, args);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4057
                    bugs.add(method);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4058
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4059
                catch (java.lang.reflect.InvocationTargetException expected) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4060
                    if (! (expected.getCause() instanceof UnsupportedOperationException)) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4061
                        bugs.add(method);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4062
                        // expected.getCause().printStackTrace();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4063
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4064
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4065
                catch (ReflectiveOperationException bad) { throw new Error(bad); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4066
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4067
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4068
        if (!bugs.isEmpty())
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4069
            throw new Error("Methods did not throw UOE: " + bugs);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4070
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4071
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4072
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4073
     * minimalStage.toCompletableFuture() returns a CompletableFuture that
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4074
     * is completed normally, with the same value, when source is.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4075
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4076
    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4077
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4078
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4079
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4080
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4081
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4082
        if (!createIncomplete) assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4083
        CompletableFuture<Integer> g = minimal.toCompletableFuture();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4084
        if (createIncomplete) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4085
            checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4086
            checkIncomplete(g);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4087
            assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4088
        }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4089
        checkCompletedNormally(f, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4090
        checkCompletedNormally(g, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4091
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4092
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4093
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4094
     * minimalStage.toCompletableFuture() returns a CompletableFuture that
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4095
     * is completed exceptionally when source is.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4096
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4097
    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4098
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4099
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4100
        CFException ex = new CFException();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4101
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4102
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4103
        if (!createIncomplete) f.completeExceptionally(ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4104
        CompletableFuture<Integer> g = minimal.toCompletableFuture();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4105
        if (createIncomplete) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4106
            checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4107
            checkIncomplete(g);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4108
            f.completeExceptionally(ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4109
        }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4110
        checkCompletedExceptionally(f, ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4111
        checkCompletedWithWrappedException(g, ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4112
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4113
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4114
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4115
     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4116
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4117
    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4118
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4119
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4120
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4121
        CompletionStage minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4122
        CompletableFuture<Integer> g = minimal.toCompletableFuture();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4123
        assertTrue(g.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4124
        checkCompletedNormally(g, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4125
        checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4126
        checkIncomplete(minimal.toCompletableFuture());
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4127
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4129
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4130
     * minimalStage.toCompletableFuture().join() awaits completion
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4131
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4132
    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4133
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4134
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4135
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4136
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4137
        if (!createIncomplete) assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4138
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4139
        if (createIncomplete) assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4140
        assertEquals(v1, minimal.toCompletableFuture().join());
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4141
        assertEquals(v1, minimal.toCompletableFuture().get());
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4142
        checkCompletedNormally(minimal.toCompletableFuture(), v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4143
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4144
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4145
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4146
     * Completion of a toCompletableFuture copy of a minimal stage
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4147
     * does not complete its source.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4148
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4149
    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4150
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4151
        CompletionStage<Integer> g = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4152
        assertTrue(g.toCompletableFuture().complete(1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4153
        assertTrue(g.toCompletableFuture().complete(null));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4154
        assertTrue(g.toCompletableFuture().cancel(true));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4155
        assertTrue(g.toCompletableFuture().cancel(false));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4156
        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4157
        checkIncomplete(g.toCompletableFuture());
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4158
        f.complete(1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4159
        checkCompletedNormally(g.toCompletableFuture(), 1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4160
    }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4161
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4162
    /** Demo utility method for external reliable toCompletableFuture */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4163
    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4164
        CompletableFuture<T> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4165
        stage.handle((T t, Throwable ex) -> {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4166
                         if (ex != null) f.completeExceptionally(ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4167
                         else f.complete(t);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4168
                         return null;
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4169
                     });
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4170
        return f;
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4171
    }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4172
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4173
    /** Demo utility method to join a CompletionStage */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4174
    static <T> T join(CompletionStage<T> stage) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4175
        return toCompletableFuture(stage).join();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4176
    }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4177
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4178
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4179
     * Joining a minimal stage "by hand" works
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4180
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4181
    public void testMinimalCompletionStage_join_by_hand() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4182
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4183
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4184
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4185
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4186
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4187
        CompletableFuture<Integer> g = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4188
        if (!createIncomplete) assertTrue(f.complete(v1));
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4189
        minimal.thenAccept(x -> g.complete(x));
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4190
        if (createIncomplete) assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4191
        g.join();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4192
        checkCompletedNormally(g, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4193
        checkCompletedNormally(f, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4194
        assertEquals(v1, join(minimal));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4195
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4196
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4197
    static class Monad {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4198
        static class ZeroException extends RuntimeException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4199
            public ZeroException() { super("monadic zero"); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4200
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4201
        // "return", "unit"
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4202
        static <T> CompletableFuture<T> unit(T value) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4203
            return completedFuture(value);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4204
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4205
        // monadic zero ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4206
        static <T> CompletableFuture<T> zero() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4207
            return failedFuture(new ZeroException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4208
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4209
        // >=>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4210
        static <T,U,V> Function<T, CompletableFuture<V>> compose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4211
            (Function<T, CompletableFuture<U>> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4212
             Function<U, CompletableFuture<V>> g) {
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4213
            return x -> f.apply(x).thenCompose(g);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4214
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4215
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4216
        static void assertZero(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4217
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4218
                f.getNow(null);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  4219
                throw new AssertionError("should throw");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4220
            } catch (CompletionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4221
                assertTrue(success.getCause() instanceof ZeroException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4222
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4223
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4224
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4225
        static <T> void assertFutureEquals(CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4226
                                           CompletableFuture<T> g) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4227
            T fval = null, gval = null;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4228
            Throwable fex = null, gex = null;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4229
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4230
            try { fval = f.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4231
            catch (ExecutionException ex) { fex = ex.getCause(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4232
            catch (Throwable ex) { fex = ex; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4233
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4234
            try { gval = g.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4235
            catch (ExecutionException ex) { gex = ex.getCause(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4236
            catch (Throwable ex) { gex = ex; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4237
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4238
            if (fex != null || gex != null)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4239
                assertSame(fex.getClass(), gex.getClass());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4240
            else
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4241
                assertEquals(fval, gval);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4242
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4243
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4244
        static class PlusFuture<T> extends CompletableFuture<T> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4245
            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4246
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4247
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4248
        /** Implements "monadic plus". */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4249
        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4250
                                             CompletableFuture<? extends T> g) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4251
            PlusFuture<T> plus = new PlusFuture<T>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4252
            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4253
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4254
                    if (ex == null) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4255
                        if (plus.complete(result))
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4256
                            if (plus.firstFailure.get() != null)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4257
                                plus.firstFailure.set(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4258
                    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4259
                    else if (plus.firstFailure.compareAndSet(null, ex)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4260
                        if (plus.isDone())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4261
                            plus.firstFailure.set(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4262
                    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4263
                    else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4264
                        // first failure has precedence
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4265
                        Throwable first = plus.firstFailure.getAndSet(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4266
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4267
                        // may fail with "Self-suppression not permitted"
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4268
                        try { first.addSuppressed(ex); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4269
                        catch (Exception ignored) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4270
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4271
                        plus.completeExceptionally(first);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4272
                    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4273
                } catch (Throwable unexpected) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4274
                    plus.completeExceptionally(unexpected);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4275
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4276
            };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4277
            f.whenComplete(action);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4278
            g.whenComplete(action);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4279
            return plus;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4280
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4281
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4282
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4283
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4284
     * CompletableFuture is an additive monad - sort of.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4285
     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4286
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4287
    public void testAdditiveMonad() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4288
        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4289
        CompletableFuture<Long> zero = Monad.zero();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4290
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4291
        // Some mutually non-commutative functions
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4292
        Function<Long, CompletableFuture<Long>> triple
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4293
            = x -> Monad.unit(3 * x);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4294
        Function<Long, CompletableFuture<Long>> inc
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4295
            = x -> Monad.unit(x + 1);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4296
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4297
        // unit is a right identity: m >>= unit === m
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4298
        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4299
                                 inc.apply(5L));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4300
        // unit is a left identity: (unit x) >>= f === f x
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4301
        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4302
                                 inc.apply(5L));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4303
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4304
        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4305
        Monad.assertFutureEquals(
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4306
            unit.apply(5L).thenCompose(inc).thenCompose(triple),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4307
            unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple)));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4308
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4309
        // The case for CompletableFuture as an additive monad is weaker...
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4310
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4311
        // zero is a monadic zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4312
        Monad.assertZero(zero);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4313
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4314
        // left zero: zero >>= f === zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4315
        Monad.assertZero(zero.thenCompose(inc));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4316
        // right zero: f >>= (\x -> zero) === zero
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4317
        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4318
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4319
        // f plus zero === f
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4320
        Monad.assertFutureEquals(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4321
                                 Monad.plus(Monad.unit(5L), zero));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4322
        // zero plus f === f
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4323
        Monad.assertFutureEquals(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4324
                                 Monad.plus(zero, Monad.unit(5L)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4325
        // zero plus zero === zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4326
        Monad.assertZero(Monad.plus(zero, zero));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4327
        {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4328
            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4329
                                                   Monad.unit(8L));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4330
            // non-determinism
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4331
            assertTrue(f.get() == 5L || f.get() == 8L);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4332
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4333
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4334
        CompletableFuture<Long> godot = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4335
        // f plus godot === f (doesn't wait for godot)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4336
        Monad.assertFutureEquals(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4337
                                 Monad.plus(Monad.unit(5L), godot));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4338
        // godot plus f === f (doesn't wait for godot)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4339
        Monad.assertFutureEquals(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4340
                                 Monad.plus(godot, Monad.unit(5L)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4341
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4342
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4343
    /** Test long recursive chains of CompletableFutures with cascading completions */
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  4344
    @SuppressWarnings("FutureReturnValueIgnored")
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4345
    public void testRecursiveChains() throws Throwable {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4346
        for (ExecutionMode m : ExecutionMode.values())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4347
        for (boolean addDeadEnds : new boolean[] { true, false })
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4348
    {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4349
        final int val = 42;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4350
        final int n = expensiveTests ? 1_000 : 2;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4351
        CompletableFuture<Integer> head = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4352
        CompletableFuture<Integer> tail = head;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4353
        for (int i = 0; i < n; i++) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4354
            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4355
            tail = m.thenApply(tail, v -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4356
            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4357
            tail = m.applyToEither(tail, tail, v -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4358
            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4359
            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4360
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4361
        head.complete(val);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4362
        assertEquals(val + 3 * n, (int) tail.join());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4363
    }}
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4364
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4365
    /**
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4366
     * A single CompletableFuture with many dependents.
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4367
     * A demo of scalability - runtime is O(n).
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4368
     */
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  4369
    @SuppressWarnings("FutureReturnValueIgnored")
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4370
    public void testManyDependents() throws Throwable {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4371
        final int n = expensiveTests ? 1_000_000 : 10;
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4372
        final CompletableFuture<Void> head = new CompletableFuture<>();
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4373
        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4374
        final AtomicInteger count = new AtomicInteger(0);
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4375
        for (int i = 0; i < n; i++) {
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4376
            head.thenRun(() -> count.getAndIncrement());
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4377
            head.thenAccept(x -> count.getAndIncrement());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4378
            head.thenApply(x -> count.getAndIncrement());
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4379
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4380
            head.runAfterBoth(complete, () -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4381
            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4382
            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4383
            complete.runAfterBoth(head, () -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4384
            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4385
            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4386
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4387
            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4388
            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4389
            head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4390
            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4391
            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4392
            new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement());
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4393
        }
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4394
        head.complete(null);
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4395
        assertEquals(5 * 3 * n, count.get());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4396
    }
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4397
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4398
    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  4399
    @SuppressWarnings("FutureReturnValueIgnored")
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4400
    public void testCoCompletionGarbageRetention() throws Throwable {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4401
        final int n = expensiveTests ? 1_000_000 : 10;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4402
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4403
        CompletableFuture<Integer> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4404
        for (int i = 0; i < n; i++) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4405
            f = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4406
            f.runAfterEither(incomplete, () -> {});
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4407
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4408
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4409
            f = new CompletableFuture<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4410
            f.acceptEither(incomplete, x -> {});
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4411
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4412
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4413
            f = new CompletableFuture<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4414
            f.applyToEither(incomplete, x -> x);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4415
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4416
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4417
            f = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4418
            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4419
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4420
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4421
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4422
        for (int i = 0; i < n; i++) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4423
            f = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4424
            incomplete.runAfterEither(f, () -> {});
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4425
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4426
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4427
            f = new CompletableFuture<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4428
            incomplete.acceptEither(f, x -> {});
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4429
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4430
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4431
            f = new CompletableFuture<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4432
            incomplete.applyToEither(f, x -> x);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4433
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4434
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4435
            f = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4436
            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4437
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4438
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4439
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4440
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4441
    /**
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4442
     * Reproduction recipe for:
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4443
     * 8160402: Garbage retention with CompletableFuture.anyOf
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4444
     * cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4445
     */
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4446
    public void testAnyOfGarbageRetention() throws Throwable {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4447
        for (Integer v : new Integer[] { 1, null })
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4448
    {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4449
        final int n = expensiveTests ? 100_000 : 10;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4450
        CompletableFuture<Integer>[] fs
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4451
            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4452
        for (int i = 0; i < fs.length; i++)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4453
            fs[i] = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4454
        fs[fs.length - 1].complete(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4455
        for (int i = 0; i < n; i++)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4456
            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4457
    }}
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4458
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4459
    /**
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4460
     * Checks for garbage retention with allOf.
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4461
     *
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4462
     * As of 2016-07, fails with OOME:
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4463
     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4464
     */
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4465
    public void testCancelledAllOfGarbageRetention() throws Throwable {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4466
        final int n = expensiveTests ? 100_000 : 10;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4467
        CompletableFuture<Integer>[] fs
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4468
            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4469
        for (int i = 0; i < fs.length; i++)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4470
            fs[i] = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4471
        for (int i = 0; i < n; i++)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4472
            assertTrue(CompletableFuture.allOf(fs).cancel(false));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4473
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4474
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4475
    /**
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4476
     * Checks for garbage retention when a dependent future is
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4477
     * cancelled and garbage-collected.
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4478
     * 8161600: Garbage retention when source CompletableFutures are never completed
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4479
     *
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4480
     * As of 2016-07, fails with OOME:
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4481
     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4482
     */
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4483
    public void testCancelledGarbageRetention() throws Throwable {
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4484
        final int n = expensiveTests ? 100_000 : 10;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4485
        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4486
        for (int i = 0; i < n; i++)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4487
            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4488
    }
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4489
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4490
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4491
     * Checks for garbage retention when MinimalStage.toCompletableFuture()
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4492
     * is invoked many times.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4493
     * 8161600: Garbage retention when source CompletableFutures are never completed
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4494
     *
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4495
     * As of 2016-07, fails with OOME:
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4496
     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4497
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4498
    public void testToCompletableFutureGarbageRetention() throws Throwable {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4499
        final int n = expensiveTests ? 900_000 : 10;
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4500
        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4501
        CompletionStage minimal = neverCompleted.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4502
        for (int i = 0; i < n; i++)
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4503
            assertTrue(minimal.toCompletableFuture().cancel(true));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4504
    }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4505
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4506
//     static <U> U join(CompletionStage<U> stage) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4507
//         CompletableFuture<U> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4508
//         stage.whenComplete((v, ex) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4509
//             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4510
//         });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4511
//         return f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4512
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4513
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4514
//     static <U> boolean isDone(CompletionStage<U> stage) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4515
//         CompletableFuture<U> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4516
//         stage.whenComplete((v, ex) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4517
//             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4518
//         });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4519
//         return f.isDone();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4520
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4521
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4522
//     static <U> U join2(CompletionStage<U> stage) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4523
//         return stage.toCompletableFuture().copy().join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4524
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4525
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4526
//     static <U> boolean isDone2(CompletionStage<U> stage) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4527
//         return stage.toCompletableFuture().copy().isDone();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4528
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4529
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4530
}