From 594629ce8c469e69e63719a90e196f8026be987f Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 10 Aug 2024 23:21:39 +0800 Subject: [PATCH] WIP --- .../cffu/CompletableFutureUtilsTest.java | 21 ++++++++----------- .../cffu/ListenableFutureUtilsTest.java | 7 ++++--- .../CompletableFutureUsageStudyCaseTest.kt | 17 ++++----------- .../java/io/foldright/test_utils/TestUtils.kt | 15 +++++++++++++ 4 files changed, 32 insertions(+), 28 deletions(-) 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 99047459..a119c8ad 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -711,7 +711,7 @@ void test_anyOf__concurrent() throws Exception { incompleteCf(), incompleteCf(), CompletableFuture.supplyAsync(() -> { - sleep(300); + snoreZzz(); return anotherN; }), completedFuture(n) @@ -720,11 +720,11 @@ void test_anyOf__concurrent() throws Exception { // wait/success then success assertEquals(n, anyOf( CompletableFuture.supplyAsync(() -> { - sleep(300); + snoreZzz(); return anotherN; }), CompletableFuture.supplyAsync(() -> { - sleep(300); + snoreZzz(); return anotherN; }), completedFuture(n) @@ -735,7 +735,7 @@ void test_anyOf__concurrent() throws Exception { incompleteCf(), incompleteCf(), CompletableFuture.supplyAsync(() -> { - sleep(300); + snoreZzz(); throw rte; }), completedFuture(n) @@ -760,7 +760,7 @@ void test_anyOf__concurrent() throws Exception { incompleteCf(), incompleteCf(), CompletableFuture.supplyAsync(() -> { - sleep(300); + snoreZzz(); return anotherN; }), completedFuture(n) @@ -769,11 +769,11 @@ void test_anyOf__concurrent() throws Exception { // wait/success then success assertEquals(n, anySuccessOf( CompletableFuture.supplyAsync(() -> { - sleep(300); + snoreZzz(); return anotherN; }), CompletableFuture.supplyAsync(() -> { - sleep(300); + snoreZzz(); return anotherN; }), completedFuture(n) @@ -784,7 +784,7 @@ void test_anyOf__concurrent() throws Exception { incompleteCf(), incompleteCf(), CompletableFuture.supplyAsync(() -> { - sleep(300); + snoreZzz(); throw rte; }), completedFuture(n) @@ -1226,10 +1226,7 @@ void test_both() throws Exception { @Test void both_fastFail() throws Exception { - CompletableFuture cf_n = CompletableFuture.supplyAsync(() -> { - sleep(2_000); - return n; - }); + CompletableFuture cf_n = completeLaterCf(n); final CompletableFuture failed = failedFuture(rte); final CompletableFuture cf_ee = failedFuture(anotherRte); diff --git a/cffu-core/src/test/java/io/foldright/cffu/ListenableFutureUtilsTest.java b/cffu-core/src/test/java/io/foldright/cffu/ListenableFutureUtilsTest.java index 31bb5156..d38c1934 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/ListenableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/ListenableFutureUtilsTest.java @@ -12,8 +12,9 @@ import java.util.concurrent.*; import static io.foldright.cffu.ListenableFutureUtils.*; +import static io.foldright.test_utils.TestUtils.nap; +import static io.foldright.test_utils.TestUtils.snoreZzz; import static io.foldright.test_utils.TestingConstants.*; -import static io.foldright.test_utils.TestUtils.sleep; import static java.util.concurrent.CompletableFuture.completedFuture; import static org.junit.jupiter.api.Assertions.*; @@ -134,7 +135,7 @@ void test_lf2cf_setCancellationExceptionToCf_cancellationAndPropagation() throws final CompletableFuture cf = toCompletableFuture(lf, executorService, true); assertTrue(cf.completeExceptionally(new IllegalArgumentException())); - sleep(100); + snoreZzz(); waitForAllCfsToComplete(cf); assertFalse(lf.isDone()); @@ -159,7 +160,7 @@ void test_lf2cf_dependentLf() throws Exception { toCompletableFuture(lf, MoreExecutors.directExecutor(), true)); cf.cancel(false); - sleep(20); + nap(); assertTrue(cf.isCancelled()); assertThrowsExactly(CancellationException.class, cf::get); diff --git a/cffu-core/src/test/java/io/foldright/study/CompletableFutureUsageStudyCaseTest.kt b/cffu-core/src/test/java/io/foldright/study/CompletableFutureUsageStudyCaseTest.kt index 5f8510a3..f68549b4 100644 --- a/cffu-core/src/test/java/io/foldright/study/CompletableFutureUsageStudyCaseTest.kt +++ b/cffu-core/src/test/java/io/foldright/study/CompletableFutureUsageStudyCaseTest.kt @@ -322,20 +322,14 @@ class CompletableFutureUsageStudyCaseTest : FunSpec({ } test("timeout control: normally completed with replacement value").config(enabledIf = java9Plus) { - val f = CompletableFuture.supplyAsync { - sleep(1000) - n - }.completeOnTimeout(anotherN, 1, TimeUnit.MILLISECONDS) + val f = completeLaterCf(n) + .completeOnTimeout(anotherN, 1, TimeUnit.MILLISECONDS) f.await() shouldBe anotherN } test("timeout control: exceptionally completed with java.util.concurrent.TimeoutException").config(enabledIf = java9Plus) { - val f = CompletableFuture - .supplyAsync { - sleep(1000) - n - } + val f = completeLaterCf(n) .orTimeout(1, TimeUnit.MILLISECONDS) .exceptionally { it.shouldBeTypeOf() @@ -363,10 +357,7 @@ class CompletableFutureUsageStudyCaseTest : FunSpec({ xtest("performance CF then*").config(invocations = 10) { val times = 1_000_000 - var f = CompletableFuture.supplyAsync( - { sleep(); currentTimeMillis() }, - testThreadPoolExecutor - ) + var f = completeLaterCf({ currentTimeMillis() }, executor = testForkJoinPoolExecutor) val tick = currentTimeMillis() repeat(times) { diff --git a/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt b/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt index 2383d5d7..cf75b7de 100644 --- a/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt +++ b/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt @@ -14,6 +14,7 @@ import io.kotest.matchers.shouldBe import org.apache.commons.lang3.JavaVersion import org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast import java.util.concurrent.* +import java.util.function.Supplier //////////////////////////////////////////////////////////////////////////////// @@ -28,6 +29,20 @@ fun completeLaterCf(value: T, millis: Long = 100): CompletableFuture = Co value } +private val DEFAULT_EXECUTOR: Executor = Executor { /* do nothing */ } + +@JvmOverloads +fun completeLaterCf( + value: () -> T, millis: Long = 100, executor: Executor = DEFAULT_EXECUTOR +): CompletableFuture { + val action = Supplier { + sleep(millis) + value() + } + return if (executor === DEFAULT_EXECUTOR) CompletableFuture.supplyAsync(action) + else CompletableFuture.supplyAsync(action, executor) +} + @JvmOverloads fun cancelledFuture(mayInterruptIfRunning: Boolean = false): CompletableFuture = CompletableFuture().apply { cancel(mayInterruptIfRunning)