test/jdk/java/util/concurrent/tck/CompletableFutureTest.java
author dl
Wed, 28 Nov 2018 15:25:14 -0800
changeset 52730 345266000aba
parent 51950 a1c24d06e2b5
child 54686 09f09b4e7808
permissions -rw-r--r--
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11 Reviewed-by: martin, chegar
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.*\\]"));
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
    89
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
    90
        Object result = null;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    91
        try {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
    92
            result = f.getNow(null);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    93
        } catch (Throwable fail) { threadUnexpectedException(fail); }
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
    94
        assertNull(result);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
    95
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    96
        try {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
    97
            f.get(randomExpiredTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    98
            shouldThrow();
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
        catch (TimeoutException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   101
        catch (Throwable fail) { threadUnexpectedException(fail); }
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
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   104
    <T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   105
        checkTimedGet(f, expectedValue);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   106
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   107
        assertEquals(expectedValue, f.join());
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   108
        assertEquals(expectedValue, f.getNow(null));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   109
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   110
        T result = null;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   111
        try {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   112
            result = f.get();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
        } catch (Throwable fail) { threadUnexpectedException(fail); }
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   114
        assertEquals(expectedValue, result);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   115
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   116
        assertTrue(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   117
        assertFalse(f.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   118
        assertFalse(f.isCompletedExceptionally());
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   119
        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   120
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   121
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
     * Returns the "raw" internal exceptional completion of f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   124
     * without any additional wrapping with CompletionException.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   125
     */
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
   126
    Throwable exceptionalCompletion(CompletableFuture<?> f) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
   127
        // handle (and whenComplete and exceptionally) can distinguish
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
   128
        // between "direct" and "wrapped" exceptional completion
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
   129
        return f.handle((u, t) -> t).join();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   130
    }
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
    void checkCompletedExceptionally(CompletableFuture<?> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   133
                                     boolean wrapped,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   134
                                     Consumer<Throwable> checker) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   135
        Throwable cause = exceptionalCompletion(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   136
        if (wrapped) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   137
            assertTrue(cause instanceof CompletionException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   138
            cause = cause.getCause();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   139
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   140
        checker.accept(cause);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   141
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   142
        long startTime = System.nanoTime();
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.get(LONG_DELAY_MS, MILLISECONDS);
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 (ExecutionException 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
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   150
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   151
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   152
            f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   153
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   154
        } catch (CompletionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   155
            assertSame(cause, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   156
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   157
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   158
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   159
            f.getNow(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   160
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   161
        } catch (CompletionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   162
            assertSame(cause, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   163
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   164
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   165
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   166
            f.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   167
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   168
        } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   169
            assertSame(cause, success.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   170
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   171
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   172
        assertFalse(f.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   173
        assertTrue(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   174
        assertTrue(f.isCompletedExceptionally());
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   175
        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   176
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   177
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   178
    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   179
        checkCompletedExceptionally(f, true,
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   180
            t -> assertTrue(t instanceof CFException));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   181
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   182
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   183
    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   184
        checkCompletedExceptionally(f, true,
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   185
            t -> assertTrue(t instanceof CancellationException));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   186
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   187
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   188
    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   189
        checkCompletedExceptionally(f, false,
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   190
            t -> assertTrue(t instanceof TimeoutException));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   191
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   192
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   193
    void checkCompletedWithWrappedException(CompletableFuture<?> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   194
                                            Throwable ex) {
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   195
        checkCompletedExceptionally(f, true, t -> assertSame(t, ex));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   196
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   197
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   198
    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
   199
        checkCompletedExceptionally(f, false, t -> assertSame(t, ex));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   200
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   201
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   202
    void checkCancelled(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   203
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   204
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   205
            f.get(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   206
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   207
        } catch (CancellationException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   208
        } catch (Throwable fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   209
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   210
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.join();
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
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   216
            f.getNow(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   217
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   218
        } catch (CancellationException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   219
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   220
            f.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   221
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   222
        } catch (CancellationException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   223
        } catch (Throwable fail) { threadUnexpectedException(fail); }
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
        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   226
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   227
        assertTrue(f.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   228
        assertTrue(f.isCompletedExceptionally());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   229
        assertTrue(f.isCancelled());
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   230
        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   231
    }
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
     * A newly constructed CompletableFuture is incomplete, as indicated
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   235
     * by methods isDone, isCancelled, and getNow
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   236
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   237
    public void testConstructor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   238
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   239
        checkIncomplete(f);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   242
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   243
     * complete completes normally, as indicated by methods isDone,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   244
     * isCancelled, join, get, and getNow
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   245
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   246
    public void testComplete() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   247
        for (Integer v1 : new Integer[] { 1, null })
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
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   250
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   251
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   252
        assertFalse(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   253
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   254
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   255
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   256
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   257
     * completeExceptionally completes exceptionally, as indicated by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   258
     * methods isDone, isCancelled, join, get, and getNow
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
    public void testCompleteExceptionally() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   261
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   262
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   263
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   264
        f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   265
        checkCompletedExceptionally(f, ex);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   268
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   269
     * cancel completes exceptionally and reports cancelled, as indicated by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   270
     * methods isDone, isCancelled, join, get, and getNow
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   271
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   272
    public void testCancel() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   273
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   276
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   277
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   278
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   279
        assertTrue(f.cancel(!mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   280
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   281
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   282
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   283
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   284
     * obtrudeValue forces completion with given value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   285
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   286
    public void testObtrudeValue() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   287
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   288
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   289
        assertTrue(f.complete(one));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   290
        checkCompletedNormally(f, one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   291
        f.obtrudeValue(three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   292
        checkCompletedNormally(f, three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   293
        f.obtrudeValue(two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   294
        checkCompletedNormally(f, two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   295
        f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   296
        f.obtrudeValue(three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   297
        checkCompletedNormally(f, three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   298
        f.obtrudeValue(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   299
        checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   300
        f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   301
        f.completeExceptionally(new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   302
        f.obtrudeValue(four);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   303
        checkCompletedNormally(f, four);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   304
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   305
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   306
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   307
     * obtrudeException forces completion with given exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   308
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   309
    public void testObtrudeException() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   310
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   311
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   312
        CFException ex;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   313
        CompletableFuture<Integer> f;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   314
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   315
        f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   316
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   317
        for (int i = 0; i < 2; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   318
            f.obtrudeException(ex = new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   319
            checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   320
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   321
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   322
        f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   323
        for (int i = 0; i < 2; i++) {
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
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   327
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   328
        f = new CompletableFuture<>();
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   329
        f.completeExceptionally(new CFException());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   330
        f.obtrudeValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   331
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   332
        f.obtrudeException(ex = new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   333
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   334
        f.completeExceptionally(new CFException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   335
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   336
        assertFalse(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   337
        checkCompletedExceptionally(f, ex);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   340
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   341
     * getNumberOfDependents returns number of dependent tasks
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   342
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   343
    public void testGetNumberOfDependents() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   344
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   345
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   346
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   347
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   348
        assertEquals(0, f.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   349
        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   350
        assertEquals(1, 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
        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   353
        assertEquals(2, f.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   354
        assertEquals(0, h.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   355
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   356
        checkCompletedNormally(g, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   357
        checkCompletedNormally(h, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   358
        assertEquals(0, f.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   359
        assertEquals(0, g.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   360
        assertEquals(0, h.getNumberOfDependents());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   361
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   362
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   363
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   364
     * toString indicates current completion state
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   365
     */
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   366
    public void testToString_incomplete() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   367
        CompletableFuture<String> f = new CompletableFuture<>();
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   368
        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   369
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   370
            assertEquals(identityString(f) + "[Not completed]",
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   371
                         f.toString());
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   372
    }
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
    public void testToString_normal() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   375
        CompletableFuture<String> f = new CompletableFuture<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   376
        assertTrue(f.complete("foo"));
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   377
        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   378
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   379
            assertEquals(identityString(f) + "[Completed normally]",
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   380
                         f.toString());
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   381
    }
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
    public void testToString_exception() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   384
        CompletableFuture<String> f = new CompletableFuture<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   385
        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   386
        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   387
        if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   388
            assertTrue(f.toString().startsWith(
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   389
                               identityString(f) + "[Completed exceptionally: "));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   390
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   391
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   392
    public void testToString_cancelled() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   393
        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   394
            CompletableFuture<String> f = new CompletableFuture<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   395
            assertTrue(f.cancel(mayInterruptIfRunning));
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   396
            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   397
            if (testImplementationDetails)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   398
                assertTrue(f.toString().startsWith(
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   399
                                   identityString(f) + "[Completed exceptionally: "));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   400
        }
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   403
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   404
     * completedFuture returns a completed CompletableFuture with given value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   405
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   406
    public void testCompletedFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   407
        CompletableFuture<String> f = CompletableFuture.completedFuture("test");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   408
        checkCompletedNormally(f, "test");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   409
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   410
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   411
    abstract static class CheckedAction {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   412
        int invocationCount = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   413
        final ExecutionMode m;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   414
        CheckedAction(ExecutionMode m) { this.m = m; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   415
        void invoked() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   416
            m.checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   417
            assertEquals(0, invocationCount++);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   418
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   419
        void assertNotInvoked() { assertEquals(0, invocationCount); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   420
        void assertInvoked() { assertEquals(1, invocationCount); }
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
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   423
    abstract static class CheckedIntegerAction extends CheckedAction {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   424
        Integer value;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   425
        CheckedIntegerAction(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   426
        void assertValue(Integer expected) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   427
            assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   428
            assertEquals(expected, value);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   429
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   430
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   431
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   432
    static class IntegerSupplier extends CheckedAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   433
        implements Supplier<Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   434
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   435
        final Integer value;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   436
        IntegerSupplier(ExecutionMode m, Integer value) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   437
            super(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   438
            this.value = value;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   439
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   440
        public Integer get() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   441
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   442
            return value;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   443
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   444
    }
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
    // A function that handles and produces null values as well.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   447
    static Integer inc(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   448
        return (x == null) ? null : x + 1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   449
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   450
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   451
    static class NoopConsumer extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   452
        implements Consumer<Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   453
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   454
        NoopConsumer(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   455
        public void accept(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   456
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   457
            value = x;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   458
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   459
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   460
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   461
    static class IncFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   462
        implements Function<Integer,Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   463
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   464
        IncFunction(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   465
        public Integer apply(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   466
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   467
            return value = inc(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   468
        }
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   471
    // Choose non-commutative actions for better coverage
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   472
    // 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
   473
    static Integer subtract(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   474
        return (x == null && y == null) ? null :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   475
            ((x == null) ? 42 : x.intValue())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   476
            - ((y == null) ? 99 : y.intValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   477
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   478
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   479
    static class SubtractAction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   480
        implements BiConsumer<Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   481
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   482
        SubtractAction(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   483
        public void accept(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   484
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   485
            value = subtract(x, y);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   486
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   487
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   488
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   489
    static class SubtractFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   490
        implements BiFunction<Integer, Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   491
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   492
        SubtractFunction(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   493
        public Integer apply(Integer x, Integer y) {
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
            return value = subtract(x, y);
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   498
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   499
    static class Noop extends CheckedAction implements Runnable {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   500
        Noop(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   501
        public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   502
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   503
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   504
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   505
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   506
    static class FailingSupplier extends CheckedAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   507
        implements Supplier<Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   508
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   509
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   510
        FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   511
        public Integer get() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   512
            invoked();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   513
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   514
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   515
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   516
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   517
    static class FailingConsumer extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   518
        implements Consumer<Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   519
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   520
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   521
        FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   522
        public void accept(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   523
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   524
            value = x;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   525
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   526
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   527
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   528
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   529
    static class FailingBiConsumer extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   530
        implements BiConsumer<Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   531
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   532
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   533
        FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   534
        public void accept(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   535
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   536
            value = subtract(x, y);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   537
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   538
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   539
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   540
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   541
    static class FailingFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   542
        implements Function<Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   543
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   544
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   545
        FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   546
        public Integer apply(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   547
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   548
            value = x;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   549
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   550
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   551
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   552
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   553
    static class FailingBiFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   554
        implements BiFunction<Integer, Integer, Integer>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   555
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   556
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   557
        FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   558
        public Integer apply(Integer x, Integer y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   559
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   560
            value = subtract(x, y);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   561
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   562
        }
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
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   565
    static class FailingRunnable extends CheckedAction implements Runnable {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   566
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   567
        FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   568
        public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   569
            invoked();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   570
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   571
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   572
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   573
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   574
    static class CompletableFutureInc extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   575
        implements Function<Integer, CompletableFuture<Integer>>
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
        CompletableFutureInc(ExecutionMode m) { super(m); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   578
        public CompletableFuture<Integer> apply(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   579
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   580
            value = x;
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   581
            return CompletableFuture.completedFuture(inc(x));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   582
        }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   583
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   584
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   585
    static class FailingExceptionalCompletableFutureFunction extends CheckedAction
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   586
        implements Function<Throwable, CompletableFuture<Integer>>
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   587
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   588
        final CFException ex;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   589
        FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   590
        public CompletableFuture<Integer> apply(Throwable x) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   591
            invoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   592
            throw ex;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   593
        }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   594
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   595
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   596
    static class ExceptionalCompletableFutureFunction extends CheckedAction
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   597
        implements Function<Throwable, CompletionStage<Integer>> {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   598
        final Integer value = 3;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   599
        ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   600
        public CompletionStage<Integer> apply(Throwable x) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   601
            invoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   602
            return CompletableFuture.completedFuture(value);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   603
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   604
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   605
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
   606
    static class FailingCompletableFutureFunction extends CheckedIntegerAction
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   607
        implements Function<Integer, CompletableFuture<Integer>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   608
    {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   609
        final CFException ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   610
        FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   611
        public CompletableFuture<Integer> apply(Integer x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   612
            invoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   613
            value = x;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   614
            throw ex;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   615
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   616
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   617
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   618
    static class CountingRejectingExecutor implements Executor {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   619
        final RejectedExecutionException ex = new RejectedExecutionException();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   620
        final AtomicInteger count = new AtomicInteger(0);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   621
        public void execute(Runnable r) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   622
            count.getAndIncrement();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
   623
            throw ex;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   624
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   625
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   626
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   627
    // Used for explicit executor tests
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   628
    static final class ThreadExecutor implements Executor {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   629
        final AtomicInteger count = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   630
        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   631
        static boolean startedCurrentThread() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   632
            return Thread.currentThread().getThreadGroup() == tg;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   633
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   634
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   635
        public void execute(Runnable r) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   636
            count.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   637
            new Thread(tg, r).start();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   638
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   639
    }
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
    static final boolean defaultExecutorIsCommonPool
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   642
        = ForkJoinPool.getCommonPoolParallelism() > 1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   643
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
     * Permits the testing of parallel code for the 3 different
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   646
     * execution modes without copy/pasting all the test methods.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   647
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   648
    enum ExecutionMode {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   649
        SYNC {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   650
            public void checkExecutionMode() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   651
                assertFalse(ThreadExecutor.startedCurrentThread());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   652
                assertNull(ForkJoinTask.getPool());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   653
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   654
            public CompletableFuture<Void> runAsync(Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   655
                throw new UnsupportedOperationException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   656
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   657
            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   658
                throw new UnsupportedOperationException();
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> CompletableFuture<Void> thenRun
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   661
                (CompletableFuture<T> f, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   662
                return f.thenRun(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> CompletableFuture<Void> thenAccept
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   665
                (CompletableFuture<T> f, Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   666
                return f.thenAccept(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   667
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   668
            public <T,U> CompletableFuture<U> thenApply
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   669
                (CompletableFuture<T> f, Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   670
                return f.thenApply(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   671
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   672
            public <T,U> CompletableFuture<U> thenCompose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   673
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   674
                 Function<? super T,? extends CompletionStage<U>> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   675
                return f.thenCompose(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   676
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   677
            public <T,U> CompletableFuture<U> handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   678
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   679
                 BiFunction<? super T,Throwable,? extends U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   680
                return f.handle(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<T> whenComplete
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
                 BiConsumer<? super T,? super Throwable> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   685
                return f.whenComplete(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   686
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   687
            public <T,U> CompletableFuture<Void> runAfterBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   688
                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   689
                return f.runAfterBoth(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   690
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   691
            public <T,U> CompletableFuture<Void> thenAcceptBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   692
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   693
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   694
                 BiConsumer<? super T,? super U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   695
                return f.thenAcceptBoth(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   696
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   697
            public <T,U,V> CompletableFuture<V> thenCombine
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   698
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   699
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   700
                 BiFunction<? super T,? super U,? extends V> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   701
                return f.thenCombine(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   702
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   703
            public <T> CompletableFuture<Void> runAfterEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   704
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   705
                 CompletionStage<?> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   706
                 java.lang.Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   707
                return f.runAfterEither(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   708
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   709
            public <T> CompletableFuture<Void> acceptEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   710
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   711
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   712
                 Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   713
                return f.acceptEither(g, 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> applyToEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   716
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   717
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   718
                 Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   719
                return f.applyToEither(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   720
            }
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   721
            public <T> CompletableFuture<T> exceptionally
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   722
                (CompletableFuture<T> f,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   723
                 Function<Throwable, ? extends T> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   724
                return f.exceptionally(fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   725
            }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   726
            public <T> CompletableFuture<T> exceptionallyCompose
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   727
                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   728
                return f.exceptionallyCompose(fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   729
            }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   730
        },
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   731
        ASYNC {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   732
            public void checkExecutionMode() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   733
                assertEquals(defaultExecutorIsCommonPool,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   734
                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   735
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   736
            public CompletableFuture<Void> runAsync(Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   737
                return CompletableFuture.runAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   738
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   739
            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   740
                return CompletableFuture.supplyAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   741
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   742
            public <T> CompletableFuture<Void> thenRun
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   743
                (CompletableFuture<T> f, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   744
                return f.thenRunAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   745
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   746
            public <T> CompletableFuture<Void> thenAccept
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   747
                (CompletableFuture<T> f, Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   748
                return f.thenAcceptAsync(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,U> CompletableFuture<U> thenApply
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   751
                (CompletableFuture<T> f, Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   752
                return f.thenApplyAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   753
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   754
            public <T,U> CompletableFuture<U> thenCompose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   755
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   756
                 Function<? super T,? extends CompletionStage<U>> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   757
                return f.thenComposeAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   758
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   759
            public <T,U> CompletableFuture<U> handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   760
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   761
                 BiFunction<? super T,Throwable,? extends U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   762
                return f.handleAsync(a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   763
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   764
            public <T> CompletableFuture<T> whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   765
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   766
                 BiConsumer<? super T,? super Throwable> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   767
                return f.whenCompleteAsync(a);
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
            public <T,U> CompletableFuture<Void> runAfterBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   770
                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   771
                return f.runAfterBothAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   772
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   773
            public <T,U> CompletableFuture<Void> thenAcceptBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   774
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   775
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   776
                 BiConsumer<? super T,? super U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   777
                return f.thenAcceptBothAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   778
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   779
            public <T,U,V> CompletableFuture<V> thenCombine
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   780
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   781
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   782
                 BiFunction<? super T,? super U,? extends V> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   783
                return f.thenCombineAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   784
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   785
            public <T> CompletableFuture<Void> runAfterEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   786
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   787
                 CompletionStage<?> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   788
                 java.lang.Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   789
                return f.runAfterEitherAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   790
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   791
            public <T> CompletableFuture<Void> acceptEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   792
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   793
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   794
                 Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   795
                return f.acceptEitherAsync(g, a);
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> applyToEither
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
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   800
                 Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   801
                return f.applyToEitherAsync(g, a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   802
            }
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   803
            public <T> CompletableFuture<T> exceptionally
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   804
                (CompletableFuture<T> f,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   805
                 Function<Throwable, ? extends T> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   806
                return f.exceptionallyAsync(fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   807
            }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   808
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   809
            public <T> CompletableFuture<T> exceptionallyCompose
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   810
                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   811
                return f.exceptionallyComposeAsync(fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   812
            }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   813
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   814
        },
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   815
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   816
        EXECUTOR {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   817
            public void checkExecutionMode() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   818
                assertTrue(ThreadExecutor.startedCurrentThread());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   819
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   820
            public CompletableFuture<Void> runAsync(Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   821
                return CompletableFuture.runAsync(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 <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   824
                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   825
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   826
            public <T> CompletableFuture<Void> thenRun
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   827
                (CompletableFuture<T> f, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   828
                return f.thenRunAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   829
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   830
            public <T> CompletableFuture<Void> thenAccept
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   831
                (CompletableFuture<T> f, Consumer<? super T> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   832
                return f.thenAcceptAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   833
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   834
            public <T,U> CompletableFuture<U> thenApply
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   835
                (CompletableFuture<T> f, Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   836
                return f.thenApplyAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   837
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   838
            public <T,U> CompletableFuture<U> thenCompose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   839
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   840
                 Function<? super T,? extends CompletionStage<U>> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   841
                return f.thenComposeAsync(a, new ThreadExecutor());
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 <T,U> CompletableFuture<U> handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   844
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   845
                 BiFunction<? super T,Throwable,? extends U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   846
                return f.handleAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   847
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   848
            public <T> CompletableFuture<T> whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   849
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   850
                 BiConsumer<? super T,? super Throwable> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   851
                return f.whenCompleteAsync(a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   852
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   853
            public <T,U> CompletableFuture<Void> runAfterBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   854
                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   855
                return f.runAfterBothAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   856
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   857
            public <T,U> CompletableFuture<Void> thenAcceptBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   858
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   859
                 CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   860
                 BiConsumer<? super T,? super U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   861
                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   862
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   863
            public <T,U,V> CompletableFuture<V> thenCombine
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
                 BiFunction<? super T,? super U,? extends V> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   867
                return f.thenCombineAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   868
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   869
            public <T> CompletableFuture<Void> runAfterEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   870
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   871
                 CompletionStage<?> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   872
                 java.lang.Runnable a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   873
                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   874
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   875
            public <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
                return f.acceptEitherAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   880
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   881
            public <T,U> CompletableFuture<U> applyToEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   882
                (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   883
                 CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   884
                 Function<? super T,U> a) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   885
                return f.applyToEitherAsync(g, a, new ThreadExecutor());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   886
            }
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   887
            public <T> CompletableFuture<T> exceptionally
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   888
                (CompletableFuture<T> f,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   889
                 Function<Throwable, ? extends T> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   890
                return f.exceptionallyAsync(fn, new ThreadExecutor());
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   891
            }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   892
            public <T> CompletableFuture<T> exceptionallyCompose
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   893
                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   894
                return f.exceptionallyComposeAsync(fn, new ThreadExecutor());
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   895
            }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   896
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   897
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   898
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   899
        public abstract void checkExecutionMode();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   900
        public abstract CompletableFuture<Void> runAsync(Runnable a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   901
        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   902
        public abstract <T> CompletableFuture<Void> thenRun
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   903
            (CompletableFuture<T> f, Runnable a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   904
        public abstract <T> CompletableFuture<Void> thenAccept
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   905
            (CompletableFuture<T> f, Consumer<? super T> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   906
        public abstract <T,U> CompletableFuture<U> thenApply
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   907
            (CompletableFuture<T> f, Function<? super T,U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   908
        public abstract <T,U> CompletableFuture<U> thenCompose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   909
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   910
             Function<? super T,? extends CompletionStage<U>> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   911
        public abstract <T,U> CompletableFuture<U> handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   912
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   913
             BiFunction<? super T,Throwable,? extends U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   914
        public abstract <T> CompletableFuture<T> whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   915
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   916
             BiConsumer<? super T,? super Throwable> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   917
        public abstract <T,U> CompletableFuture<Void> runAfterBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   918
            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   919
        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   920
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   921
             CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   922
             BiConsumer<? super T,? super U> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   923
        public abstract <T,U,V> CompletableFuture<V> thenCombine
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   924
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   925
             CompletionStage<? extends U> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   926
             BiFunction<? super T,? super U,? extends V> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   927
        public abstract <T> CompletableFuture<Void> runAfterEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   928
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   929
             CompletionStage<?> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   930
             java.lang.Runnable a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   931
        public abstract <T> CompletableFuture<Void> acceptEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   932
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   933
             CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   934
             Consumer<? super T> a);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   935
        public abstract <T,U> CompletableFuture<U> applyToEither
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   936
            (CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   937
             CompletionStage<? extends T> g,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   938
             Function<? super T,U> a);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   939
        public abstract <T> CompletableFuture<T> exceptionally
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   940
            (CompletableFuture<T> f,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   941
             Function<Throwable, ? extends T> fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   942
        public abstract <T> CompletableFuture<T> exceptionallyCompose
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   943
            (CompletableFuture<T> f,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   944
             Function<Throwable, ? extends CompletionStage<T>> fn);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   945
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   946
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   947
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   948
     * exceptionally action is not invoked when source completes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   949
     * normally, and source result is propagated
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   950
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   951
    public void testExceptionally_normalCompletion() {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   952
        for (ExecutionMode m : ExecutionMode.values())
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   953
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   954
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   955
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
   956
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   957
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   958
        if (!createIncomplete) assertTrue(f.complete(v1));
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   959
        final CompletableFuture<Integer> g = m.exceptionally
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   960
            (f, (Throwable t) -> {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
   961
                ran.getAndIncrement();
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
   962
                throw new AssertionError("should not be called");
35394
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
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   965
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   966
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   967
        checkCompletedNormally(f, v1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
   968
        assertEquals(0, ran.get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   969
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   970
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   971
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   972
     * exceptionally action completes with function value on source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   973
     * exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   974
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   975
    public void testExceptionally_exceptionalCompletion() {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   976
        for (ExecutionMode m : ExecutionMode.values())
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   977
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   978
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   979
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
   980
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   981
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   982
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   983
        if (!createIncomplete) f.completeExceptionally(ex);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   984
        final CompletableFuture<Integer> g = m.exceptionally
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   985
            (f, (Throwable t) -> {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
   986
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
   987
                assertSame(t, ex);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
   988
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   989
                return v1;
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
        if (createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   992
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   993
        checkCompletedNormally(g, v1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
   994
        assertEquals(1, ran.get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   995
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   996
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   997
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   998
     * If an "exceptionally action" throws an exception, it completes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   999
     * exceptionally with that exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1000
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1001
    public void testExceptionally_exceptionalCompletionActionFailed() {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  1002
        for (ExecutionMode m : ExecutionMode.values())
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1003
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1004
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1005
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1006
        final CFException ex1 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1007
        final CFException ex2 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1008
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1009
        if (!createIncomplete) f.completeExceptionally(ex1);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  1010
        final CompletableFuture<Integer> g = m.exceptionally
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  1011
            (f, (Throwable t) -> {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  1012
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1013
                assertSame(t, ex1);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1014
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1015
                throw ex2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1016
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1017
        if (createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1018
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1019
        checkCompletedWithWrappedException(g, ex2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1020
        checkCompletedExceptionally(f, ex1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1021
        assertEquals(1, ran.get());
35394
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1024
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1025
     * whenComplete action executes on normal completion, propagating
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1026
     * source result.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1027
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1028
    public void testWhenComplete_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1029
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1030
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1031
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1032
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1033
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1034
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1035
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1036
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1037
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1038
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1039
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1040
                assertSame(result, v1);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1041
                assertNull(t);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1042
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1043
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1044
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1045
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1046
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1047
        checkCompletedNormally(f, v1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1048
        assertEquals(1, ran.get());
35394
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1051
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1052
     * whenComplete action executes on exceptional completion, propagating
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1053
     * source result.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1054
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1055
    public void testWhenComplete_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1056
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1057
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1058
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1059
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1060
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1061
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1062
        if (!createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1063
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1064
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1065
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1066
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1067
                assertNull(result);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1068
                assertSame(t, ex);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1069
                ran.getAndIncrement();
35394
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 (createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1072
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1073
        checkCompletedWithWrappedException(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1074
        checkCompletedExceptionally(f, ex);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1075
        assertEquals(1, ran.get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1076
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1077
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
     * whenComplete action executes on cancelled source, propagating
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1080
     * CancellationException.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1081
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1082
    public void testWhenComplete_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1083
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1084
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1085
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1086
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1087
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1088
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1089
        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1090
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1091
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1092
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1093
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1094
                assertNull(result);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1095
                assertTrue(t instanceof CancellationException);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1096
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1097
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1098
        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1099
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1100
        checkCompletedWithWrappedCancellationException(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1101
        checkCancelled(f);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1102
        assertEquals(1, ran.get());
35394
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
     * If a whenComplete action throws an exception when triggered by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1107
     * a normal completion, it completes exceptionally
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 testWhenComplete_sourceCompletedNormallyActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1110
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1111
        for (ExecutionMode m : ExecutionMode.values())
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
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1114
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1115
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1116
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1117
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1118
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1119
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1120
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1121
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1122
                assertSame(result, v1);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1123
                assertNull(t);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1124
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1125
                throw ex;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1126
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1127
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1128
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1129
        checkCompletedWithWrappedException(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1130
        checkCompletedNormally(f, v1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1131
        assertEquals(1, ran.get());
35394
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1135
     * If a whenComplete action throws an exception when triggered by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1136
     * a source completion that also throws an exception, the source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1137
     * exception takes precedence (unlike handle)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1138
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1139
    public void testWhenComplete_sourceFailedActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1140
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1141
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1142
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1143
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1144
        final CFException ex1 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1145
        final CFException ex2 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1146
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1147
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1148
        if (!createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1149
        final CompletableFuture<Integer> g = m.whenComplete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1150
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1151
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1152
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1153
                assertSame(t, ex1);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1154
                assertNull(result);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1155
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1156
                throw ex2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1157
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1158
        if (createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1159
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1160
        checkCompletedWithWrappedException(g, ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1161
        checkCompletedExceptionally(f, ex1);
35980
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35397
diff changeset
  1162
        if (testImplementationDetails) {
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35397
diff changeset
  1163
            assertEquals(1, ex1.getSuppressed().length);
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35397
diff changeset
  1164
            assertSame(ex2, ex1.getSuppressed()[0]);
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35397
diff changeset
  1165
        }
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1166
        assertEquals(1, ran.get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1167
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1168
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1169
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1170
     * handle action completes normally with function value on normal
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1171
     * completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1172
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1173
    public void testHandle_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1174
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1175
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1176
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1177
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1178
        final CompletableFuture<Integer> f = new CompletableFuture<>();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1179
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1180
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1181
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1182
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1183
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1184
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1185
                assertSame(result, v1);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1186
                assertNull(t);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1187
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1188
                return inc(v1);
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
        if (createIncomplete) assertTrue(f.complete(v1));
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
        checkCompletedNormally(g, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1193
        checkCompletedNormally(f, v1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1194
        assertEquals(1, ran.get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1195
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1196
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1197
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1198
     * handle action completes normally with function value on
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1199
     * exceptional completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1200
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1201
    public void testHandle_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1202
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1203
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1204
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1205
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1206
        final CompletableFuture<Integer> f = new CompletableFuture<>();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1207
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1208
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1209
        if (!createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1210
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1211
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1212
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1213
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1214
                assertNull(result);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1215
                assertSame(t, ex);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1216
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1217
                return v1;
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
        if (createIncomplete) f.completeExceptionally(ex);
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
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1222
        checkCompletedExceptionally(f, ex);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1223
        assertEquals(1, ran.get());
35394
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1226
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1227
     * handle action completes normally with function value on
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1228
     * cancelled source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1229
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1230
    public void testHandle_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1231
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1232
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1233
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1234
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1235
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1236
        final CompletableFuture<Integer> f = new CompletableFuture<>();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1237
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1238
        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1239
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1240
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1241
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1242
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1243
                assertNull(result);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1244
                assertTrue(t instanceof CancellationException);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1245
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1246
                return v1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1247
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1248
        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
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
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1251
        checkCancelled(f);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1252
        assertEquals(1, ran.get());
35394
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1255
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1256
     * If a "handle action" throws an exception when triggered by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1257
     * a normal completion, it completes exceptionally
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
    public void testHandle_sourceCompletedNormallyActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1260
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1261
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1262
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1263
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1264
        final CompletableFuture<Integer> f = new CompletableFuture<>();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1265
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1266
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1267
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1268
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1269
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1270
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1271
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1272
                assertSame(result, v1);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1273
                assertNull(t);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1274
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1275
                throw ex;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1276
            });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1277
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1278
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1279
        checkCompletedWithWrappedException(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1280
        checkCompletedNormally(f, v1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1281
        assertEquals(1, ran.get());
35394
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1284
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1285
     * If a "handle action" throws an exception when triggered by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1286
     * a source completion that also throws an exception, the action
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1287
     * exception takes precedence (unlike whenComplete)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1288
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1289
    public void testHandle_sourceFailedActionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1290
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1291
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1292
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1293
        final AtomicInteger ran = new AtomicInteger(0);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1294
        final CFException ex1 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1295
        final CFException ex2 = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1296
        final CompletableFuture<Integer> f = new CompletableFuture<>();
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
        if (!createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1299
        final CompletableFuture<Integer> g = m.handle
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1300
            (f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1301
             (Integer result, Throwable t) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1302
                m.checkExecutionMode();
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1303
                assertNull(result);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1304
                assertSame(ex1, t);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1305
                ran.getAndIncrement();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1306
                throw ex2;
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
        if (createIncomplete) f.completeExceptionally(ex1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1309
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1310
        checkCompletedWithWrappedException(g, ex2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1311
        checkCompletedExceptionally(f, ex1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  1312
        assertEquals(1, ran.get());
35394
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
     * runAsync completes after running Runnable
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 testRunAsync_normalCompletion() {
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
        final Noop r = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1326
        final CompletableFuture<Void> f = m.runAsync(r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1327
        assertNull(f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1328
        checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1329
        r.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1330
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1331
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1332
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1333
     * failing runAsync completes exceptionally after running Runnable
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1334
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1335
    public void testRunAsync_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1336
        ExecutionMode[] executionModes = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1337
            ExecutionMode.ASYNC,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1338
            ExecutionMode.EXECUTOR,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1339
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1340
        for (ExecutionMode m : executionModes)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1341
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1342
        final FailingRunnable r = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1343
        final CompletableFuture<Void> f = m.runAsync(r);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1344
        checkCompletedWithWrappedException(f, r.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1345
        r.assertInvoked();
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
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  1348
    @SuppressWarnings("FutureReturnValueIgnored")
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1349
    public void testRunAsync_rejectingExecutor() {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1350
        CountingRejectingExecutor e = new CountingRejectingExecutor();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1351
        try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1352
            CompletableFuture.runAsync(() -> {}, e);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1353
            shouldThrow();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1354
        } catch (Throwable t) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1355
            assertSame(e.ex, t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1356
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1357
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1358
        assertEquals(1, e.count.get());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1359
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1360
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1361
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1362
     * supplyAsync completes with result of supplier
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1363
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1364
    public void testSupplyAsync_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1365
        ExecutionMode[] executionModes = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1366
            ExecutionMode.ASYNC,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1367
            ExecutionMode.EXECUTOR,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1368
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1369
        for (ExecutionMode m : executionModes)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1370
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1371
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1372
        final IntegerSupplier r = new IntegerSupplier(m, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1373
        final CompletableFuture<Integer> f = m.supplyAsync(r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1374
        assertSame(v1, f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1375
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1376
        r.assertInvoked();
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1380
     * Failing supplyAsync completes exceptionally
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 testSupplyAsync_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1383
        ExecutionMode[] executionModes = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1384
            ExecutionMode.ASYNC,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1385
            ExecutionMode.EXECUTOR,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1386
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1387
        for (ExecutionMode m : executionModes)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1388
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1389
        FailingSupplier r = new FailingSupplier(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1390
        CompletableFuture<Integer> f = m.supplyAsync(r);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1391
        checkCompletedWithWrappedException(f, r.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1392
        r.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1393
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1394
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  1395
    @SuppressWarnings("FutureReturnValueIgnored")
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1396
    public void testSupplyAsync_rejectingExecutor() {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1397
        CountingRejectingExecutor e = new CountingRejectingExecutor();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1398
        try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1399
            CompletableFuture.supplyAsync(() -> null, e);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1400
            shouldThrow();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1401
        } catch (Throwable t) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1402
            assertSame(e.ex, t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1403
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1404
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1405
        assertEquals(1, e.count.get());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1406
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1407
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1408
    // seq completion methods
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
     * thenRun result completes normally after normal completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1412
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1413
    public void testThenRun_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1414
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1415
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1416
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1417
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1418
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1419
        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
  1420
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1421
        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1422
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1423
        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1424
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1425
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1426
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1427
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1428
        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1429
        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1430
        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1431
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1432
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1433
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1434
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1435
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1436
        checkCompletedNormally(h4, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1437
        checkCompletedNormally(h5, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1438
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1439
        for (Noop r : rs) r.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1440
    }}
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
     * thenRun result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1444
     * completion of source
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_exceptionalCompletion() {
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
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1449
        final CFException ex = new CFException();
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 Noop[] rs = new Noop[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 Noop(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
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1458
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1459
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1460
        assertTrue(f.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1461
        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1462
        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1463
        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1464
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1465
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1466
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1467
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1468
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1469
        checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1470
        checkCompletedWithWrappedException(h5, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1471
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1472
        for (Noop r : rs) r.assertNotInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1475
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1476
     * thenRun result completes exceptionally if source cancelled
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
    public void testThenRun_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1479
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1480
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1483
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1484
        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
  1485
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1486
        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1487
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1488
        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1489
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1490
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1491
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1492
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1493
        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1494
        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1495
        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
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
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1498
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1499
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1500
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1501
        checkCompletedWithWrappedCancellationException(h4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1502
        checkCompletedWithWrappedCancellationException(h5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1503
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1504
        for (Noop r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1505
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1506
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1507
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1508
     * thenRun result completes exceptionally if action does
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
    public void testThenRun_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1511
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1512
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1513
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1514
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1515
        final FailingRunnable[] rs = new FailingRunnable[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1516
        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
  1517
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1518
        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1519
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1520
        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1521
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1522
        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1523
        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1524
        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1525
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1526
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1527
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1528
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1529
        checkCompletedWithWrappedException(h3, rs[3].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1530
        checkCompletedWithWrappedException(h4, rs[4].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1531
        checkCompletedWithWrappedException(h5, rs[5].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1532
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1533
    }}
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1536
     * thenApply result completes normally after normal completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1537
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1538
    public void testThenApply_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1539
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1540
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1541
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1542
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1543
        final IncFunction[] rs = new IncFunction[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1544
        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
  1545
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1546
        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1547
        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1548
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1549
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1550
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1551
        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1552
        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1553
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1554
        checkCompletedNormally(h0, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1555
        checkCompletedNormally(h1, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1556
        checkCompletedNormally(h2, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1557
        checkCompletedNormally(h3, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1558
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1559
        for (IncFunction r : rs) r.assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1560
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1561
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1562
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1563
     * thenApply result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1564
     * completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1565
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1566
    public void testThenApply_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1567
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1568
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1569
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1570
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1571
        final IncFunction[] rs = new IncFunction[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1572
        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
  1573
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1574
        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1575
        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1576
        assertTrue(f.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1577
        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1578
        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
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
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1581
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1582
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1583
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1584
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1585
        for (IncFunction r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1586
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1587
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1588
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1589
     * thenApply result completes exceptionally if source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1590
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1591
    public void testThenApply_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1592
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1593
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1594
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1595
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1596
        final IncFunction[] rs = new IncFunction[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1597
        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
  1598
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1599
        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1600
        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1601
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1602
        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1603
        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1604
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1605
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1606
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1607
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1608
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1609
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1610
        for (IncFunction r : rs) r.assertNotInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1613
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1614
     * thenApply result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1615
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1616
    public void testThenApply_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1617
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1618
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1619
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1620
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1621
        final FailingFunction[] rs = new FailingFunction[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1622
        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
  1623
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1624
        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1625
        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1626
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1627
        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1628
        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1629
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1630
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1631
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1632
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1633
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1634
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1635
    }}
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1638
     * thenAccept result completes normally after normal completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1639
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1640
    public void testThenAccept_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1641
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1642
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1643
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1644
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1645
        final NoopConsumer[] rs = new NoopConsumer[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1646
        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
  1647
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1648
        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1649
        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1650
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1651
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1652
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1653
        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1654
        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1655
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1656
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1657
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1658
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1659
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1660
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1661
        for (NoopConsumer r : rs) r.assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1662
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1663
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1664
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1665
     * thenAccept result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1666
     * completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1667
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1668
    public void testThenAccept_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1669
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1670
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1671
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1672
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1673
        final NoopConsumer[] rs = new NoopConsumer[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1674
        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
  1675
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1676
        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1677
        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1678
        assertTrue(f.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1679
        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1680
        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1681
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1682
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1683
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1684
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1685
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1686
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1687
        for (NoopConsumer r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1688
    }}
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1691
     * thenAccept result completes exceptionally if source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1692
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1693
    public void testThenAccept_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1694
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1695
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1696
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1697
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1698
        final NoopConsumer[] rs = new NoopConsumer[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1699
        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
  1700
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1701
        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1702
        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1703
        assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1704
        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1705
        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1706
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1707
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1708
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1709
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1710
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1711
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1712
        for (NoopConsumer r : rs) r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1713
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1714
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
     * thenAccept result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1717
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1718
    public void testThenAccept_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1719
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1720
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1721
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1722
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1723
        final FailingConsumer[] rs = new FailingConsumer[4];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1724
        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
  1725
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1726
        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1727
        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1728
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1729
        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1730
        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1731
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1732
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1733
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1734
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1735
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1736
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1737
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1738
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1739
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1740
     * thenCombine result completes normally after normal completion
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1741
     * of sources
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1742
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1743
    public void testThenCombine_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1744
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1745
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1746
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1747
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1748
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1749
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1750
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1751
        final SubtractFunction[] rs = new SubtractFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1752
        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
  1753
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1754
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1755
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1756
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1757
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1758
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1759
        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1760
        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1761
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1762
        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1763
        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1764
        checkIncomplete(h0); rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1765
        checkIncomplete(h2); rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1766
        checkCompletedNormally(h1, subtract(w1, w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1767
        checkCompletedNormally(h3, subtract(w1, w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1768
        rs[1].assertValue(subtract(w1, w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1769
        rs[3].assertValue(subtract(w1, w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1770
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1771
        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1772
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1773
        checkCompletedNormally(h0, subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1774
        checkCompletedNormally(h2, subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1775
        checkCompletedNormally(h4, subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1776
        rs[0].assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1777
        rs[2].assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1778
        rs[4].assertValue(subtract(v1, v2));
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
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1781
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1782
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1783
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1784
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1785
     * thenCombine result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1786
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1787
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1788
    public void testThenCombine_exceptionalCompletion() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1789
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1790
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1791
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1792
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1793
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1794
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1795
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1796
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1797
        final SubtractFunction r1 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1798
        final SubtractFunction r2 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1799
        final SubtractFunction r3 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1800
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1801
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1802
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1803
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1804
            () -> fst.completeExceptionally(ex) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1805
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1806
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1807
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1808
            () -> snd.completeExceptionally(ex);
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
        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1811
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1812
        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1813
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1814
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1815
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1816
        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1817
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1818
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1819
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1820
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1821
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1822
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1823
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1824
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1825
        checkCompletedExceptionally(failFirst ? fst : snd, ex);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1828
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1829
     * thenCombine result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1830
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1831
    public void testThenCombine_sourceCancelled() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1832
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1833
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1834
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1835
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1836
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1837
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1838
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1839
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1840
        final SubtractFunction r1 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1841
        final SubtractFunction r2 = new SubtractFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1842
        final SubtractFunction r3 = new SubtractFunction(m);
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
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1845
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1846
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1847
            () -> fst.cancel(mayInterruptIfRunning) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1848
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1849
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1850
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1851
            () -> snd.cancel(mayInterruptIfRunning);
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> h1 = m.thenCombine(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1854
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1855
        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1856
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1857
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1858
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1859
        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1860
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1861
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1862
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1863
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1864
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1865
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1866
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1867
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1868
        checkCancelled(failFirst ? fst : snd);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1869
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1870
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1871
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1872
     * thenCombine result completes exceptionally if action does
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
    public void testThenCombine_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1875
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1876
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1877
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1878
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1879
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1880
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1881
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1882
        final FailingBiFunction r1 = new FailingBiFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1883
        final FailingBiFunction r2 = new FailingBiFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1884
        final FailingBiFunction r3 = new FailingBiFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1885
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1886
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1887
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1888
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1889
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1890
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1891
        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1892
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1893
        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1894
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1895
        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1896
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1897
        checkCompletedWithWrappedException(h1, r1.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1898
        checkCompletedWithWrappedException(h2, r2.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  1899
        checkCompletedWithWrappedException(h3, r3.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1900
        r1.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1901
        r2.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1902
        r3.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1903
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1904
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1905
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1906
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1907
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1908
     * thenAcceptBoth result completes normally after normal
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1909
     * completion of sources
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1910
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1911
    public void testThenAcceptBoth_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1912
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1913
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1914
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1915
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1916
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1917
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1918
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1919
        final SubtractAction r1 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1920
        final SubtractAction r2 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1921
        final SubtractAction r3 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1922
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1923
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1924
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1925
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1926
        final Integer w2 = !fFirst ? v1 : v2;
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
        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1929
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1930
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1931
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1932
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1933
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1934
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1935
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1936
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
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
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1939
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1940
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1941
        r1.assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1942
        r2.assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1943
        r3.assertValue(subtract(v1, v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1944
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1945
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1946
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1947
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1948
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1949
     * thenAcceptBoth result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1950
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1951
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1952
    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1953
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1954
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1955
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1956
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1957
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1958
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1959
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1960
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1961
        final SubtractAction r1 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1962
        final SubtractAction r2 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1963
        final SubtractAction r3 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1964
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1965
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1966
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1967
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1968
            () -> fst.completeExceptionally(ex) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1969
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1970
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1971
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1972
            () -> snd.completeExceptionally(ex);
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
        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1975
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1976
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1977
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1978
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1979
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1980
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1981
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1982
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1983
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1984
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1985
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1986
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1987
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1988
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1989
        checkCompletedExceptionally(failFirst ? fst : snd, ex);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1992
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1993
     * thenAcceptBoth result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1994
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1995
    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1996
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1997
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1998
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1999
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2000
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2001
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2002
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2003
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2004
        final SubtractAction r1 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2005
        final SubtractAction r2 = new SubtractAction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2006
        final SubtractAction r3 = new SubtractAction(m);
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
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2009
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2010
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2011
            () -> fst.cancel(mayInterruptIfRunning) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2012
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2013
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2014
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2015
            () -> snd.cancel(mayInterruptIfRunning);
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<Void> h1 = m.thenAcceptBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2018
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2019
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2020
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2021
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2022
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2023
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2024
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2025
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2026
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2027
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2028
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2029
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2030
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2031
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2032
        checkCancelled(failFirst ? fst : snd);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2033
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2034
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2035
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2036
     * thenAcceptBoth result completes exceptionally if action does
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
    public void testThenAcceptBoth_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2039
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2040
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2041
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2042
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2043
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2044
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2045
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2046
        final FailingBiConsumer r1 = new FailingBiConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2047
        final FailingBiConsumer r2 = new FailingBiConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2048
        final FailingBiConsumer r3 = new FailingBiConsumer(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2049
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2050
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2051
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2052
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2053
        final Integer w2 = !fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2054
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2055
        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2056
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2057
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2058
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2059
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2060
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2061
        checkCompletedWithWrappedException(h1, r1.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2062
        checkCompletedWithWrappedException(h2, r2.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2063
        checkCompletedWithWrappedException(h3, r3.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2064
        r1.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2065
        r2.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2066
        r3.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2067
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2068
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2069
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2070
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2071
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2072
     * runAfterBoth result completes normally after normal
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2073
     * completion of sources
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2074
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2075
    public void testRunAfterBoth_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2076
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2077
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2078
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2079
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2080
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2081
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2082
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2083
        final Noop r1 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2084
        final Noop r2 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2085
        final Noop r3 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2086
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2087
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2088
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2089
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2090
        final Integer w2 = !fFirst ? v1 : v2;
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
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2093
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2094
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2095
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2096
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2097
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2098
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2099
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2100
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
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
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2103
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2104
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2105
        r1.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2106
        r2.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2107
        r3.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2108
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2109
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2110
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2111
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2112
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2113
     * runAfterBoth result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2114
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2115
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2116
    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2117
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2118
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2119
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2120
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2121
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2122
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2123
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2124
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2125
        final Noop r1 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2126
        final Noop r2 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2127
        final Noop r3 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2128
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2129
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2130
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2131
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2132
            () -> fst.completeExceptionally(ex) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2133
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2134
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2135
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2136
            () -> snd.completeExceptionally(ex);
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
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2139
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2140
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2141
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2142
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2143
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2144
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2145
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2146
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2147
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2148
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2149
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2150
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2151
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2152
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2153
        checkCompletedExceptionally(failFirst ? fst : snd, ex);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2156
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2157
     * runAfterBoth result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2158
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2159
    public void testRunAfterBoth_sourceCancelled() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2160
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2161
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2162
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2163
        for (boolean failFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2164
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2165
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2166
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2167
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2168
        final Noop r1 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2169
        final Noop r2 = new Noop(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2170
        final Noop r3 = new Noop(m);
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
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2173
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2174
        final Callable<Boolean> complete1 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2175
            () -> fst.cancel(mayInterruptIfRunning) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2176
            () -> fst.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2177
        final Callable<Boolean> complete2 = failFirst ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2178
            () -> snd.complete(v1) :
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2179
            () -> snd.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2180
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2181
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2182
        assertTrue(complete1.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2183
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2184
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2185
        checkIncomplete(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2186
        assertTrue(complete2.call());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2187
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2188
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2189
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2190
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2191
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2192
        r1.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2193
        r2.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2194
        r3.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2195
        checkCompletedNormally(failFirst ? snd : fst, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2196
        checkCancelled(failFirst ? fst : snd);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2197
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2198
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
     * runAfterBoth result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2201
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2202
    public void testRunAfterBoth_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2203
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2204
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2205
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2206
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2207
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2208
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2209
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2210
        final FailingRunnable r1 = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2211
        final FailingRunnable r2 = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2212
        final FailingRunnable r3 = new FailingRunnable(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2213
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2214
        final CompletableFuture<Integer> fst =  fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2215
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2216
        final Integer w1 =  fFirst ? v1 : v2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2217
        final Integer w2 = !fFirst ? v1 : v2;
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
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2220
        assertTrue(fst.complete(w1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2221
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2222
        assertTrue(snd.complete(w2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2223
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2224
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2225
        checkCompletedWithWrappedException(h1, r1.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2226
        checkCompletedWithWrappedException(h2, r2.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2227
        checkCompletedWithWrappedException(h3, r3.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2228
        r1.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2229
        r2.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2230
        r3.assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2231
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2232
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2233
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2234
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2235
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2236
     * applyToEither result completes normally after normal completion
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2237
     * of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2238
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2239
    public void testApplyToEither_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2240
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2241
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2242
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2243
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2244
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2245
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2246
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2247
        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
  2248
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2249
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2250
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2251
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2252
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2253
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2254
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2255
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2256
        checkCompletedNormally(h0, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2257
        checkCompletedNormally(h1, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2258
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2259
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2260
        checkCompletedNormally(h2, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2261
        checkCompletedNormally(h3, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2262
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2263
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2264
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2265
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2266
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2267
        rs[4].assertValue(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2268
        rs[5].assertValue(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2269
        assertTrue(Objects.equals(inc(v1), h4.join()) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2270
                   Objects.equals(inc(v2), h4.join()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2271
        assertTrue(Objects.equals(inc(v1), h5.join()) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2272
                   Objects.equals(inc(v2), h5.join()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2273
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2274
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2275
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2276
        checkCompletedNormally(h0, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2277
        checkCompletedNormally(h1, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2278
        checkCompletedNormally(h2, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2279
        checkCompletedNormally(h3, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2280
        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
  2281
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2282
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2283
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2284
     * applyToEither result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2285
     * completion of either source
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
    public void testApplyToEither_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2288
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2289
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2290
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2291
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2292
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2293
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2294
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2295
        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
  2296
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2297
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2298
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2299
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2300
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2301
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2302
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2303
        f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2304
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2305
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2306
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2307
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2308
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2309
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2310
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2311
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2312
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2313
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2314
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2315
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2316
            assertEquals(inc(v1), h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2317
            rs[4].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2318
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2319
            checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2320
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2321
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2322
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2323
            assertEquals(inc(v1), h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2324
            rs[5].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2325
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2326
            checkCompletedWithWrappedException(h5, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2327
            rs[5].assertNotInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2330
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2331
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2332
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2333
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2334
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2335
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2336
        checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2337
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2338
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2339
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2340
    public void testApplyToEither_exceptionalCompletion2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2341
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2342
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2343
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2344
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2345
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2346
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2347
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2348
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2349
        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
  2350
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2351
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2352
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2353
        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2354
        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2355
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2356
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2357
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2358
        // unspecified behavior - both source completions available
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), h0.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2361
            rs[0].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
            checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2364
            rs[0].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), h1.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2368
            rs[1].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
            checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2371
            rs[1].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
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2374
            assertEquals(inc(v1), h2.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2375
            rs[2].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2376
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2377
            checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2378
            rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2379
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2380
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2381
            assertEquals(inc(v1), h3.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2382
            rs[3].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2383
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2384
            checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2385
            rs[3].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2386
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2387
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2388
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2389
        checkCompletedExceptionally(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2390
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2391
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2392
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2393
     * applyToEither result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2394
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2395
    public void testApplyToEither_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2396
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2397
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2398
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2399
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2400
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2401
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2402
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2403
        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
  2404
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2405
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2406
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2407
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2408
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2409
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2410
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2411
        f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2412
        checkCompletedWithWrappedCancellationException(h0);
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
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2415
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2416
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2417
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2418
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2419
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2420
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2421
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2422
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
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), h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2425
            rs[4].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(h4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2428
            rs[4].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
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2431
            assertEquals(inc(v1), h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2432
            rs[5].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2433
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2434
            checkCompletedWithWrappedCancellationException(h5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2435
            rs[5].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2436
        }
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
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2439
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2440
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2441
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2442
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2443
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2444
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2445
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2446
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2447
    public void testApplyToEither_sourceCancelled2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2448
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2449
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2450
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2451
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2452
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2453
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2454
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2455
        final IncFunction[] rs = new IncFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2456
        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
  2457
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2458
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2459
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2460
        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2461
        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2462
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2463
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2464
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2465
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2466
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2467
            assertEquals(inc(v1), h0.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2468
            rs[0].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2469
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2470
            checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2471
            rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2472
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2473
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2474
            assertEquals(inc(v1), h1.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2475
            rs[1].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2476
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2477
            checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2478
            rs[1].assertNotInvoked();
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
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2481
            assertEquals(inc(v1), h2.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2482
            rs[2].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2483
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2484
            checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2485
            rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2486
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2487
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2488
            assertEquals(inc(v1), h3.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2489
            rs[3].assertValue(inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2490
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2491
            checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2492
            rs[3].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2493
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2494
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2495
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2496
        checkCancelled(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2497
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2498
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2499
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2500
     * applyToEither result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2501
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2502
    public void testApplyToEither_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2503
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2504
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2505
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2506
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2507
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2508
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2509
        final FailingFunction[] rs = new FailingFunction[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2510
        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
  2511
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2512
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2513
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2514
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2515
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2516
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2517
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2518
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2519
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2520
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2521
        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
  2522
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2523
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2524
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2525
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2526
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2527
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2528
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2529
        checkCompletedWithWrappedException(h4, rs[4].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2530
        assertTrue(Objects.equals(v1, rs[4].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2531
                   Objects.equals(v2, rs[4].value));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2532
        checkCompletedWithWrappedException(h5, rs[5].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2533
        assertTrue(Objects.equals(v1, rs[5].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2534
                   Objects.equals(v2, rs[5].value));
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
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2537
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2538
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2539
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2540
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2541
     * acceptEither result completes normally after normal completion
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2542
     * of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2543
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2544
    public void testAcceptEither_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2545
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2546
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2547
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2548
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2549
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2550
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2551
        final NoopConsumer[] rs = new NoopConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2552
        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
  2553
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2554
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2555
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2556
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2557
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2558
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2559
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2560
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2561
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2562
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2563
        rs[0].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2564
        rs[1].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2565
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2566
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2567
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2568
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2569
        rs[2].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2570
        rs[3].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2571
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2572
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2573
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2574
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2575
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2576
        checkCompletedNormally(h4, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2577
        checkCompletedNormally(h5, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2578
        assertTrue(Objects.equals(v1, rs[4].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2579
                   Objects.equals(v2, rs[4].value));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2580
        assertTrue(Objects.equals(v1, rs[5].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2581
                   Objects.equals(v2, rs[5].value));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2582
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2583
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2584
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2585
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2586
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2587
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2588
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2589
        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
  2590
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2591
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2592
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2593
     * acceptEither result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2594
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2595
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2596
    public void testAcceptEither_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2597
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2598
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2599
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2600
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2601
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2602
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2603
        final NoopConsumer[] rs = new NoopConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2604
        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
  2605
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2606
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2607
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2608
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2609
        checkIncomplete(h1);
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
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2612
        f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2613
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2614
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2615
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2616
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2617
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2618
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2619
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2620
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2621
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2622
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2623
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2624
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2625
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2626
            assertNull(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2627
            rs[4].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2628
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2629
            checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2630
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2631
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2632
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2633
            assertNull(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2634
            rs[5].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2635
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2636
            checkCompletedWithWrappedException(h5, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2637
            rs[5].assertNotInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2640
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2641
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2642
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2643
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2644
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2645
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2646
        checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2647
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2648
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2649
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2650
    public void testAcceptEither_exceptionalCompletion2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2651
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2652
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2653
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2654
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2655
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2656
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2657
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2658
        final NoopConsumer[] rs = new NoopConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2659
        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
  2660
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2661
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2662
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2663
        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2664
        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2665
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2666
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2667
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2668
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2669
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2670
            assertNull(h0.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2671
            rs[0].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2672
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2673
            checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2674
            rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2675
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2676
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2677
            assertNull(h1.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2678
            rs[1].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2679
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2680
            checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2681
            rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2682
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2683
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2684
            assertNull(h2.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2685
            rs[2].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2686
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2687
            checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2688
            rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2689
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2690
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2691
            assertNull(h3.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2692
            rs[3].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2693
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2694
            checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2695
            rs[3].assertNotInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2698
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2699
        checkCompletedExceptionally(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2700
    }}
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2703
     * acceptEither result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2704
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2705
    public void testAcceptEither_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2706
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2707
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2708
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2709
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2710
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2711
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2712
        final NoopConsumer[] rs = new NoopConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2713
        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
  2714
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2715
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2716
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2717
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2718
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2719
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2720
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2721
        f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2722
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2723
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2724
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2725
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2726
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2727
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2728
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2729
        g.complete(v1);
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
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2732
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2733
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2734
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2735
            assertNull(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2736
            rs[4].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2737
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2738
            checkCompletedWithWrappedCancellationException(h4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2739
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2740
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2741
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2742
            assertNull(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2743
            rs[5].assertValue(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2744
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2745
            checkCompletedWithWrappedCancellationException(h5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2746
            rs[5].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2747
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2748
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2749
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2750
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2751
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2752
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2753
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2754
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2755
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2756
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2757
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2758
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2759
     * acceptEither result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2760
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2761
    public void testAcceptEither_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2762
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2763
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2764
        for (Integer v2 : new Integer[] { 2, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2765
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2766
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2767
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2768
        final FailingConsumer[] rs = new FailingConsumer[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2769
        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
  2770
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2771
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2772
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2773
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2774
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2775
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2776
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2777
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2778
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2779
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2780
        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
  2781
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2782
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2783
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2784
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2785
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2786
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2787
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2788
        checkCompletedWithWrappedException(h4, rs[4].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2789
        assertTrue(Objects.equals(v1, rs[4].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2790
                   Objects.equals(v2, rs[4].value));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2791
        checkCompletedWithWrappedException(h5, rs[5].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2792
        assertTrue(Objects.equals(v1, rs[5].value) ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2793
                   Objects.equals(v2, rs[5].value));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2794
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2795
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2796
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2797
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2798
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2799
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2800
     * runAfterEither result completes normally after normal completion
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2801
     * of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2802
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2803
    public void testRunAfterEither_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2804
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2805
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2806
        for (Integer v2 : new Integer[] { 2, null })
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2807
        for (boolean pushNop : new boolean[] { true, false })
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2808
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2809
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2810
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2811
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2812
        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
  2813
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2814
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2815
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2816
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2817
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2818
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2819
        rs[1].assertNotInvoked();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2820
        if (pushNop) {          // ad hoc test of intra-completion interference
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2821
            m.thenRun(f, () -> {});
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2822
            m.thenRun(g, () -> {});
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  2823
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2824
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2825
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2826
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2827
        rs[0].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2828
        rs[1].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2829
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2830
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2831
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2832
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2833
        rs[2].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2834
        rs[3].assertInvoked();
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
        g.complete(v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2837
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2838
        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2839
        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2840
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2841
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2842
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2843
        checkCompletedNormally(h0, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2844
        checkCompletedNormally(h1, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2845
        checkCompletedNormally(h2, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2846
        checkCompletedNormally(h3, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2847
        checkCompletedNormally(h4, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2848
        checkCompletedNormally(h5, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2849
        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2852
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2853
     * runAfterEither result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2854
     * completion of either source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2855
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2856
    public void testRunAfterEither_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2857
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2858
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2859
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2860
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2861
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2862
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2863
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2864
        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
  2865
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2866
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2867
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2868
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2869
        checkIncomplete(h1);
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
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2872
        assertTrue(f.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2873
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2874
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2875
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2876
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2877
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2878
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2879
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2880
        assertTrue(g.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2881
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2882
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2883
        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2884
        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2885
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2886
            assertNull(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2887
            rs[4].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2888
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2889
            checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2890
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2891
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2892
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2893
            assertNull(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2894
            rs[5].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2895
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2896
            checkCompletedWithWrappedException(h5, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2897
            rs[5].assertNotInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2900
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2901
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2902
        checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2903
        checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2904
        checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2905
        checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2906
        checkCompletedWithWrappedException(h4, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2907
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2908
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2909
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2910
    public void testRunAfterEither_exceptionalCompletion2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2911
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2912
        for (boolean fFirst : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2913
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2914
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2915
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2916
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2917
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2918
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2919
        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
  2920
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2921
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2922
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2923
        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2924
        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2925
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2926
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2927
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2928
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2929
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2930
            assertNull(h0.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2931
            rs[0].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2932
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2933
            checkCompletedWithWrappedException(h0, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2934
            rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2935
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2936
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2937
            assertNull(h1.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2938
            rs[1].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2939
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2940
            checkCompletedWithWrappedException(h1, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2941
            rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2942
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2943
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2944
            assertNull(h2.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2945
            rs[2].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2946
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2947
            checkCompletedWithWrappedException(h2, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2948
            rs[2].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2949
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2950
        try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43522
diff changeset
  2951
            assertNull(h3.join());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2952
            rs[3].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2953
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2954
            checkCompletedWithWrappedException(h3, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2955
            rs[3].assertNotInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2958
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2959
        checkCompletedExceptionally(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2960
    }}
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2963
     * runAfterEither result completes exceptionally if either source cancelled
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2964
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2965
    public void testRunAfterEither_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2966
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2967
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2968
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2969
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2970
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2971
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2972
        final Noop[] rs = new Noop[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2973
        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
  2974
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2975
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2976
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2977
        checkIncomplete(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2978
        checkIncomplete(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2979
        rs[0].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2980
        rs[1].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2981
        f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2982
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2983
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2984
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2985
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2986
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2987
        checkCompletedWithWrappedCancellationException(h3);
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
        assertTrue(g.complete(v1));
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
        // unspecified behavior - both source completions available
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2992
        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2993
        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2994
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2995
            assertNull(h4.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2996
            rs[4].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2997
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2998
            checkCompletedWithWrappedCancellationException(h4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2999
            rs[4].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3000
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3001
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3002
            assertNull(h5.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3003
            rs[5].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3004
        } catch (CompletionException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3005
            checkCompletedWithWrappedCancellationException(h5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3006
            rs[5].assertNotInvoked();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3009
        checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3010
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3011
        checkCompletedWithWrappedCancellationException(h0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3012
        checkCompletedWithWrappedCancellationException(h1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3013
        checkCompletedWithWrappedCancellationException(h2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3014
        checkCompletedWithWrappedCancellationException(h3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3015
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3016
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3017
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3018
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3019
     * runAfterEither result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3020
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3021
    public void testRunAfterEither_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3022
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3023
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3024
        for (Integer v2 : new Integer[] { 2, null })
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
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3027
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3028
        final FailingRunnable[] rs = new FailingRunnable[6];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3029
        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
  3030
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3031
        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3032
        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3033
        assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3034
        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3035
        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3036
        checkCompletedWithWrappedException(h0, rs[0].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3037
        checkCompletedWithWrappedException(h1, rs[1].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3038
        checkCompletedWithWrappedException(h2, rs[2].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3039
        checkCompletedWithWrappedException(h3, rs[3].ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3040
        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3041
        assertTrue(g.complete(v2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3042
        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3043
        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3044
        checkCompletedWithWrappedException(h4, rs[4].ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3045
        checkCompletedWithWrappedException(h5, rs[5].ex);
35394
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
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3048
        checkCompletedNormally(g, v2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3049
        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3050
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3051
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3052
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3053
     * thenCompose result completes normally after normal completion of source
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3054
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3055
    public void testThenCompose_normalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3056
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3057
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3058
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3059
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3060
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3061
        final CompletableFutureInc r = new CompletableFutureInc(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3062
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3063
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3064
        if (createIncomplete) assertTrue(f.complete(v1));
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
        checkCompletedNormally(g, inc(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3067
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3068
        r.assertValue(v1);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3071
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3072
     * thenCompose result completes exceptionally after exceptional
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3073
     * completion of source
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
    public void testThenCompose_exceptionalCompletion() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3076
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3077
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3078
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3079
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3080
        final CompletableFutureInc r = new CompletableFutureInc(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3081
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3082
        if (!createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3083
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3084
        if (createIncomplete) f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3085
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3086
        checkCompletedWithWrappedException(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3087
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3088
        r.assertNotInvoked();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3089
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3090
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3091
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3092
     * thenCompose result completes exceptionally if action does
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3093
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3094
    public void testThenCompose_actionFailed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3095
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3096
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3097
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3098
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3099
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3100
        final FailingCompletableFutureFunction r
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3101
            = new FailingCompletableFutureFunction(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3102
        if (!createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3103
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3104
        if (createIncomplete) assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3105
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3106
        checkCompletedWithWrappedException(g, r.ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3107
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3108
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3109
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3110
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3111
     * thenCompose result completes exceptionally if source cancelled
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
    public void testThenCompose_sourceCancelled() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3114
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3115
        for (boolean createIncomplete : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3116
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3119
        final CompletableFutureInc r = new CompletableFutureInc(m);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3120
        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3121
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3122
        if (createIncomplete) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3123
            checkIncomplete(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3124
            assertTrue(f.cancel(mayInterruptIfRunning));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3125
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3126
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3127
        checkCompletedWithWrappedCancellationException(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3128
        checkCancelled(f);
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3132
     * 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
  3133
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3134
    public void testThenCompose_actionReturnsFailingFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3135
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3136
        for (int order = 0; order < 6; order++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3137
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3138
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3139
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3140
        final CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3141
        final CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3142
        final CompletableFuture<Integer> h;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3143
        // Test all permutations of orders
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3144
        switch (order) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3145
        case 0:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3146
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3147
            assertTrue(g.completeExceptionally(ex));
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3148
            h = m.thenCompose(f, x -> g);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3149
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3150
        case 1:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3151
            assertTrue(f.complete(v1));
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3152
            h = m.thenCompose(f, x -> g);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3153
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3154
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3155
        case 2:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3156
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3157
            assertTrue(f.complete(v1));
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3158
            h = m.thenCompose(f, x -> g);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3159
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3160
        case 3:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3161
            assertTrue(g.completeExceptionally(ex));
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3162
            h = m.thenCompose(f, x -> g);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3163
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3164
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3165
        case 4:
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3166
            h = m.thenCompose(f, x -> g);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3167
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3168
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3169
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3170
        case 5:
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3171
            h = m.thenCompose(f, x -> g);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3172
            assertTrue(f.complete(v1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3173
            assertTrue(g.completeExceptionally(ex));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3174
            break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3175
        default: throw new AssertionError();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3176
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3177
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3178
        checkCompletedExceptionally(g, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3179
        checkCompletedWithWrappedException(h, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3180
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3181
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3182
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3183
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3184
     * exceptionallyCompose result completes normally after normal
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3185
     * completion of source
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3186
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3187
    public void testExceptionallyCompose_normalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3188
        for (ExecutionMode m : ExecutionMode.values())
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3189
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3190
        for (Integer v1 : new Integer[] { 1, null })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3191
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3192
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3193
        final ExceptionalCompletableFutureFunction r =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3194
            new ExceptionalCompletableFutureFunction(m);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3195
        if (!createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3196
        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3197
        if (createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3198
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3199
        checkCompletedNormally(f, v1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3200
        checkCompletedNormally(g, v1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3201
        r.assertNotInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3202
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3203
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3204
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3205
     * exceptionallyCompose result completes normally after exceptional
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3206
     * completion of source
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3207
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3208
    public void testExceptionallyCompose_exceptionalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3209
        for (ExecutionMode m : ExecutionMode.values())
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3210
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3211
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3212
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3213
        final ExceptionalCompletableFutureFunction r =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3214
            new ExceptionalCompletableFutureFunction(m);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3215
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3216
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3217
        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3218
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3219
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3220
        checkCompletedExceptionally(f, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3221
        checkCompletedNormally(g, r.value);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3222
        r.assertInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3223
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3224
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3225
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3226
     * exceptionallyCompose completes exceptionally on exception if action does
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3227
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3228
    public void testExceptionallyCompose_actionFailed() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3229
        for (ExecutionMode m : ExecutionMode.values())
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3230
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3231
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3232
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3233
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3234
        final FailingExceptionalCompletableFutureFunction r
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3235
            = new FailingExceptionalCompletableFutureFunction(m);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3236
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3237
        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3238
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3239
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3240
        checkCompletedExceptionally(f, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3241
        checkCompletedWithWrappedException(g, r.ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3242
        r.assertInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3243
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3244
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3245
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3246
     * exceptionallyCompose result completes exceptionally if the
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3247
     * result of the action does
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3248
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3249
    public void testExceptionallyCompose_actionReturnsFailingFuture() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3250
        for (ExecutionMode m : ExecutionMode.values())
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3251
        for (int order = 0; order < 6; order++)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3252
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3253
        final CFException ex0 = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3254
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3255
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3256
        final CompletableFuture<Integer> g = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3257
        final CompletableFuture<Integer> h;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3258
        // Test all permutations of orders
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3259
        switch (order) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3260
        case 0:
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3261
            assertTrue(f.completeExceptionally(ex0));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3262
            assertTrue(g.completeExceptionally(ex));
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3263
            h = m.exceptionallyCompose(f, x -> g);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3264
            break;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3265
        case 1:
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3266
            assertTrue(f.completeExceptionally(ex0));
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3267
            h = m.exceptionallyCompose(f, x -> g);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3268
            assertTrue(g.completeExceptionally(ex));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3269
            break;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3270
        case 2:
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3271
            assertTrue(g.completeExceptionally(ex));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3272
            assertTrue(f.completeExceptionally(ex0));
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3273
            h = m.exceptionallyCompose(f, x -> g);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3274
            break;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3275
        case 3:
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3276
            assertTrue(g.completeExceptionally(ex));
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3277
            h = m.exceptionallyCompose(f, x -> g);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3278
            assertTrue(f.completeExceptionally(ex0));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3279
            break;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3280
        case 4:
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3281
            h = m.exceptionallyCompose(f, x -> g);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3282
            assertTrue(f.completeExceptionally(ex0));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3283
            assertTrue(g.completeExceptionally(ex));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3284
            break;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3285
        case 5:
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  3286
            h = m.exceptionallyCompose(f, x -> g);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3287
            assertTrue(f.completeExceptionally(ex0));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3288
            assertTrue(g.completeExceptionally(ex));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3289
            break;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3290
        default: throw new AssertionError();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3291
        }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3292
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3293
        checkCompletedExceptionally(g, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3294
        checkCompletedWithWrappedException(h, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3295
        checkCompletedExceptionally(f, ex0);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3296
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  3297
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3298
    // other static methods
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3301
     * allOf(no component futures) returns a future completed normally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3302
     * with the value null
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3303
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3304
    public void testAllOf_empty() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3305
        CompletableFuture<Void> f = CompletableFuture.allOf();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3306
        checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3307
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3308
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
     * allOf returns a future completed normally with the value null
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3311
     * when all components complete normally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3312
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3313
    public void testAllOf_normal() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3314
        for (int k = 1; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3315
            CompletableFuture<Integer>[] fs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3316
                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3317
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3318
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3319
            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3320
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3321
                checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3322
                checkIncomplete(CompletableFuture.allOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3323
                fs[i].complete(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3324
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3325
            checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3326
            checkCompletedNormally(CompletableFuture.allOf(fs), null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3327
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3328
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3329
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3330
    public void testAllOf_normal_backwards() throws Exception {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3331
        for (int k = 1; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3332
            CompletableFuture<Integer>[] fs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3333
                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3334
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3335
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3336
            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3337
            for (int i = k - 1; i >= 0; i--) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3338
                checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3339
                checkIncomplete(CompletableFuture.allOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3340
                fs[i].complete(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3341
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3342
            checkCompletedNormally(f, null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3343
            checkCompletedNormally(CompletableFuture.allOf(fs), null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3344
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3345
    }
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
    public void testAllOf_exceptional() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3348
        for (int k = 1; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3349
            CompletableFuture<Integer>[] fs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3350
                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3351
            CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3352
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3353
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3354
            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3355
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3356
                checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3357
                checkIncomplete(CompletableFuture.allOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3358
                if (i != k / 2) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3359
                    fs[i].complete(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3360
                    checkCompletedNormally(fs[i], i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3361
                } else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3362
                    fs[i].completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3363
                    checkCompletedExceptionally(fs[i], ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3364
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3365
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3366
            checkCompletedWithWrappedException(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3367
            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3368
        }
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
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
     * anyOf(no component futures) returns an incomplete future
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3373
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3374
    public void testAnyOf_empty() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3375
        for (Integer v1 : new Integer[] { 1, null })
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<Object> f = CompletableFuture.anyOf();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3378
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3379
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3380
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3381
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3382
    }}
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3385
     * anyOf returns a future completed normally with a value when
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3386
     * a component future does
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
    public void testAnyOf_normal() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3389
        for (int k = 0; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3390
            CompletableFuture[] fs = new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3391
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3392
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3393
            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3394
            checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3395
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3396
                fs[i].complete(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3397
                checkCompletedNormally(f, 0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3398
                int x = (int) CompletableFuture.anyOf(fs).join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3399
                assertTrue(0 <= x && x <= i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3400
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3401
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3402
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3403
    public void testAnyOf_normal_backwards() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3404
        for (int k = 0; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3405
            CompletableFuture[] fs = new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3406
            for (int i = 0; i < k; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3407
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3408
            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3409
            checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3410
            for (int i = k - 1; i >= 0; i--) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3411
                fs[i].complete(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3412
                checkCompletedNormally(f, k - 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3413
                int x = (int) CompletableFuture.anyOf(fs).join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3414
                assertTrue(i <= x && x <= k - 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3415
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3416
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3417
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3418
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3419
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3420
     * anyOf result completes exceptionally when any component does.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3421
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3422
    public void testAnyOf_exceptional() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3423
        for (int k = 0; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3424
            CompletableFuture[] fs = new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3425
            CFException[] exs = new CFException[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3426
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3427
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3428
                exs[i] = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3429
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3430
            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3431
            checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3432
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3433
                fs[i].completeExceptionally(exs[i]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3434
                checkCompletedWithWrappedException(f, exs[0]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3435
                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3436
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3437
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3438
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3439
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3440
    public void testAnyOf_exceptional_backwards() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3441
        for (int k = 0; k < 10; k++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3442
            CompletableFuture[] fs = new CompletableFuture[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3443
            CFException[] exs = new CFException[k];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3444
            for (int i = 0; i < k; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3445
                fs[i] = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3446
                exs[i] = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3447
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3448
            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3449
            checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3450
            for (int i = k - 1; i >= 0; i--) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3451
                fs[i].completeExceptionally(exs[i]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3452
                checkCompletedWithWrappedException(f, exs[k - 1]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3453
                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3454
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3455
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3456
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3457
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3458
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3459
     * Completion methods throw NullPointerException with null arguments
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3460
     */
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  3461
    @SuppressWarnings("FutureReturnValueIgnored")
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3462
    public void testNPE() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3463
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3464
        CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3465
        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3466
        ThreadExecutor exec = new ThreadExecutor();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3467
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3468
        Runnable[] throwingActions = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3469
            () -> CompletableFuture.supplyAsync(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3470
            () -> CompletableFuture.supplyAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3471
            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3472
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3473
            () -> CompletableFuture.runAsync(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3474
            () -> CompletableFuture.runAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3475
            () -> CompletableFuture.runAsync(() -> {}, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3476
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3477
            () -> f.completeExceptionally(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3478
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3479
            () -> f.thenApply(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3480
            () -> f.thenApplyAsync(null),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3481
            () -> f.thenApplyAsync(x -> x, null),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3482
            () -> f.thenApplyAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3483
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3484
            () -> f.thenAccept(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3485
            () -> f.thenAcceptAsync(null),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3486
            () -> f.thenAcceptAsync(x -> {} , null),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3487
            () -> f.thenAcceptAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3488
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3489
            () -> f.thenRun(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3490
            () -> f.thenRunAsync(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3491
            () -> f.thenRunAsync(() -> {} , null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3492
            () -> f.thenRunAsync(null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3493
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3494
            () -> f.thenCombine(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3495
            () -> f.thenCombineAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3496
            () -> f.thenCombineAsync(g, null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3497
            () -> f.thenCombine(nullFuture, (x, y) -> x),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3498
            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3499
            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3500
            () -> f.thenCombineAsync(g, (x, y) -> x, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3501
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3502
            () -> f.thenAcceptBoth(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3503
            () -> f.thenAcceptBothAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3504
            () -> f.thenAcceptBothAsync(g, null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3505
            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3506
            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3507
            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3508
            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3509
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3510
            () -> f.runAfterBoth(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3511
            () -> f.runAfterBothAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3512
            () -> f.runAfterBothAsync(g, null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3513
            () -> f.runAfterBoth(nullFuture, () -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3514
            () -> f.runAfterBothAsync(nullFuture, () -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3515
            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3516
            () -> f.runAfterBothAsync(g, () -> {}, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3517
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3518
            () -> f.applyToEither(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3519
            () -> f.applyToEitherAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3520
            () -> f.applyToEitherAsync(g, null, exec),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3521
            () -> f.applyToEither(nullFuture, x -> x),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3522
            () -> f.applyToEitherAsync(nullFuture, x -> x),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3523
            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3524
            () -> f.applyToEitherAsync(g, x -> x, null),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3525
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3526
            () -> f.acceptEither(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3527
            () -> f.acceptEitherAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3528
            () -> f.acceptEitherAsync(g, null, exec),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3529
            () -> f.acceptEither(nullFuture, x -> {}),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3530
            () -> f.acceptEitherAsync(nullFuture, x -> {}),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3531
            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3532
            () -> f.acceptEitherAsync(g, x -> {}, null),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3533
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3534
            () -> f.runAfterEither(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3535
            () -> f.runAfterEitherAsync(g, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3536
            () -> f.runAfterEitherAsync(g, null, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3537
            () -> f.runAfterEither(nullFuture, () -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3538
            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3539
            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3540
            () -> f.runAfterEitherAsync(g, () -> {}, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3541
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3542
            () -> f.thenCompose(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3543
            () -> f.thenComposeAsync(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3544
            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3545
            () -> f.thenComposeAsync(null, exec),
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
            () -> f.exceptionally(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3548
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3549
            () -> f.handle(null),
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
            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3552
            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3553
            () -> CompletableFuture.allOf(f, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3554
            () -> CompletableFuture.allOf(null, f),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3555
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3556
            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3557
            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3558
            () -> CompletableFuture.anyOf(f, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3559
            () -> CompletableFuture.anyOf(null, f),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3560
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3561
            () -> f.obtrudeException(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3562
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3563
            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3564
            () -> CompletableFuture.delayedExecutor(1L, null, exec),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3565
            () -> CompletableFuture.delayedExecutor(1L, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3566
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3567
            () -> f.orTimeout(1L, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3568
            () -> f.completeOnTimeout(42, 1L, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3569
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3570
            () -> CompletableFuture.failedFuture(null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3571
            () -> CompletableFuture.failedStage(null),
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3574
        assertThrows(NullPointerException.class, throwingActions);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3575
        assertEquals(0, exec.count.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3576
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3577
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3578
    /**
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3579
     * Test submissions to an executor that rejects all tasks.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3580
     */
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3581
    public void testRejectingExecutor() {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3582
        for (Integer v : new Integer[] { 1, null })
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3583
    {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3584
        final CountingRejectingExecutor e = new CountingRejectingExecutor();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3585
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3586
        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3587
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3588
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3589
        List<CompletableFuture<?>> futures = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3590
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3591
        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3592
        srcs.add(complete);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3593
        srcs.add(incomplete);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3594
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3595
        for (CompletableFuture<Integer> src : srcs) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3596
            List<CompletableFuture<?>> fs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3597
            fs.add(src.thenRunAsync(() -> {}, e));
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3598
            fs.add(src.thenAcceptAsync(z -> {}, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3599
            fs.add(src.thenApplyAsync(z -> z, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3600
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3601
            fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3602
            fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3603
            fs.add(src.runAfterBothAsync(src, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3604
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3605
            fs.add(src.applyToEitherAsync(src, z -> z, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3606
            fs.add(src.acceptEitherAsync(src, z -> {}, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3607
            fs.add(src.runAfterEitherAsync(src, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3608
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3609
            fs.add(src.thenComposeAsync(z -> null, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3610
            fs.add(src.whenCompleteAsync((z, t) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3611
            fs.add(src.handleAsync((z, t) -> null, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3612
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3613
            for (CompletableFuture<?> future : fs) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3614
                if (src.isDone())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3615
                    checkCompletedWithWrappedException(future, e.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3616
                else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3617
                    checkIncomplete(future);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3618
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3619
            futures.addAll(fs);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3620
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3621
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3622
        {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3623
            List<CompletableFuture<?>> fs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3624
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3625
            fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3626
            fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3627
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3628
            fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3629
            fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3630
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3631
            fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3632
            fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3633
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3634
            for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3635
                checkIncomplete(future);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3636
            futures.addAll(fs);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3637
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3638
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3639
        {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3640
            List<CompletableFuture<?>> fs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3641
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3642
            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3643
            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3644
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3645
            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3646
            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3647
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3648
            fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3649
            fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3650
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3651
            for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3652
                checkCompletedWithWrappedException(future, e.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3653
            futures.addAll(fs);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3654
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3655
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3656
        incomplete.complete(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3657
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3658
        for (CompletableFuture<?> future : futures)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3659
            checkCompletedWithWrappedException(future, e.ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3660
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3661
        assertEquals(futures.size(), e.count.get());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3662
    }}
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3663
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3664
    /**
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3665
     * Test submissions to an executor that rejects all tasks, but
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3666
     * should never be invoked because the dependent future is
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3667
     * explicitly completed.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3668
     */
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3669
    public void testRejectingExecutorNeverInvoked() {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3670
        for (Integer v : new Integer[] { 1, null })
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3671
    {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3672
        final CountingRejectingExecutor e = new CountingRejectingExecutor();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3673
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3674
        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3675
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3676
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3677
        List<CompletableFuture<?>> fs = new ArrayList<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3678
        fs.add(incomplete.thenRunAsync(() -> {}, e));
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3679
        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3680
        fs.add(incomplete.thenApplyAsync(z -> z, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3681
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3682
        fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3683
        fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3684
        fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3685
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3686
        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3687
        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3688
        fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3689
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3690
        fs.add(incomplete.thenComposeAsync(z -> null, e));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3691
        fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3692
        fs.add(incomplete.handleAsync((z, t) -> null, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3693
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3694
        fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3695
        fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3696
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3697
        fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3698
        fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3699
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3700
        fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3701
        fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3702
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3703
        for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3704
            checkIncomplete(future);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3705
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3706
        for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3707
            future.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3708
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3709
        incomplete.complete(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3710
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3711
        for (CompletableFuture<?> future : fs)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3712
            checkCompletedNormally(future, null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3713
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3714
        assertEquals(0, e.count.get());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3715
    }}
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3716
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  3717
    /**
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3718
     * toCompletableFuture returns this CompletableFuture.
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
    public void testToCompletableFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3721
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3722
        assertSame(f, f.toCompletableFuture());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3723
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3724
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3725
    // jdk9
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3726
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3727
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3728
     * newIncompleteFuture returns an incomplete CompletableFuture
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3729
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3730
    public void testNewIncompleteFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3731
        for (Integer v1 : new Integer[] { 1, null })
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
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3734
        CompletableFuture<Integer> g = f.newIncompleteFuture();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3735
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3736
        checkIncomplete(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3737
        f.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3738
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3739
        checkIncomplete(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3740
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3741
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3742
        assertSame(g.getClass(), CompletableFuture.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3743
    }}
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
     * completedStage returns a completed CompletionStage
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3747
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3748
    public void testCompletedStage() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3749
        AtomicInteger x = new AtomicInteger(0);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
  3750
        AtomicReference<Throwable> r = new AtomicReference<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3751
        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3752
        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
  3753
        assertEquals(x.get(), 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3754
        assertNull(r.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3755
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3756
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3757
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3758
     * defaultExecutor by default returns the commonPool if
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3759
     * it supports more than one thread.
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
    public void testDefaultExecutor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3762
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3763
        Executor e = f.defaultExecutor();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3764
        Executor c = ForkJoinPool.commonPool();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3765
        if (ForkJoinPool.getCommonPoolParallelism() > 1)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3766
            assertSame(e, c);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3767
        else
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3768
            assertNotSame(e, c);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3769
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3770
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3771
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3772
     * failedFuture returns a CompletableFuture completed
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3773
     * exceptionally with the given Exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3774
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3775
    public void testFailedFuture() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3776
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3777
        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3778
        checkCompletedExceptionally(f, ex);
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3781
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3782
     * failedFuture(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3783
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3784
    public void testFailedFuture_null() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3785
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3786
            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3787
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3788
        } catch (NullPointerException success) {}
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
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
     * copy returns a CompletableFuture that is completed normally,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3793
     * with the same value, when source is.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3794
     */
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3795
    public void testCopy_normalCompletion() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3796
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3797
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3798
    {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3799
        CompletableFuture<Integer> f = new CompletableFuture<>();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3800
        if (!createIncomplete) assertTrue(f.complete(v1));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3801
        CompletableFuture<Integer> g = f.copy();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3802
        if (createIncomplete) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3803
            checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3804
            checkIncomplete(g);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3805
            assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3806
        }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3807
        checkCompletedNormally(f, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3808
        checkCompletedNormally(g, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3809
    }}
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3810
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3811
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3812
     * copy returns a CompletableFuture that is completed exceptionally
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3813
     * when source is.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3814
     */
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3815
    public void testCopy_exceptionalCompletion() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3816
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3817
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3818
        CFException ex = new CFException();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3819
        CompletableFuture<Integer> f = new CompletableFuture<>();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3820
        if (!createIncomplete) f.completeExceptionally(ex);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3821
        CompletableFuture<Integer> g = f.copy();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3822
        if (createIncomplete) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3823
            checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3824
            checkIncomplete(g);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3825
            f.completeExceptionally(ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3826
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3827
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3828
        checkCompletedWithWrappedException(g, ex);
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3829
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3830
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3831
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3832
     * Completion of a copy does not complete its source.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3833
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3834
    public void testCopy_oneWayPropagation() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3835
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3836
        assertTrue(f.copy().complete(1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3837
        assertTrue(f.copy().complete(null));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3838
        assertTrue(f.copy().cancel(true));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3839
        assertTrue(f.copy().cancel(false));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3840
        assertTrue(f.copy().completeExceptionally(new CFException()));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  3841
        checkIncomplete(f);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3842
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3843
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3844
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3845
     * minimalCompletionStage returns a CompletableFuture that is
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3846
     * completed normally, with the same value, when source is.
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
    public void testMinimalCompletionStage() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3849
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3850
        CompletionStage<Integer> g = f.minimalCompletionStage();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3851
        AtomicInteger x = new AtomicInteger(0);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
  3852
        AtomicReference<Throwable> r = new AtomicReference<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3853
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3854
        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
  3855
        f.complete(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3856
        checkCompletedNormally(f, 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3857
        assertEquals(x.get(), 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3858
        assertNull(r.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3859
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3860
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3861
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3862
     * minimalCompletionStage returns a CompletableFuture that is
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3863
     * completed exceptionally when source is.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3864
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3865
    public void testMinimalCompletionStage2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3866
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3867
        CompletionStage<Integer> g = f.minimalCompletionStage();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3868
        AtomicInteger x = new AtomicInteger(0);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
  3869
        AtomicReference<Throwable> r = new AtomicReference<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3870
        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
  3871
        checkIncomplete(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3872
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3873
        f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3874
        checkCompletedExceptionally(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3875
        assertEquals(x.get(), 0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3876
        assertEquals(r.get().getCause(), ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3877
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3878
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3879
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3880
     * failedStage returns a CompletionStage completed
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3881
     * exceptionally with the given Exception
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3882
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3883
    public void testFailedStage() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3884
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3885
        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3886
        AtomicInteger x = new AtomicInteger(0);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42322
diff changeset
  3887
        AtomicReference<Throwable> r = new AtomicReference<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3888
        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
  3889
        assertEquals(x.get(), 0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3890
        assertEquals(r.get(), ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3891
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3892
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3893
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3894
     * completeAsync completes with value of given supplier
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3895
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3896
    public void testCompleteAsync() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3897
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3898
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3899
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3900
        f.completeAsync(() -> v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3901
        f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3902
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3903
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3904
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3905
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3906
     * completeAsync completes exceptionally if given supplier throws
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3907
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3908
    public void testCompleteAsync2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3909
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3910
        CFException ex = new CFException();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3911
        f.completeAsync(() -> { throw ex; });
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3912
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3913
            f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3914
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3915
        } catch (CompletionException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3916
        checkCompletedWithWrappedException(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3917
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3918
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3919
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3920
     * completeAsync with given executor completes with value of given supplier
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3921
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3922
    public void testCompleteAsync3() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3923
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3924
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3925
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3926
        ThreadExecutor executor = new ThreadExecutor();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3927
        f.completeAsync(() -> v1, executor);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3928
        assertSame(v1, f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3929
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3930
        assertEquals(1, executor.count.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3931
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3932
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3933
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3934
     * completeAsync with given executor completes exceptionally if
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3935
     * given supplier throws
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3936
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3937
    public void testCompleteAsync4() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3938
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3939
        CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3940
        ThreadExecutor executor = new ThreadExecutor();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  3941
        f.completeAsync(() -> { throw ex; }, executor);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3942
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3943
            f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3944
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3945
        } catch (CompletionException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3946
        checkCompletedWithWrappedException(f, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3947
        assertEquals(1, executor.count.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3948
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3949
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3950
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3951
     * orTimeout completes with TimeoutException if not complete
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
    public void testOrTimeout_timesOut() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3954
        long timeoutMillis = timeoutMillis();
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
        long startTime = System.nanoTime();
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3957
        assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3958
        checkCompletedWithTimeoutException(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3959
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3960
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3961
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
     * orTimeout completes normally if completed before timeout
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3964
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3965
    public void testOrTimeout_completed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3966
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3967
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3968
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3969
        CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3970
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3971
        f.complete(v1);
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3972
        assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS));
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3973
        assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3974
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3975
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3976
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3977
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3978
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3979
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3980
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3981
     * completeOnTimeout completes with given value if not complete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3982
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3983
    public void testCompleteOnTimeout_timesOut() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3984
        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3985
                       () -> testCompleteOnTimeout_timesOut(null));
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
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3988
    /**
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3989
     * completeOnTimeout completes with given value if not complete
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3990
     */
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3991
    public void testCompleteOnTimeout_timesOut(Integer v) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3992
        long timeoutMillis = timeoutMillis();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3993
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3994
        long startTime = System.nanoTime();
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  3995
        assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3996
        assertSame(v, f.join());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3997
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3998
        f.complete(99);         // should have no effect
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  3999
        checkCompletedNormally(f, v);
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4003
     * completeOnTimeout has no effect if completed within timeout
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 testCompleteOnTimeout_completed() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4006
        for (Integer v1 : new Integer[] { 1, null })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4007
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4008
        CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4009
        CompletableFuture<Integer> g = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4010
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4011
        f.complete(v1);
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  4012
        assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36954
diff changeset
  4013
        assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4014
        g.complete(v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4015
        checkCompletedNormally(f, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4016
        checkCompletedNormally(g, v1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4017
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4018
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4019
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4020
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4021
     * delayedExecutor returns an executor that delays submission
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
    public void testDelayedExecutor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4024
        testInParallel(() -> testDelayedExecutor(null, null),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4025
                       () -> testDelayedExecutor(null, 1),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4026
                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4027
                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4028
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4029
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4030
    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4031
        long timeoutMillis = timeoutMillis();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4032
        // Use an "unreasonably long" long timeout to catch lingering threads
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4033
        long longTimeoutMillis = 1000 * 60 * 60 * 24;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4034
        final Executor delayer, longDelayer;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4035
        if (executor == null) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4036
            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4037
            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4038
        } else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4039
            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4040
            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4041
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4042
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4043
        CompletableFuture<Integer> f =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4044
            CompletableFuture.supplyAsync(() -> v, delayer);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4045
        CompletableFuture<Integer> g =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4046
            CompletableFuture.supplyAsync(() -> v, longDelayer);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4047
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4048
        assertNull(g.getNow(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4049
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4050
        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4051
        long millisElapsed = millisElapsedSince(startTime);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4052
        assertTrue(millisElapsed >= timeoutMillis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4053
        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4054
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4055
        checkCompletedNormally(f, v);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4056
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4057
        checkIncomplete(g);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4058
        assertTrue(g.cancel(true));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4059
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4060
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4061
    //--- tests of implementation details; not part of official tck ---
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4062
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4063
    Object resultOf(CompletableFuture<?> f) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4064
        SecurityManager sm = System.getSecurityManager();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4065
        if (sm != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4066
            try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4067
                System.setSecurityManager(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4068
            } catch (SecurityException giveUp) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4069
                return "Reflection not available";
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4070
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4071
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4072
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4073
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4074
            java.lang.reflect.Field resultField
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4075
                = CompletableFuture.class.getDeclaredField("result");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4076
            resultField.setAccessible(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4077
            return resultField.get(f);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4078
        } catch (Throwable t) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4079
            throw new AssertionError(t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4080
        } finally {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4081
            if (sm != null) System.setSecurityManager(sm);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4082
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4083
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4084
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4085
    public void testExceptionPropagationReusesResultObject() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4086
        if (!testImplementationDetails) return;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4087
        for (ExecutionMode m : ExecutionMode.values())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4088
    {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4089
        final CFException ex = new CFException();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4090
        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4091
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4092
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4093
        final Runnable noopRunnable = new Noop(m);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4094
        final Consumer<Integer> noopConsumer = new NoopConsumer(m);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4095
        final Function<Integer, Integer> incFunction = new IncFunction(m);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4096
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4097
        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4098
            = new ArrayList<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4099
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4100
        funs.add(y -> m.thenRun(y, noopRunnable));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4101
        funs.add(y -> m.thenAccept(y, noopConsumer));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4102
        funs.add(y -> m.thenApply(y, incFunction));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4103
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4104
        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4105
        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4106
        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4107
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4108
        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4109
        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4110
        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
  4111
        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
  4112
        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
  4113
        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
  4114
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4115
        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
  4116
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4117
        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4118
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4119
        funs.add(y -> CompletableFuture.allOf(y));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4120
        funs.add(y -> CompletableFuture.allOf(y, v42));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4121
        funs.add(y -> CompletableFuture.allOf(v42, y));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4122
        funs.add(y -> CompletableFuture.anyOf(y));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4123
        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4124
        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4125
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4126
        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4127
                 fun : funs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4128
            CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4129
            f.completeExceptionally(ex);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4130
            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4131
            checkCompletedWithWrappedException(src, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4132
            CompletableFuture<?> dep = fun.apply(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4133
            checkCompletedWithWrappedException(dep, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4134
            assertSame(resultOf(src), resultOf(dep));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4135
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4136
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4137
        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4138
                 fun : funs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4139
            CompletableFuture<Integer> f = new CompletableFuture<>();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4140
            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4141
            CompletableFuture<?> dep = fun.apply(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4142
            f.completeExceptionally(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4143
            checkCompletedWithWrappedException(src, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4144
            checkCompletedWithWrappedException(dep, ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4145
            assertSame(resultOf(src), resultOf(dep));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4146
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4147
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4148
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4149
        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4150
                 fun : funs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4151
            CompletableFuture<Integer> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4152
            f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4153
            checkCancelled(f);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4154
            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4155
            checkCompletedWithWrappedCancellationException(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4156
            CompletableFuture<?> dep = fun.apply(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4157
            checkCompletedWithWrappedCancellationException(dep);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4158
            assertSame(resultOf(src), resultOf(dep));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4159
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4160
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4161
        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4162
        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4163
                 fun : funs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4164
            CompletableFuture<Integer> f = new CompletableFuture<>();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4165
            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4166
            CompletableFuture<?> dep = fun.apply(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4167
            f.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4168
            checkCancelled(f);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4169
            checkCompletedWithWrappedCancellationException(src);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4170
            checkCompletedWithWrappedCancellationException(dep);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4171
            assertSame(resultOf(src), resultOf(dep));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4172
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4173
    }}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4174
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4175
    /**
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4176
     * 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
  4177
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4178
    public void testMinimalCompletionStage_minimality() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4179
        if (!testImplementationDetails) return;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4180
        Function<Method, String> toSignature =
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4181
            method -> method.getName() + Arrays.toString(method.getParameterTypes());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4182
        Predicate<Method> isNotStatic =
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4183
            method -> (method.getModifiers() & Modifier.STATIC) == 0;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4184
        List<Method> minimalMethods =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4185
            Stream.of(Object.class, CompletionStage.class)
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4186
            .flatMap(klazz -> Stream.of(klazz.getMethods()))
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4187
            .filter(isNotStatic)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4188
            .collect(Collectors.toList());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4189
        // Methods from CompletableFuture permitted NOT to throw UOE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4190
        String[] signatureWhitelist = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4191
            "newIncompleteFuture[]",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4192
            "defaultExecutor[]",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4193
            "minimalCompletionStage[]",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4194
            "copy[]",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4195
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4196
        Set<String> permittedMethodSignatures =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4197
            Stream.concat(minimalMethods.stream().map(toSignature),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4198
                          Stream.of(signatureWhitelist))
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4199
            .collect(Collectors.toSet());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4200
        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4201
            .filter(isNotStatic)
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4202
            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4203
            .collect(Collectors.toList());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4204
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4205
        List<CompletionStage<Integer>> stages = new ArrayList<>();
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4206
        CompletionStage<Integer> min =
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4207
            new CompletableFuture<Integer>().minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4208
        stages.add(min);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4209
        stages.add(min.thenApply(x -> x));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4210
        stages.add(CompletableFuture.completedStage(1));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4211
        stages.add(CompletableFuture.failedStage(new CFException()));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4212
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4213
        List<Method> bugs = new ArrayList<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4214
        for (Method method : allMethods) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4215
            Class<?>[] parameterTypes = method.getParameterTypes();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4216
            Object[] args = new Object[parameterTypes.length];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4217
            // Manufacture boxed primitives for primitive params
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4218
            for (int i = 0; i < args.length; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4219
                Class<?> type = parameterTypes[i];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4220
                if (parameterTypes[i] == boolean.class)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4221
                    args[i] = false;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4222
                else if (parameterTypes[i] == int.class)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4223
                    args[i] = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4224
                else if (parameterTypes[i] == long.class)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4225
                    args[i] = 0L;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4226
            }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4227
            for (CompletionStage<Integer> stage : stages) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4228
                try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4229
                    method.invoke(stage, args);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4230
                    bugs.add(method);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4231
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4232
                catch (java.lang.reflect.InvocationTargetException expected) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4233
                    if (! (expected.getCause() instanceof UnsupportedOperationException)) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4234
                        bugs.add(method);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4235
                        // expected.getCause().printStackTrace();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4236
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4237
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4238
                catch (ReflectiveOperationException bad) { throw new Error(bad); }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4239
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4240
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4241
        if (!bugs.isEmpty())
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4242
            throw new Error("Methods did not throw UOE: " + bugs);
35394
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
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4245
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4246
     * minimalStage.toCompletableFuture() returns a CompletableFuture that
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4247
     * is completed normally, with the same value, when source is.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4248
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4249
    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4250
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4251
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4252
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4253
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4254
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4255
        if (!createIncomplete) assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4256
        CompletableFuture<Integer> g = minimal.toCompletableFuture();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4257
        if (createIncomplete) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4258
            checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4259
            checkIncomplete(g);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4260
            assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4261
        }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4262
        checkCompletedNormally(f, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4263
        checkCompletedNormally(g, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4264
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4265
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4266
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4267
     * minimalStage.toCompletableFuture() returns a CompletableFuture that
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4268
     * is completed exceptionally when source is.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4269
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4270
    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4271
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4272
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4273
        CFException ex = new CFException();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4274
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4275
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4276
        if (!createIncomplete) f.completeExceptionally(ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4277
        CompletableFuture<Integer> g = minimal.toCompletableFuture();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4278
        if (createIncomplete) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4279
            checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4280
            checkIncomplete(g);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4281
            f.completeExceptionally(ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4282
        }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4283
        checkCompletedExceptionally(f, ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4284
        checkCompletedWithWrappedException(g, ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4285
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4286
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4287
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4288
     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4289
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4290
    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4291
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4292
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4293
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4294
        CompletionStage minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4295
        CompletableFuture<Integer> g = minimal.toCompletableFuture();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4296
        assertTrue(g.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4297
        checkCompletedNormally(g, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4298
        checkIncomplete(f);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4299
        checkIncomplete(minimal.toCompletableFuture());
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4300
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4301
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4302
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4303
     * minimalStage.toCompletableFuture().join() awaits completion
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4304
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4305
    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4306
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4307
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4308
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4309
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4310
        if (!createIncomplete) assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4311
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4312
        if (createIncomplete) assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4313
        assertEquals(v1, minimal.toCompletableFuture().join());
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4314
        assertEquals(v1, minimal.toCompletableFuture().get());
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4315
        checkCompletedNormally(minimal.toCompletableFuture(), v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4316
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4317
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4318
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4319
     * Completion of a toCompletableFuture copy of a minimal stage
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4320
     * does not complete its source.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4321
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4322
    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4323
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4324
        CompletionStage<Integer> g = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4325
        assertTrue(g.toCompletableFuture().complete(1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4326
        assertTrue(g.toCompletableFuture().complete(null));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4327
        assertTrue(g.toCompletableFuture().cancel(true));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4328
        assertTrue(g.toCompletableFuture().cancel(false));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4329
        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4330
        checkIncomplete(g.toCompletableFuture());
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4331
        f.complete(1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4332
        checkCompletedNormally(g.toCompletableFuture(), 1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4333
    }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4334
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4335
    /** Demo utility method for external reliable toCompletableFuture */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4336
    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4337
        CompletableFuture<T> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4338
        stage.handle((T t, Throwable ex) -> {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4339
                         if (ex != null) f.completeExceptionally(ex);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4340
                         else f.complete(t);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4341
                         return null;
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4342
                     });
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4343
        return f;
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4344
    }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4345
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4346
    /** Demo utility method to join a CompletionStage */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4347
    static <T> T join(CompletionStage<T> stage) {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4348
        return toCompletableFuture(stage).join();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4349
    }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4350
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4351
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4352
     * Joining a minimal stage "by hand" works
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4353
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4354
    public void testMinimalCompletionStage_join_by_hand() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4355
        for (boolean createIncomplete : new boolean[] { true, false })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4356
        for (Integer v1 : new Integer[] { 1, null })
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4357
    {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4358
        CompletableFuture<Integer> f = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4359
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4360
        CompletableFuture<Integer> g = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4361
        if (!createIncomplete) assertTrue(f.complete(v1));
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4362
        minimal.thenAccept(x -> g.complete(x));
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4363
        if (createIncomplete) assertTrue(f.complete(v1));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4364
        g.join();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4365
        checkCompletedNormally(g, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4366
        checkCompletedNormally(f, v1);
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4367
        assertEquals(v1, join(minimal));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4368
    }}
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4369
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4370
    static class Monad {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4371
        static class ZeroException extends RuntimeException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4372
            public ZeroException() { super("monadic zero"); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4373
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4374
        // "return", "unit"
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4375
        static <T> CompletableFuture<T> unit(T value) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4376
            return completedFuture(value);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4377
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4378
        // monadic zero ?
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4379
        static <T> CompletableFuture<T> zero() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4380
            return failedFuture(new ZeroException());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4381
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4382
        // >=>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4383
        static <T,U,V> Function<T, CompletableFuture<V>> compose
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4384
            (Function<T, CompletableFuture<U>> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4385
             Function<U, CompletableFuture<V>> g) {
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4386
            return x -> f.apply(x).thenCompose(g);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4387
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4388
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4389
        static void assertZero(CompletableFuture<?> f) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4390
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4391
                f.getNow(null);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  4392
                throw new AssertionError("should throw");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4393
            } catch (CompletionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4394
                assertTrue(success.getCause() instanceof ZeroException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4395
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4396
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4397
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4398
        static <T> void assertFutureEquals(CompletableFuture<T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4399
                                           CompletableFuture<T> g) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4400
            T fval = null, gval = null;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4401
            Throwable fex = null, gex = null;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4402
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4403
            try { fval = f.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4404
            catch (ExecutionException ex) { fex = ex.getCause(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4405
            catch (Throwable ex) { fex = ex; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4406
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4407
            try { gval = g.get(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4408
            catch (ExecutionException ex) { gex = ex.getCause(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4409
            catch (Throwable ex) { gex = ex; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4410
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4411
            if (fex != null || gex != null)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4412
                assertSame(fex.getClass(), gex.getClass());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4413
            else
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4414
                assertEquals(fval, gval);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4415
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4416
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4417
        static class PlusFuture<T> extends CompletableFuture<T> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4418
            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4419
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4420
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4421
        /** Implements "monadic plus". */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4422
        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4423
                                             CompletableFuture<? extends T> g) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4424
            PlusFuture<T> plus = new PlusFuture<T>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4425
            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4426
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4427
                    if (ex == null) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4428
                        if (plus.complete(result))
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4429
                            if (plus.firstFailure.get() != null)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4430
                                plus.firstFailure.set(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4431
                    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4432
                    else if (plus.firstFailure.compareAndSet(null, ex)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4433
                        if (plus.isDone())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4434
                            plus.firstFailure.set(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4435
                    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4436
                    else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4437
                        // first failure has precedence
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4438
                        Throwable first = plus.firstFailure.getAndSet(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4439
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4440
                        // may fail with "Self-suppression not permitted"
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4441
                        try { first.addSuppressed(ex); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4442
                        catch (Exception ignored) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4443
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4444
                        plus.completeExceptionally(first);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4445
                    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4446
                } catch (Throwable unexpected) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4447
                    plus.completeExceptionally(unexpected);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4448
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4449
            };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4450
            f.whenComplete(action);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4451
            g.whenComplete(action);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4452
            return plus;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4453
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4454
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4455
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4456
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4457
     * CompletableFuture is an additive monad - sort of.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4458
     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4459
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4460
    public void testAdditiveMonad() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4461
        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4462
        CompletableFuture<Long> zero = Monad.zero();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4463
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4464
        // Some mutually non-commutative functions
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4465
        Function<Long, CompletableFuture<Long>> triple
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4466
            = x -> Monad.unit(3 * x);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4467
        Function<Long, CompletableFuture<Long>> inc
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4468
            = x -> Monad.unit(x + 1);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4469
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4470
        // unit is a right identity: m >>= unit === m
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4471
        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4472
                                 inc.apply(5L));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4473
        // unit is a left identity: (unit x) >>= f === f x
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4474
        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4475
                                 inc.apply(5L));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4476
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4477
        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4478
        Monad.assertFutureEquals(
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4479
            unit.apply(5L).thenCompose(inc).thenCompose(triple),
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4480
            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
  4481
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4482
        // The case for CompletableFuture as an additive monad is weaker...
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4483
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4484
        // zero is a monadic zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4485
        Monad.assertZero(zero);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4486
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4487
        // left zero: zero >>= f === zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4488
        Monad.assertZero(zero.thenCompose(inc));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4489
        // right zero: f >>= (\x -> zero) === zero
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4490
        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4491
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4492
        // f plus zero === f
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4493
        Monad.assertFutureEquals(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4494
                                 Monad.plus(Monad.unit(5L), zero));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4495
        // zero plus f === f
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4496
        Monad.assertFutureEquals(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4497
                                 Monad.plus(zero, Monad.unit(5L)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4498
        // zero plus zero === zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4499
        Monad.assertZero(Monad.plus(zero, zero));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4500
        {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4501
            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4502
                                                   Monad.unit(8L));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4503
            // non-determinism
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4504
            assertTrue(f.get() == 5L || f.get() == 8L);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4505
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4506
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4507
        CompletableFuture<Long> godot = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4508
        // f plus godot === f (doesn't wait for godot)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4509
        Monad.assertFutureEquals(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4510
                                 Monad.plus(Monad.unit(5L), godot));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4511
        // godot plus f === f (doesn't wait for godot)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4512
        Monad.assertFutureEquals(Monad.unit(5L),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4513
                                 Monad.plus(godot, Monad.unit(5L)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4514
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4515
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4516
    /** 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
  4517
    @SuppressWarnings("FutureReturnValueIgnored")
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4518
    public void testRecursiveChains() throws Throwable {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4519
        for (ExecutionMode m : ExecutionMode.values())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4520
        for (boolean addDeadEnds : new boolean[] { true, false })
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4521
    {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4522
        final int val = 42;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4523
        final int n = expensiveTests ? 1_000 : 2;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4524
        CompletableFuture<Integer> head = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4525
        CompletableFuture<Integer> tail = head;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4526
        for (int i = 0; i < n; i++) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4527
            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4528
            tail = m.thenApply(tail, v -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4529
            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4530
            tail = m.applyToEither(tail, tail, v -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4531
            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4532
            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4533
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4534
        head.complete(val);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4535
        assertEquals(val + 3 * n, (int) tail.join());
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4536
    }}
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4537
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4538
    /**
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4539
     * A single CompletableFuture with many dependents.
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4540
     * A demo of scalability - runtime is O(n).
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4541
     */
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
  4542
    @SuppressWarnings("FutureReturnValueIgnored")
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4543
    public void testManyDependents() throws Throwable {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4544
        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
  4545
        final CompletableFuture<Void> head = new CompletableFuture<>();
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4546
        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4547
        final AtomicInteger count = new AtomicInteger(0);
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4548
        for (int i = 0; i < n; i++) {
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4549
            head.thenRun(() -> count.getAndIncrement());
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4550
            head.thenAccept(x -> count.getAndIncrement());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4551
            head.thenApply(x -> count.getAndIncrement());
36954
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4552
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4553
            head.runAfterBoth(complete, () -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4554
            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4555
            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4556
            complete.runAfterBoth(head, () -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4557
            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4558
            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4559
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4560
            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4561
            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4562
            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
  4563
            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4564
            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4565
            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
  4566
        }
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4567
        head.complete(null);
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4568
        assertEquals(5 * 3 * n, count.get());
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4569
    }
7e9b8a9b0074 8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents: 35980
diff changeset
  4570
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4571
    /** 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
  4572
    @SuppressWarnings("FutureReturnValueIgnored")
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4573
    public void testCoCompletionGarbageRetention() throws Throwable {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4574
        final int n = expensiveTests ? 1_000_000 : 10;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4575
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4576
        CompletableFuture<Integer> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4577
        for (int i = 0; i < n; i++) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4578
            f = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4579
            f.runAfterEither(incomplete, () -> {});
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4580
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4581
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4582
            f = new CompletableFuture<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4583
            f.acceptEither(incomplete, x -> {});
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4584
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4585
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4586
            f = new CompletableFuture<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4587
            f.applyToEither(incomplete, x -> x);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4588
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4589
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4590
            f = new CompletableFuture<>();
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 48843
diff changeset
  4591
            CompletableFuture.anyOf(f, incomplete);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4592
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4593
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4594
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4595
        for (int i = 0; i < n; i++) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4596
            f = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4597
            incomplete.runAfterEither(f, () -> {});
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4598
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4599
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4600
            f = new CompletableFuture<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4601
            incomplete.acceptEither(f, x -> {});
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4602
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4603
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4604
            f = new CompletableFuture<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41128
diff changeset
  4605
            incomplete.applyToEither(f, x -> x);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4606
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4607
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4608
            f = new CompletableFuture<>();
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 48843
diff changeset
  4609
            CompletableFuture.anyOf(incomplete, f);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4610
            f.complete(null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4611
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4612
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4613
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4614
    /**
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4615
     * Reproduction recipe for:
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4616
     * 8160402: Garbage retention with CompletableFuture.anyOf
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4617
     * 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
  4618
     */
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4619
    public void testAnyOfGarbageRetention() throws Throwable {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4620
        for (Integer v : new Integer[] { 1, null })
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4621
    {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4622
        final int n = expensiveTests ? 100_000 : 10;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4623
        CompletableFuture<Integer>[] fs
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4624
            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4625
        for (int i = 0; i < fs.length; i++)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4626
            fs[i] = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4627
        fs[fs.length - 1].complete(v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4628
        for (int i = 0; i < n; i++)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4629
            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4630
    }}
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4631
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4632
    /**
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4633
     * Checks for garbage retention with allOf.
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4634
     *
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4635
     * As of 2016-07, fails with OOME:
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4636
     * 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
  4637
     */
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4638
    public void testCancelledAllOfGarbageRetention() throws Throwable {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4639
        final int n = expensiveTests ? 100_000 : 10;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4640
        CompletableFuture<Integer>[] fs
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4641
            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4642
        for (int i = 0; i < fs.length; i++)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4643
            fs[i] = new CompletableFuture<>();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4644
        for (int i = 0; i < n; i++)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4645
            assertTrue(CompletableFuture.allOf(fs).cancel(false));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4646
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 38551
diff changeset
  4647
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4648
    /**
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4649
     * Checks for garbage retention when a dependent future is
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4650
     * cancelled and garbage-collected.
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4651
     * 8161600: Garbage retention when source CompletableFutures are never completed
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4652
     *
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4653
     * As of 2016-07, fails with OOME:
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4654
     * 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
  4655
     */
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4656
    public void testCancelledGarbageRetention() throws Throwable {
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4657
        final int n = expensiveTests ? 100_000 : 10;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4658
        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4659
        for (int i = 0; i < n; i++)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4660
            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4661
    }
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  4662
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4663
    /**
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4664
     * Checks for garbage retention when MinimalStage.toCompletableFuture()
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4665
     * is invoked many times.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4666
     * 8161600: Garbage retention when source CompletableFutures are never completed
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4667
     *
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4668
     * As of 2016-07, fails with OOME:
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4669
     * 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
  4670
     */
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4671
    public void testToCompletableFutureGarbageRetention() throws Throwable {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4672
        final int n = expensiveTests ? 900_000 : 10;
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4673
        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4674
        CompletionStage minimal = neverCompleted.minimalCompletionStage();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4675
        for (int i = 0; i < n; i++)
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4676
            assertTrue(minimal.toCompletableFuture().cancel(true));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4677
    }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  4678
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4679
//     static <U> U join(CompletionStage<U> stage) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4680
//         CompletableFuture<U> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4681
//         stage.whenComplete((v, ex) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4682
//             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4683
//         });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4684
//         return f.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4685
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4686
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4687
//     static <U> boolean isDone(CompletionStage<U> stage) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4688
//         CompletableFuture<U> f = new CompletableFuture<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4689
//         stage.whenComplete((v, ex) -> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4690
//             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4691
//         });
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4692
//         return f.isDone();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4693
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4694
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4695
//     static <U> U join2(CompletionStage<U> stage) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4696
//         return stage.toCompletableFuture().copy().join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4697
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4698
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4699
//     static <U> boolean isDone2(CompletionStage<U> stage) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4700
//         return stage.toCompletableFuture().copy().isDone();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4701
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  4702
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4703
    // For testing default implementations
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4704
    // Only non-default interface methods defined.
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4705
    static final class DelegatedCompletionStage<T> implements CompletionStage<T> {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4706
        final CompletableFuture<T> cf;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4707
        DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4708
        public CompletableFuture<T> toCompletableFuture() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4709
            return cf; }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4710
        public CompletionStage<Void> thenRun
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4711
            (Runnable action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4712
            return cf.thenRun(action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4713
        public CompletionStage<Void> thenRunAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4714
            (Runnable action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4715
            return cf.thenRunAsync(action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4716
        public CompletionStage<Void> thenRunAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4717
            (Runnable action,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4718
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4719
            return cf.thenRunAsync(action, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4720
        public CompletionStage<Void> thenAccept
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4721
            (Consumer<? super T> action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4722
            return cf.thenAccept(action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4723
        public CompletionStage<Void> thenAcceptAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4724
            (Consumer<? super T> action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4725
            return cf.thenAcceptAsync(action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4726
        public CompletionStage<Void> thenAcceptAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4727
            (Consumer<? super T> action,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4728
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4729
            return cf.thenAcceptAsync(action, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4730
        public <U> CompletionStage<U> thenApply
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4731
            (Function<? super T,? extends U> a) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4732
            return cf.thenApply(a); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4733
        public <U> CompletionStage<U> thenApplyAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4734
            (Function<? super T,? extends U> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4735
            return cf.thenApplyAsync(fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4736
        public <U> CompletionStage<U> thenApplyAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4737
            (Function<? super T,? extends U> fn,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4738
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4739
            return cf.thenApplyAsync(fn, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4740
        public <U,V> CompletionStage<V> thenCombine
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4741
            (CompletionStage<? extends U> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4742
             BiFunction<? super T,? super U,? extends V> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4743
            return cf.thenCombine(other, fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4744
        public <U,V> CompletionStage<V> thenCombineAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4745
            (CompletionStage<? extends U> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4746
             BiFunction<? super T,? super U,? extends V> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4747
            return cf.thenCombineAsync(other, fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4748
        public <U,V> CompletionStage<V> thenCombineAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4749
            (CompletionStage<? extends U> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4750
             BiFunction<? super T,? super U,? extends V> fn,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4751
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4752
            return cf.thenCombineAsync(other, fn, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4753
        public <U> CompletionStage<Void> thenAcceptBoth
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4754
            (CompletionStage<? extends U> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4755
             BiConsumer<? super T, ? super U> action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4756
            return cf.thenAcceptBoth(other, action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4757
        public <U> CompletionStage<Void> thenAcceptBothAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4758
            (CompletionStage<? extends U> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4759
             BiConsumer<? super T, ? super U> action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4760
            return cf.thenAcceptBothAsync(other, action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4761
        public <U> CompletionStage<Void> thenAcceptBothAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4762
            (CompletionStage<? extends U> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4763
             BiConsumer<? super T, ? super U> action,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4764
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4765
            return cf.thenAcceptBothAsync(other, action, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4766
        public CompletionStage<Void> runAfterBoth
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4767
            (CompletionStage<?> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4768
             Runnable action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4769
            return cf.runAfterBoth(other, action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4770
        public CompletionStage<Void> runAfterBothAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4771
            (CompletionStage<?> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4772
             Runnable action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4773
            return cf.runAfterBothAsync(other, action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4774
        public CompletionStage<Void> runAfterBothAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4775
            (CompletionStage<?> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4776
             Runnable action,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4777
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4778
            return cf.runAfterBothAsync(other, action, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4779
        public <U> CompletionStage<U> applyToEither
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4780
            (CompletionStage<? extends T> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4781
             Function<? super T, U> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4782
            return cf.applyToEither(other, fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4783
        public <U> CompletionStage<U> applyToEitherAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4784
            (CompletionStage<? extends T> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4785
             Function<? super T, U> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4786
            return cf.applyToEitherAsync(other, fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4787
        public <U> CompletionStage<U> applyToEitherAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4788
            (CompletionStage<? extends T> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4789
             Function<? super T, U> fn,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4790
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4791
            return cf.applyToEitherAsync(other, fn, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4792
        public CompletionStage<Void> acceptEither
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4793
            (CompletionStage<? extends T> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4794
             Consumer<? super T> action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4795
            return cf.acceptEither(other, action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4796
        public CompletionStage<Void> acceptEitherAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4797
            (CompletionStage<? extends T> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4798
             Consumer<? super T> action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4799
            return cf.acceptEitherAsync(other, action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4800
        public CompletionStage<Void> acceptEitherAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4801
            (CompletionStage<? extends T> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4802
             Consumer<? super T> action,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4803
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4804
            return cf.acceptEitherAsync(other, action, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4805
        public CompletionStage<Void> runAfterEither
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4806
            (CompletionStage<?> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4807
             Runnable action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4808
            return cf.runAfterEither(other, action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4809
        public CompletionStage<Void> runAfterEitherAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4810
            (CompletionStage<?> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4811
             Runnable action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4812
            return cf.runAfterEitherAsync(other, action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4813
        public CompletionStage<Void> runAfterEitherAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4814
            (CompletionStage<?> other,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4815
             Runnable action,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4816
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4817
            return cf.runAfterEitherAsync(other, action, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4818
        public <U> CompletionStage<U> thenCompose
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4819
            (Function<? super T, ? extends CompletionStage<U>> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4820
            return cf.thenCompose(fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4821
        public <U> CompletionStage<U> thenComposeAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4822
            (Function<? super T, ? extends CompletionStage<U>> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4823
            return cf.thenComposeAsync(fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4824
        public <U> CompletionStage<U> thenComposeAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4825
            (Function<? super T, ? extends CompletionStage<U>> fn,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4826
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4827
            return cf.thenComposeAsync(fn, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4828
        public <U> CompletionStage<U> handle
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4829
            (BiFunction<? super T, Throwable, ? extends U> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4830
            return cf.handle(fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4831
        public <U> CompletionStage<U> handleAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4832
            (BiFunction<? super T, Throwable, ? extends U> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4833
            return cf.handleAsync(fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4834
        public <U> CompletionStage<U> handleAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4835
            (BiFunction<? super T, Throwable, ? extends U> fn,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4836
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4837
            return cf.handleAsync(fn, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4838
        public CompletionStage<T> whenComplete
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4839
            (BiConsumer<? super T, ? super Throwable> action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4840
            return cf.whenComplete(action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4841
        public CompletionStage<T> whenCompleteAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4842
            (BiConsumer<? super T, ? super Throwable> action) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4843
            return cf.whenCompleteAsync(action); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4844
        public CompletionStage<T> whenCompleteAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4845
            (BiConsumer<? super T, ? super Throwable> action,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4846
             Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4847
            return cf.whenCompleteAsync(action, executor); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4848
        public CompletionStage<T> exceptionally
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4849
            (Function<Throwable, ? extends T> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4850
            return cf.exceptionally(fn); }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4851
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4852
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4853
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4854
     * default-implemented exceptionallyAsync action is not invoked when
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4855
     * source completes normally, and source result is propagated
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4856
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4857
    public void testDefaultExceptionallyAsync_normalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4858
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4859
        for (Integer v1 : new Integer[] { 1, null })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4860
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4861
        final AtomicInteger ran = new AtomicInteger(0);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4862
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4863
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4864
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4865
        if (!createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4866
        final CompletionStage<Integer> g = d.exceptionallyAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4867
            ((Throwable t) -> {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4868
                ran.getAndIncrement();
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4869
                throw new AssertionError("should not be called");
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4870
            });
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4871
        if (createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4872
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4873
        checkCompletedNormally(g.toCompletableFuture(), v1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4874
        checkCompletedNormally(f, v1);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4875
        assertEquals(0, ran.get());
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4876
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4877
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4878
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4879
     * default-implemented exceptionallyAsync action completes with
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4880
     * function value on source exception
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4881
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4882
    public void testDefaultExceptionallyAsync_exceptionalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4883
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4884
        for (Integer v1 : new Integer[] { 1, null })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4885
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4886
        final AtomicInteger ran = new AtomicInteger(0);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4887
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4888
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4889
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4890
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4891
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4892
        final CompletionStage<Integer> g = d.exceptionallyAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4893
            ((Throwable t) -> {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4894
                assertSame(t, ex);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4895
                ran.getAndIncrement();
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4896
                return v1;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4897
            });
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4898
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4899
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4900
        checkCompletedNormally(g.toCompletableFuture(), v1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4901
        checkCompletedExceptionally(f, ex);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4902
        assertEquals(1, ran.get());
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4903
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4904
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4905
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4906
     * Under default implementation, if an "exceptionally action"
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4907
     * throws an exception, it completes exceptionally with that
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4908
     * exception
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4909
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4910
    public void testDefaultExceptionallyAsync_exceptionalCompletionActionFailed() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4911
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4912
    {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4913
        final AtomicInteger ran = new AtomicInteger(0);
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4914
        final CFException ex1 = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4915
        final CFException ex2 = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4916
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4917
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4918
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4919
        if (!createIncomplete) f.completeExceptionally(ex1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4920
        final CompletionStage<Integer> g = d.exceptionallyAsync
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4921
            ((Throwable t) -> {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4922
                assertSame(t, ex1);
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4923
                ran.getAndIncrement();
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4924
                throw ex2;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4925
            });
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4926
        if (createIncomplete) f.completeExceptionally(ex1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4927
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4928
        checkCompletedWithWrappedException(g.toCompletableFuture(), ex2);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4929
        checkCompletedExceptionally(f, ex1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4930
        checkCompletedExceptionally(d.toCompletableFuture(), ex1);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51950
diff changeset
  4931
        assertEquals(1, ran.get());
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4932
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4933
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4934
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4935
     * default-implemented exceptionallyCompose result completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4936
     * normally after normal completion of source
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4937
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4938
    public void testDefaultExceptionallyCompose_normalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4939
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4940
        for (Integer v1 : new Integer[] { 1, null })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4941
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4942
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4943
        final ExceptionalCompletableFutureFunction r =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4944
            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4945
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4946
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4947
        if (!createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4948
        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4949
        if (createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4951
        checkCompletedNormally(f, v1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4952
        checkCompletedNormally(g.toCompletableFuture(), v1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4953
        r.assertNotInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4954
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4955
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4956
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4957
     * default-implemented exceptionallyCompose result completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4958
     * normally after exceptional completion of source
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4959
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4960
    public void testDefaultExceptionallyCompose_exceptionalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4961
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4962
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4963
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4964
        final ExceptionalCompletableFutureFunction r =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4965
            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4966
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4967
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4968
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4969
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4970
        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4971
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4972
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4973
        checkCompletedExceptionally(f, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4974
        checkCompletedNormally(g.toCompletableFuture(), r.value);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4975
        r.assertInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4976
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4977
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4978
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4979
     * default-implemented exceptionallyCompose completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4980
     * exceptionally on exception if action does
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4981
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4982
    public void testDefaultExceptionallyCompose_actionFailed() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4983
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4984
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4985
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4986
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4987
        final FailingExceptionalCompletableFutureFunction r
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4988
            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4989
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4990
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4991
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4992
        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4993
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4994
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4995
        checkCompletedExceptionally(f, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4996
        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4997
        r.assertInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4998
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  4999
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5000
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5001
     * default-implemented exceptionallyComposeAsync result completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5002
     * normally after normal completion of source
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5003
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5004
    public void testDefaultExceptionallyComposeAsync_normalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5005
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5006
        for (Integer v1 : new Integer[] { 1, null })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5007
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5008
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5009
        final ExceptionalCompletableFutureFunction r =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5010
            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5011
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5012
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5013
        if (!createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5014
        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5015
        if (createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5016
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5017
        checkCompletedNormally(f, v1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5018
        checkCompletedNormally(g.toCompletableFuture(), v1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5019
        r.assertNotInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5020
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5021
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5022
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5023
     * default-implemented exceptionallyComposeAsync result completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5024
     * normally after exceptional completion of source
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5025
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5026
    public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5027
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5028
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5029
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5030
        final ExceptionalCompletableFutureFunction r =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5031
            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5032
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5033
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5034
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5035
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5036
        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5037
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5038
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5039
        checkCompletedExceptionally(f, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5040
        checkCompletedNormally(g.toCompletableFuture(), r.value);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5041
        r.assertInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5042
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5043
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5044
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5045
     * default-implemented exceptionallyComposeAsync completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5046
     * exceptionally on exception if action does
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5047
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5048
    public void testDefaultExceptionallyComposeAsync_actionFailed() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5049
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5050
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5051
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5052
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5053
        final FailingExceptionalCompletableFutureFunction r
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5054
            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5055
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5056
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5057
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5058
        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5059
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5060
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5061
        checkCompletedExceptionally(f, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5062
        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5063
        r.assertInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5064
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5065
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5066
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5067
     * default-implemented exceptionallyComposeAsync result completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5068
     * normally after normal completion of source
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5069
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5070
    public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5071
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5072
        for (Integer v1 : new Integer[] { 1, null })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5073
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5074
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5075
        final ExceptionalCompletableFutureFunction r =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5076
            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5077
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5078
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5079
        if (!createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5080
        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5081
        if (createIncomplete) assertTrue(f.complete(v1));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5082
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5083
        checkCompletedNormally(f, v1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5084
        checkCompletedNormally(g.toCompletableFuture(), v1);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5085
        r.assertNotInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5086
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5087
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5088
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5089
     * default-implemented exceptionallyComposeAsync result completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5090
     * normally after exceptional completion of source
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5091
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5092
    public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5093
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5094
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5095
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5096
        final ExceptionalCompletableFutureFunction r =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5097
            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5098
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5099
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5100
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5101
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5102
        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5103
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5104
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5105
        checkCompletedExceptionally(f, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5106
        checkCompletedNormally(g.toCompletableFuture(), r.value);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5107
        r.assertInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5108
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5109
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5110
    /**
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5111
     * default-implemented exceptionallyComposeAsync completes
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5112
     * exceptionally on exception if action does
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5113
     */
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5114
    public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5115
        for (boolean createIncomplete : new boolean[] { true, false })
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5116
    {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5117
        final CFException ex = new CFException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5118
        final CompletableFuture<Integer> f = new CompletableFuture<>();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5119
        final FailingExceptionalCompletableFutureFunction r
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5120
            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5121
        final DelegatedCompletionStage<Integer> d =
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5122
            new DelegatedCompletionStage<Integer>(f);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5123
        if (!createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5124
        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5125
        if (createIncomplete) f.completeExceptionally(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5126
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5127
        checkCompletedExceptionally(f, ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5128
        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5129
        r.assertInvoked();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5130
    }}
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 50764
diff changeset
  5131
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  5132
}