From 177eb2beea93f17daafd384da2b3243603765ae8 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 31 Aug 2024 19:54:04 +0800 Subject: [PATCH] test: adjust wait time for more stable testing --- .../io/foldright/cffu/CffuFactoryTest.java | 45 ++-- .../test/java/io/foldright/cffu/CffuTest.java | 73 +++--- .../java/io/foldright/cffu/CffuTestHelper.kt | 1 + .../cffu/CompletableFutureUtilsTest.java | 223 +++++++++--------- .../cffu/ListenableFutureUtilsTest.java | 9 +- .../java/io/foldright/test_utils/TestUtils.kt | 14 +- .../foldright/test_utils/TestingConstants.kt | 4 + 7 files changed, 189 insertions(+), 180 deletions(-) diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java index 7357f738..a04d3aad 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -24,6 +24,7 @@ import static io.foldright.test_utils.TestingExecutorUtils.testExecutor; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.ForkJoinPool.commonPool; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.function.Function.identity; import static org.junit.jupiter.api.Assertions.*; @@ -82,8 +83,8 @@ void test_mSupply() throws Exception { testCffuFac.mSupplyFastFailAsync(testExecutor, supplier, supplier), testCffuFac.mSupplyAllSuccessAsync(anotherN, supplier, supplier), testCffuFac.mSupplyAllSuccessAsync(anotherN, testExecutor, supplier, supplier), - testCffuFac.mSupplyMostSuccessAsync(anotherN, 500, TimeUnit.MILLISECONDS, supplier, supplier), - testCffuFac.mSupplyMostSuccessAsync(anotherN, testExecutor, 500, TimeUnit.MILLISECONDS, supplier, supplier), + testCffuFac.mSupplyMostSuccessAsync(anotherN, LONG_WAIT_MS, MILLISECONDS, supplier, supplier), + testCffuFac.mSupplyMostSuccessAsync(anotherN, testExecutor, LONG_WAIT_MS, MILLISECONDS, supplier, supplier), testCffuFac.mSupplyAsync(supplier, supplier), testCffuFac.mSupplyAsync(testExecutor, supplier, supplier), }; @@ -172,17 +173,17 @@ void test_tupleMSupplyMostSuccessAsync() throws Exception { nap(); return nnn; }; - assertEquals(Tuple2.of(n, s), testCffuFac.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get()); - assertEquals(Tuple2.of(n, s), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get()); + assertEquals(Tuple2.of(n, s), testCffuFac.tupleMSupplyMostSuccessAsync(100, MILLISECONDS, supplier_n, supplier_s).get()); + assertEquals(Tuple2.of(n, s), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, MILLISECONDS, supplier_n, supplier_s).get()); - assertEquals(Tuple3.of(n, s, d), testCffuFac.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); - assertEquals(Tuple3.of(n, s, d), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); + assertEquals(Tuple3.of(n, s, d), testCffuFac.tupleMSupplyMostSuccessAsync(100, MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); + assertEquals(Tuple3.of(n, s, d), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); - assertEquals(Tuple4.of(n, s, d, anotherN), testCffuFac.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); - assertEquals(Tuple4.of(n, s, d, anotherN), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), testCffuFac.tupleMSupplyMostSuccessAsync(100, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); - assertEquals(Tuple5.of(n, s, d, anotherN, nnn), testCffuFac.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); - assertEquals(Tuple5.of(n, s, d, anotherN, nnn), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, nnn), testCffuFac.tupleMSupplyMostSuccessAsync(100, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, nnn), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); } // endregion @@ -295,22 +296,22 @@ void test_mostOf() throws Exception { final Cffu cancelled = testCffuFac.toCffu(cancelledFuture()); final Cffu incomplete = testCffuFac.toCffu(incompleteCf()); - assertEquals(0, testCffuFac.mostSuccessResultsOf(null, 10, TimeUnit.MILLISECONDS).get().size()); + assertEquals(0, testCffuFac.mostSuccessResultsOf(null, SHORT_WAIT_MS, MILLISECONDS).get().size()); assertEquals(Arrays.asList(n, null, null, null), testCffuFac.mostSuccessResultsOf( - null, 10, TimeUnit.MILLISECONDS, completed, failed, cancelled, incomplete + null, SHORT_WAIT_MS, MILLISECONDS, completed, failed, cancelled, incomplete ).get()); assertEquals(Arrays.asList(n, anotherN, anotherN, anotherN), testCffuFac.mostSuccessResultsOf( - anotherN, 10, TimeUnit.MILLISECONDS, completedStage, failed, cancelled, incomplete + anotherN, SHORT_WAIT_MS, MILLISECONDS, completedStage, failed, cancelled, incomplete ).get()); assertEquals(Arrays.asList(anotherN, anotherN, anotherN), testCffuFac.mostSuccessResultsOf( - anotherN, 10, TimeUnit.MILLISECONDS, failed, cancelled, incomplete + anotherN, SHORT_WAIT_MS, MILLISECONDS, failed, cancelled, incomplete ).get()); // do not wait for failed and cancelled assertEquals(Arrays.asList(anotherN, anotherN), testCffuFac.mostSuccessResultsOf( - anotherN, 10, TimeUnit.DAYS, failed, cancelled + anotherN, Long.MAX_VALUE, MILLISECONDS, failed, cancelled ).get()); } @@ -599,28 +600,28 @@ void test_mostSuccessTupleOf() throws Exception { final Cffu incomplete = testCffuFac.toCffu(incompleteCf()); assertEquals(Tuple2.of(n, s), testCffuFac.mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, completed, anotherCompleted + SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted ).get()); assertEquals(Tuple2.of(n, null), testCffuFac.mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, completed, failed + SHORT_WAIT_MS, MILLISECONDS, completed, failed ).get()); assertEquals(Tuple3.of(n, s, null), testCffuFac.mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled + SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted, cancelled ).get()); assertEquals(Tuple3.of(null, null, s), testCffuFac.mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, incomplete, failed, anotherCompleted + SHORT_WAIT_MS, MILLISECONDS, incomplete, failed, anotherCompleted ).get()); assertEquals(Tuple4.of(n, s, null, null), testCffuFac.mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled, incomplete + SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted, cancelled, incomplete ).get()); assertEquals(Tuple4.of(null, null, null, null), testCffuFac.mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, incomplete, failed, cancelled, incomplete + SHORT_WAIT_MS, MILLISECONDS, incomplete, failed, cancelled, incomplete ).get()); assertEquals(Tuple5.of(null, n, s, null, null), testCffuFac.mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed + SHORT_WAIT_MS, MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed ).get()); } diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java index 542aff60..8e7f46ba 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java @@ -23,6 +23,7 @@ import static io.foldright.test_utils.TestingExecutorUtils.testCffuFac; import static io.foldright.test_utils.TestingExecutorUtils.testExecutor; import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.function.Function.identity; import static org.junit.jupiter.api.Assertions.*; @@ -67,8 +68,8 @@ void test_thenMApply() throws Exception { completed.thenMApplyFastFailAsync(testExecutor, function_n, function_n), completed.thenMApplyAllSuccessAsync(anotherN, function_n, function_n), completed.thenMApplyAllSuccessAsync(anotherN, testExecutor, function_n, function_n), - completed.thenMApplyMostSuccessAsync(100, 500, TimeUnit.MILLISECONDS, function_n, function_n), - completed.thenMApplyMostSuccessAsync(100, testExecutor, 500, TimeUnit.MILLISECONDS, function_n, function_n), + completed.thenMApplyMostSuccessAsync(anotherN, LONG_WAIT_MS, MILLISECONDS, function_n, function_n), + completed.thenMApplyMostSuccessAsync(anotherN, testExecutor, LONG_WAIT_MS, MILLISECONDS, function_n, function_n), completed.thenMApplyAsync(function_n, function_n), completed.thenMApplyAsync(testExecutor, function_n, function_n) }; @@ -194,14 +195,14 @@ void test_thenTupleMApplyAsync() throws Exception { assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyAllSuccessAsync(function_n, function_s, function_d, function_an, function_nnn).get()); assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyAllSuccessAsync(testExecutor, function_n, function_s, function_d, function_an, function_nnn).get()); - assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s).get()); - assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, function_n, function_s).get()); - assertEquals(Tuple3.of(n + n, s + n, d + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get()); - assertEquals(Tuple3.of(n + n, s + n, d + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get()); - assertEquals(Tuple4.of(n + n, s + n, d + n, anotherN + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get()); - assertEquals(Tuple4.of(n + n, s + n, d + n, anotherN + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get()); - assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nnn).get()); - assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nnn).get()); + assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyMostSuccessAsync(MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s).get()); + assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s).get()); + assertEquals(Tuple3.of(n + n, s + n, d + n), completed.thenTupleMApplyMostSuccessAsync(MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d).get()); + assertEquals(Tuple3.of(n + n, s + n, d + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d).get()); + assertEquals(Tuple4.of(n + n, s + n, d + n, anotherN + n), completed.thenTupleMApplyMostSuccessAsync(MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an).get()); + assertEquals(Tuple4.of(n + n, s + n, d + n, anotherN + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an).get()); + assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyMostSuccessAsync(MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an, function_nnn).get()); + assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an, function_nnn).get()); assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyAsync(function_n, function_s).get()); assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyAsync(testExecutor, function_n, function_s).get()); @@ -221,7 +222,7 @@ void test_thenTupleMApplyAsync() throws Exception { @Test void both_fastFail() throws Exception { Cffu cf = testCffuFac.supplyAsync(() -> { - snoreZzz(2_000); + snoreZzz(LONG_WAIT_MS); return n; }); final Cffu failed = testCffuFac.failedFuture(rte); @@ -229,35 +230,35 @@ void both_fastFail() throws Exception { final Runnable runnable = () -> { }; assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.runAfterBothFastFail(failed, runnable).get(1, TimeUnit.MILLISECONDS) + cf.runAfterBothFastFail(failed, runnable).get(1, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.runAfterBothFastFailAsync(failed, runnable).get(1, TimeUnit.MILLISECONDS) + cf.runAfterBothFastFailAsync(failed, runnable).get(1, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.runAfterBothFastFailAsync(failed, runnable, testExecutor).get(1, TimeUnit.MILLISECONDS) + cf.runAfterBothFastFailAsync(failed, runnable, testExecutor).get(1, MILLISECONDS) ).getCause()); BiConsumer bc = (i1, i2) -> { }; assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.thenAcceptBothFastFail(failed, bc).get(1, TimeUnit.MILLISECONDS) + cf.thenAcceptBothFastFail(failed, bc).get(1, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.thenAcceptBothFastFailAsync(failed, bc).get(1, TimeUnit.MILLISECONDS) + cf.thenAcceptBothFastFailAsync(failed, bc).get(1, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.thenAcceptBothFastFailAsync(failed, bc, testExecutor).get(1, TimeUnit.MILLISECONDS) + cf.thenAcceptBothFastFailAsync(failed, bc, testExecutor).get(1, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.thenCombineFastFail(failed, Integer::sum).get(1, TimeUnit.MILLISECONDS) + cf.thenCombineFastFail(failed, Integer::sum).get(1, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.thenCombineFastFailAsync(failed, Integer::sum).get(1, TimeUnit.MILLISECONDS) + cf.thenCombineFastFailAsync(failed, Integer::sum).get(1, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cf.thenCombineFastFailAsync(failed, Integer::sum, testExecutor).get(1, TimeUnit.MILLISECONDS) + cf.thenCombineFastFailAsync(failed, Integer::sum, testExecutor).get(1, MILLISECONDS) ).getCause()); } @@ -325,32 +326,32 @@ void test_catching() throws Exception { @Test void test_timeout() throws Exception { assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - testCffuFac.newIncompleteCffu().orTimeout(1, TimeUnit.MILLISECONDS).get() + testCffuFac.newIncompleteCffu().orTimeout(1, MILLISECONDS).get() ).getCause()); assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - testCffuFac.newIncompleteCffu().orTimeout(testExecutor, 1, TimeUnit.MILLISECONDS).get() + testCffuFac.newIncompleteCffu().orTimeout(testExecutor, 1, MILLISECONDS).get() ).getCause()); assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - testCffuFac.newIncompleteCffu().unsafeOrTimeout(1, TimeUnit.MILLISECONDS).get() + testCffuFac.newIncompleteCffu().unsafeOrTimeout(1, MILLISECONDS).get() ).getCause()); - assertEquals(n, testCffuFac.completedFuture(n).orTimeout(1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, testCffuFac.completedFuture(n).orTimeout(testExecutor, 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, testCffuFac.completedFuture(n).unsafeOrTimeout(1, TimeUnit.MILLISECONDS).get()); + assertEquals(n, testCffuFac.completedFuture(n).orTimeout(1, MILLISECONDS).get()); + assertEquals(n, testCffuFac.completedFuture(n).orTimeout(testExecutor, 1, MILLISECONDS).get()); + assertEquals(n, testCffuFac.completedFuture(n).unsafeOrTimeout(1, MILLISECONDS).get()); assertEquals(n, testCffuFac.newIncompleteCffu().completeOnTimeout( - n, 1, TimeUnit.MILLISECONDS).get()); + n, 1, MILLISECONDS).get()); assertEquals(n, testCffuFac.newIncompleteCffu().completeOnTimeout( - n, testExecutor, 1, TimeUnit.MILLISECONDS).get()); + n, testExecutor, 1, MILLISECONDS).get()); assertEquals(n, testCffuFac.newIncompleteCffu().unsafeCompleteOnTimeout( - n, 1, TimeUnit.MILLISECONDS).get()); + n, 1, MILLISECONDS).get()); assertEquals(n, testCffuFac.completedFuture(n).completeOnTimeout( - anotherN, 1, TimeUnit.MILLISECONDS).get()); + anotherN, 1, MILLISECONDS).get()); assertEquals(n, testCffuFac.completedFuture(n).completeOnTimeout( - anotherN, testExecutor, 1, TimeUnit.MILLISECONDS).get()); + anotherN, testExecutor, 1, MILLISECONDS).get()); assertEquals(n, testCffuFac.completedFuture(n).unsafeCompleteOnTimeout( - anotherN, 1, TimeUnit.MILLISECONDS).get()); + anotherN, 1, MILLISECONDS).get()); } // endregion @@ -414,18 +415,18 @@ void test_peek() throws Exception { @Test void test_cffuJoin() { // Completed Future - assertEquals(n, testCffuFac.completedFuture(n).join(1, TimeUnit.MILLISECONDS)); + assertEquals(n, testCffuFac.completedFuture(n).join(1, MILLISECONDS)); // Incomplete Future -> CompletionException with TimeoutException Cffu incomplete = testCffuFac.newIncompleteCffu(); assertInstanceOf(TimeoutException.class, assertThrowsExactly(CompletionException.class, () -> - incomplete.join(1, TimeUnit.MILLISECONDS) + incomplete.join(1, MILLISECONDS) ).getCause()); // Failed Future -> CompletionException Cffu failed = testCffuFac.failedFuture(rte); assertSame(rte, assertThrowsExactly(CompletionException.class, () -> - failed.join(1, TimeUnit.MILLISECONDS) + failed.join(1, MILLISECONDS) ).getCause()); // Incomplete Future -> join before timeout @@ -434,7 +435,7 @@ void test_cffuJoin() { snoreZzz(); return n; }); - assertEquals(n, cffu.join(3, TimeUnit.SECONDS)); + assertEquals(n, cffu.join(LONG_WAIT_MS, MILLISECONDS)); } @Test diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuTestHelper.kt b/cffu-core/src/test/java/io/foldright/cffu/CffuTestHelper.kt index 76ae8153..1694a75c 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuTestHelper.kt +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuTestHelper.kt @@ -5,6 +5,7 @@ package io.foldright.cffu import io.foldright.cffu.CffuFactoryBuilder.CffuMadeExecutor import java.util.concurrent.Executor + fun CffuFactory.unwrapMadeExecutor(): Executor { val executor = defaultExecutor() as CffuMadeExecutor return executor.unwrap() 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 55f24c6f..0316b1b4 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -27,6 +27,7 @@ import static java.lang.Thread.currentThread; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.ForkJoinPool.commonPool; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.function.Function.identity; import static org.junit.jupiter.api.Assertions.*; @@ -55,8 +56,8 @@ void test_mSupply() throws Exception { mSupplyFastFailAsync(testExecutor, supplier, supplier), mSupplyAllSuccessAsync(anotherN, supplier, supplier), mSupplyAllSuccessAsync(anotherN, testExecutor, supplier, supplier), - mSupplyMostSuccessAsync(anotherN, 500, TimeUnit.MILLISECONDS, supplier, supplier), - mSupplyMostSuccessAsync(anotherN, testExecutor, 500, TimeUnit.MILLISECONDS, supplier, supplier), + mSupplyMostSuccessAsync(anotherN, LONG_WAIT_MS, MILLISECONDS, supplier, supplier), + mSupplyMostSuccessAsync(anotherN, testExecutor, LONG_WAIT_MS, MILLISECONDS, supplier, supplier), mSupplyAsync(supplier, supplier), mSupplyAsync(testExecutor, supplier, supplier), }; @@ -132,17 +133,17 @@ void test_tupleMSupplyMostSuccessAsync() throws Exception { nap(); return n + n; }; - assertEquals(Tuple2.of(n, s), tupleMSupplyMostSuccessAsync(200, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get()); - assertEquals(Tuple2.of(n, s), tupleMSupplyMostSuccessAsync(testExecutor, 200, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get()); + assertEquals(Tuple2.of(n, s), tupleMSupplyMostSuccessAsync(LONG_WAIT_MS, MILLISECONDS, supplier_n, supplier_s).get()); + assertEquals(Tuple2.of(n, s), tupleMSupplyMostSuccessAsync(testExecutor, LONG_WAIT_MS, MILLISECONDS, supplier_n, supplier_s).get()); - assertEquals(Tuple3.of(n, s, d), tupleMSupplyMostSuccessAsync(200, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); - assertEquals(Tuple3.of(n, s, d), tupleMSupplyMostSuccessAsync(testExecutor, 200, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); + assertEquals(Tuple3.of(n, s, d), tupleMSupplyMostSuccessAsync(LONG_WAIT_MS, MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); + assertEquals(Tuple3.of(n, s, d), tupleMSupplyMostSuccessAsync(testExecutor, LONG_WAIT_MS, MILLISECONDS, supplier_n, supplier_s, supplier_d).get()); - assertEquals(Tuple4.of(n, s, d, anotherN), tupleMSupplyMostSuccessAsync(200, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); - assertEquals(Tuple4.of(n, s, d, anotherN), tupleMSupplyMostSuccessAsync(testExecutor, 200, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), tupleMSupplyMostSuccessAsync(LONG_WAIT_MS, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), tupleMSupplyMostSuccessAsync(testExecutor, LONG_WAIT_MS, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get()); - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), tupleMSupplyMostSuccessAsync(1000, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), tupleMSupplyMostSuccessAsync(testExecutor, 200, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), tupleMSupplyMostSuccessAsync(LONG_WAIT_MS, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), tupleMSupplyMostSuccessAsync(testExecutor, LONG_WAIT_MS, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); } @Test @@ -327,7 +328,7 @@ void test_allOf__exceptionally() throws Exception { completedFuture(n), failedFuture(rte), incompleteCf() - ).get(30, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ); // incomplete fail incomplete @@ -336,7 +337,7 @@ void test_allOf__exceptionally() throws Exception { incompleteCf(), failedFuture(rte), incompleteCf() - ).get(200, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ); //////////////////////////////////////////////////////////////////////////////// @@ -386,7 +387,7 @@ void test_allOf__exceptionally() throws Exception { completedFuture(n), failedFuture(rte), incompleteCf() - ).get(30, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); // incomplete fail incomplete @@ -395,7 +396,7 @@ void test_allOf__exceptionally() throws Exception { incompleteCf(), failedFuture(rte), incompleteCf() - ).get(200, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); //////////////////////////////////////////////////////////////////////////////// @@ -445,7 +446,7 @@ void test_allOf__exceptionally() throws Exception { completedFuture(n), failedFuture(rte), incompleteCf() - ).get(30, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); // incomplete fail incomplete @@ -454,7 +455,7 @@ void test_allOf__exceptionally() throws Exception { incompleteCf(), failedFuture(rte), incompleteCf() - ).get(200, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); //////////////////////////////////////////////////////////////////////////////// @@ -504,7 +505,7 @@ void test_allOf__exceptionally() throws Exception { completedFuture(n), failedFuture(rte), incompleteCf() - ).get(30, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ); // incomplete fail incomplete @@ -513,7 +514,7 @@ void test_allOf__exceptionally() throws Exception { incompleteCf(), failedFuture(rte), incompleteCf() - ).get(200, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ); } @@ -526,36 +527,36 @@ void test_mostOf() throws Exception { final CompletableFuture incomplete = incompleteCf(); // 0 input cf - assertEquals(0, mostSuccessResultsOf(null, 10, TimeUnit.MILLISECONDS).get().size()); + assertEquals(0, mostSuccessResultsOf(null, SHORT_WAIT_MS, MILLISECONDS).get().size()); // 1 input cf assertEquals(Collections.singletonList(n), mostSuccessResultsOf( - null, 10, TimeUnit.MILLISECONDS, completed).get()); + null, SHORT_WAIT_MS, MILLISECONDS, completed).get()); assertEquals(Collections.singletonList(n), mostSuccessResultsOf( - anotherN, 10, TimeUnit.MILLISECONDS, completedStage).get()); + anotherN, SHORT_WAIT_MS, MILLISECONDS, completedStage).get()); assertEquals(Collections.singletonList(anotherN), mostSuccessResultsOf( - anotherN, 10, TimeUnit.MILLISECONDS, failed).get()); + anotherN, SHORT_WAIT_MS, MILLISECONDS, failed).get()); assertEquals(Collections.singletonList(anotherN), mostSuccessResultsOf( - anotherN, 10, TimeUnit.MILLISECONDS, cancelled).get()); + anotherN, SHORT_WAIT_MS, MILLISECONDS, cancelled).get()); assertEquals(Collections.singletonList(anotherN), mostSuccessResultsOf( - anotherN, 10, TimeUnit.MILLISECONDS, incomplete).get()); + anotherN, SHORT_WAIT_MS, MILLISECONDS, incomplete).get()); // more input cf assertEquals(Arrays.asList(n, null, null, null), mostSuccessResultsOf( - null, 10, TimeUnit.MILLISECONDS, completed, failed, cancelled, incomplete + null, SHORT_WAIT_MS, MILLISECONDS, completed, failed, cancelled, incomplete ).get()); assertEquals(Arrays.asList(n, anotherN, anotherN, anotherN), mostSuccessResultsOf( - anotherN, 10, TimeUnit.MILLISECONDS, completedStage, failed, cancelled, incomplete + anotherN, SHORT_WAIT_MS, MILLISECONDS, completedStage, failed, cancelled, incomplete ).get()); assertEquals(Arrays.asList(anotherN, anotherN, anotherN), mostSuccessResultsOf( - anotherN, 10, TimeUnit.MILLISECONDS, failed, cancelled, incomplete + anotherN, SHORT_WAIT_MS, MILLISECONDS, failed, cancelled, incomplete ).get()); // do not wait for failed and cancelled assertEquals(Arrays.asList(anotherN, anotherN), mostSuccessResultsOf( - anotherN, 10, TimeUnit.DAYS, failed, cancelled + anotherN, LONG_WAIT_MS, MILLISECONDS, failed, cancelled ).get()); } @@ -565,10 +566,10 @@ void test_mostOf_wontModifyInputCf() throws Exception { final CompletableFuture incomplete2 = incompleteCf(); assertEquals(Collections.singletonList(null), mostSuccessResultsOf( - null, 10, TimeUnit.MILLISECONDS, incomplete + null, SHORT_WAIT_MS, MILLISECONDS, incomplete ).get()); assertEquals(Arrays.asList(null, null), mostSuccessResultsOf( - null, 10, TimeUnit.MILLISECONDS, incomplete, incomplete2 + null, SHORT_WAIT_MS, MILLISECONDS, incomplete, incomplete2 ).get()); assertEquals(CffuState.RUNNING, state(incomplete)); @@ -700,7 +701,7 @@ void test_anyOf__exceptionally() throws Exception { incompleteCf(), failedFuture(rte), incompleteCf() - ).get(30, TimeUnit.MILLISECONDS) + ).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause(); } @@ -876,16 +877,16 @@ void test_allTupleOf_NotFastFail() throws Exception { final CompletionStage cf_an = completedStage(anotherN); assertThrowsExactly(TimeoutException.class, () -> - allTupleOf(incomplete, fail).get(200, TimeUnit.MILLISECONDS) + allTupleOf(incomplete, fail).get(SHORT_WAIT_MS, MILLISECONDS) ); assertThrowsExactly(TimeoutException.class, () -> - allTupleOf(incomplete, fail, cf_s).get(200, TimeUnit.MILLISECONDS) + allTupleOf(incomplete, fail, cf_s).get(SHORT_WAIT_MS, MILLISECONDS) ); assertThrowsExactly(TimeoutException.class, () -> - allTupleOf(incomplete, fail, cf_d, cf_s).get(200, TimeUnit.MILLISECONDS) + allTupleOf(incomplete, fail, cf_d, cf_s).get(SHORT_WAIT_MS, MILLISECONDS) ); assertThrowsExactly(TimeoutException.class, () -> - allTupleOf(incomplete, cf_d, fail, cf_s, cf_an).get(200, TimeUnit.MILLISECONDS) + allTupleOf(incomplete, cf_d, fail, cf_s, cf_an).get(SHORT_WAIT_MS, MILLISECONDS) ); } @@ -910,7 +911,7 @@ void test_failedFuture() throws Exception { void test_delayedExecutor() throws Exception { final AtomicReference holder = new AtomicReference<>(); - Executor delayer = delayedExecutor(1, TimeUnit.MILLISECONDS); + Executor delayer = delayedExecutor(1, MILLISECONDS); CompletableFuture.runAsync(() -> holder.set(testName), delayer).get(); assertEquals(testName, holder.get()); } @@ -991,8 +992,8 @@ void test_thenMApply() throws Exception { thenMApplyFastFailAsync(completed, testExecutor, supplier, supplier), thenMApplyAllSuccessAsync(completed, anotherN, supplier, supplier), thenMApplyAllSuccessAsync(completed, anotherN, testExecutor, supplier, supplier), - thenMApplyMostSuccessAsync(completed, anotherN, 1000, TimeUnit.MILLISECONDS, supplier, supplier), - thenMApplyMostSuccessAsync(completed, anotherN, testExecutor, 1000, TimeUnit.MILLISECONDS, supplier, supplier), + thenMApplyMostSuccessAsync(completed, anotherN, LONG_WAIT_MS, MILLISECONDS, supplier, supplier), + thenMApplyMostSuccessAsync(completed, anotherN, testExecutor, LONG_WAIT_MS, MILLISECONDS, supplier, supplier), thenMApplyAsync(completed, supplier, supplier), thenMApplyAsync(completed, testExecutor, supplier, supplier), }; @@ -1047,17 +1048,17 @@ void test_thenTupleMApplyMostSuccessAsync() throws Exception { snoreZzz(); return n + n; }; - assertEquals(Tuple2.of(n, s), thenTupleMApplyMostSuccessAsync(completed, 500, TimeUnit.MILLISECONDS, function_n, function_s).get()); - assertEquals(Tuple2.of(n, s), thenTupleMApplyMostSuccessAsync(completed, testExecutor, 500, TimeUnit.MILLISECONDS, function_n, function_s).get()); + assertEquals(Tuple2.of(n, s), thenTupleMApplyMostSuccessAsync(completed, LONG_WAIT_MS, MILLISECONDS, function_n, function_s).get()); + assertEquals(Tuple2.of(n, s), thenTupleMApplyMostSuccessAsync(completed, testExecutor, LONG_WAIT_MS, MILLISECONDS, function_n, function_s).get()); - assertEquals(Tuple3.of(n, s, d), thenTupleMApplyMostSuccessAsync(completed, 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get()); - assertEquals(Tuple3.of(n, s, d), thenTupleMApplyMostSuccessAsync(completed, testExecutor, 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get()); + assertEquals(Tuple3.of(n, s, d), thenTupleMApplyMostSuccessAsync(completed, LONG_WAIT_MS, MILLISECONDS, function_n, function_s, function_d).get()); + assertEquals(Tuple3.of(n, s, d), thenTupleMApplyMostSuccessAsync(completed, testExecutor, LONG_WAIT_MS, MILLISECONDS, function_n, function_s, function_d).get()); - assertEquals(Tuple4.of(n, s, d, anotherN), thenTupleMApplyMostSuccessAsync(completed, 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get()); - assertEquals(Tuple4.of(n, s, d, anotherN), thenTupleMApplyMostSuccessAsync(completed, testExecutor, 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), thenTupleMApplyMostSuccessAsync(completed, LONG_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), thenTupleMApplyMostSuccessAsync(completed, testExecutor, LONG_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an).get()); - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), thenTupleMApplyMostSuccessAsync(completed, 1000, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nn).get()); - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), thenTupleMApplyMostSuccessAsync(completed, testExecutor, 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), thenTupleMApplyMostSuccessAsync(completed, LONG_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an, function_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), thenTupleMApplyMostSuccessAsync(completed, testExecutor, LONG_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an, function_nn).get()); } @Test @@ -1145,55 +1146,55 @@ void test_mostSuccessTupleOf() throws Exception { final CompletableFuture incomplete = incompleteCf(); assertEquals(Tuple2.of(n, s), mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, completed, anotherCompleted + SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted ).get()); assertEquals(Tuple2.of(n, null), mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, completed, failed + SHORT_WAIT_MS, MILLISECONDS, completed, failed ).get()); assertEquals(Tuple3.of(n, s, null), mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled + SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted, cancelled ).get()); assertEquals(Tuple3.of(null, null, s), mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, incomplete, failed, anotherCompleted + SHORT_WAIT_MS, MILLISECONDS, incomplete, failed, anotherCompleted ).get()); assertEquals(Tuple4.of(n, s, null, null), mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled, incomplete + SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted, cancelled, incomplete ).get()); assertEquals(Tuple4.of(null, null, null, null), mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, incomplete, failed, cancelled, incomplete + SHORT_WAIT_MS, MILLISECONDS, incomplete, failed, cancelled, incomplete ).get()); assertEquals(Tuple5.of(null, n, s, null, null), mostSuccessTupleOf( - 10, TimeUnit.MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed + SHORT_WAIT_MS, MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed ).get()); // with `executorWhenTimeout` assertEquals(Tuple2.of(n, s), mostSuccessTupleOf( - testExecutor, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted + testExecutor, SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted ).get()); assertEquals(Tuple2.of(n, null), mostSuccessTupleOf( - testExecutor, 10, TimeUnit.MILLISECONDS, completed, failed + testExecutor, SHORT_WAIT_MS, MILLISECONDS, completed, failed ).get()); assertEquals(Tuple3.of(n, s, null), mostSuccessTupleOf( - testExecutor, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled + testExecutor, SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted, cancelled ).get()); assertEquals(Tuple3.of(null, null, s), mostSuccessTupleOf( - testExecutor, 10, TimeUnit.MILLISECONDS, incomplete, failed, anotherCompleted + testExecutor, SHORT_WAIT_MS, MILLISECONDS, incomplete, failed, anotherCompleted ).get()); assertEquals(Tuple4.of(n, s, null, null), mostSuccessTupleOf( - testExecutor, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled, incomplete + testExecutor, SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted, cancelled, incomplete ).get()); assertEquals(Tuple4.of(null, null, null, null), mostSuccessTupleOf( - testExecutor, 10, TimeUnit.MILLISECONDS, incomplete, failed, cancelled, incomplete + testExecutor, SHORT_WAIT_MS, MILLISECONDS, incomplete, failed, cancelled, incomplete ).get()); assertEquals(Tuple5.of(null, n, s, null, null), mostSuccessTupleOf( - testExecutor, 10, TimeUnit.MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed + testExecutor, SHORT_WAIT_MS, MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed ).get()); } @@ -1233,89 +1234,89 @@ void both_fastFail() throws Exception { final Runnable runnable = () -> { }; assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFail(cf_n, failed, runnable).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFail(cf_n, failed, runnable).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFailAsync(cf_n, failed, runnable).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFailAsync(cf_n, failed, runnable).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFailAsync(cf_n, failed, runnable, testExecutor).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFailAsync(cf_n, failed, runnable, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFail(failed, cf_n, runnable).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFail(failed, cf_n, runnable).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFailAsync(failed, cf_n, runnable).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFailAsync(failed, cf_n, runnable).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFailAsync(failed, cf_n, runnable, testExecutor).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFailAsync(failed, cf_n, runnable, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFail(failed, cf_ee, runnable).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFail(failed, cf_ee, runnable).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFailAsync(failed, cf_ee, runnable).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFailAsync(failed, cf_ee, runnable).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - runAfterBothFastFailAsync(failed, cf_ee, runnable, testExecutor).get(1, TimeUnit.MILLISECONDS) + runAfterBothFastFailAsync(failed, cf_ee, runnable, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); BiConsumer bc = (i1, i2) -> { }; assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFail(cf_n, failed, bc).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFail(cf_n, failed, bc).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFailAsync(cf_n, failed, bc).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFailAsync(cf_n, failed, bc).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFailAsync(cf_n, failed, bc, testExecutor).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFailAsync(cf_n, failed, bc, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFail(failed, cf_n, bc).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFail(failed, cf_n, bc).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFailAsync(failed, cf_n, bc).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFailAsync(failed, cf_n, bc).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFailAsync(failed, cf_n, bc, testExecutor).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFailAsync(failed, cf_n, bc, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFail(failed, cf_ee, bc).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFail(failed, cf_ee, bc).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFailAsync(failed, cf_ee, bc).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFailAsync(failed, cf_ee, bc).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenAcceptBothFastFailAsync(failed, cf_ee, bc, testExecutor).get(1, TimeUnit.MILLISECONDS) + thenAcceptBothFastFailAsync(failed, cf_ee, bc, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFail(cf_n, failed, Integer::sum).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFail(cf_n, failed, Integer::sum).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFailAsync(cf_n, failed, Integer::sum).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFailAsync(cf_n, failed, Integer::sum).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFailAsync(cf_n, failed, Integer::sum, testExecutor).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFailAsync(cf_n, failed, Integer::sum, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFail(failed, cf_n, Integer::sum).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFail(failed, cf_n, Integer::sum).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFailAsync(failed, cf_n, Integer::sum).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFailAsync(failed, cf_n, Integer::sum).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFailAsync(failed, cf_n, Integer::sum, testExecutor).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFailAsync(failed, cf_n, Integer::sum, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFail(failed, cf_ee, Integer::sum).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFail(failed, cf_ee, Integer::sum).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFailAsync(failed, cf_ee, Integer::sum).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFailAsync(failed, cf_ee, Integer::sum).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - thenCombineFastFailAsync(failed, cf_ee, Integer::sum, testExecutor).get(1, TimeUnit.MILLISECONDS) + thenCombineFastFailAsync(failed, cf_ee, Integer::sum, testExecutor).get(SHORT_WAIT_MS, MILLISECONDS) ).getCause()); } @@ -1456,26 +1457,26 @@ void test_catching() throws Exception { @Test void test_timeout() throws Exception { assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - orTimeout(incompleteCf(), 1, TimeUnit.MILLISECONDS).get() + orTimeout(incompleteCf(), SHORT_WAIT_MS, MILLISECONDS).get() ).getCause()); assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - cffuOrTimeout(incompleteCf(), 1, TimeUnit.MILLISECONDS).get() + cffuOrTimeout(incompleteCf(), SHORT_WAIT_MS, MILLISECONDS).get() ).getCause()); assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - cffuOrTimeout(incompleteCf(), testExecutor, 1, TimeUnit.MILLISECONDS).get() + cffuOrTimeout(incompleteCf(), testExecutor, SHORT_WAIT_MS, MILLISECONDS).get() ).getCause()); - assertEquals(n, orTimeout(completedFuture(n), 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, cffuOrTimeout(completedFuture(n), 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, cffuOrTimeout(completedFuture(n), testExecutor, 1, TimeUnit.MILLISECONDS).get()); + assertEquals(n, orTimeout(completedFuture(n), SHORT_WAIT_MS, MILLISECONDS).get()); + assertEquals(n, cffuOrTimeout(completedFuture(n), SHORT_WAIT_MS, MILLISECONDS).get()); + assertEquals(n, cffuOrTimeout(completedFuture(n), testExecutor, SHORT_WAIT_MS, MILLISECONDS).get()); - assertEquals(n, completeOnTimeout(incompleteCf(), n, 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, cffuCompleteOnTimeout(incompleteCf(), n, 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, cffuCompleteOnTimeout(incompleteCf(), n, testExecutor, 1, TimeUnit.MILLISECONDS).get()); + assertEquals(n, completeOnTimeout(incompleteCf(), n, SHORT_WAIT_MS, MILLISECONDS).get()); + assertEquals(n, cffuCompleteOnTimeout(incompleteCf(), n, SHORT_WAIT_MS, MILLISECONDS).get()); + assertEquals(n, cffuCompleteOnTimeout(incompleteCf(), n, testExecutor, SHORT_WAIT_MS, MILLISECONDS).get()); - assertEquals(n, completeOnTimeout(completedFuture(n), anotherN, 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, cffuCompleteOnTimeout(completedFuture(n), anotherN, 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, cffuCompleteOnTimeout(completedFuture(n), anotherN, testExecutor, 1, TimeUnit.MILLISECONDS).get()); + assertEquals(n, completeOnTimeout(completedFuture(n), anotherN, SHORT_WAIT_MS, MILLISECONDS).get()); + assertEquals(n, cffuCompleteOnTimeout(completedFuture(n), anotherN, SHORT_WAIT_MS, MILLISECONDS).get()); + assertEquals(n, cffuCompleteOnTimeout(completedFuture(n), anotherN, testExecutor, SHORT_WAIT_MS, MILLISECONDS).get()); } @Test @@ -1484,7 +1485,7 @@ void test_safeBehavior_orTimeout() { final List results = IntStream.range(0, 10).boxed().collect(Collectors.toList()); assertEquals(results, results.stream().map(i -> - orTimeout(incompleteCf(), 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + orTimeout(incompleteCf(), SHORT_WAIT_MS, MILLISECONDS).handle((v, ex) -> { assertInstanceOf(TimeoutException.class, ex); assertTrue(Delayer.atCfDelayerThread()); return i; @@ -1492,7 +1493,7 @@ void test_safeBehavior_orTimeout() { ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuOrTimeout(incompleteCf(), 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + cffuOrTimeout(incompleteCf(), SHORT_WAIT_MS, MILLISECONDS).handle((v, ex) -> { assertInstanceOf(TimeoutException.class, ex); assertFalse(Delayer.atCfDelayerThread()); assertNotSame(testThread, currentThread()); @@ -1500,7 +1501,7 @@ void test_safeBehavior_orTimeout() { }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuOrTimeout(incompleteCf(), testExecutor, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + cffuOrTimeout(incompleteCf(), testExecutor, SHORT_WAIT_MS, MILLISECONDS).handle((v, ex) -> { assertInstanceOf(TimeoutException.class, ex); assertFalse(Delayer.atCfDelayerThread()); assertRunningInExecutor(testExecutor); @@ -1515,7 +1516,7 @@ void test_safeBehavior_completeOnTimeout() { final List results = IntStream.range(0, 10).boxed().collect(Collectors.toList()); assertEquals(results, results.stream().map(i -> - completeOnTimeout(incompleteCf(), i, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + completeOnTimeout(incompleteCf(), i, SHORT_WAIT_MS, MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertTrue(Delayer.atCfDelayerThread()); return v; @@ -1523,7 +1524,7 @@ void test_safeBehavior_completeOnTimeout() { ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuCompleteOnTimeout(incompleteCf(), i, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + cffuCompleteOnTimeout(incompleteCf(), i, SHORT_WAIT_MS, MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertFalse(Delayer.atCfDelayerThread()); assertNotSame(testThread, currentThread()); @@ -1531,7 +1532,7 @@ void test_safeBehavior_completeOnTimeout() { }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuCompleteOnTimeout(incompleteCf(), i, testExecutor, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + cffuCompleteOnTimeout(incompleteCf(), i, testExecutor, SHORT_WAIT_MS, MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertFalse(Delayer.atCfDelayerThread()); assertRunningInExecutor(testExecutor); @@ -1626,7 +1627,7 @@ void test_read() { final CffuFactory cffuFactory = CffuFactory.builder(Executors.newCachedThreadPool()).build(); final Cffu completedCffu = cffuFactory.completedFuture(n); - assertEquals(n, join(completed, 1, TimeUnit.MILLISECONDS)); + assertEquals(n, join(completed, SHORT_WAIT_MS, MILLISECONDS)); assertEquals(n, getSuccessNow(completed, anotherN)); assertEquals(n, getSuccessNow(completed, null)); assertEquals(n, resultNow(completed)); @@ -1661,7 +1662,7 @@ void test_read() { assertSame(rte, assertThrowsExactly(CompletionException.class, failed::join).getCause()); // same as CompletableFuture.join method assertSame(rte, assertThrowsExactly(CompletionException.class, () -> - join(failed, 1, TimeUnit.MILLISECONDS) + join(failed, SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertEquals(anotherN, getSuccessNow(failed, anotherN)); assertNull(getSuccessNow(failed, null)); @@ -1693,7 +1694,7 @@ void test_read() { assertThrowsExactly(CancellationException.class, cancelled::join); // same as CompletableFuture.join method assertThrowsExactly(CancellationException.class, () -> - join(cancelled, 1, TimeUnit.MILLISECONDS) + join(cancelled, SHORT_WAIT_MS, MILLISECONDS) ); final String m3 = assertThrowsExactly(IllegalStateException.class, () -> resultNow(cancelled) @@ -1712,7 +1713,7 @@ void test_read() { final CompletableFuture incomplete = incompleteCf(); assertInstanceOf(TimeoutException.class, assertThrowsExactly(CompletionException.class, () -> - join(incomplete, 1, TimeUnit.MILLISECONDS) + join(incomplete, SHORT_WAIT_MS, MILLISECONDS) ).getCause()); assertEquals(anotherN, getSuccessNow(incomplete, anotherN)); assertNull(getSuccessNow(incomplete, null)); @@ -1728,7 +1729,7 @@ void test_read() { // Incomplete Future -> join before timeout CompletableFuture later = completeLaterCf(n); - assertEquals(n, join(later, 3, TimeUnit.SECONDS)); + assertEquals(n, join(later, LONG_WAIT_MS, MILLISECONDS)); } @Test @@ -1825,7 +1826,7 @@ void test_completableFutureListToArray() { void test_unwrapCfException() { CompletableFuture failed = failedFuture(rte); - ExecutionException ee = assertThrowsExactly(ExecutionException.class, () -> failed.get(0, TimeUnit.MILLISECONDS)); + ExecutionException ee = assertThrowsExactly(ExecutionException.class, () -> failed.get(SHORT_WAIT_MS, MILLISECONDS)); assertSame(rte, unwrapCfException(ee)); CompletionException ce = assertThrowsExactly(CompletionException.class, failed::join); 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 fa1c41b2..3f7af2d0 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/ListenableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/ListenableFutureUtilsTest.java @@ -17,6 +17,7 @@ import static io.foldright.test_utils.TestingExecutorUtils.testCffuFac; import static io.foldright.test_utils.TestingExecutorUtils.testExecutor; import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import static org.junit.jupiter.api.Assertions.*; @@ -54,7 +55,7 @@ void test_toListenableFuture() throws Exception { ListenableFuture lf = toListenableFuture(cf); assertEquals(n, lf.get()); - assertEquals(n, lf.get(10, TimeUnit.MILLISECONDS)); + assertEquals(n, lf.get(SHORT_WAIT_MS, MILLISECONDS)); assertTrue(lf.isDone()); assertEquals("ListenableFutureAdapter@ListenableFutureUtils.toListenableFuture of " + cf, lf.toString()); @@ -136,7 +137,7 @@ void test_lf2cf_setCancellationExceptionToCf_cancellationAndPropagation() throws final AtomicBoolean interrupted = new AtomicBoolean(false); final ListenableFuture lf = Futures.submit(() -> { try { - Thread.sleep(2000); + Thread.sleep(LONG_WAIT_MS); } catch (InterruptedException ex) { interrupted.set(true); } @@ -262,7 +263,7 @@ void showCase_CompletableFuture_cancellationAndPropagation() throws Exception { static void waitForAllLfsToComplete(ListenableFuture... lfs) throws Exception { for (ListenableFuture lf : lfs) { try { - lf.get(2, TimeUnit.SECONDS); + lf.get(LONG_WAIT_MS, MILLISECONDS); } catch (TimeoutException ex) { throw ex; } catch (Exception ignored) { @@ -271,6 +272,6 @@ static void waitForAllLfsToComplete(ListenableFuture... lfs) throws Exception } static void waitForAllCfsToComplete(CompletableFuture... cfs) throws Exception { - CompletableFutureUtils.mostSuccessResultsOf(null, 2, TimeUnit.SECONDS, cfs).join(); + CompletableFutureUtils.mostSuccessResultsOf(null, LONG_WAIT_MS, MILLISECONDS, cfs).join(); } } 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 807317ba..5cfbe26a 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 @@ -25,14 +25,14 @@ import java.util.function.Supplier fun incompleteCf(): CompletableFuture = CompletableFuture() @JvmOverloads -fun completeLaterCf(value: T, millis: Long = 100): CompletableFuture = CompletableFuture.supplyAsync { +fun completeLaterCf(value: T, millis: Long = MEDIAN_WAIT_MS): CompletableFuture = CompletableFuture.supplyAsync { sleep(millis) value } @JvmOverloads fun completeLaterCf( - value: () -> T, millis: Long = 100, executor: Executor = DEFAULT_EXECUTOR + value: () -> T, millis: Long = MEDIAN_WAIT_MS, executor: Executor = DEFAULT_EXECUTOR ): CompletableFuture { val action = Supplier { sleep(millis) @@ -50,13 +50,13 @@ fun cancelledFuture(mayInterruptIfRunning: Boolean = false): CompletableFutu private val DEFAULT_EXECUTOR: Executor = Executor { /* do nothing */ } @JvmOverloads -fun supplyLater(value: T, millis: Long = 100) = Supplier { +fun supplyLater(value: T, millis: Long = MEDIAN_WAIT_MS) = Supplier { sleep(millis) value } @JvmOverloads -fun supplyLater(ex: Throwable, millis: Long = 100) = Supplier { +fun supplyLater(ex: Throwable, millis: Long = MEDIAN_WAIT_MS) = Supplier { sleep(millis) throw ex } @@ -110,7 +110,7 @@ fun assertCffuRunInThreadOf(executorService: ExecutorService) { * sleep without throwing checked exception */ @JvmOverloads -fun sleep(millis: Long = 10) { +fun sleep(millis: Long = SHORT_WAIT_MS) { Thread.sleep(millis) } @@ -118,7 +118,7 @@ fun sleep(millis: Long = 10) { * sleep short time */ @JvmOverloads -fun nap(millis: Long = 10) { +fun nap(millis: Long = SHORT_WAIT_MS) { Thread.sleep(millis) } @@ -126,7 +126,7 @@ fun nap(millis: Long = 10) { * sleep long time */ @JvmOverloads -fun snoreZzz(millis: Long = 100) { +fun snoreZzz(millis: Long = MEDIAN_WAIT_MS) { Thread.sleep(millis) } diff --git a/cffu-core/src/test/java/io/foldright/test_utils/TestingConstants.kt b/cffu-core/src/test/java/io/foldright/test_utils/TestingConstants.kt index a5fe0384..ebce6e9f 100644 --- a/cffu-core/src/test/java/io/foldright/test_utils/TestingConstants.kt +++ b/cffu-core/src/test/java/io/foldright/test_utils/TestingConstants.kt @@ -18,3 +18,7 @@ val rte = RuntimeException("Bang") @JvmField val anotherRte = RuntimeException("AnotherBang") + +const val LONG_WAIT_MS = 1000L +const val MEDIAN_WAIT_MS = 100L +const val SHORT_WAIT_MS = 10L