From 6c3dcf1afb4c6945ef8c500a136d22f9115cc825 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 10 Aug 2024 02:38:03 +0800 Subject: [PATCH] WIP --- .../io/foldright/cffu/CffuFactoryTest.java | 74 +++---- .../java/io/foldright/cffu/CffuStateTest.java | 30 --- .../java/io/foldright/cffu/CffuStateTest.kt | 26 +++ .../test/java/io/foldright/cffu/CffuTest.java | 27 +-- .../cffu/CompletableFutureUtilsTest.java | 160 +++++++------- .../cffu/ListenableFutureUtilsTest.java | 25 +-- .../CffuApiCompatibilityTest.java | 205 +++++++++--------- ...CompletableFutureApiCompatibilityTest.java | 203 ++++++++--------- .../java/io/foldright/test_utils/TestUtils.kt | 47 ++-- ...fuTestConstants.kt => TestingConstants.kt} | 7 +- ...PoolManager.kt => TestingExecutorUtils.kt} | 14 +- .../TypeParameterDeclarationTest.java | 6 +- .../test/CompletableFutureExtensionsTest.kt | 8 +- scripts/gen_CffuApiCompatibilityTest.sh | 2 +- 14 files changed, 398 insertions(+), 436 deletions(-) delete mode 100644 cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.java create mode 100644 cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.kt rename cffu-core/src/test/java/io/foldright/test_utils/{CffuTestConstants.kt => TestingConstants.kt} (78%) rename cffu-core/src/test/java/io/foldright/test_utils/{TestThreadPoolManager.kt => TestingExecutorUtils.kt} (93%) 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 8de246836..6c47fc1ba 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -4,9 +4,7 @@ import io.foldright.cffu.tuple.Tuple3; import io.foldright.cffu.tuple.Tuple4; import io.foldright.cffu.tuple.Tuple5; -import io.foldright.test_utils.TestThreadPoolManager; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import io.foldright.test_utils.TestingExecutorUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.JRE; @@ -20,8 +18,8 @@ import static io.foldright.cffu.CffuTestHelper.unwrapMadeExecutor; import static io.foldright.cffu.CompletableFutureUtils.failedFuture; import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray; -import static io.foldright.test_utils.CffuTestConstants.*; import static io.foldright.test_utils.TestUtils.*; +import static io.foldright.test_utils.TestingConstants.*; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.ForkJoinPool.commonPool; import static java.util.function.Function.identity; @@ -313,8 +311,8 @@ void test_mostOf() throws Exception { final Cffu completed = cffuFactory.completedFuture(n); final CompletionStage completedStage = cffuFactory.completedStage(n); final Cffu failed = cffuFactory.failedFuture(rte); - final Cffu cancelled = cffuFactory.toCffu(createCancelledFuture()); - final Cffu incomplete = cffuFactory.toCffu(createIncompleteFuture()); + final Cffu cancelled = cffuFactory.toCffu(cancelledFuture()); + final Cffu incomplete = cffuFactory.toCffu(incompleteCf()); assertEquals(0, cffuFactory.mostSuccessResultsOf(null, 10, TimeUnit.MILLISECONDS).get().size()); @@ -343,7 +341,7 @@ void test_mostOf() throws Exception { @Test void test_anyOf() throws Exception { assertEquals(n, cffuFactory.anyOf( - createIncompleteFuture(), + incompleteCf(), completedFuture(n) ).get()); assertEquals(n, cffuFactory.anyOf( @@ -362,7 +360,7 @@ void test_anyOf() throws Exception { //////////////////////////////////////// - assertEquals(n, cffuFactory.anySuccessOf(createIncompleteFuture(), completedFuture(n)).get()); + assertEquals(n, cffuFactory.anySuccessOf(incompleteCf(), completedFuture(n)).get()); assertEquals(n, cffuFactory.anySuccessOf(completedFuture(n)).get()); assertSame(NoCfsProvidedException.class, cffuFactory.anySuccessOf().exceptionNow().getClass()); @@ -405,16 +403,16 @@ void test_anyOf_exceptionally() throws Exception { // even later cfs normally completed! assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - cffuFactory.anyOf(createIncompleteFuture(), failedFuture(rte), createIncompleteFuture()).get() + cffuFactory.anyOf(incompleteCf(), failedFuture(rte), incompleteCf()).get() ).getCause()); // first normally completed anyOf cf win, // even later cfs exceptionally completed! assertEquals(n, cffuFactory.anyOf( - createIncompleteFuture(), + incompleteCf(), completedFuture(n), - createIncompleteFuture() + incompleteCf() ).get()); } @@ -458,8 +456,8 @@ void test_anySuccessOf__trivial_case() throws Exception { // success then success assertEquals(n, cffuFactory.anySuccessOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), CompletableFuture.supplyAsync(() -> { sleep(300); return anotherN; @@ -468,8 +466,8 @@ void test_anySuccessOf__trivial_case() throws Exception { ).get()); // success then failed assertEquals(n, cffuFactory.anySuccessOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), CompletableFuture.supplyAsync(() -> { sleep(300); throw rte; @@ -640,8 +638,8 @@ void test_mostSuccessTupleOf() throws Exception { final Cffu completed = cffuFactory.completedFuture(n); final CompletionStage anotherCompleted = cffuFactory.completedStage(s); final Cffu failed = cffuFactory.failedFuture(rte); - final Cffu cancelled = cffuFactory.toCffu(createCancelledFuture()); - final Cffu incomplete = cffuFactory.toCffu(createIncompleteFuture()); + final Cffu cancelled = cffuFactory.toCffu(cancelledFuture()); + final Cffu incomplete = cffuFactory.toCffu(incompleteCf()); assertEquals(Tuple2.of(n, s), cffuFactory.mostSuccessTupleOf( 10, TimeUnit.MILLISECONDS, completed, anotherCompleted @@ -808,15 +806,15 @@ void test_toCffu() throws Exception { assertEquals(n, cf.get()); shouldNotBeMinimalStage(cf); - final Cffu cffu_in = cffuFactory.completedFuture(42); + final Cffu cffu_in = cffuFactory.completedFuture(n); - CffuFactory fac = CffuFactory.builder(anotherExecutorService).forbidObtrudeMethods(true).build(); + CffuFactory fac = CffuFactory.builder(dummyExecutor).forbidObtrudeMethods(true).build(); Cffu cffu = fac.toCffu(cffu_in); assertNotSame(cffu_in, cffu); - assertSame(anotherExecutorService, unwrapMadeExecutor(cffu)); + assertSame(dummyExecutor, unwrapMadeExecutor(cffu)); assertSame(fac, cffu.cffuFactory()); assertEquals("obtrude methods is forbidden by cffu", assertThrowsExactly(UnsupportedOperationException.class, () -> - cffu.obtrudeValue(44) + cffu.obtrudeValue(anotherN) ).getMessage()); assertSame(cffu_in, cffuFactory.toCffu(cffu_in)); @@ -921,12 +919,12 @@ void test_getter() { assertSame(executorService, unwrapMadeExecutor(cffuFactory)); assertFalse(cffuFactory.forbidObtrudeMethods()); - CffuFactory fac = CffuFactory.builder(anotherExecutorService).forbidObtrudeMethods(true).build(); - assertSame(anotherExecutorService, unwrapMadeExecutor(fac)); + CffuFactory fac = CffuFactory.builder(dummyExecutor).forbidObtrudeMethods(true).build(); + assertSame(dummyExecutor, unwrapMadeExecutor(fac)); assertTrue(fac.forbidObtrudeMethods()); - final CffuFactory fac2 = cffuFactory.resetDefaultExecutor(anotherExecutorService); - assertSame(anotherExecutorService, unwrapMadeExecutor(fac2)); + final CffuFactory fac2 = cffuFactory.resetDefaultExecutor(dummyExecutor); + assertSame(dummyExecutor, unwrapMadeExecutor(fac2)); assertEquals(cffuFactory.forbidObtrudeMethods(), fac2.forbidObtrudeMethods()); final CffuFactory fac3 = cffuFactory.resetDefaultExecutor(fac2.defaultExecutor()); @@ -941,7 +939,7 @@ void test_forbidObtrudeMethods_property() { Cffu cf = fac2.newIncompleteCffu(); assertEquals("obtrude methods is forbidden by cffu", assertThrowsExactly(UnsupportedOperationException.class, () -> - cf.obtrudeValue(42)).getMessage() + cf.obtrudeValue(n)).getMessage() ); assertEquals("obtrude methods is forbidden by cffu", assertThrowsExactly(UnsupportedOperationException.class, () -> cf.obtrudeException(rte)).getMessage() @@ -960,31 +958,17 @@ void test_executorSetting_MayBe_ThreadPerTaskExecutor() throws Exception { assertTrue(executorClassName.endsWith("$ThreadPerTaskExecutor")); } - assertEquals(42, fac.supplyAsync(() -> 42).get()); + assertEquals(n, fac.supplyAsync(() -> n).get()); } - // endregion //////////////////////////////////////////////////////////////////////////////// - // region# Test helper methods/fields + // region# Test helper fields //////////////////////////////////////////////////////////////////////////////// - private static CffuFactory cffuFactory; - - private static ExecutorService executorService; - - private static ExecutorService anotherExecutorService; + private final CffuFactory cffuFactory = TestingExecutorUtils.getTestCffuFactory(); - @BeforeAll - static void beforeAll() { - executorService = TestThreadPoolManager.createThreadPool("CffuFactoryTest"); - anotherExecutorService = TestThreadPoolManager.createThreadPool("CffuFactoryTest-Another", true); + private final ExecutorService executorService = TestingExecutorUtils.getTestThreadPoolExecutor(); - cffuFactory = CffuFactory.builder(executorService).build(); - } - - @AfterAll - static void afterAll() { - TestThreadPoolManager.shutdownExecutorService(executorService, anotherExecutorService); - } + private final Executor dummyExecutor = Runnable::run; } diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.java b/cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.java deleted file mode 100644 index f066e09a3..000000000 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.foldright.cffu; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.EnabledForJreRange; -import org.junit.jupiter.api.condition.JRE; - -import java.util.concurrent.Future; - -import static org.junit.jupiter.api.Assertions.assertEquals; - - -class CffuStateTest { - @Test - @EnabledForJreRange(min = JRE.JAVA_19) - void toCffuState() { - assertEquals(CffuState.RUNNING, CffuState.toCffuState(Future.State.RUNNING)); - assertEquals(CffuState.SUCCESS, CffuState.toCffuState(Future.State.SUCCESS)); - assertEquals(CffuState.FAILED, CffuState.toCffuState(Future.State.FAILED)); - assertEquals(CffuState.CANCELLED, CffuState.toCffuState(Future.State.CANCELLED)); - } - - @Test - @EnabledForJreRange(min = JRE.JAVA_19) - void toFutureState() { - assertEquals(Future.State.RUNNING, CffuState.RUNNING.toFutureState()); - assertEquals(Future.State.SUCCESS, CffuState.SUCCESS.toFutureState()); - assertEquals(Future.State.FAILED, CffuState.FAILED.toFutureState()); - assertEquals(Future.State.CANCELLED, CffuState.CANCELLED.toFutureState()); - } -} diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.kt b/cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.kt new file mode 100644 index 000000000..cdd9f97fe --- /dev/null +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.kt @@ -0,0 +1,26 @@ +package io.foldright.cffu + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.condition.EnabledForJreRange +import org.junit.jupiter.api.condition.JRE.JAVA_19 +import java.util.concurrent.Future + +@EnabledForJreRange(min = JAVA_19) +private class CffuStateTest { + @Test + fun toCffuState() { + CffuState.toCffuState(Future.State.RUNNING) shouldBe CffuState.RUNNING + CffuState.toCffuState(Future.State.SUCCESS) shouldBe CffuState.SUCCESS + CffuState.toCffuState(Future.State.FAILED) shouldBe CffuState.FAILED + CffuState.toCffuState(Future.State.CANCELLED) shouldBe CffuState.CANCELLED + } + + @Test + fun toFutureState() { + CffuState.RUNNING.toFutureState() shouldBe Future.State.RUNNING + CffuState.SUCCESS.toFutureState() shouldBe Future.State.SUCCESS + CffuState.FAILED.toFutureState() shouldBe Future.State.FAILED + CffuState.CANCELLED.toFutureState() shouldBe Future.State.CANCELLED + } +} 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 9c63e2df5..66f905b08 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java @@ -4,9 +4,7 @@ import io.foldright.cffu.tuple.Tuple3; import io.foldright.cffu.tuple.Tuple4; import io.foldright.cffu.tuple.Tuple5; -import io.foldright.test_utils.TestThreadPoolManager; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import io.foldright.test_utils.TestingExecutorUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.JRE; @@ -19,7 +17,7 @@ import java.util.function.Function; import static io.foldright.cffu.CffuTestHelper.unwrapMadeExecutor; -import static io.foldright.test_utils.CffuTestConstants.*; +import static io.foldright.test_utils.TestingConstants.*; import static io.foldright.test_utils.TestUtils.sleep; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.function.Function.identity; @@ -564,25 +562,12 @@ void test_toString() { // endregion //////////////////////////////////////////////////////////////////////////////// - // region# Test helper methods/fields + // region# Test helper fields //////////////////////////////////////////////////////////////////////////////// - private static ExecutorService executorService; + private final CffuFactory cffuFactory = TestingExecutorUtils.getTestCffuFactory(); - private static CffuFactory cffuFactory; + private final ExecutorService executorService = TestingExecutorUtils.getTestThreadPoolExecutor(); - private static CffuFactory forbidObtrudeMethodsCffuFactory; - - @BeforeAll - static void beforeAll() { - executorService = TestThreadPoolManager.createThreadPool("CffuTest"); - - cffuFactory = CffuFactory.builder(executorService).build(); - forbidObtrudeMethodsCffuFactory = CffuFactory.builder(executorService).forbidObtrudeMethods(true).build(); - } - - @AfterAll - static void afterAll() { - TestThreadPoolManager.shutdownExecutorService(executorService); - } + private final CffuFactory forbidObtrudeMethodsCffuFactory = CffuFactory.builder(executorService).forbidObtrudeMethods(true).build(); } 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 64b0c2682..bcbd7d096 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -4,9 +4,7 @@ import io.foldright.cffu.tuple.Tuple3; import io.foldright.cffu.tuple.Tuple4; import io.foldright.cffu.tuple.Tuple5; -import io.foldright.test_utils.TestThreadPoolManager; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import io.foldright.test_utils.TestingExecutorUtils; import org.junit.jupiter.api.Test; import java.util.Arrays; @@ -22,9 +20,9 @@ import java.util.stream.IntStream; import static io.foldright.cffu.CompletableFutureUtils.*; -import static io.foldright.test_utils.CffuTestConstants.*; -import static io.foldright.test_utils.TestThreadPoolManager.assertRunningInExecutor; import static io.foldright.test_utils.TestUtils.*; +import static io.foldright.test_utils.TestingConstants.*; +import static io.foldright.test_utils.TestingExecutorUtils.assertRunningInExecutor; import static java.lang.Thread.currentThread; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.ForkJoinPool.commonPool; @@ -329,16 +327,16 @@ void test_allOf__exceptionally() throws Exception { allResultsOf( completedFuture(n), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(30, TimeUnit.MILLISECONDS) ); // incomplete fail incomplete assertThrowsExactly(TimeoutException.class, () -> allResultsOf( - createIncompleteFuture(), + incompleteCf(), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(100, TimeUnit.MILLISECONDS) ); @@ -388,16 +386,16 @@ void test_allOf__exceptionally() throws Exception { allResultsFastFailOf( completedFuture(n), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(30, TimeUnit.MILLISECONDS) ).getCause()); // incomplete fail incomplete assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> allResultsFastFailOf( - createIncompleteFuture(), + incompleteCf(), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(100, TimeUnit.MILLISECONDS) ).getCause()); @@ -447,16 +445,16 @@ void test_allOf__exceptionally() throws Exception { allFastFailOf( completedFuture(n), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(30, TimeUnit.MILLISECONDS) ).getCause()); // incomplete fail incomplete assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> allFastFailOf( - createIncompleteFuture(), + incompleteCf(), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(100, TimeUnit.MILLISECONDS) ).getCause()); @@ -506,16 +504,16 @@ void test_allOf__exceptionally() throws Exception { allOf( completedFuture(n), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(30, TimeUnit.MILLISECONDS) ); // incomplete fail incomplete assertThrowsExactly(TimeoutException.class, () -> allOf( - createIncompleteFuture(), + incompleteCf(), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(100, TimeUnit.MILLISECONDS) ); } @@ -525,8 +523,8 @@ void test_mostOf() throws Exception { final CompletableFuture completed = completedFuture(n); final CompletionStage completedStage = completedStage(n); final CompletableFuture failed = failedFuture(rte); - final CompletableFuture cancelled = createCancelledFuture(); - final CompletableFuture incomplete = createIncompleteFuture(); + final CompletableFuture cancelled = cancelledFuture(); + final CompletableFuture incomplete = incompleteCf(); // 0 input cf assertEquals(0, mostSuccessResultsOf(null, 10, TimeUnit.MILLISECONDS).get().size()); @@ -564,8 +562,8 @@ void test_mostOf() throws Exception { @Test void test_mostOf_wontModifyInputCf() throws Exception { - final CompletableFuture incomplete = createIncompleteFuture(); - final CompletableFuture incomplete2 = createIncompleteFuture(); + final CompletableFuture incomplete = incompleteCf(); + final CompletableFuture incomplete2 = incompleteCf(); assertEquals(Collections.singletonList(null), mostSuccessResultsOf( null, 10, TimeUnit.MILLISECONDS, incomplete @@ -603,13 +601,13 @@ void test_anySuccessOf__trivial_case() throws Exception { // success with incomplete CF assertEquals(n, anyOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), completedFuture(n) ).get()); assertEquals(n, anyOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), completedStage(n) ).get()); @@ -634,13 +632,13 @@ void test_anySuccessOf__trivial_case() throws Exception { // success with incomplete CF assertEquals(n, anySuccessOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), completedFuture(n) ).get()); assertEquals(n, anySuccessOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), completedStage(n) ).get()); } @@ -672,9 +670,9 @@ void test_anyOf__exceptionally() throws Exception { // incomplete fail incomplete assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> anyOf( - createIncompleteFuture(), + incompleteCf(), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get() ).getCause()); @@ -700,9 +698,9 @@ void test_anyOf__exceptionally() throws Exception { // incomplete fail incomplete assertThrowsExactly(TimeoutException.class, () -> anySuccessOf( - createIncompleteFuture(), + incompleteCf(), failedFuture(rte), - createIncompleteFuture() + incompleteCf() ).get(30, TimeUnit.MILLISECONDS) ).getCause(); } @@ -711,8 +709,8 @@ void test_anyOf__exceptionally() throws Exception { void test_anyOf__concurrent() throws Exception { // incomplete/wait-success then success assertEquals(n, anyOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), CompletableFuture.supplyAsync(() -> { sleep(300); return anotherN; @@ -735,8 +733,8 @@ void test_anyOf__concurrent() throws Exception { // success then failed assertEquals(n, anyOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), CompletableFuture.supplyAsync(() -> { sleep(300); throw rte; @@ -760,8 +758,8 @@ void test_anyOf__concurrent() throws Exception { // incomplete/wait-success then success assertEquals(n, anySuccessOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), CompletableFuture.supplyAsync(() -> { sleep(300); return anotherN; @@ -784,8 +782,8 @@ void test_anyOf__concurrent() throws Exception { // success then failed assertEquals(n, anySuccessOf( - createIncompleteFuture(), - createIncompleteFuture(), + incompleteCf(), + incompleteCf(), CompletableFuture.supplyAsync(() -> { sleep(300); throw rte; @@ -1144,8 +1142,8 @@ void test_mostSuccessTupleOf() throws Exception { final CompletableFuture completed = completedFuture(n); final CompletionStage anotherCompleted = completedStage(s); final CompletableFuture failed = failedFuture(rte); - final CompletableFuture cancelled = createCancelledFuture(); - final CompletableFuture incomplete = createIncompleteFuture(); + final CompletableFuture cancelled = cancelledFuture(); + final CompletableFuture incomplete = incompleteCf(); assertEquals(Tuple2.of(n, s), mostSuccessTupleOf( 10, TimeUnit.MILLISECONDS, completed, anotherCompleted @@ -1333,7 +1331,7 @@ void both_fastFail() throws Exception { @Test void test_either() throws Exception { final CompletableFuture cf_n = completedFuture(n); - CompletableFuture incomplete = createIncompleteFuture(); + CompletableFuture incomplete = incompleteCf(); final Runnable runnable = () -> { }; @@ -1462,22 +1460,22 @@ void test_catching() throws Exception { @Test void test_timeout() throws Exception { assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - orTimeout(createIncompleteFuture(), 1, TimeUnit.MILLISECONDS).get() + orTimeout(incompleteCf(), 1, TimeUnit.MILLISECONDS).get() ).getCause()); assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - cffuOrTimeout(createIncompleteFuture(), 1, TimeUnit.MILLISECONDS).get() + cffuOrTimeout(incompleteCf(), 1, TimeUnit.MILLISECONDS).get() ).getCause()); assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () -> - cffuOrTimeout(createIncompleteFuture(), defaultExecutor(), 1, TimeUnit.MILLISECONDS).get() + cffuOrTimeout(incompleteCf(), defaultExecutor(), 1, TimeUnit.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), defaultExecutor(), 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, completeOnTimeout(createIncompleteFuture(), n, 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, cffuCompleteOnTimeout(createIncompleteFuture(), n, 1, TimeUnit.MILLISECONDS).get()); - assertEquals(n, cffuCompleteOnTimeout(createIncompleteFuture(), n, defaultExecutor(), 1, TimeUnit.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, defaultExecutor(), 1, TimeUnit.MILLISECONDS).get()); assertEquals(n, completeOnTimeout(completedFuture(n), anotherN, 1, TimeUnit.MILLISECONDS).get()); assertEquals(n, cffuCompleteOnTimeout(completedFuture(n), anotherN, 1, TimeUnit.MILLISECONDS).get()); @@ -1490,7 +1488,7 @@ void test_safeBehavior_orTimeout() { final List results = IntStream.range(0, 10).boxed().collect(Collectors.toList()); assertEquals(results, results.stream().map(i -> - orTimeout(createIncompleteFuture(), 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + orTimeout(incompleteCf(), 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertInstanceOf(TimeoutException.class, ex); assertTrue(Delayer.atCfDelayerThread()); return i; @@ -1498,7 +1496,7 @@ void test_safeBehavior_orTimeout() { ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuOrTimeout(createIncompleteFuture(), 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + cffuOrTimeout(incompleteCf(), 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertInstanceOf(TimeoutException.class, ex); assertFalse(Delayer.atCfDelayerThread()); assertNotSame(testThread, currentThread()); @@ -1506,7 +1504,7 @@ void test_safeBehavior_orTimeout() { }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuOrTimeout(createIncompleteFuture(), executorService, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + cffuOrTimeout(incompleteCf(), executorService, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertInstanceOf(TimeoutException.class, ex); assertFalse(Delayer.atCfDelayerThread()); assertRunningInExecutor(executorService); @@ -1521,7 +1519,7 @@ void test_safeBehavior_completeOnTimeout() { final List results = IntStream.range(0, 10).boxed().collect(Collectors.toList()); assertEquals(results, results.stream().map(i -> - completeOnTimeout(createIncompleteFuture(), i, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + completeOnTimeout(incompleteCf(), i, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertTrue(Delayer.atCfDelayerThread()); return v; @@ -1529,7 +1527,7 @@ void test_safeBehavior_completeOnTimeout() { ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuCompleteOnTimeout(createIncompleteFuture(), i, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + cffuCompleteOnTimeout(incompleteCf(), i, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertFalse(Delayer.atCfDelayerThread()); assertNotSame(testThread, currentThread()); @@ -1537,7 +1535,7 @@ void test_safeBehavior_completeOnTimeout() { }) ).collect(Collectors.toList()).stream().map(CompletableFuture::join).collect(Collectors.toList())); assertEquals(results, results.stream().map(i -> - cffuCompleteOnTimeout(createIncompleteFuture(), i, executorService, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { + cffuCompleteOnTimeout(incompleteCf(), i, executorService, 100, TimeUnit.MILLISECONDS).handle((v, ex) -> { assertNull(ex); assertFalse(Delayer.atCfDelayerThread()); assertRunningInExecutor(executorService); @@ -1694,7 +1692,7 @@ void test_read() { // cancelled tasks //////////////////////////////////////////////////////////////////////////////// - CompletableFuture cancelled = createCancelledFuture(); + CompletableFuture cancelled = cancelledFuture(); assertThrowsExactly(CancellationException.class, cancelled::join); // same as CompletableFuture.join method @@ -1715,7 +1713,7 @@ void test_read() { // incomplete tasks //////////////////////////////////////////////////////////////////////////////// - final CompletableFuture incomplete = createIncompleteFuture(); + final CompletableFuture incomplete = incompleteCf(); assertInstanceOf(TimeoutException.class, assertThrowsExactly(CompletionException.class, () -> join(incomplete, 1, TimeUnit.MILLISECONDS) @@ -1733,22 +1731,26 @@ void test_read() { assertSame(CffuState.RUNNING, state(incomplete)); // Incomplete Future -> join before timeout - CompletableFuture later = createFutureCompleteLater(n); + CompletableFuture later = completeLaterCf(n); assertEquals(n, join(later, 3, TimeUnit.SECONDS)); } @Test void test_write() throws Exception { - assertEquals(n, completeAsync(createIncompleteFuture(), () -> n).get()); - assertEquals(n, completeAsync(createIncompleteFuture(), () -> n, commonPool()).get()); + assertEquals(n, completeAsync(incompleteCf(), () -> n).get()); + assertEquals(n, completeAsync(incompleteCf(), () -> n, commonPool()).get()); if (isJava9Plus()) { - CompletableFuture f = (CompletableFuture) completedStage(42); + CompletableFuture f = (CompletableFuture) completedStage(n); assertThrowsExactly(UnsupportedOperationException.class, () -> completeAsync(f, () -> null) ); + } else { + CompletableFuture f = (CompletableFuture) completedStage(n); + completeAsync(f, () -> null); } + assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - completeAsync(createIncompleteFuture(), () -> { + completeAsync(incompleteCf(), () -> { throw rte; }).get() ).getCause()); @@ -1759,14 +1761,18 @@ void test_write() throws Exception { //////////////////////////////////////// assertSame(rte, assertThrowsExactly(ExecutionException.class, () -> - completeExceptionallyAsync(createIncompleteFuture(), () -> rte).get() + completeExceptionallyAsync(incompleteCf(), () -> rte).get() ).getCause()); if (isJava9Plus()) { - CompletableFuture f = (CompletableFuture) completedStage(42); + CompletableFuture f = (CompletableFuture) completedStage(n); assertThrowsExactly(UnsupportedOperationException.class, () -> completeExceptionallyAsync(f, () -> rte) ); + } else { + CompletableFuture f = (CompletableFuture) completedStage(n); + completeExceptionallyAsync(f, () -> rte); } + assertEquals(n, completeExceptionallyAsync(completedFuture(n), () -> rte).get()); } @@ -1780,7 +1786,7 @@ void test_re_config() throws Exception { CompletionStage mf = minimalCompletionStage(completedFuture(n)); assertEquals(n, mf.toCompletableFuture().get()); - CompletableFuture cf = createIncompleteFuture(); + CompletableFuture cf = incompleteCf(); copy(cf).complete(n); assertFalse(cf.isDone()); @@ -1793,9 +1799,9 @@ void test_re_config() throws Exception { @Test void test_executor() { Executor executor = defaultExecutor(); - assertIsDefaultExecutor(executor); + assertIsCfDefaultExecutor(executor); - assertIsDefaultExecutor(screenExecutor(commonPool())); + assertIsCfDefaultExecutor(screenExecutor(commonPool())); ExecutorService e = Executors.newCachedThreadPool(); assertSame(e, screenExecutor(e)); @@ -1841,7 +1847,7 @@ void test_unwrapCfException() { // region# Test helper methods/fields //////////////////////////////////////////////////////////////////////////////// - private static void assertIsDefaultExecutor(Executor executor) { + private static void assertIsCfDefaultExecutor(Executor executor) { final boolean USE_COMMON_POOL = ForkJoinPool.getCommonPoolParallelism() > 1; if (USE_COMMON_POOL) { assertSame(commonPool(), executor); @@ -1851,16 +1857,12 @@ private static void assertIsDefaultExecutor(Executor executor) { } } - private static final String testName = "CompletableFutureUtilsTest"; - private static ExecutorService executorService; + // endregion + //////////////////////////////////////////////////////////////////////////////// + // region# Test helper fields + //////////////////////////////////////////////////////////////////////////////// - @BeforeAll - static void beforeAll() { - executorService = TestThreadPoolManager.createThreadPool("CffuTest"); - } + private final ExecutorService executorService = TestingExecutorUtils.getTestThreadPoolExecutor(); - @AfterAll - static void afterAll() { - TestThreadPoolManager.shutdownExecutorService(executorService); - } + private static final String testName = "CompletableFutureUtilsTest"; } 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 35c8348e7..31bb51562 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/ListenableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/ListenableFutureUtilsTest.java @@ -4,9 +4,7 @@ import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; -import io.foldright.test_utils.TestThreadPoolManager; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import io.foldright.test_utils.TestingExecutorUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.JRE; @@ -14,7 +12,7 @@ import java.util.concurrent.*; import static io.foldright.cffu.ListenableFutureUtils.*; -import static io.foldright.test_utils.CffuTestConstants.*; +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.*; @@ -242,19 +240,12 @@ static void waitForAllCfsToComplete(CompletableFuture... cfs) throws Exceptio CompletableFutureUtils.mostSuccessResultsOf(null, 2, TimeUnit.SECONDS, cfs).join(); } - private static ExecutorService executorService; - - private static CffuFactory cffuFactory; - - @BeforeAll - static void beforeAll() { - executorService = TestThreadPoolManager.createThreadPool("ListenableFutureUtilsTest"); + // endregion + //////////////////////////////////////////////////////////////////////////////// + // region# Test helper fields + //////////////////////////////////////////////////////////////////////////////// - cffuFactory = CffuFactory.builder(executorService).build(); - } + private final CffuFactory cffuFactory = TestingExecutorUtils.getTestCffuFactory(); - @AfterAll - static void afterAll() { - TestThreadPoolManager.shutdownExecutorService(executorService); - } + private final ExecutorService executorService = TestingExecutorUtils.getTestThreadPoolExecutor(); } diff --git a/cffu-core/src/test/java/io/foldright/compatibility_test/CffuApiCompatibilityTest.java b/cffu-core/src/test/java/io/foldright/compatibility_test/CffuApiCompatibilityTest.java index 5c8ec629a..97c376464 100644 --- a/cffu-core/src/test/java/io/foldright/compatibility_test/CffuApiCompatibilityTest.java +++ b/cffu-core/src/test/java/io/foldright/compatibility_test/CffuApiCompatibilityTest.java @@ -9,10 +9,10 @@ // by script gen_CffuApiCompatibilityTest.sh //////////////////////////////////////////////////////////////////////////////// +import io.foldright.test_utils.TestUtils; import io.foldright.cffu.Cffu; import io.foldright.cffu.CffuFactory; -import io.foldright.test_utils.TestThreadPoolManager; -import io.foldright.test_utils.TestUtils; +import io.foldright.test_utils.TestingExecutorUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -22,6 +22,7 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicReference; +import static io.foldright.test_utils.TestingConstants.*; import static org.junit.jupiter.api.Assertions.*; @@ -29,8 +30,6 @@ class CffuApiCompatibilityTest { private static final String hello = "Cffu API Compatibility Test - Hello"; - private static final RuntimeException rte = new RuntimeException("Bang"); - //////////////////////////////////////////////////////////////////////////////// // Static Methods //////////////////////////////////////////////////////////////////////////////// @@ -113,8 +112,8 @@ void constructor() throws Exception { Cffu cf = cffuFactory.newIncompleteCffu(); assertFalse(cf.isDone()); - cf.complete(42); - assertEquals(42, cf.get()); + cf.complete(n); + assertEquals(n, cf.get()); } //////////////////////////////////////// @@ -123,9 +122,9 @@ void constructor() throws Exception { @Test void staticMethods_allOf_anyOf() throws Exception { - Cffu f1 = cffuFactory.completedFuture(42); - Cffu f2 = cffuFactory.completedFuture(4242); - Cffu f3 = cffuFactory.completedFuture(424242); + Cffu f1 = cffuFactory.completedFuture(n); + Cffu f2 = cffuFactory.completedFuture(anotherN); + Cffu f3 = cffuFactory.completedFuture(nnn); assertNull(cffuFactory.allOf().get()); assertNull(cffuFactory.allOf(f1, f2).get()); @@ -168,7 +167,7 @@ void staticMethods_delayedExecutor() throws Exception { void simpleThenMethods() throws Exception { final AtomicReference holder = new AtomicReference<>(); - final Cffu cf = cffuFactory.completedFuture(42); + final Cffu cf = cffuFactory.completedFuture(n); cf.thenRun(() -> holder.set(hello)).get(); assertEquals(hello, holder.get()); @@ -201,12 +200,12 @@ void simpleThenMethods() throws Exception { }, anotherExecutorService).get(); assertEquals(hello, holder.get()); - assertEquals(43, cf.thenApply(x -> x + 1).get()); - assertEquals(44, cf.thenApplyAsync(x -> { + assertEquals(n + 1, cf.thenApply(x -> x + 1).get()); + assertEquals(n + 2, cf.thenApplyAsync(x -> { TestUtils.assertCffuRunInDefaultThread(executorService); return x + 2; }).get()); - assertEquals(45, cf.thenApplyAsync(x -> { + assertEquals(n + 3, cf.thenApplyAsync(x -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); return x + 3; }, anotherExecutorService).get()); @@ -216,7 +215,7 @@ void simpleThenMethods() throws Exception { void thenBoth_Methods() throws Exception { final AtomicReference holder = new AtomicReference<>(); - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); cf.runAfterBoth(cf, () -> holder.set(hello)).get(); assertEquals(hello, holder.get()); @@ -249,12 +248,12 @@ void thenBoth_Methods() throws Exception { }, anotherExecutorService).get(); assertEquals(hello, holder.get()); - assertEquals(84, cf.thenCombine(cf, Integer::sum).get()); - assertEquals(84, cf.thenCombineAsync(cf, (a, b) -> { + assertEquals(n * 2, cf.thenCombine(cf, Integer::sum).get()); + assertEquals(n * 2, cf.thenCombineAsync(cf, (a, b) -> { TestUtils.assertCffuRunInDefaultThread(executorService); return Integer.sum(a, b); }).get()); - assertEquals(84, cf.thenCombineAsync(cf, (a, b) -> { + assertEquals(n * 2, cf.thenCombineAsync(cf, (a, b) -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); return Integer.sum(a, b); }, anotherExecutorService).get()); @@ -264,7 +263,7 @@ void thenBoth_Methods() throws Exception { void thenEither_Methods() throws Exception { final AtomicReference holder = new AtomicReference<>(); - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); cf.runAfterEither(cf, () -> holder.set(hello)).get(); assertEquals(hello, holder.get()); @@ -297,12 +296,12 @@ void thenEither_Methods() throws Exception { }, anotherExecutorService).get(); assertEquals(hello, holder.get()); - assertEquals(43, cf.applyToEither(cf, x -> x + 1).get()); - assertEquals(44, cf.applyToEitherAsync(cf, x -> { + assertEquals(n + 1, cf.applyToEither(cf, x -> x + 1).get()); + assertEquals(n + 2, cf.applyToEitherAsync(cf, x -> { TestUtils.assertCffuRunInDefaultThread(executorService); return x + 2; }).get()); - assertEquals(45, cf.applyToEitherAsync(cf, x -> { + assertEquals(n + 3, cf.applyToEitherAsync(cf, x -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); return x + 3; }, anotherExecutorService).get()); @@ -310,12 +309,12 @@ void thenEither_Methods() throws Exception { @Test void errorHandling_methods() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); // exceptionally - assertEquals(42, cf.exceptionally(t -> 43).get()); - assertEquals(43, failed.exceptionally(t -> 43).get()); + assertEquals(n, cf.exceptionally(t -> anotherN).get()); + assertEquals(anotherN, failed.exceptionally(t -> anotherN).get()); // below methods is tested in below test method // - exceptionallyAsync @@ -323,37 +322,37 @@ void errorHandling_methods() throws Exception { @Test void errorHandling_methods__Java9() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); - assertEquals(42, cf.exceptionallyAsync(t -> { + assertEquals(n, cf.exceptionallyAsync(t -> { TestUtils.assertCffuRunInDefaultThread(executorService); - return 43; + return anotherN; }).get()); - assertEquals(43, failed.exceptionallyAsync(t -> { + assertEquals(anotherN, failed.exceptionallyAsync(t -> { TestUtils.assertCffuRunInDefaultThread(executorService); - return 43; + return anotherN; }).get()); - assertEquals(42, cf.exceptionallyAsync(t -> { + assertEquals(n, cf.exceptionallyAsync(t -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); - return 44; + return anotherN; }, anotherExecutorService).get()); - assertEquals(44, failed.exceptionallyAsync(t -> { + assertEquals(anotherN, failed.exceptionallyAsync(t -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); - return 44; + return anotherN; }, anotherExecutorService).get()); } @Test void timeoutControl_methods() throws Exception { // For completed - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); - assertEquals(42, + assertEquals(n, cf.orTimeout(20, TimeUnit.MILLISECONDS).get()); - assertEquals(42, - cf.completeOnTimeout(43, 20, TimeUnit.MILLISECONDS).get()); + assertEquals(n, + cf.completeOnTimeout(anotherN, 20, TimeUnit.MILLISECONDS).get()); // for incomplete Cffu incomplete = cffuFactory.newIncompleteCffu(); @@ -362,65 +361,65 @@ void timeoutControl_methods() throws Exception { ).getCause().getClass()); Cffu incomplete2 = cffuFactory.newIncompleteCffu(); - assertEquals(43, incomplete2.completeOnTimeout(43, 20, TimeUnit.MILLISECONDS).get()); + assertEquals(anotherN, incomplete2.completeOnTimeout(anotherN, 20, TimeUnit.MILLISECONDS).get()); } @Test void advancedMethods__thenCompose() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); - assertEquals(43, cf.thenCompose(x -> cffuFactory.completedFuture(43)).get()); - assertEquals(44, cf.thenComposeAsync(x -> { + assertEquals(anotherN, cf.thenCompose(x -> cffuFactory.completedFuture(anotherN)).get()); + assertEquals(anotherN, cf.thenComposeAsync(x -> { TestUtils.assertCffuRunInDefaultThread(executorService); - return cffuFactory.completedFuture(44); + return cffuFactory.completedFuture(anotherN); }).get()); - assertEquals(45, cf.thenComposeAsync(x -> { + assertEquals(anotherN, cf.thenComposeAsync(x -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); - return cffuFactory.completedFuture(45); + return cffuFactory.completedFuture(anotherN); }, anotherExecutorService).get()); } @Test void advancedMethods__exceptionallyCompose() throws Exception { // for success - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); - assertEquals(42, cf.exceptionallyCompose(x -> cffuFactory.completedFuture(43)).get()); - assertEquals(42, cf.exceptionallyComposeAsync(x -> { + assertEquals(n, cf.exceptionallyCompose(x -> cffuFactory.completedFuture(anotherN)).get()); + assertEquals(n, cf.exceptionallyComposeAsync(x -> { TestUtils.assertCffuRunInDefaultThread(executorService); - return cffuFactory.completedFuture(44); + return cffuFactory.completedFuture(anotherN); }).get()); - assertEquals(42, cf.exceptionallyComposeAsync(x -> { + assertEquals(n, cf.exceptionallyComposeAsync(x -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); - return cffuFactory.completedFuture(45); + return cffuFactory.completedFuture(anotherN); }, anotherExecutorService).get()); // for failed Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); - assertEquals(43, failed.exceptionallyCompose(x -> cffuFactory.completedFuture(43)).get()); - assertEquals(44, failed.exceptionallyComposeAsync(x -> { + assertEquals(anotherN, failed.exceptionallyCompose(x -> cffuFactory.completedFuture(anotherN)).get()); + assertEquals(anotherN, failed.exceptionallyComposeAsync(x -> { TestUtils.assertCffuRunInDefaultThread(executorService); - return cffuFactory.completedFuture(44); + return cffuFactory.completedFuture(anotherN); }).get()); - assertEquals(45, failed.exceptionallyComposeAsync(x -> { + assertEquals(anotherN, failed.exceptionallyComposeAsync(x -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); - return cffuFactory.completedFuture(45); + return cffuFactory.completedFuture(anotherN); }, anotherExecutorService).get()); } @Test void advancedMethods__handle_whenComplete() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); - assertEquals(43, cf.handle((x, e) -> 43).get()); - assertEquals(44, cf.handleAsync((x, e) -> { + assertEquals(anotherN, cf.handle((x, e) -> anotherN).get()); + assertEquals(anotherN, cf.handleAsync((x, e) -> { TestUtils.assertCffuRunInDefaultThread(executorService); - return 44; + return anotherN; }).get()); - assertEquals(45, cf.handleAsync((x, e) -> { + assertEquals(anotherN, cf.handleAsync((x, e) -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); - return 45; + return anotherN; }, anotherExecutorService).get()); final AtomicReference holder = new AtomicReference<>(); @@ -443,11 +442,11 @@ void advancedMethods__handle_whenComplete() throws Exception { @Test void readExplicitlyMethods() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); Integer r = cf.get(); - assertEquals(42, r); + assertEquals(n, r); assertSame(rte, assertThrowsExactly(ExecutionException.class, failed::get).getCause()); assertEquals(r, cf.get(1, TimeUnit.MILLISECONDS)); @@ -482,10 +481,10 @@ void readExplicitlyMethods() throws Exception { @Test void readExplicitlyMethods__Java19() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); Integer r = cf.get(); - assertEquals(42, r); + assertEquals(n, r); // resultNow assertEquals(r, cf.resultNow()); @@ -499,7 +498,7 @@ void readExplicitlyMethods__Java19() throws Exception { @Test @EnabledForJreRange(min = JRE.JAVA_19) /* GEN_MARK_KEEP */ void readExplicitlyMethods__Java19_CanNotCompatible() { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); Cffu incomplete = cffuFactory.newIncompleteCffu(); @@ -511,20 +510,20 @@ void readExplicitlyMethods__Java19_CanNotCompatible() { @Test void writeMethods() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); Cffu incomplete = cffuFactory.newIncompleteCffu(); - assertFalse(cf.complete(4242)); - assertEquals(42, cf.get()); + assertFalse(cf.complete(anotherN)); + assertEquals(n, cf.get()); - assertTrue(incomplete.complete(4242)); - assertEquals(4242, incomplete.get()); + assertTrue(incomplete.complete(anotherN)); + assertEquals(anotherN, incomplete.get()); // below methods is tested in below test method // - completeAsync // completeExceptionally assertFalse(cf.completeExceptionally(rte)); - assertEquals(42, cf.get()); + assertEquals(n, cf.get()); incomplete = cffuFactory.newIncompleteCffu(); assertTrue(incomplete.completeExceptionally(rte)); @@ -538,27 +537,27 @@ void writeMethods() throws Exception { @Test void writeMethods__Java9() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); // completeAsync - assertEquals(42, cf.completeAsync(() -> { + assertEquals(n, cf.completeAsync(() -> { TestUtils.assertCffuRunInDefaultThread(executorService); - return 4242; + return anotherN; }).get()); - assertEquals(42, cf.completeAsync(() -> { + assertEquals(n, cf.completeAsync(() -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); - return 424242; + return anotherN; }, anotherExecutorService).get()); Cffu incomplete = cffuFactory.newIncompleteCffu(); - assertEquals(4242, incomplete.completeAsync(() -> { + assertEquals(anotherN, incomplete.completeAsync(() -> { TestUtils.assertCffuRunInDefaultThread(executorService); - return 4242; + return anotherN; }).get()); incomplete = cffuFactory.newIncompleteCffu(); - assertEquals(424242, incomplete.completeAsync(() -> { + assertEquals(anotherN, incomplete.completeAsync(() -> { TestUtils.assertCffuRunInThreadOf(anotherExecutorService); - return 424242; + return anotherN; }, anotherExecutorService).get()); RuntimeException ex = new RuntimeException(); @@ -579,26 +578,26 @@ void reConfigMethods() throws Exception { // - minimalCompletionStage // - copy - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); // toCompletableFuture - assertEquals(42, cf.toCompletableFuture().get()); + assertEquals(n, cf.toCompletableFuture().get()); } @Test void reConfigMethods__Java9() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); // minimalCompletionStage TestUtils.shouldBeMinimalStage((Cffu) cf.minimalCompletionStage()); // copy - assertEquals(42, cf.copy().get()); + assertEquals(n, cf.copy().get()); } @Test void test_getterMethods() { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); // defaultExecutor assertNotNull(cf.defaultExecutor()); @@ -606,7 +605,7 @@ void test_getterMethods() { @Test void test_inspectionMethods() { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); // getNumberOfDependents assertNotEquals(-1, cf.getNumberOfDependents()); @@ -614,11 +613,11 @@ void test_inspectionMethods() { @Test void test_dangerousMethods() throws Exception { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); // obtrudeValue - cf.obtrudeValue(44); - assertEquals(44, cf.get()); + cf.obtrudeValue(anotherN); + assertEquals(anotherN, cf.get()); // obtrudeException cf.obtrudeException(rte); @@ -627,7 +626,7 @@ void test_dangerousMethods() throws Exception { @Test void test_forApiCompatibility() { - Cffu cf = cffuFactory.completedFuture(42); + Cffu cf = cffuFactory.completedFuture(n); // newIncompleteFuture assertFalse(cf.newIncompleteFuture().isDone()); @@ -663,10 +662,10 @@ void test_similarities_and_differences_between_cancelled_and_exceptionally() thr assertSame(rte, assertThrowsExactly(CompletionException.class, exceptionallyCf::join).getCause()); assertThrowsExactly(CancellationException.class, () -> - cancelledCf.getNow(42) + cancelledCf.getNow(n) ); assertSame(rte, assertThrowsExactly(CompletionException.class, () -> - exceptionallyCf.getNow(42) + exceptionallyCf.getNow(n) ).getCause()); assertTrue(cancelledCf.isCancelled()); @@ -682,14 +681,14 @@ void test_similarities_and_differences_between_cancelled_and_exceptionally() thr assertTrue(cancelledCf.isDone()); assertTrue(exceptionallyCf.isDone()); - assertEquals(42, cancelledCf.exceptionally(throwable -> { + assertEquals(n, cancelledCf.exceptionally(throwable -> { assertInstanceOf(CancellationException.class, throwable); assertNull(throwable.getCause()); - return 42; + return n; }).get()); - assertEquals(42, exceptionallyCf.exceptionally(throwable -> { + assertEquals(n, exceptionallyCf.exceptionally(throwable -> { assertSame(rte, throwable); - return 42; + return n; }).get()); } @@ -723,7 +722,7 @@ void nested_exception() throws Exception { // auto unwrap first level CompletionException checkNo1MoreLevelForCompletionException(cf); - cf = cffuFactory.completedFuture(42); + cf = cffuFactory.completedFuture(n); // auto unwrap first level CompletionException checkNo1MoreLevelForCompletionException(cf.thenRun(() -> { throw new CompletionException(rte); @@ -734,7 +733,7 @@ void nested_exception() throws Exception { // auto unwrap first level ExecutionException check1MoreLevelForExecutionException(cf); - cf = cffuFactory.completedFuture(42); + cf = cffuFactory.completedFuture(n); // auto unwrap first level ExecutionException check1MoreLevelForExecutionException(cf.thenRun(() -> TestUtils.sneakyThrow(new ExecutionException(rte)) @@ -770,14 +769,16 @@ private void check1MoreLevelForExecutionException(Cffu cf) throws Excepti @BeforeAll static void beforeAll() { - executorService = TestThreadPoolManager.createThreadPool(hello); + executorService = TestingExecutorUtils.createThreadPool(hello); cffuFactory = CffuFactory.builder(executorService).build(); - anotherExecutorService = TestThreadPoolManager.createThreadPool(hello, true); + anotherExecutorService = TestingExecutorUtils.createThreadPool(hello, true); + + TestingExecutorUtils.warmupExecutorService(executorService, anotherExecutorService); } @AfterAll static void afterAll() { - TestThreadPoolManager.shutdownExecutorService(executorService, anotherExecutorService); + TestingExecutorUtils.shutdownExecutorService(executorService, anotherExecutorService); } } diff --git a/cffu-core/src/test/java/io/foldright/compatibility_test/CompletableFutureApiCompatibilityTest.java b/cffu-core/src/test/java/io/foldright/compatibility_test/CompletableFutureApiCompatibilityTest.java index 7fb112436..8d5059275 100644 --- a/cffu-core/src/test/java/io/foldright/compatibility_test/CompletableFutureApiCompatibilityTest.java +++ b/cffu-core/src/test/java/io/foldright/compatibility_test/CompletableFutureApiCompatibilityTest.java @@ -9,8 +9,8 @@ // by script gen_CffuApiCompatibilityTest.sh //////////////////////////////////////////////////////////////////////////////// -import io.foldright.test_utils.TestThreadPoolManager; import io.foldright.test_utils.TestUtils; +import io.foldright.test_utils.TestingExecutorUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -20,6 +20,7 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicReference; +import static io.foldright.test_utils.TestingConstants.*; import static org.junit.jupiter.api.Assertions.*; @@ -27,8 +28,6 @@ class CompletableFutureApiCompatibilityTest { private static final String hello = "CompletableFuture API Compatibility Test - Hello"; - private static final RuntimeException rte = new RuntimeException("Bang"); - //////////////////////////////////////////////////////////////////////////////// // Static Methods //////////////////////////////////////////////////////////////////////////////// @@ -112,8 +111,8 @@ void constructor() throws Exception { CompletableFuture cf = new CompletableFuture<>(); assertFalse(cf.isDone()); - cf.complete(42); - assertEquals(42, cf.get()); + cf.complete(n); + assertEquals(n, cf.get()); } //////////////////////////////////////// @@ -122,9 +121,9 @@ void constructor() throws Exception { @Test void staticMethods_allOf_anyOf() throws Exception { - CompletableFuture f1 = CompletableFuture.completedFuture(42); - CompletableFuture f2 = CompletableFuture.completedFuture(4242); - CompletableFuture f3 = CompletableFuture.completedFuture(424242); + CompletableFuture f1 = CompletableFuture.completedFuture(n); + CompletableFuture f2 = CompletableFuture.completedFuture(anotherN); + CompletableFuture f3 = CompletableFuture.completedFuture(nnn); assertNull(CompletableFuture.allOf().get()); assertNull(CompletableFuture.allOf(f1, f2).get()); @@ -168,7 +167,7 @@ void staticMethods_delayedExecutor() throws Exception { void simpleThenMethods() throws Exception { final AtomicReference holder = new AtomicReference<>(); - final CompletableFuture cf = CompletableFuture.completedFuture(42); + final CompletableFuture cf = CompletableFuture.completedFuture(n); cf.thenRun(() -> holder.set(hello)).get(); assertEquals(hello, holder.get()); @@ -201,12 +200,12 @@ void simpleThenMethods() throws Exception { }, anotherExecutorService).get(); assertEquals(hello, holder.get()); - assertEquals(43, cf.thenApply(x -> x + 1).get()); - assertEquals(44, cf.thenApplyAsync(x -> { + assertEquals(n + 1, cf.thenApply(x -> x + 1).get()); + assertEquals(n + 2, cf.thenApplyAsync(x -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); return x + 2; }).get()); - assertEquals(45, cf.thenApplyAsync(x -> { + assertEquals(n + 3, cf.thenApplyAsync(x -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return x + 3; }, anotherExecutorService).get()); @@ -216,7 +215,7 @@ void simpleThenMethods() throws Exception { void thenBoth_Methods() throws Exception { final AtomicReference holder = new AtomicReference<>(); - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); cf.runAfterBoth(cf, () -> holder.set(hello)).get(); assertEquals(hello, holder.get()); @@ -249,12 +248,12 @@ void thenBoth_Methods() throws Exception { }, anotherExecutorService).get(); assertEquals(hello, holder.get()); - assertEquals(84, cf.thenCombine(cf, Integer::sum).get()); - assertEquals(84, cf.thenCombineAsync(cf, (a, b) -> { + assertEquals(n * 2, cf.thenCombine(cf, Integer::sum).get()); + assertEquals(n * 2, cf.thenCombineAsync(cf, (a, b) -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); return Integer.sum(a, b); }).get()); - assertEquals(84, cf.thenCombineAsync(cf, (a, b) -> { + assertEquals(n * 2, cf.thenCombineAsync(cf, (a, b) -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return Integer.sum(a, b); }, anotherExecutorService).get()); @@ -264,7 +263,7 @@ void thenBoth_Methods() throws Exception { void thenEither_Methods() throws Exception { final AtomicReference holder = new AtomicReference<>(); - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); cf.runAfterEither(cf, () -> holder.set(hello)).get(); assertEquals(hello, holder.get()); @@ -297,12 +296,12 @@ void thenEither_Methods() throws Exception { }, anotherExecutorService).get(); assertEquals(hello, holder.get()); - assertEquals(43, cf.applyToEither(cf, x -> x + 1).get()); - assertEquals(44, cf.applyToEitherAsync(cf, x -> { + assertEquals(n + 1, cf.applyToEither(cf, x -> x + 1).get()); + assertEquals(n + 2, cf.applyToEitherAsync(cf, x -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); return x + 2; }).get()); - assertEquals(45, cf.applyToEitherAsync(cf, x -> { + assertEquals(n + 3, cf.applyToEitherAsync(cf, x -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return x + 3; }, anotherExecutorService).get()); @@ -310,12 +309,12 @@ void thenEither_Methods() throws Exception { @Test void errorHandling_methods() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); // exceptionally - assertEquals(42, cf.exceptionally(t -> 43).get()); - assertEquals(43, failed.exceptionally(t -> 43).get()); + assertEquals(n, cf.exceptionally(t -> anotherN).get()); + assertEquals(anotherN, failed.exceptionally(t -> anotherN).get()); // below methods is tested in below test method // - exceptionallyAsync @@ -324,25 +323,25 @@ void errorHandling_methods() throws Exception { @Test @EnabledForJreRange(min = JRE.JAVA_12) void errorHandling_methods__Java9() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); - assertEquals(42, cf.exceptionallyAsync(t -> { + assertEquals(n, cf.exceptionallyAsync(t -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); - return 43; + return anotherN; }).get()); - assertEquals(43, failed.exceptionallyAsync(t -> { + assertEquals(anotherN, failed.exceptionallyAsync(t -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); - return 43; + return anotherN; }).get()); - assertEquals(42, cf.exceptionallyAsync(t -> { + assertEquals(n, cf.exceptionallyAsync(t -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); - return 44; + return anotherN; }, anotherExecutorService).get()); - assertEquals(44, failed.exceptionallyAsync(t -> { + assertEquals(anotherN, failed.exceptionallyAsync(t -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); - return 44; + return anotherN; }, anotherExecutorService).get()); } @@ -350,12 +349,12 @@ void errorHandling_methods__Java9() throws Exception { @EnabledForJreRange(min = JRE.JAVA_9) void timeoutControl_methods() throws Exception { // For completed - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); - assertEquals(42, + assertEquals(n, cf.orTimeout(20, TimeUnit.MILLISECONDS).get()); - assertEquals(42, - cf.completeOnTimeout(43, 20, TimeUnit.MILLISECONDS).get()); + assertEquals(n, + cf.completeOnTimeout(anotherN, 20, TimeUnit.MILLISECONDS).get()); // for incomplete CompletableFuture incomplete = new CompletableFuture<>(); @@ -364,21 +363,21 @@ void timeoutControl_methods() throws Exception { ).getCause().getClass()); CompletableFuture incomplete2 = new CompletableFuture<>(); - assertEquals(43, incomplete2.completeOnTimeout(43, 20, TimeUnit.MILLISECONDS).get()); + assertEquals(anotherN, incomplete2.completeOnTimeout(anotherN, 20, TimeUnit.MILLISECONDS).get()); } @Test void advancedMethods__thenCompose() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); - assertEquals(43, cf.thenCompose(x -> CompletableFuture.completedFuture(43)).get()); - assertEquals(44, cf.thenComposeAsync(x -> { + assertEquals(anotherN, cf.thenCompose(x -> CompletableFuture.completedFuture(anotherN)).get()); + assertEquals(anotherN, cf.thenComposeAsync(x -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); - return CompletableFuture.completedFuture(44); + return CompletableFuture.completedFuture(anotherN); }).get()); - assertEquals(45, cf.thenComposeAsync(x -> { + assertEquals(anotherN, cf.thenComposeAsync(x -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); - return CompletableFuture.completedFuture(45); + return CompletableFuture.completedFuture(anotherN); }, anotherExecutorService).get()); } @@ -386,44 +385,44 @@ void advancedMethods__thenCompose() throws Exception { @EnabledForJreRange(min = JRE.JAVA_12) void advancedMethods__exceptionallyCompose() throws Exception { // for success - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); - assertEquals(42, cf.exceptionallyCompose(x -> CompletableFuture.completedFuture(43)).get()); - assertEquals(42, cf.exceptionallyComposeAsync(x -> { + assertEquals(n, cf.exceptionallyCompose(x -> CompletableFuture.completedFuture(anotherN)).get()); + assertEquals(n, cf.exceptionallyComposeAsync(x -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); - return CompletableFuture.completedFuture(44); + return CompletableFuture.completedFuture(anotherN); }).get()); - assertEquals(42, cf.exceptionallyComposeAsync(x -> { + assertEquals(n, cf.exceptionallyComposeAsync(x -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); - return CompletableFuture.completedFuture(45); + return CompletableFuture.completedFuture(anotherN); }, anotherExecutorService).get()); // for failed CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); - assertEquals(43, failed.exceptionallyCompose(x -> CompletableFuture.completedFuture(43)).get()); - assertEquals(44, failed.exceptionallyComposeAsync(x -> { + assertEquals(anotherN, failed.exceptionallyCompose(x -> CompletableFuture.completedFuture(anotherN)).get()); + assertEquals(anotherN, failed.exceptionallyComposeAsync(x -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); - return CompletableFuture.completedFuture(44); + return CompletableFuture.completedFuture(anotherN); }).get()); - assertEquals(45, failed.exceptionallyComposeAsync(x -> { + assertEquals(anotherN, failed.exceptionallyComposeAsync(x -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); - return CompletableFuture.completedFuture(45); + return CompletableFuture.completedFuture(anotherN); }, anotherExecutorService).get()); } @Test void advancedMethods__handle_whenComplete() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); - assertEquals(43, cf.handle((x, e) -> 43).get()); - assertEquals(44, cf.handleAsync((x, e) -> { + assertEquals(anotherN, cf.handle((x, e) -> anotherN).get()); + assertEquals(anotherN, cf.handleAsync((x, e) -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); - return 44; + return anotherN; }).get()); - assertEquals(45, cf.handleAsync((x, e) -> { + assertEquals(anotherN, cf.handleAsync((x, e) -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); - return 45; + return anotherN; }, anotherExecutorService).get()); final AtomicReference holder = new AtomicReference<>(); @@ -446,11 +445,11 @@ void advancedMethods__handle_whenComplete() throws Exception { @Test void readExplicitlyMethods() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); Integer r = cf.get(); - assertEquals(42, r); + assertEquals(n, r); assertSame(rte, assertThrowsExactly(ExecutionException.class, failed::get).getCause()); assertEquals(r, cf.get(1, TimeUnit.MILLISECONDS)); @@ -486,10 +485,10 @@ void readExplicitlyMethods() throws Exception { @Test @EnabledForJreRange(min = JRE.JAVA_19) void readExplicitlyMethods__Java19() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); Integer r = cf.get(); - assertEquals(42, r); + assertEquals(n, r); // resultNow assertEquals(r, cf.resultNow()); @@ -503,7 +502,7 @@ void readExplicitlyMethods__Java19() throws Exception { @Test @EnabledForJreRange(min = JRE.JAVA_19) /* GEN_MARK_KEEP */ void readExplicitlyMethods__Java19_CanNotCompatible() { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); CompletableFuture incomplete = new CompletableFuture<>(); @@ -515,20 +514,20 @@ void readExplicitlyMethods__Java19_CanNotCompatible() { @Test void writeMethods() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); CompletableFuture incomplete = new CompletableFuture<>(); - assertFalse(cf.complete(4242)); - assertEquals(42, cf.get()); + assertFalse(cf.complete(anotherN)); + assertEquals(n, cf.get()); - assertTrue(incomplete.complete(4242)); - assertEquals(4242, incomplete.get()); + assertTrue(incomplete.complete(anotherN)); + assertEquals(anotherN, incomplete.get()); // below methods is tested in below test method // - completeAsync // completeExceptionally assertFalse(cf.completeExceptionally(rte)); - assertEquals(42, cf.get()); + assertEquals(n, cf.get()); incomplete = new CompletableFuture<>(); assertTrue(incomplete.completeExceptionally(rte)); @@ -543,27 +542,27 @@ void writeMethods() throws Exception { @Test @EnabledForJreRange(min = JRE.JAVA_9) void writeMethods__Java9() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); // completeAsync - assertEquals(42, cf.completeAsync(() -> { + assertEquals(n, cf.completeAsync(() -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); - return 4242; + return anotherN; }).get()); - assertEquals(42, cf.completeAsync(() -> { + assertEquals(n, cf.completeAsync(() -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); - return 424242; + return anotherN; }, anotherExecutorService).get()); CompletableFuture incomplete = new CompletableFuture<>(); - assertEquals(4242, incomplete.completeAsync(() -> { + assertEquals(anotherN, incomplete.completeAsync(() -> { TestUtils.assertCompletableFutureRunInDefaultThread(executorService); - return 4242; + return anotherN; }).get()); incomplete = new CompletableFuture<>(); - assertEquals(424242, incomplete.completeAsync(() -> { + assertEquals(anotherN, incomplete.completeAsync(() -> { TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); - return 424242; + return anotherN; }, anotherExecutorService).get()); RuntimeException ex = new RuntimeException(); @@ -584,28 +583,28 @@ void reConfigMethods() throws Exception { // - minimalCompletionStage // - copy - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); // toCompletableFuture - assertEquals(42, cf.toCompletableFuture().get()); + assertEquals(n, cf.toCompletableFuture().get()); } @Test @EnabledForJreRange(min = JRE.JAVA_9) void reConfigMethods__Java9() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); // minimalCompletionStage TestUtils.shouldBeMinimalStage((CompletableFuture) cf.minimalCompletionStage()); // copy - assertEquals(42, cf.copy().get()); + assertEquals(n, cf.copy().get()); } @Test @EnabledForJreRange(min = JRE.JAVA_9) void test_getterMethods() { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); // defaultExecutor assertNotNull(cf.defaultExecutor()); @@ -613,7 +612,7 @@ void test_getterMethods() { @Test void test_inspectionMethods() { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); // getNumberOfDependents assertNotEquals(-1, cf.getNumberOfDependents()); @@ -621,11 +620,11 @@ void test_inspectionMethods() { @Test void test_dangerousMethods() throws Exception { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); // obtrudeValue - cf.obtrudeValue(44); - assertEquals(44, cf.get()); + cf.obtrudeValue(anotherN); + assertEquals(anotherN, cf.get()); // obtrudeException cf.obtrudeException(rte); @@ -635,7 +634,7 @@ void test_dangerousMethods() throws Exception { @Test @EnabledForJreRange(min = JRE.JAVA_9) void test_forApiCompatibility() { - CompletableFuture cf = CompletableFuture.completedFuture(42); + CompletableFuture cf = CompletableFuture.completedFuture(n); // newIncompleteFuture assertFalse(cf.newIncompleteFuture().isDone()); @@ -671,10 +670,10 @@ void test_similarities_and_differences_between_cancelled_and_exceptionally() thr assertSame(rte, assertThrowsExactly(CompletionException.class, exceptionallyCf::join).getCause()); assertThrowsExactly(CancellationException.class, () -> - cancelledCf.getNow(42) + cancelledCf.getNow(n) ); assertSame(rte, assertThrowsExactly(CompletionException.class, () -> - exceptionallyCf.getNow(42) + exceptionallyCf.getNow(n) ).getCause()); assertTrue(cancelledCf.isCancelled()); @@ -690,14 +689,14 @@ void test_similarities_and_differences_between_cancelled_and_exceptionally() thr assertTrue(cancelledCf.isDone()); assertTrue(exceptionallyCf.isDone()); - assertEquals(42, cancelledCf.exceptionally(throwable -> { + assertEquals(n, cancelledCf.exceptionally(throwable -> { assertInstanceOf(CancellationException.class, throwable); assertNull(throwable.getCause()); - return 42; + return n; }).get()); - assertEquals(42, exceptionallyCf.exceptionally(throwable -> { + assertEquals(n, exceptionallyCf.exceptionally(throwable -> { assertSame(rte, throwable); - return 42; + return n; }).get()); } @@ -732,7 +731,7 @@ void nested_exception() throws Exception { // auto unwrap first level CompletionException checkNo1MoreLevelForCompletionException(cf); - cf = CompletableFuture.completedFuture(42); + cf = CompletableFuture.completedFuture(n); // auto unwrap first level CompletionException checkNo1MoreLevelForCompletionException(cf.thenRun(() -> { throw new CompletionException(rte); @@ -743,7 +742,7 @@ void nested_exception() throws Exception { // auto unwrap first level ExecutionException check1MoreLevelForExecutionException(cf); - cf = CompletableFuture.completedFuture(42); + cf = CompletableFuture.completedFuture(n); // auto unwrap first level ExecutionException check1MoreLevelForExecutionException(cf.thenRun(() -> TestUtils.sneakyThrow(new ExecutionException(rte)) @@ -779,14 +778,16 @@ private void check1MoreLevelForExecutionException(CompletableFuture cf) t @BeforeAll static void beforeAll() { - executorService = TestThreadPoolManager.createThreadPool(hello); + executorService = TestingExecutorUtils.createThreadPool(hello); /* GEN_MARK_FACTORY_INIT */ - anotherExecutorService = TestThreadPoolManager.createThreadPool(hello, true); + anotherExecutorService = TestingExecutorUtils.createThreadPool(hello, true); + + TestingExecutorUtils.warmupExecutorService(executorService, anotherExecutorService); } @AfterAll static void afterAll() { - TestThreadPoolManager.shutdownExecutorService(executorService, anotherExecutorService); + TestingExecutorUtils.shutdownExecutorService(executorService, anotherExecutorService); } } 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 78342991b..8eeb9de1a 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 @@ -17,31 +17,25 @@ import java.util.concurrent.* //////////////////////////////////////////////////////////////////////////////// -// util methods for testing +// region# util methods for testing //////////////////////////////////////////////////////////////////////////////// -fun createIncompleteFuture(): CompletableFuture = CompletableFuture() +fun incompleteCf(): CompletableFuture = CompletableFuture() @JvmOverloads -fun createFutureCompleteLater(value: T, millis: Long = 100): CompletableFuture = CompletableFuture.supplyAsync { +fun completeLaterCf(value: T, millis: Long = 100): CompletableFuture = CompletableFuture.supplyAsync { sleep(millis) value } -fun createCancelledFuture(): CompletableFuture = CompletableFuture().apply { - cancel(false) -} - -/** - * sleep without throwing checked exception - */ @JvmOverloads -fun sleep(millis: Long = 10) { - Thread.sleep(millis) +fun cancelledFuture(mayInterruptIfRunning: Boolean = false): CompletableFuture = CompletableFuture().apply { + cancel(mayInterruptIfRunning) } +// endregion //////////////////////////////////////////////////////////////////////////////// -// Helper functions for api compatibility test: +// region# Helper functions for API compatibility test // - CompletableFutureApiCompatibilityTest // - CffuApiCompatibilityTest //////////////////////////////////////////////////////////////////////////////// @@ -63,9 +57,8 @@ fun safeNewFailedCffu(executorService: ExecutorService, t: Throwable): Cffu< return CffuFactory.builder(executorService).build().failedFuture(t) } -@Suppress("UNUSED_PARAMETER") fun assertCompletableFutureRunInDefaultThread(executorService: ExecutorService) { - // do nothing + assertNotRunningInExecutor(executorService) } fun assertCompletableFutureRunInThreadOf(executorService: ExecutorService) { @@ -80,10 +73,21 @@ fun assertCffuRunInThreadOf(executorService: ExecutorService) { assertRunningInExecutor(executorService) } +// endregion //////////////////////////////////////////////////////////////////////////////// -// Simple util functions +// region# some simple util functions //////////////////////////////////////////////////////////////////////////////// +/** + * sleep without throwing checked exception + */ +@JvmOverloads +fun sleep(millis: Long = 10) { + Thread.sleep(millis) +} + +fun sneakyThrow(ex: Throwable): Unit = throw ex + fun merge(list1: List, list2: List) = list1.merge(list2) @JvmName("mergeExt") @@ -94,10 +98,9 @@ infix fun List.merge(other: List): List = mutableListOf().apply fun addCurrentThreadName(names: List) = names + Thread.currentThread().name -fun sneakyThrow(ex: Throwable): Unit = throw ex - +// endregion //////////////////////////////////////////////////////////////////////////////// -// Assertion functions for CF/Cffu +// region# Assertion functions for CF/Cffu //////////////////////////////////////////////////////////////////////////////// fun CompletableFuture.shouldBeMinimalStage() { @@ -741,8 +744,9 @@ private fun CompletionStage.shouldCompletionStageMethodsAllowed(recursive toString() } +// endregion //////////////////////////////////////////////////////////////////////////////// -// Java Version Checker +// region# Java Version Checker //////////////////////////////////////////////////////////////////////////////// fun isJava9Plus() = isJavaVersionAtLeast(JavaVersion.JAVA_9) @@ -755,8 +759,9 @@ fun isJava19Plus(): Boolean = isJavaVersionAtLeast(JavaVersion.JAVA_19) // https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables fun isCiEnv(): Boolean = System.getenv("CI")?.lowercase() == "true" +// endregion //////////////////////////////////////////////////////////////////////////////// -// Kotest conditions +// region# Kotest conditions //////////////////////////////////////////////////////////////////////////////// val java9Plus: (TestCase) -> Boolean = { isJava9Plus() } diff --git a/cffu-core/src/test/java/io/foldright/test_utils/CffuTestConstants.kt b/cffu-core/src/test/java/io/foldright/test_utils/TestingConstants.kt similarity index 78% rename from cffu-core/src/test/java/io/foldright/test_utils/CffuTestConstants.kt rename to cffu-core/src/test/java/io/foldright/test_utils/TestingConstants.kt index bdc4eebe8..a5fe03842 100644 --- a/cffu-core/src/test/java/io/foldright/test_utils/CffuTestConstants.kt +++ b/cffu-core/src/test/java/io/foldright/test_utils/TestingConstants.kt @@ -1,4 +1,4 @@ -@file:JvmName("CffuTestConstants") +@file:JvmName("TestingConstants") package io.foldright.test_utils @@ -8,9 +8,10 @@ package io.foldright.test_utils //////////////////////////////////////////////////////////////////////////////// const val n = 42 -const val anotherN = 424242 +const val anotherN = 4242 +const val nnn = 424242 const val s = "S42" -const val d = 42.1 +const val d = 42.5 @JvmField val rte = RuntimeException("Bang") diff --git a/cffu-core/src/test/java/io/foldright/test_utils/TestThreadPoolManager.kt b/cffu-core/src/test/java/io/foldright/test_utils/TestingExecutorUtils.kt similarity index 93% rename from cffu-core/src/test/java/io/foldright/test_utils/TestThreadPoolManager.kt rename to cffu-core/src/test/java/io/foldright/test_utils/TestingExecutorUtils.kt index db9c4573d..56e61df79 100644 --- a/cffu-core/src/test/java/io/foldright/test_utils/TestThreadPoolManager.kt +++ b/cffu-core/src/test/java/io/foldright/test_utils/TestingExecutorUtils.kt @@ -1,10 +1,9 @@ -@file:JvmName("TestThreadPoolManager") +@file:JvmName("TestingExecutorUtils") package io.foldright.test_utils import io.foldright.cffu.CffuFactory import io.kotest.core.config.AbstractProjectConfig -import io.kotest.core.listeners.AfterProjectListener import io.kotest.core.listeners.BeforeProjectListener import io.kotest.matchers.booleans.shouldBeFalse import io.kotest.matchers.booleans.shouldBeTrue @@ -15,7 +14,7 @@ import kotlin.random.Random import kotlin.random.nextULong //////////////////////////////////////////////////////////////////////////////// -// region## test executors for kotest +// region# test executors for kotest //////////////////////////////////////////////////////////////////////////////// val THREAD_COUNT_OF_POOL: Int = (Runtime.getRuntime().availableProcessors() * 2).coerceAtLeast(4).coerceAtMost(15) @@ -38,7 +37,7 @@ fun createThreadPool(threadNamePrefix: String, isForkJoin: Boolean = false): Exe val executorService = if (!isForkJoin) ThreadPoolExecutor( /* corePoolSize = */ THREAD_COUNT_OF_POOL, /* maximumPoolSize = */ THREAD_COUNT_OF_POOL, - /* keepAliveTime = */ 1, /* unit = */ TimeUnit.DAYS, + /* keepAliveTime = */ 2, TimeUnit.MINUTES, /* workQueue = */ ArrayBlockingQueue(5000) ) { r -> Thread(r).apply { @@ -123,9 +122,10 @@ fun shutdownExecutorService(vararg executors: ExecutorService) { /** * https://kotest.io/docs/framework/project-config.html */ -object CffuKotestProjectConfig : AbstractProjectConfig(), BeforeProjectListener, AfterProjectListener { +object CffuKotestProjectConfig : AbstractProjectConfig(), BeforeProjectListener { override suspend fun beforeProject() { println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") + println("call beforeProject") println("Env Infos:") println("Parallelism of ForkJoinPool: ${ForkJoinPool.getCommonPoolParallelism()}") println("Available Processors of Runtime: ${Runtime.getRuntime().availableProcessors()}") @@ -136,8 +136,4 @@ object CffuKotestProjectConfig : AbstractProjectConfig(), BeforeProjectListener, warmupExecutorService(testThreadPoolExecutor, testForkJoinPoolExecutor) } - - override suspend fun afterProject() { - shutdownExecutorService(testThreadPoolExecutor, testForkJoinPoolExecutor) - } } diff --git a/cffu-kotlin/src/test/java/io/foldright/cffu/generics/TypeParameterDeclarationTest.java b/cffu-kotlin/src/test/java/io/foldright/cffu/generics/TypeParameterDeclarationTest.java index 17d0671f4..9545bee5f 100644 --- a/cffu-kotlin/src/test/java/io/foldright/cffu/generics/TypeParameterDeclarationTest.java +++ b/cffu-kotlin/src/test/java/io/foldright/cffu/generics/TypeParameterDeclarationTest.java @@ -1,7 +1,7 @@ package io.foldright.cffu.generics; import io.foldright.cffu.CompletableFutureUtils; -import io.foldright.test_utils.TestThreadPoolManager; +import io.foldright.test_utils.TestingExecutorUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -66,11 +66,11 @@ void checkTypeParameterDeclaration_get() throws Exception { @BeforeAll static void beforeAll() { - executorService = TestThreadPoolManager.createThreadPool("TypeParameterDeclarationTest"); + executorService = TestingExecutorUtils.createThreadPool("TypeParameterDeclarationTest"); } @AfterAll static void afterAll() { - TestThreadPoolManager.shutdownExecutorService(executorService); + TestingExecutorUtils.shutdownExecutorService(executorService); } } diff --git a/cffu-kotlin/src/test/java/io/foldright/cffu/test/CompletableFutureExtensionsTest.kt b/cffu-kotlin/src/test/java/io/foldright/cffu/test/CompletableFutureExtensionsTest.kt index 7ef18ce62..8b268f9b7 100644 --- a/cffu-kotlin/src/test/java/io/foldright/cffu/test/CompletableFutureExtensionsTest.kt +++ b/cffu-kotlin/src/test/java/io/foldright/cffu/test/CompletableFutureExtensionsTest.kt @@ -425,15 +425,15 @@ class CompletableFutureExtensionsTest : FunSpec({ } test("write methods") { - createIncompleteFuture().completeAsync { n }.get() shouldBe n - createIncompleteFuture().completeAsync({ n }, testThreadPoolExecutor).get() shouldBe n + incompleteCf().completeAsync { n }.get() shouldBe n + incompleteCf().completeAsync({ n }, testThreadPoolExecutor).get() shouldBe n shouldThrowExactly { - createIncompleteFuture().completeExceptionallyAsync { rte }.get() + incompleteCf().completeExceptionallyAsync { rte }.get() }.cause shouldBeSameInstanceAs rte shouldThrowExactly { - createIncompleteFuture().completeExceptionallyAsync({ rte }, testThreadPoolExecutor).get() + incompleteCf().completeExceptionallyAsync({ rte }, testThreadPoolExecutor).get() }.cause shouldBeSameInstanceAs rte } diff --git a/scripts/gen_CffuApiCompatibilityTest.sh b/scripts/gen_CffuApiCompatibilityTest.sh index 50ef8f6a3..166e4c059 100755 --- a/scripts/gen_CffuApiCompatibilityTest.sh +++ b/scripts/gen_CffuApiCompatibilityTest.sh @@ -14,7 +14,7 @@ sed -r '/^import /,${ /\bGEN_MARK_KEEP\b/b # adjust imports - s/import io\.foldright\.test_utils\.TestThreadPoolManager;/import io.foldright.cffu.Cffu;\nimport io.foldright.cffu.CffuFactory;\nimport io.foldright.test_utils.TestThreadPoolManager;/ + s/import io\.foldright\.test_utils\.TestingExecutorUtils;/import io.foldright.cffu.Cffu;\nimport io.foldright.cffu.CffuFactory;\nimport io.foldright.test_utils.TestingExecutorUtils;/ /import java.util.concurrent.CompletableFuture;/d # replace JUnit test class name