From 7c259b74ea0174e109aa8b1af3baab243e39b7af Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 25 May 2024 20:22:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20add=20missing=20the=20`minimal=20stage`?= =?UTF-8?q?=20check=20=F0=9F=91=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/foldright/cffu/CompletableFutureUtils.java | 6 ++++++ .../io/foldright/cffu/CompletableFutureUtilsTest.java | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java index 1f4fa7c7..08703cb2 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -1532,6 +1532,7 @@ public static > C cffuOrTimeout( public static > 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 { @@ -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 { @@ -1745,6 +1747,7 @@ public static T join(CompletableFuture cf, long timeout, TimeUnit unit) { @Nullable public static T getSuccessNow(CompletableFuture 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; } @@ -1918,6 +1921,8 @@ C completeAsync(C cf, Supplier 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)); } @@ -1951,6 +1956,7 @@ C completeExceptionallyAsync(C cf, Supplier 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; diff --git a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java index 1e01a77d..aae9ad8a 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -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 f = (CompletableFuture) completedStage(42); + assertThrows(UnsupportedOperationException.class, () -> completeAsync(f, () -> null)); + } assertSame(rte, assertThrows(ExecutionException.class, () -> completeAsync(createIncompleteFuture(), () -> { throw rte; @@ -1181,6 +1185,10 @@ void test_write() throws Exception { assertSame(rte, assertThrows(ExecutionException.class, () -> completeExceptionallyAsync(createIncompleteFuture(), () -> rte).get() ).getCause()); + if (isJava9Plus()) { + CompletableFuture f = (CompletableFuture) completedStage(42); + assertThrows(UnsupportedOperationException.class, () -> completeExceptionallyAsync(f, () -> rte)); + } assertEquals(n, completeExceptionallyAsync(completedFuture(n), () -> rte).get()); }