Skip to content

Commit

Permalink
fix: add missing the minimal stage check 👀
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 3, 2024
1 parent be172ee commit fa0c826
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,7 @@ public static <C extends CompletableFuture<?>> C cffuOrTimeout(
public static <C extends CompletableFuture<?>> C orTimeout(C cf, long timeout, TimeUnit unit) {
requireNonNull(cf, "cf is null");
requireNonNull(unit, "unit is null");
// NOTE: No need check minimal stage, since checked at cf.orTimeout() / cf.isDone()
if (IS_JAVA9_PLUS) {
cf.orTimeout(timeout, unit);
} else {
Expand Down Expand Up @@ -1608,6 +1609,7 @@ C cffuCompleteOnTimeout(C cf, @Nullable T value, Executor executorWhenTimeout, l
C completeOnTimeout(C cf, @Nullable T value, long timeout, TimeUnit unit) {
requireNonNull(cf, "cf is null");
requireNonNull(unit, "unit is null");
// NOTE: No need check minimal stage, since checked at cf.completeOnTimeout() / cf.isDone()
if (IS_JAVA9_PLUS) {
cf.completeOnTimeout(value, timeout, unit);
} else {
Expand Down Expand Up @@ -1745,6 +1747,7 @@ public static <T> T join(CompletableFuture<T> cf, long timeout, TimeUnit unit) {
@Nullable
public static <T> T getSuccessNow(CompletableFuture<? extends T> cf, @Nullable T valueIfNotSuccess) {
requireNonNull(cf, "cf is null");
// NOTE: No need check minimal stage, since checked at cf.isDone()
return cf.isDone() && !cf.isCompletedExceptionally() ? cf.join() : valueIfNotSuccess;
}

Expand Down Expand Up @@ -1918,6 +1921,8 @@ C completeAsync(C cf, Supplier<? extends T> supplier, Executor executor) {
if (IS_JAVA9_PLUS) {
cf.completeAsync(supplier, executor);
} else {
// NOTE: No need check minimal stage, because on Java 8(not Java 9+) NOT support minimal stage

// below code is copied from CompletableFuture#completeAsync with small adoption
executor.execute(new CfCompleterBySupplier<>(cf, supplier));
}
Expand Down Expand Up @@ -1951,6 +1956,7 @@ C completeExceptionallyAsync(C cf, Supplier<? extends Throwable> supplier, Execu
requireNonNull(cf, "cf is null");
requireNonNull(supplier, "supplier is null");
requireNonNull(executor, "executor is null");
if (isMinStageCf(cf)) throw new UnsupportedOperationException();

executor.execute(new CfExCompleterBySupplier(cf, supplier));
return cf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,10 @@ void test_read() {
void test_write() throws Exception {
assertEquals(n, completeAsync(createIncompleteFuture(), () -> n).get());
assertEquals(n, completeAsync(createIncompleteFuture(), () -> n, commonPool()).get());
if (isJava9Plus()) {
CompletableFuture<Integer> f = (CompletableFuture<Integer>) completedStage(42);
assertThrows(UnsupportedOperationException.class, () -> completeAsync(f, () -> null));
}
assertSame(rte, assertThrows(ExecutionException.class, () ->
completeAsync(createIncompleteFuture(), () -> {
throw rte;
Expand All @@ -1181,6 +1185,10 @@ void test_write() throws Exception {
assertSame(rte, assertThrows(ExecutionException.class, () ->
completeExceptionallyAsync(createIncompleteFuture(), () -> rte).get()
).getCause());
if (isJava9Plus()) {
CompletableFuture<Integer> f = (CompletableFuture<Integer>) completedStage(42);
assertThrows(UnsupportedOperationException.class, () -> completeExceptionallyAsync(f, () -> rte));
}
assertEquals(n, completeExceptionallyAsync(completedFuture(n), () -> rte).get());
}

Expand Down

0 comments on commit fa0c826

Please sign in to comment.