diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bdc900fb..997a5562 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -58,7 +58,7 @@ jobs: JAVA21_HOME: ${{ env.JAVA_HOME_21_X64 }} - name: remove self maven install files - run: rm -rf $HOME/.m2/repository/io/foldright/cffu/ + run: rm -rf $HOME/.m2/repository/io/foldright/cffu* - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v3 diff --git a/.github/workflows/fast_ci.yaml b/.github/workflows/fast_ci.yaml index a29d3f52..6644c809 100644 --- a/.github/workflows/fast_ci.yaml +++ b/.github/workflows/fast_ci.yaml @@ -2,7 +2,7 @@ # https://docs.github.com/en/actions/quickstart name: Fast CI -on: [push, pull_request, workflow_dispatch] +on: [ push, pull_request, workflow_dispatch ] jobs: test: # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#choosing-github-hosted-runners @@ -18,23 +18,29 @@ jobs: steps: - uses: actions/checkout@v3 - - name: setup Java 8 - uses: actions/setup-java@v3 - with: - java-version: 8 - distribution: zulu - # only first java setup need enable cache - cache: maven - name: setup Java 19 uses: actions/setup-java@v3 with: java-version: 19 distribution: temurin - + # only first java setup need enable cache + cache: maven - name: build and test with Java 19 - run: ./mvnw -V --no-transfer-progress clean package + run: ./mvnw -V --no-transfer-progress clean install + + - name: setup Java 8 + uses: actions/setup-java@v3 + with: + java-version: 8 + distribution: zulu - name: test with Java 8 - run: ../mvnw -V --no-transfer-progress surefire:test - working-directory: cffu-core - env: - JAVA_HOME: ${{ env.JAVA_HOME_8_X64 }} + run: ./mvnw -V --no-transfer-progress surefire:test + + - name: 'remove self maven install files(OS: *nix)' + run: rm -rf $HOME/.m2/repository/io/foldright/cffu* + # https://docs.github.com/en/actions/learn-github-actions/expressions + # https://docs.github.com/en/actions/learn-github-actions/variables#detecting-the-operating-system + if: runner.os != 'Windows' + - name: 'remove self maven install files(OS: Windows)' + run: Remove-Item -Recurse -Force $home/.m2/repository/io/foldright/cffu* + if: runner.os == 'Windows' diff --git a/cffu-core/pom.xml b/cffu-core/pom.xml index d0bb858e..ab7e4b90 100644 --- a/cffu-core/pom.xml +++ b/cffu-core/pom.xml @@ -11,7 +11,7 @@ cffu CompletableFuture-Fu(cffu) - 🦝 A tiny 0-dependency sidekick library for CompletableFuture to improve user experience and reduce misuse. + 🦝 A tiny sidekick library for CompletableFuture to improve user experience and reduce misuse. https://github.com/foldright/cffu 2023 @@ -54,11 +54,25 @@ - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - true + io.foldright + cffu-utils + ${project.version} + + + io.foldright + cffu-utils + ${project.version} + + tests + test-jar + test + org.apache.commons commons-lang3 diff --git a/cffu-core/src/main/java/io/foldright/cffu/package-info.java b/cffu-core/src/main/java/io/foldright/cffu/package-info.java index a7e99558..17fa29f1 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/package-info.java +++ b/cffu-core/src/main/java/io/foldright/cffu/package-info.java @@ -1,8 +1,7 @@ /** - * A tiny 0-dependency sidekick library for CompletableFuture - * to improve user experience and reduce misuse. + * A tiny sidekick library for CompletableFuture to improve user experience and reduce misuse. *

- * the core class is {@link io.foldright.cffu.Cffu}. + * The core class is {@link io.foldright.cffu.Cffu}. * * @see io.foldright.cffu.Cffu * @see io.foldright.cffu.CffuFactory diff --git a/cffu-core/src/main/javadoc/overview.html b/cffu-core/src/main/javadoc/overview.html index 49902493..9b115b98 100644 --- a/cffu-core/src/main/javadoc/overview.html +++ b/cffu-core/src/main/javadoc/overview.html @@ -1,12 +1,12 @@

This is the API documentation for the - - 🦝 Java CompletableFuture Fu(aka. CF-Fu, pronounced "Shifu"), - a tiny 0-dependency sidekick library for - CompletableFuture - to improve user experience and reduce misuse. + + 🦝 Java CompletableFuture Fu(aka. CF-Fu, pronounced "Shifu"), + a tiny sidekick library for + CompletableFuture + to improve user experience and reduce misuse.

The CF-Fu GitHub repo - also include the best practice/traps guide 📚 for CompletableFuture 💗. + also include the best practice/traps guide 📚 for CompletableFuture 💗. 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 496134d1..260bb19a 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -17,7 +17,9 @@ import java.util.function.Function; import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder; +import static io.foldright.cffu.CompletableFutureUtils.failedFuture; import static io.foldright.test_utils.TestUtils.*; +import static io.foldright.test_utils.CoreTestUtils.*; import static java.util.concurrent.ForkJoinPool.commonPool; import static org.junit.jupiter.api.Assertions.*; @@ -26,22 +28,6 @@ * see io.foldright.compatibility_test.CffuApiCompatibilityTest */ class CffuFactoryTest { - //////////////////////////////////////////////////////////////////////////////// - // test constants - //////////////////////////////////////////////////////////////////////////////// - - static final int n = 42; - static final int another_n = 424242; - - static final String s = "S42"; - - static final double d = 42.1; - - static final RuntimeException rte = new RuntimeException("Bang"); - static final RuntimeException another_rte = new RuntimeException("BangAnother"); - - private static CffuFactory cffuFactory; - /////////////////////////////////////////////////////////////////////////////// //# Factory Methods, equivalent to same name static methods of CompletableFuture // @@ -90,7 +76,7 @@ void test_failedStage() throws Exception { CompletionStage se = stage.exceptionally(throwable -> n); try { - createFailedFuture(rte).toCompletableFuture().join(); + failedFuture(rte).toCompletableFuture().join(); fail(); } catch (CompletionException expected) { assertSame(rte, expected.getCause()); @@ -183,10 +169,10 @@ void test_asCffuArray() throws Exception { void test_allOf_CompletableFuture() throws Exception { cffuFactory.allOf( CompletableFuture.completedFuture(n), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get(); cffuFactory.allOf( - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get(); assertNull(cffuFactory.allOf().get()); @@ -195,20 +181,20 @@ void test_allOf_CompletableFuture() throws Exception { cffuFactory.allOfFastFail( CompletableFuture.completedFuture(n), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get(); cffuFactory.allOfFastFail( - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get(); assertNull(cffuFactory.allOfFastFail().get()); cffuFactory.allOfFastFail( cffuFactory.completedFuture(n), - cffuFactory.completedFuture(another_n) + cffuFactory.completedFuture(anotherN) ).get(); cffuFactory.allOfFastFail( - cffuFactory.completedFuture(another_n) + cffuFactory.completedFuture(anotherN) ).get(); } @@ -216,10 +202,10 @@ void test_allOf_CompletableFuture() throws Exception { void test_anyOf_CompletableFuture() throws Exception { cffuFactory.anyOf( CompletableFuture.completedFuture(n), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get(); - assertEquals(another_n, cffuFactory.anyOf( - CompletableFuture.completedFuture(another_n) + assertEquals(anotherN, cffuFactory.anyOf( + CompletableFuture.completedFuture(anotherN) ).get()); assertFalse(cffuFactory.anyOf().isDone()); @@ -228,10 +214,10 @@ void test_anyOf_CompletableFuture() throws Exception { cffuFactory.anyOfSuccess( CompletableFuture.completedFuture(n), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get(); - assertEquals(another_n, cffuFactory.anyOfSuccess( - CompletableFuture.completedFuture(another_n) + assertEquals(anotherN, cffuFactory.anyOfSuccess( + CompletableFuture.completedFuture(anotherN) ).get()); try { @@ -243,10 +229,10 @@ void test_anyOf_CompletableFuture() throws Exception { cffuFactory.anyOfSuccess( cffuFactory.completedFuture(n), - cffuFactory.completedFuture(another_n) + cffuFactory.completedFuture(anotherN) ).get(); - assertEquals(another_n, cffuFactory.anyOfSuccess( - cffuFactory.completedFuture(another_n) + assertEquals(anotherN, cffuFactory.anyOfSuccess( + cffuFactory.completedFuture(anotherN) ).get()); } @@ -403,7 +389,7 @@ void test_cffuAnyOf_exceptionally() throws Exception { try { cffuFactory.cffuAnyOf( createIncompleteFuture(), - createFailedFuture(rte), + failedFuture(rte), createIncompleteFuture() ).get(); @@ -430,7 +416,7 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { cffuFactory.newIncompleteCffu(), cffuFactory.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), cffuFactory.completedFuture(n) ).get()); @@ -449,11 +435,11 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { assertEquals(n, cffuFactory.cffuAnyOfSuccess( cffuFactory.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), cffuFactory.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), cffuFactory.completedFuture(n) ).get()); @@ -466,7 +452,7 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.completedFuture(n) ).get()); @@ -485,11 +471,11 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { assertEquals(n, cffuFactory.cffuAnyOfSuccess( CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.completedFuture(n) ).get()); @@ -517,18 +503,18 @@ void test_cffuCombine() throws Exception { CompletableFuture.completedFuture(d) ).get()); - assertEquals(Tuple4.of(n, s, d, another_n), cffuFactory.cffuCombine( + assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.cffuCombine( CompletableFuture.completedFuture(n), CompletableFuture.completedFuture(s), CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get()); - assertEquals(Tuple5.of(n, s, d, another_n, n + n), cffuFactory.cffuCombine( + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.cffuCombine( CompletableFuture.completedFuture(n), CompletableFuture.completedFuture(s), CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(another_n), + CompletableFuture.completedFuture(anotherN), CompletableFuture.completedFuture(n + n) ).get()); @@ -545,18 +531,18 @@ void test_cffuCombine() throws Exception { cffuFactory.completedFuture(d) ).get()); - assertEquals(Tuple4.of(n, s, d, another_n), cffuFactory.cffuCombine( + assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.cffuCombine( cffuFactory.completedFuture(n), cffuFactory.completedFuture(s), cffuFactory.completedFuture(d), - cffuFactory.completedFuture(another_n) + cffuFactory.completedFuture(anotherN) ).get()); - assertEquals(Tuple5.of(n, s, d, another_n, n + n), cffuFactory.cffuCombine( + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.cffuCombine( cffuFactory.completedFuture(n), cffuFactory.completedFuture(s), cffuFactory.completedFuture(d), - cffuFactory.completedFuture(another_n), + cffuFactory.completedFuture(anotherN), cffuFactory.completedFuture(n + n) ).get()); @@ -571,16 +557,16 @@ void test_cffuCombine() throws Exception { CompletableFuture.completedFuture(d) ).get()); - assertEquals(Tuple4.of(n, s, d, another_n), cffuFactory.completedFuture(n).cffuCombine( + assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).cffuCombine( CompletableFuture.completedFuture(s), CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get()); - assertEquals(Tuple5.of(n, s, d, another_n, n + n), cffuFactory.completedFuture(n).cffuCombine( + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).cffuCombine( CompletableFuture.completedFuture(s), CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(another_n), + CompletableFuture.completedFuture(anotherN), CompletableFuture.completedFuture(n + n) ).get()); @@ -595,16 +581,16 @@ void test_cffuCombine() throws Exception { cffuFactory.completedFuture(d) ).get()); - assertEquals(Tuple4.of(n, s, d, another_n), cffuFactory.completedFuture(n).cffuCombine( + assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).cffuCombine( cffuFactory.completedFuture(s), cffuFactory.completedFuture(d), - cffuFactory.completedFuture(another_n) + cffuFactory.completedFuture(anotherN) ).get()); - assertEquals(Tuple5.of(n, s, d, another_n, n + n), cffuFactory.completedFuture(n).cffuCombine( + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).cffuCombine( cffuFactory.completedFuture(s), cffuFactory.completedFuture(d), - cffuFactory.completedFuture(another_n), + cffuFactory.completedFuture(anotherN), cffuFactory.completedFuture(n + n) ).get()); } @@ -614,7 +600,7 @@ void test_cffuCombine_exceptionally() throws Exception { try { cffuFactory.cffuCombine( CompletableFuture.completedFuture(n), - createFailedFuture(rte) + failedFuture(rte) ).get(); fail(); @@ -625,7 +611,7 @@ void test_cffuCombine_exceptionally() throws Exception { try { cffuFactory.cffuCombine( CompletableFuture.completedFuture(n), - createFailedFuture(rte), + failedFuture(rte), CompletableFuture.completedFuture(s) ).get(); @@ -638,9 +624,9 @@ void test_cffuCombine_exceptionally() throws Exception { cffuFactory.cffuCombine( CompletableFuture.completedFuture(n), CompletableFuture.completedFuture(d), - createFailedFuture(rte), + failedFuture(rte), CompletableFuture.completedFuture(s), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get(); fail(); @@ -664,7 +650,7 @@ void test_toCompletableFutureArray() { @SuppressWarnings("unchecked") CompletableFuture[] cfArray = new CompletableFuture[]{ CompletableFuture.completedFuture(n), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) }; @SuppressWarnings("unchecked") CompletionStage[] csArray = new CompletableFuture[]{ @@ -687,7 +673,7 @@ void test_cffuArrayUnwrap() { @SuppressWarnings("unchecked") CompletableFuture[] cfArray = new CompletableFuture[]{ CompletableFuture.completedFuture(n), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) }; @SuppressWarnings("unchecked") Cffu[] input = new Cffu[]{ @@ -702,7 +688,7 @@ void test_cffuListToArray() { @SuppressWarnings("unchecked") Cffu[] input = new Cffu[]{ cffuFactory.completedFuture(n), - cffuFactory.completedFuture(another_n), + cffuFactory.completedFuture(anotherN), cffuFactory.newIncompleteCffu() }; @@ -714,7 +700,7 @@ void test_completableFutureListToArray() { @SuppressWarnings("unchecked") CompletableFuture[] input = new CompletableFuture[]{ CompletableFuture.completedFuture(n), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) }; assertArrayEquals(input, CffuFactory.completableFutureListToArray(Arrays.asList(input))); @@ -772,6 +758,8 @@ void test_executorSetting_MayBe_ThreadPerTaskExecutor() throws Exception { //# Test helper methods //////////////////////////////////////////////////////////////////////////////// + private static CffuFactory cffuFactory; + private static ExecutorService executorService; private static ExecutorService anotherExecutorService; 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 20b8e842..0201cfbf 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java @@ -1,7 +1,6 @@ package io.foldright.cffu; import io.foldright.test_utils.TestThreadPoolManager; -import io.foldright.test_utils.TestUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -11,8 +10,7 @@ import java.util.concurrent.*; import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder; -import static io.foldright.cffu.CffuFactoryTest.n; -import static io.foldright.cffu.CffuFactoryTest.rte; +import static io.foldright.test_utils.TestUtils.*; import static org.junit.jupiter.api.Assertions.*; @@ -76,7 +74,7 @@ void test_cffuJoin() { // Incomplete Future -> join before timeout Cffu cffu = cffuFactory.supplyAsync(() -> { - TestUtils.sleep(300); + sleep(300); return 42; }); assertEquals(42, cffu.cffuJoin(3, TimeUnit.SECONDS)); 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 2b7802aa..9def890b 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 @@ -13,7 +13,7 @@ import io.foldright.cffu.CffuFactory; import io.foldright.cffu.CffuFactoryBuilder; import io.foldright.test_utils.TestThreadPoolManager; -import io.foldright.test_utils.TestUtils; +import io.foldright.test_utils.CoreTestUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -44,7 +44,7 @@ void factoryMethods_byImmediateValue() throws Exception { // completedFuture Cffu f0 = cffuFactory.completedFuture(hello); assertEquals(hello, f0.get()); - TestUtils.shouldNotBeMinimalStage(f0); + CoreTestUtils.shouldNotBeMinimalStage(f0); // below methods is tested in below test method // - completedStage // - failedFuture @@ -56,7 +56,7 @@ void factoryMethods_byImmediateValue__Java9() throws Exception { // completedStage Cffu cf = (Cffu) cffuFactory.completedStage(hello); assertEquals(hello, cf.toCompletableFuture().get()); - TestUtils.shouldBeMinimalStage(cf); + CoreTestUtils.shouldBeMinimalStage(cf); // failedFuture cf = cffuFactory.failedFuture(rte); @@ -66,7 +66,7 @@ void factoryMethods_byImmediateValue__Java9() throws Exception { } catch (ExecutionException expected) { assertSame(rte, expected.getCause()); } - TestUtils.shouldNotBeMinimalStage(cf); + CoreTestUtils.shouldNotBeMinimalStage(cf); // failedStage cf = (Cffu) cffuFactory.failedStage(rte); @@ -76,7 +76,7 @@ void factoryMethods_byImmediateValue__Java9() throws Exception { } catch (ExecutionException expected) { assertSame(rte, expected.getCause()); } - TestUtils.shouldBeMinimalStage(cf); + CoreTestUtils.shouldBeMinimalStage(cf); } @Test @@ -85,35 +85,35 @@ void factoryMethods_byLambda() throws Exception { // runAsync Cffu cf = cffuFactory.runAsync(() -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }); assertNull(cf.get()); assertEquals(hello, holder.get()); - TestUtils.shouldNotBeMinimalStage(cf); + CoreTestUtils.shouldNotBeMinimalStage(cf); holder.set(null); cf = cffuFactory.runAsync(() -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService); assertNull(cf.get()); assertEquals(hello, holder.get()); - TestUtils.shouldNotBeMinimalStage(cf); + CoreTestUtils.shouldNotBeMinimalStage(cf); // supplyAsync Cffu s_cf = cffuFactory.supplyAsync(() -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return hello; }); assertEquals(hello, s_cf.get()); - TestUtils.shouldNotBeMinimalStage(s_cf); + CoreTestUtils.shouldNotBeMinimalStage(s_cf); s_cf = cffuFactory.supplyAsync(() -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return hello; }, anotherExecutorService); assertEquals(hello, s_cf.get()); - TestUtils.shouldNotBeMinimalStage(s_cf); + CoreTestUtils.shouldNotBeMinimalStage(s_cf); } @Test @@ -154,7 +154,7 @@ void staticMethods_delayedExecutor() throws Exception { Executor delayer = cffuFactory.delayedExecutor(1, TimeUnit.MILLISECONDS); cffuFactory.runAsync(() -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }, delayer).get(); assertEquals(hello, holder.get()); @@ -162,7 +162,7 @@ void staticMethods_delayedExecutor() throws Exception { holder.set(null); delayer = cffuFactory.delayedExecutor(1, TimeUnit.MILLISECONDS, anotherExecutorService); cffuFactory.runAsync(() -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, delayer).get(); assertEquals(hello, holder.get()); @@ -182,13 +182,13 @@ void simpleThenMethods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.thenRunAsync(() -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.thenRunAsync(() -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); @@ -198,24 +198,24 @@ void simpleThenMethods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.thenAcceptAsync(x -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.thenAcceptAsync(x -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); assertEquals(43, cf.thenApply(x -> x + 1).get()); assertEquals(44, cf.thenApplyAsync(x -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return x + 2; }).get()); assertEquals(45, cf.thenApplyAsync(x -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return x + 3; }, anotherExecutorService).get()); } @@ -230,13 +230,13 @@ void thenBoth_Methods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.runAfterBothAsync(cf, () -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.runAfterBothAsync(cf, () -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); @@ -246,24 +246,24 @@ void thenBoth_Methods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.thenAcceptBothAsync(cf, (x, y) -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.thenAcceptBothAsync(cf, (x, y) -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); assertEquals(84, cf.thenCombine(cf, Integer::sum).get()); assertEquals(84, cf.thenCombineAsync(cf, (a, b) -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return Integer.sum(a, b); }).get()); assertEquals(84, cf.thenCombineAsync(cf, (a, b) -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return Integer.sum(a, b); }, anotherExecutorService).get()); } @@ -278,13 +278,13 @@ void thenEither_Methods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.runAfterEitherAsync(cf, () -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.runAfterEitherAsync(cf, () -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); @@ -294,24 +294,24 @@ void thenEither_Methods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.acceptEitherAsync(cf, x -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.acceptEitherAsync(cf, x -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); assertEquals(43, cf.applyToEither(cf, x -> x + 1).get()); assertEquals(44, cf.applyToEitherAsync(cf, x -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return x + 2; }).get()); assertEquals(45, cf.applyToEitherAsync(cf, x -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return x + 3; }, anotherExecutorService).get()); } @@ -319,7 +319,7 @@ void thenEither_Methods() throws Exception { @Test void errorHandling_methods() throws Exception { Cffu cf = cffuFactory.completedFuture(42); - Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); + Cffu failed = CoreTestUtils.safeNewFailedCffu(executorService, rte); // exceptionally assertEquals(42, cf.exceptionally(t -> 43).get()); @@ -332,23 +332,23 @@ void errorHandling_methods() throws Exception { @Test void errorHandling_methods__Java9() throws Exception { Cffu cf = cffuFactory.completedFuture(42); - Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); + Cffu failed = CoreTestUtils.safeNewFailedCffu(executorService, rte); assertEquals(42, cf.exceptionallyAsync(t -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return 43; }).get()); assertEquals(43, failed.exceptionallyAsync(t -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return 43; }).get()); assertEquals(42, cf.exceptionallyAsync(t -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return 44; }, anotherExecutorService).get()); assertEquals(44, failed.exceptionallyAsync(t -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return 44; }, anotherExecutorService).get()); } @@ -382,11 +382,11 @@ void advancedMethods__thenCompose() throws Exception { assertEquals(43, cf.thenCompose(x -> cffuFactory.completedFuture(43)).get()); assertEquals(44, cf.thenComposeAsync(x -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return cffuFactory.completedFuture(44); }).get()); assertEquals(45, cf.thenComposeAsync(x -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return cffuFactory.completedFuture(45); }, anotherExecutorService).get()); } @@ -398,24 +398,24 @@ void advancedMethods__exceptionallyCompose() throws Exception { assertEquals(42, cf.exceptionallyCompose(x -> cffuFactory.completedFuture(43)).get()); assertEquals(42, cf.exceptionallyComposeAsync(x -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return cffuFactory.completedFuture(44); }).get()); assertEquals(42, cf.exceptionallyComposeAsync(x -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return cffuFactory.completedFuture(45); }, anotherExecutorService).get()); // for failed - Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); + Cffu failed = CoreTestUtils.safeNewFailedCffu(executorService, rte); assertEquals(43, failed.exceptionallyCompose(x -> cffuFactory.completedFuture(43)).get()); assertEquals(44, failed.exceptionallyComposeAsync(x -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return cffuFactory.completedFuture(44); }).get()); assertEquals(45, failed.exceptionallyComposeAsync(x -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return cffuFactory.completedFuture(45); }, anotherExecutorService).get()); } @@ -426,11 +426,11 @@ void advancedMethods__handle_whenComplete() throws Exception { assertEquals(43, cf.handle((x, e) -> 43).get()); assertEquals(44, cf.handleAsync((x, e) -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return 44; }).get()); assertEquals(45, cf.handleAsync((x, e) -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return 45; }, anotherExecutorService).get()); @@ -440,13 +440,13 @@ void advancedMethods__handle_whenComplete() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.whenCompleteAsync((x, e) -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.whenCompleteAsync((x, e) -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); @@ -455,7 +455,7 @@ void advancedMethods__handle_whenComplete() throws Exception { @Test void readExplicitlyMethods() throws Exception { Cffu cf = cffuFactory.completedFuture(42); - Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); + Cffu failed = CoreTestUtils.safeNewFailedCffu(executorService, rte); Integer r = cf.get(); assertEquals(42, r); @@ -510,7 +510,7 @@ void readExplicitlyMethods() throws Exception { @Test void readExplicitlyMethods__Java19() throws Exception { Cffu cf = cffuFactory.completedFuture(42); - Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); + Cffu failed = CoreTestUtils.safeNewFailedCffu(executorService, rte); Integer r = cf.get(); assertEquals(42, r); @@ -537,7 +537,7 @@ void readExplicitlyMethods__Java19() throws Exception { @EnabledForJreRange(min = JRE.JAVA_19) /* GEN_MARK_KEEP */ void readExplicitlyMethods__Java19_CanNotCompatible() { Cffu cf = cffuFactory.completedFuture(42); - Cffu failed = TestUtils.safeNewFailedCffu(executorService, rte); + Cffu failed = CoreTestUtils.safeNewFailedCffu(executorService, rte); Cffu incomplete = cffuFactory.newIncompleteCffu(); // state @@ -584,22 +584,22 @@ void writeMethods__Java9() throws Exception { // completeAsync assertEquals(42, cf.completeAsync(() -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return 4242; }).get()); assertEquals(42, cf.completeAsync(() -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return 424242; }, anotherExecutorService).get()); Cffu incomplete = cffuFactory.newIncompleteCffu(); assertEquals(4242, incomplete.completeAsync(() -> { - TestUtils.assertCffuRunInDefaultThread(executorService); + CoreTestUtils.assertCffuRunInDefaultThread(executorService); return 4242; }).get()); incomplete = cffuFactory.newIncompleteCffu(); assertEquals(424242, incomplete.completeAsync(() -> { - TestUtils.assertCffuRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCffuRunInThreadOf(anotherExecutorService); return 424242; }, anotherExecutorService).get()); @@ -637,7 +637,7 @@ void reConfigMethods__Java9() throws Exception { Cffu cf = cffuFactory.completedFuture(42); // minimalCompletionStage - TestUtils.shouldBeMinimalStage((Cffu) cf.minimalCompletionStage()); + CoreTestUtils.shouldBeMinimalStage((Cffu) cf.minimalCompletionStage()); // copy assertEquals(42, cf.copy().get()); @@ -695,7 +695,7 @@ void test_similarities_and_differences_between_cancelled_and_exceptionally() thr cancelledCf.cancel(false); final Cffu exceptionallyCf = - TestUtils.safeNewFailedCffu(executorService, rte); + CoreTestUtils.safeNewFailedCffu(executorService, rte); //////////////////////////////////////// // different behavior @@ -779,7 +779,7 @@ void test_similarities_and_differences_between_cancelled_and_exceptionally__Java cancelledCf.cancel(false); final Cffu exceptionallyCf = - TestUtils.safeNewFailedCffu(executorService, rte); + CoreTestUtils.safeNewFailedCffu(executorService, rte); //////////////////////////////////////// // different behavior @@ -830,7 +830,7 @@ void nested_exception() throws Exception { cf = cffuFactory.completedFuture(42); // auto unwrap first level ExecutionException check1MoreLevelForExecutionException(cf.thenRun(() -> { - TestUtils.sneakyThrow(new ExecutionException(rte)); + CoreTestUtils.sneakyThrow(new ExecutionException(rte)); })); } 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 9c005955..d342230d 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 @@ -10,7 +10,7 @@ //////////////////////////////////////////////////////////////////////////////// import io.foldright.test_utils.TestThreadPoolManager; -import io.foldright.test_utils.TestUtils; +import io.foldright.test_utils.CoreTestUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -41,7 +41,7 @@ void factoryMethods_byImmediateValue() throws Exception { // completedFuture CompletableFuture f0 = CompletableFuture.completedFuture(hello); assertEquals(hello, f0.get()); - TestUtils.shouldNotBeMinimalStage(f0); + CoreTestUtils.shouldNotBeMinimalStage(f0); // below methods is tested in below test method // - completedStage // - failedFuture @@ -54,7 +54,7 @@ void factoryMethods_byImmediateValue__Java9() throws Exception { // completedStage CompletableFuture cf = (CompletableFuture) CompletableFuture.completedStage(hello); assertEquals(hello, cf.toCompletableFuture().get()); - TestUtils.shouldBeMinimalStage(cf); + CoreTestUtils.shouldBeMinimalStage(cf); // failedFuture cf = CompletableFuture.failedFuture(rte); @@ -64,7 +64,7 @@ void factoryMethods_byImmediateValue__Java9() throws Exception { } catch (ExecutionException expected) { assertSame(rte, expected.getCause()); } - TestUtils.shouldNotBeMinimalStage(cf); + CoreTestUtils.shouldNotBeMinimalStage(cf); // failedStage cf = (CompletableFuture) CompletableFuture.failedStage(rte); @@ -74,7 +74,7 @@ void factoryMethods_byImmediateValue__Java9() throws Exception { } catch (ExecutionException expected) { assertSame(rte, expected.getCause()); } - TestUtils.shouldBeMinimalStage(cf); + CoreTestUtils.shouldBeMinimalStage(cf); } @Test @@ -83,35 +83,35 @@ void factoryMethods_byLambda() throws Exception { // runAsync CompletableFuture cf = CompletableFuture.runAsync(() -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }); assertNull(cf.get()); assertEquals(hello, holder.get()); - TestUtils.shouldNotBeMinimalStage(cf); + CoreTestUtils.shouldNotBeMinimalStage(cf); holder.set(null); cf = CompletableFuture.runAsync(() -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService); assertNull(cf.get()); assertEquals(hello, holder.get()); - TestUtils.shouldNotBeMinimalStage(cf); + CoreTestUtils.shouldNotBeMinimalStage(cf); // supplyAsync CompletableFuture s_cf = CompletableFuture.supplyAsync(() -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return hello; }); assertEquals(hello, s_cf.get()); - TestUtils.shouldNotBeMinimalStage(s_cf); + CoreTestUtils.shouldNotBeMinimalStage(s_cf); s_cf = CompletableFuture.supplyAsync(() -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return hello; }, anotherExecutorService); assertEquals(hello, s_cf.get()); - TestUtils.shouldNotBeMinimalStage(s_cf); + CoreTestUtils.shouldNotBeMinimalStage(s_cf); } @Test @@ -153,7 +153,7 @@ void staticMethods_delayedExecutor() throws Exception { Executor delayer = CompletableFuture.delayedExecutor(1, TimeUnit.MILLISECONDS); CompletableFuture.runAsync(() -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }, delayer).get(); assertEquals(hello, holder.get()); @@ -161,7 +161,7 @@ void staticMethods_delayedExecutor() throws Exception { holder.set(null); delayer = CompletableFuture.delayedExecutor(1, TimeUnit.MILLISECONDS, anotherExecutorService); CompletableFuture.runAsync(() -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, delayer).get(); assertEquals(hello, holder.get()); @@ -181,13 +181,13 @@ void simpleThenMethods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.thenRunAsync(() -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.thenRunAsync(() -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); @@ -197,24 +197,24 @@ void simpleThenMethods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.thenAcceptAsync(x -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.thenAcceptAsync(x -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); assertEquals(43, cf.thenApply(x -> x + 1).get()); assertEquals(44, cf.thenApplyAsync(x -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return x + 2; }).get()); assertEquals(45, cf.thenApplyAsync(x -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return x + 3; }, anotherExecutorService).get()); } @@ -229,13 +229,13 @@ void thenBoth_Methods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.runAfterBothAsync(cf, () -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.runAfterBothAsync(cf, () -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); @@ -245,24 +245,24 @@ void thenBoth_Methods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.thenAcceptBothAsync(cf, (x, y) -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.thenAcceptBothAsync(cf, (x, y) -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); assertEquals(84, cf.thenCombine(cf, Integer::sum).get()); assertEquals(84, cf.thenCombineAsync(cf, (a, b) -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return Integer.sum(a, b); }).get()); assertEquals(84, cf.thenCombineAsync(cf, (a, b) -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return Integer.sum(a, b); }, anotherExecutorService).get()); } @@ -277,13 +277,13 @@ void thenEither_Methods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.runAfterEitherAsync(cf, () -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.runAfterEitherAsync(cf, () -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); @@ -293,24 +293,24 @@ void thenEither_Methods() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.acceptEitherAsync(cf, x -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.acceptEitherAsync(cf, x -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); assertEquals(43, cf.applyToEither(cf, x -> x + 1).get()); assertEquals(44, cf.applyToEitherAsync(cf, x -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return x + 2; }).get()); assertEquals(45, cf.applyToEitherAsync(cf, x -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return x + 3; }, anotherExecutorService).get()); } @@ -318,7 +318,7 @@ void thenEither_Methods() throws Exception { @Test void errorHandling_methods() throws Exception { CompletableFuture cf = CompletableFuture.completedFuture(42); - CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); + CompletableFuture failed = CoreTestUtils.safeNewFailedCompletableFuture(executorService, rte); // exceptionally assertEquals(42, cf.exceptionally(t -> 43).get()); @@ -332,23 +332,23 @@ void errorHandling_methods() throws Exception { @EnabledForJreRange(min = JRE.JAVA_12) void errorHandling_methods__Java9() throws Exception { CompletableFuture cf = CompletableFuture.completedFuture(42); - CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); + CompletableFuture failed = CoreTestUtils.safeNewFailedCompletableFuture(executorService, rte); assertEquals(42, cf.exceptionallyAsync(t -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return 43; }).get()); assertEquals(43, failed.exceptionallyAsync(t -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return 43; }).get()); assertEquals(42, cf.exceptionallyAsync(t -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return 44; }, anotherExecutorService).get()); assertEquals(44, failed.exceptionallyAsync(t -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return 44; }, anotherExecutorService).get()); } @@ -383,11 +383,11 @@ void advancedMethods__thenCompose() throws Exception { assertEquals(43, cf.thenCompose(x -> CompletableFuture.completedFuture(43)).get()); assertEquals(44, cf.thenComposeAsync(x -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return CompletableFuture.completedFuture(44); }).get()); assertEquals(45, cf.thenComposeAsync(x -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return CompletableFuture.completedFuture(45); }, anotherExecutorService).get()); } @@ -400,24 +400,24 @@ void advancedMethods__exceptionallyCompose() throws Exception { assertEquals(42, cf.exceptionallyCompose(x -> CompletableFuture.completedFuture(43)).get()); assertEquals(42, cf.exceptionallyComposeAsync(x -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return CompletableFuture.completedFuture(44); }).get()); assertEquals(42, cf.exceptionallyComposeAsync(x -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return CompletableFuture.completedFuture(45); }, anotherExecutorService).get()); // for failed - CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); + CompletableFuture failed = CoreTestUtils.safeNewFailedCompletableFuture(executorService, rte); assertEquals(43, failed.exceptionallyCompose(x -> CompletableFuture.completedFuture(43)).get()); assertEquals(44, failed.exceptionallyComposeAsync(x -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return CompletableFuture.completedFuture(44); }).get()); assertEquals(45, failed.exceptionallyComposeAsync(x -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return CompletableFuture.completedFuture(45); }, anotherExecutorService).get()); } @@ -428,11 +428,11 @@ void advancedMethods__handle_whenComplete() throws Exception { assertEquals(43, cf.handle((x, e) -> 43).get()); assertEquals(44, cf.handleAsync((x, e) -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return 44; }).get()); assertEquals(45, cf.handleAsync((x, e) -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return 45; }, anotherExecutorService).get()); @@ -442,13 +442,13 @@ void advancedMethods__handle_whenComplete() throws Exception { assertEquals(hello, holder.get()); holder.set(null); cf.whenCompleteAsync((x, e) -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); holder.set(hello); }).get(); assertEquals(hello, holder.get()); holder.set(null); cf.whenCompleteAsync((x, e) -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); holder.set(hello); }, anotherExecutorService).get(); assertEquals(hello, holder.get()); @@ -457,7 +457,7 @@ void advancedMethods__handle_whenComplete() throws Exception { @Test void readExplicitlyMethods() throws Exception { CompletableFuture cf = CompletableFuture.completedFuture(42); - CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); + CompletableFuture failed = CoreTestUtils.safeNewFailedCompletableFuture(executorService, rte); Integer r = cf.get(); assertEquals(42, r); @@ -513,7 +513,7 @@ void readExplicitlyMethods() throws Exception { @EnabledForJreRange(min = JRE.JAVA_19) void readExplicitlyMethods__Java19() throws Exception { CompletableFuture cf = CompletableFuture.completedFuture(42); - CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); + CompletableFuture failed = CoreTestUtils.safeNewFailedCompletableFuture(executorService, rte); Integer r = cf.get(); assertEquals(42, r); @@ -540,7 +540,7 @@ void readExplicitlyMethods__Java19() throws Exception { @EnabledForJreRange(min = JRE.JAVA_19) /* GEN_MARK_KEEP */ void readExplicitlyMethods__Java19_CanNotCompatible() { CompletableFuture cf = CompletableFuture.completedFuture(42); - CompletableFuture failed = TestUtils.safeNewFailedCompletableFuture(executorService, rte); + CompletableFuture failed = CoreTestUtils.safeNewFailedCompletableFuture(executorService, rte); CompletableFuture incomplete = new CompletableFuture<>(); // state @@ -588,22 +588,22 @@ void writeMethods__Java9() throws Exception { // completeAsync assertEquals(42, cf.completeAsync(() -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return 4242; }).get()); assertEquals(42, cf.completeAsync(() -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return 424242; }, anotherExecutorService).get()); CompletableFuture incomplete = new CompletableFuture<>(); assertEquals(4242, incomplete.completeAsync(() -> { - TestUtils.assertCompletableFutureRunInDefaultThread(executorService); + CoreTestUtils.assertCompletableFutureRunInDefaultThread(executorService); return 4242; }).get()); incomplete = new CompletableFuture<>(); assertEquals(424242, incomplete.completeAsync(() -> { - TestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); + CoreTestUtils.assertCompletableFutureRunInThreadOf(anotherExecutorService); return 424242; }, anotherExecutorService).get()); @@ -642,7 +642,7 @@ void reConfigMethods__Java9() throws Exception { CompletableFuture cf = CompletableFuture.completedFuture(42); // minimalCompletionStage - TestUtils.shouldBeMinimalStage((CompletableFuture) cf.minimalCompletionStage()); + CoreTestUtils.shouldBeMinimalStage((CompletableFuture) cf.minimalCompletionStage()); // copy assertEquals(42, cf.copy().get()); @@ -702,7 +702,7 @@ void test_similarities_and_differences_between_cancelled_and_exceptionally() thr cancelledCf.cancel(false); final CompletableFuture exceptionallyCf = - TestUtils.safeNewFailedCompletableFuture(executorService, rte); + CoreTestUtils.safeNewFailedCompletableFuture(executorService, rte); //////////////////////////////////////// // different behavior @@ -787,7 +787,7 @@ void test_similarities_and_differences_between_cancelled_and_exceptionally__Java cancelledCf.cancel(false); final CompletableFuture exceptionallyCf = - TestUtils.safeNewFailedCompletableFuture(executorService, rte); + CoreTestUtils.safeNewFailedCompletableFuture(executorService, rte); //////////////////////////////////////// // different behavior @@ -838,7 +838,7 @@ void nested_exception() throws Exception { cf = CompletableFuture.completedFuture(42); // auto unwrap first level ExecutionException check1MoreLevelForExecutionException(cf.thenRun(() -> { - TestUtils.sneakyThrow(new ExecutionException(rte)); + CoreTestUtils.sneakyThrow(new ExecutionException(rte)); })); } diff --git a/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt b/cffu-core/src/test/java/io/foldright/test_utils/CoreTestUtils.kt similarity index 97% rename from cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt rename to cffu-core/src/test/java/io/foldright/test_utils/CoreTestUtils.kt index 4477c7f8..6c98a871 100644 --- a/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt +++ b/cffu-core/src/test/java/io/foldright/test_utils/CoreTestUtils.kt @@ -1,4 +1,4 @@ -@file:JvmName("TestUtils") +@file:JvmName("CoreTestUtils") package io.foldright.test_utils @@ -16,19 +16,6 @@ import org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast import java.util.concurrent.* -//////////////////////////////////////////////////////////////////////////////// -// CF creation util functions -// -// - with sleep delay -// - compatibility logic of Java version -//////////////////////////////////////////////////////////////////////////////// - -fun createIncompleteFuture(): CompletableFuture = CompletableFuture() - -fun createFailedFuture(ex: Throwable) = CompletableFuture().apply { - completeExceptionally(ex) -} - //////////////////////////////////////////////////////////////////////////////// // Helper functions for api compatibility test: // - CompletableFutureApiCompatibilityTest @@ -73,14 +60,6 @@ fun assertCffuRunInThreadOf(executorService: ExecutorService) { // Simple util functions //////////////////////////////////////////////////////////////////////////////// -/** - * sleep without throwing checked exception - */ -@JvmOverloads -fun sleep(millis: Long = 10) { - Thread.sleep(millis) -} - fun merge(list1: List, list2: List) = list1.merge(list2) @JvmName("mergeExt") diff --git a/cffu-kotlin/pom.xml b/cffu-kotlin/pom.xml new file mode 100644 index 00000000..5f25b068 --- /dev/null +++ b/cffu-kotlin/pom.xml @@ -0,0 +1,82 @@ + + 4.0.0 + + io.foldright + cffu-parent + 0.9.x-SNAPSHOT + ../pom.xml + + + cffu-kotlin + ${project.artifactId} + Cffu Kotlin Support + https://github.com/foldright/cffu + 2023 + + + + Apache 2 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + + + scm:git:git@github.com:foldright/cffu.git + scm:git:git@github.com:foldright/cffu.git + https://github.com/foldright/cffu + + + https://github.com/foldright/cffu/issues + GitHub Issues + + + FoldRight + https://foldright.io + + + + Jerry Lee + oldratlee + oldratlee(AT)gmail(DOT)com + + Developer + + +8 + https://github.com/oldratlee + FoldRight + https://foldright.io + + + + + + io.foldright + cffu + ${project.version} + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + true + + + + io.foldright + cffu + ${project.version} + tests + test-jar + test + + + io.foldright + cffu-utils + ${project.version} + tests + test-jar + test + + + diff --git a/cffu-core/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt b/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt similarity index 100% rename from cffu-core/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt rename to cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt diff --git a/cffu-core/src/main/java/io/foldright/cffu/kotlin/CffuTupleExtensions.kt b/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuTupleExtensions.kt similarity index 100% rename from cffu-core/src/main/java/io/foldright/cffu/kotlin/CffuTupleExtensions.kt rename to cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuTupleExtensions.kt diff --git a/cffu-core/src/main/java/io/foldright/cffu/kotlin/CompletableFutureExtensions.kt b/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CompletableFutureExtensions.kt similarity index 100% rename from cffu-core/src/main/java/io/foldright/cffu/kotlin/CompletableFutureExtensions.kt rename to cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CompletableFutureExtensions.kt diff --git a/cffu-core/src/test/java/io/foldright/cffu/kotlin/CffuExtensionsTest.kt b/cffu-kotlin/src/test/java/io/foldright/cffu/kotlin/CffuExtensionsTest.kt similarity index 100% rename from cffu-core/src/test/java/io/foldright/cffu/kotlin/CffuExtensionsTest.kt rename to cffu-kotlin/src/test/java/io/foldright/cffu/kotlin/CffuExtensionsTest.kt diff --git a/cffu-core/src/test/java/io/foldright/cffu/kotlin/CffuTupleExtensionsTest.kt b/cffu-kotlin/src/test/java/io/foldright/cffu/kotlin/CffuTupleExtensionsTest.kt similarity index 100% rename from cffu-core/src/test/java/io/foldright/cffu/kotlin/CffuTupleExtensionsTest.kt rename to cffu-kotlin/src/test/java/io/foldright/cffu/kotlin/CffuTupleExtensionsTest.kt diff --git a/cffu-core/src/test/java/io/foldright/cffu/kotlin/CompletableFutureExtensionsTest.kt b/cffu-kotlin/src/test/java/io/foldright/cffu/kotlin/CompletableFutureExtensionsTest.kt similarity index 100% rename from cffu-core/src/test/java/io/foldright/cffu/kotlin/CompletableFutureExtensionsTest.kt rename to cffu-kotlin/src/test/java/io/foldright/cffu/kotlin/CompletableFutureExtensionsTest.kt diff --git a/cffu-core/src/test/java/io/foldright/demo/Demo.kt b/cffu-kotlin/src/test/java/io/foldright/demo/Demo.kt similarity index 100% rename from cffu-core/src/test/java/io/foldright/demo/Demo.kt rename to cffu-kotlin/src/test/java/io/foldright/demo/Demo.kt diff --git a/cffu-utils/pom.xml b/cffu-utils/pom.xml new file mode 100644 index 00000000..879f2ada --- /dev/null +++ b/cffu-utils/pom.xml @@ -0,0 +1,52 @@ + + 4.0.0 + + io.foldright + cffu-parent + 0.9.x-SNAPSHOT + ../pom.xml + + + cffu-utils + ${project.artifactId} + Cffu Utils, key user class is CompletableFutureUtils + https://github.com/foldright/cffu + 2023 + + + + Apache 2 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + + + scm:git:git@github.com:foldright/cffu.git + scm:git:git@github.com:foldright/cffu.git + https://github.com/foldright/cffu + + + https://github.com/foldright/cffu/issues + GitHub Issues + + + FoldRight + https://foldright.io + + + + Jerry Lee + oldratlee + oldratlee(AT)gmail(DOT)com + + Developer + + +8 + https://github.com/oldratlee + FoldRight + https://foldright.io + + + diff --git a/cffu-core/src/main/java/io/foldright/cffu/CffuState.java b/cffu-utils/src/main/java/io/foldright/cffu/CffuState.java similarity index 87% rename from cffu-core/src/main/java/io/foldright/cffu/CffuState.java rename to cffu-utils/src/main/java/io/foldright/cffu/CffuState.java index 84da8b24..c2b94b71 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuState.java +++ b/cffu-utils/src/main/java/io/foldright/cffu/CffuState.java @@ -4,14 +4,15 @@ import org.jetbrains.annotations.Contract; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; import static java.util.Objects.requireNonNull; /** - * Same as {@link Future.State}, existed for java version compatibility, - * {@link Cffu#cffuState()} to {@link Cffu#state()}. + * Same as {@link Future.State}, existed for + * java version compatibility({@link CompletableFutureUtils#cffuState} to {@link Future#state()}). * * @author Jerry Lee (oldratlee at gmail dot com) * @see Future.State @@ -31,7 +32,7 @@ public Future.State toFutureState() { /** * The task completed with a result. * - * @see Cffu#resultNow() + * @see CompletableFutureUtils#resultNow(CompletableFuture) * @see Future#resultNow() */ SUCCESS { @@ -43,7 +44,7 @@ public Future.State toFutureState() { /** * The task completed with an exception. * - * @see Cffu#exceptionNow() + * @see CompletableFutureUtils#exceptionNow(CompletableFuture) * @see Future#exceptionNow() */ FAILED { @@ -55,7 +56,6 @@ public Future.State toFutureState() { /** * The task was cancelled. * - * @see Cffu#cancel(boolean) * @see Future#cancel(boolean) */ CANCELLED { diff --git a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java b/cffu-utils/src/main/java/io/foldright/cffu/CompletableFutureUtils.java similarity index 99% rename from cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java rename to cffu-utils/src/main/java/io/foldright/cffu/CompletableFutureUtils.java index 2949cf03..1f7b0b93 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-utils/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -454,7 +454,6 @@ public static Executor delayedExecutor(long delay, TimeUnit unit, Executor execu if (IS_JAVA9_PLUS) { return CompletableFuture.delayedExecutor(delay, unit, executor); } - requireNonNull(unit, "unit is null"); requireNonNull(executor, "executor is null"); return new DelayedExecutor(delay, unit, executor); @@ -746,8 +745,9 @@ public static Throwable exceptionNow(CompletableFuture cf) { */ @Contract(pure = true) public static CffuState cffuState(CompletableFuture cf) { - if (IS_JAVA19_PLUS) + if (IS_JAVA19_PLUS) { return CffuState.toCffuState(cf.state()); + } // below code is copied from Future#state() with small adoption diff --git a/cffu-core/src/main/java/io/foldright/cffu/DelayExecutionHelpers.java b/cffu-utils/src/main/java/io/foldright/cffu/DelayExecutionHelpers.java similarity index 94% rename from cffu-core/src/main/java/io/foldright/cffu/DelayExecutionHelpers.java rename to cffu-utils/src/main/java/io/foldright/cffu/DelayExecutionHelpers.java index cf9cf5b2..65de22da 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/DelayExecutionHelpers.java +++ b/cffu-utils/src/main/java/io/foldright/cffu/DelayExecutionHelpers.java @@ -37,7 +37,6 @@ static ScheduledFuture delay(Runnable command, long delay, TimeUnit unit) { /** * @return a Future can be used to cancel the delayed task(timeout CF) * @see FutureCanceller - * @see Cffu#orTimeout(long, TimeUnit) */ public static ScheduledFuture delayToTimoutCf(CompletableFuture cf, long delay, TimeUnit unit) { return delay(new CfTimeout(cf), delay, unit); @@ -46,7 +45,6 @@ public static ScheduledFuture delayToTimoutCf(CompletableFuture cf, long d /** * @return a Future can be used to cancel the delayed task(complete CF) * @see FutureCanceller - * @see Cffu#completeOnTimeout(Object, long, TimeUnit) */ public static ScheduledFuture delayToCompleteCf(CompletableFuture cf, T value, long delay, TimeUnit unit) { return delay(new CfCompleter<>(cf, value), delay, unit); @@ -93,9 +91,6 @@ public void execute(@NonNull Runnable r) { /** * Action to submit task(Runnable) to executor - * - * @see CffuFactory#delayedExecutor(long, TimeUnit) - * @see CffuFactory#delayedExecutor(long, TimeUnit, Executor) */ final class TaskSubmitter implements Runnable { private final Executor executor; @@ -151,8 +146,6 @@ public void run() { /** * Action to cancel unneeded scheduled task by Future (for example timeouts) * - * @see Cffu#orTimeout(long, TimeUnit) - * @see Cffu#completeOnTimeout(Object, long, TimeUnit) * @see Delayer#delay(Runnable, long, TimeUnit) * @see Delayer#delayToTimoutCf(CompletableFuture, long, TimeUnit) * @see Delayer#delayToCompleteCf(CompletableFuture, Object, long, TimeUnit) diff --git a/cffu-core/src/main/java/io/foldright/cffu/NoCfsProvidedException.java b/cffu-utils/src/main/java/io/foldright/cffu/NoCfsProvidedException.java similarity index 67% rename from cffu-core/src/main/java/io/foldright/cffu/NoCfsProvidedException.java rename to cffu-utils/src/main/java/io/foldright/cffu/NoCfsProvidedException.java index 8160f752..a9c7f5fd 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/NoCfsProvidedException.java +++ b/cffu-utils/src/main/java/io/foldright/cffu/NoCfsProvidedException.java @@ -4,13 +4,9 @@ /** - * Exception indicates that NO cfs({@link Cffu} / {@link CompletableFuture}) are provided + * Exception indicates that NO cfs({@code Cffu} / {@link CompletableFuture}) are provided * for methods require cf arguments. * - * @see CffuFactory#cffuAnyOfSuccess(Cffu[]) - * @see CffuFactory#cffuAnyOfSuccess(CompletableFuture[]) - * @see CffuFactory#anyOfSuccess(Cffu[]) - * @see CffuFactory#anyOfSuccess(CompletableFuture[]) * @see CompletableFutureUtils#anyOfSuccessWithType(CompletableFuture[]) * @see CompletableFutureUtils#anyOfSuccess(CompletableFuture[]) */ diff --git a/cffu-utils/src/main/java/io/foldright/cffu/package-info.java b/cffu-utils/src/main/java/io/foldright/cffu/package-info.java new file mode 100644 index 00000000..a035df1d --- /dev/null +++ b/cffu-utils/src/main/java/io/foldright/cffu/package-info.java @@ -0,0 +1,7 @@ +/** + * Cffu Utils for {@link java.util.concurrent.CompletableFuture}. + *

+ * The key user class is {@link io.foldright.cffu.CompletableFutureUtils} + * which contains the enhanced methods for {@link java.util.concurrent.CompletableFuture}. + */ +package io.foldright.cffu; diff --git a/cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple2.java b/cffu-utils/src/main/java/io/foldright/cffu/tuple/Tuple2.java similarity index 100% rename from cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple2.java rename to cffu-utils/src/main/java/io/foldright/cffu/tuple/Tuple2.java diff --git a/cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple3.java b/cffu-utils/src/main/java/io/foldright/cffu/tuple/Tuple3.java similarity index 100% rename from cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple3.java rename to cffu-utils/src/main/java/io/foldright/cffu/tuple/Tuple3.java diff --git a/cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple4.java b/cffu-utils/src/main/java/io/foldright/cffu/tuple/Tuple4.java similarity index 100% rename from cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple4.java rename to cffu-utils/src/main/java/io/foldright/cffu/tuple/Tuple4.java diff --git a/cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple5.java b/cffu-utils/src/main/java/io/foldright/cffu/tuple/Tuple5.java similarity index 100% rename from cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple5.java rename to cffu-utils/src/main/java/io/foldright/cffu/tuple/Tuple5.java diff --git a/cffu-core/src/main/java/io/foldright/cffu/tuple/package-info.java b/cffu-utils/src/main/java/io/foldright/cffu/tuple/package-info.java similarity index 72% rename from cffu-core/src/main/java/io/foldright/cffu/tuple/package-info.java rename to cffu-utils/src/main/java/io/foldright/cffu/tuple/package-info.java index fdcb3fd0..864363b5 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/tuple/package-info.java +++ b/cffu-utils/src/main/java/io/foldright/cffu/tuple/package-info.java @@ -5,6 +5,5 @@ * @see io.foldright.cffu.tuple.Tuple3 * @see io.foldright.cffu.tuple.Tuple4 * @see io.foldright.cffu.tuple.Tuple5 - * @see io.foldright.cffu.CffuFactory#cffuCombine(io.foldright.cffu.Cffu, io.foldright.cffu.Cffu) */ package io.foldright.cffu.tuple; diff --git a/cffu-utils/src/main/javadoc/overview.html b/cffu-utils/src/main/javadoc/overview.html new file mode 100644 index 00000000..4370ab44 --- /dev/null +++ b/cffu-utils/src/main/javadoc/overview.html @@ -0,0 +1,8 @@ + + +

This is the API documentation for the util module of + + 🦝 Java CompletableFuture Fu(aka. CF-Fu, pronounced "Shifu"), + key user class is CompletableFutureUtils. + + diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.java b/cffu-utils/src/test/java/io/foldright/cffu/CffuStateTest.java similarity index 100% rename from cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.java rename to cffu-utils/src/test/java/io/foldright/cffu/CffuStateTest.java diff --git a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java b/cffu-utils/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java similarity index 73% rename from cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java rename to cffu-utils/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java index 0731c9b6..d6bf81c8 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-utils/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -4,9 +4,6 @@ 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 org.junit.jupiter.api.Test; import java.util.Arrays; @@ -14,7 +11,6 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicReference; -import static io.foldright.cffu.CffuFactoryTest.*; import static io.foldright.cffu.CompletableFutureUtils.*; import static io.foldright.test_utils.TestUtils.*; import static java.util.concurrent.ForkJoinPool.commonPool; @@ -91,10 +87,10 @@ void test_allOf__exceptionally() throws Exception { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); allOfWithResult( - createFailedFuture(rte), - createFailedFuture(another_rte), - createFailedFuture(ex1), - createFailedFuture(ex2) + failedFuture(rte), + failedFuture(anotherRte), + failedFuture(ex1), + failedFuture(ex2) ).get(); fail(); @@ -113,9 +109,9 @@ void test_allOf__exceptionally() throws Exception { sleep(100); throw rte; }), - createFailedFuture(another_rte), - createFailedFuture(ex1), - createFailedFuture(ex2) + failedFuture(anotherRte), + failedFuture(ex1), + failedFuture(ex2) ).get(); fail(); @@ -129,9 +125,9 @@ void test_allOf__exceptionally() throws Exception { try { allOfWithResult( CompletableFuture.completedFuture(n), - createFailedFuture(rte), + failedFuture(rte), CompletableFuture.completedFuture(s), - createFailedFuture(another_rte) + failedFuture(anotherRte) ).get(); fail(); @@ -143,8 +139,8 @@ void test_allOf__exceptionally() throws Exception { try { allOfWithResult( CompletableFuture.completedFuture(n), - createFailedFuture(rte), - new CompletableFuture<>() + failedFuture(rte), + createIncompleteFuture() ).get(30, TimeUnit.MILLISECONDS); fail(); @@ -156,7 +152,7 @@ void test_allOf__exceptionally() throws Exception { try { allOfWithResult( createIncompleteFuture(), - createFailedFuture(rte), + failedFuture(rte), createIncompleteFuture() ).get(100, TimeUnit.MILLISECONDS); @@ -172,10 +168,10 @@ void test_allOf__exceptionally() throws Exception { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); allOfFastFailWithResult( - createFailedFuture(rte), - createFailedFuture(another_rte), - createFailedFuture(ex1), - createFailedFuture(ex2) + failedFuture(rte), + failedFuture(anotherRte), + failedFuture(ex1), + failedFuture(ex2) ).get(); fail(); @@ -194,25 +190,25 @@ void test_allOf__exceptionally() throws Exception { sleep(100); throw rte; }), - createFailedFuture(another_rte), - createFailedFuture(ex1), - createFailedFuture(ex2) + failedFuture(anotherRte), + failedFuture(ex1), + failedFuture(ex2) ).get(); fail(); } catch (ExecutionException expected) { // anyOfSuccessWithType: the ex of first complete(in time) cf argument win // ❗dependent on the implementation behavior of `CF.anyOf`️ - assertSame(another_rte, expected.getCause()); + assertSame(anotherRte, expected.getCause()); } // success and failed try { allOfFastFailWithResult( CompletableFuture.completedFuture(n), - createFailedFuture(rte), + failedFuture(rte), CompletableFuture.completedFuture(s), - createFailedFuture(another_rte) + failedFuture(anotherRte) ).get(); fail(); @@ -224,8 +220,8 @@ void test_allOf__exceptionally() throws Exception { try { allOfFastFailWithResult( CompletableFuture.completedFuture(n), - createFailedFuture(rte), - new CompletableFuture<>() + failedFuture(rte), + createIncompleteFuture() ).get(30, TimeUnit.MILLISECONDS); fail(); @@ -237,7 +233,7 @@ void test_allOf__exceptionally() throws Exception { try { allOfFastFailWithResult( createIncompleteFuture(), - createFailedFuture(rte), + failedFuture(rte), createIncompleteFuture() ).get(100, TimeUnit.MILLISECONDS); @@ -253,10 +249,10 @@ void test_allOf__exceptionally() throws Exception { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); allOfFastFailWithResult( - createFailedFuture(rte), - createFailedFuture(another_rte), - createFailedFuture(ex1), - createFailedFuture(ex2) + failedFuture(rte), + failedFuture(anotherRte), + failedFuture(ex1), + failedFuture(ex2) ).get(); fail(); @@ -275,25 +271,25 @@ void test_allOf__exceptionally() throws Exception { sleep(100); throw rte; }), - createFailedFuture(another_rte), - createFailedFuture(ex1), - createFailedFuture(ex2) + failedFuture(anotherRte), + failedFuture(ex1), + failedFuture(ex2) ).get(); fail(); } catch (ExecutionException expected) { // anyOfSuccessWithType: the ex of first complete(in time) cf argument win // ❗dependent on the implementation behavior of `CF.anyOf`️ - assertSame(another_rte, expected.getCause()); + assertSame(anotherRte, expected.getCause()); } // success and failed try { allOfFastFail( CompletableFuture.completedFuture(n), - createFailedFuture(rte), + failedFuture(rte), CompletableFuture.completedFuture(s), - createFailedFuture(another_rte) + failedFuture(anotherRte) ).get(); fail(); @@ -305,8 +301,8 @@ void test_allOf__exceptionally() throws Exception { try { allOfFastFail( CompletableFuture.completedFuture(n), - createFailedFuture(rte), - new CompletableFuture<>() + failedFuture(rte), + createIncompleteFuture() ).get(30, TimeUnit.MILLISECONDS); fail(); @@ -318,7 +314,7 @@ void test_allOf__exceptionally() throws Exception { try { allOfFastFail( createIncompleteFuture(), - createFailedFuture(rte), + failedFuture(rte), createIncompleteFuture() ).get(100, TimeUnit.MILLISECONDS); @@ -434,22 +430,22 @@ void test_anyOf__exceptionally() throws Exception { sleep(100); throw rte; }), - createFailedFuture(another_rte), - createFailedFuture(ex1), - createFailedFuture(ex2) + failedFuture(anotherRte), + failedFuture(ex1), + failedFuture(ex2) ).get(); fail(); } catch (ExecutionException expected) { // anyOfSuccessWithType: the ex of first complete(in time) cf argument win // ❗dependent on the implementation behavior of `CF.anyOf`️ - assertSame(another_rte, expected.getCause()); + assertSame(anotherRte, expected.getCause()); } // incomplete fail incomplete try { anyOfWithType( createIncompleteFuture(), - createFailedFuture(rte), + failedFuture(rte), createIncompleteFuture() ).get(); @@ -469,9 +465,9 @@ void test_anyOf__exceptionally() throws Exception { sleep(100); throw rte; }), - createFailedFuture(another_rte), - createFailedFuture(ex1), - createFailedFuture(ex2) + failedFuture(anotherRte), + failedFuture(ex1), + failedFuture(ex2) ).get(); fail(); @@ -484,7 +480,7 @@ void test_anyOf__exceptionally() throws Exception { try { anyOfSuccessWithType( createIncompleteFuture(), - createFailedFuture(rte), + failedFuture(rte), createIncompleteFuture() ).get(30, TimeUnit.MILLISECONDS); @@ -507,7 +503,7 @@ void test_anyOf__concurrent() throws Exception { createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.completedFuture(n) ).get()); @@ -516,11 +512,11 @@ void test_anyOf__concurrent() throws Exception { assertEquals(n, anyOfWithType( CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.completedFuture(n) ).get()); @@ -543,8 +539,8 @@ void test_anyOf__concurrent() throws Exception { sleep(100); return n; }), - createFailedFuture(rte), - createFailedFuture(rte) + failedFuture(rte), + failedFuture(rte) ).get(); fail(); @@ -560,7 +556,7 @@ void test_anyOf__concurrent() throws Exception { createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.completedFuture(n) ).get()); @@ -569,11 +565,11 @@ void test_anyOf__concurrent() throws Exception { assertEquals(n, anyOfSuccessWithType( CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.supplyAsync(() -> { sleep(300); - return another_n; + return anotherN; }), CompletableFuture.completedFuture(n) ).get()); @@ -595,8 +591,8 @@ void test_anyOf__concurrent() throws Exception { sleep(100); return n; }), - createFailedFuture(rte), - createFailedFuture(rte) + failedFuture(rte), + failedFuture(rte) ).get()); } @@ -617,18 +613,18 @@ void test_combine() throws Exception { CompletableFuture.completedFuture(d) ).get()); - assertEquals(Tuple4.of(n, s, d, another_n), CompletableFutureUtils.combine( + assertEquals(Tuple4.of(n, s, d, anotherN), CompletableFutureUtils.combine( CompletableFuture.completedFuture(n), CompletableFuture.completedFuture(s), CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get()); - assertEquals(Tuple5.of(n, s, d, another_n, n + n), CompletableFutureUtils.combine( + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), CompletableFutureUtils.combine( CompletableFuture.completedFuture(n), CompletableFuture.completedFuture(s), CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(another_n), + CompletableFuture.completedFuture(anotherN), CompletableFuture.completedFuture(n + n) ).get()); } @@ -638,7 +634,7 @@ void test_combine_exceptionally() throws Exception { try { CompletableFutureUtils.combine( CompletableFuture.completedFuture(n), - createFailedFuture(rte) + failedFuture(rte) ).get(); fail(); @@ -649,7 +645,7 @@ void test_combine_exceptionally() throws Exception { try { CompletableFutureUtils.combine( CompletableFuture.completedFuture(n), - createFailedFuture(rte), + failedFuture(rte), CompletableFuture.completedFuture(s) ).get(); @@ -662,9 +658,9 @@ void test_combine_exceptionally() throws Exception { CompletableFutureUtils.combine( CompletableFuture.completedFuture(n), CompletableFuture.completedFuture(d), - createFailedFuture(rte), + failedFuture(rte), CompletableFuture.completedFuture(s), - CompletableFuture.completedFuture(another_n) + CompletableFuture.completedFuture(anotherN) ).get(); fail(); @@ -682,64 +678,160 @@ void test_combine_exceptionally() throws Exception { @Test void test_failedFuture() throws Exception { - assertTrue(CompletableFutureUtils.failedFuture(rte).isDone()); - assertEquals(n, CompletableFutureUtils.completedStage(n).toCompletableFuture().get()); - assertTrue(CompletableFutureUtils.failedStage(rte).toCompletableFuture().isDone()); + assertTrue(failedFuture(rte).isDone()); + assertEquals(n, completedStage(n).toCompletableFuture().get()); + assertTrue(failedStage(rte).toCompletableFuture().isDone()); } @Test void test_delayedExecutor() throws Exception { final AtomicReference holder = new AtomicReference<>(); - Executor delayer = CompletableFutureUtils.delayedExecutor(1, TimeUnit.MILLISECONDS); + Executor delayer = delayedExecutor(1, TimeUnit.MILLISECONDS); CompletableFuture.runAsync(() -> holder.set(testName), delayer).get(); assertEquals(testName, holder.get()); } @Test void test_exceptionallyAsync() throws Exception { - CompletableFuture cf = CompletableFutureUtils.failedFuture(rte); - assertEquals(n, CompletableFutureUtils.exceptionallyAsync(cf, ex -> n).get()); + CompletableFuture cf = failedFuture(rte); + assertEquals(n, exceptionallyAsync(cf, ex -> n).get()); + + cf = CompletableFuture.completedFuture(n); + assertEquals(n, exceptionallyAsync(cf, ex -> anotherN).get()); + } @Test void test_timeout() throws Exception { - CompletableFuture cf = new CompletableFuture<>(); + CompletableFuture cf = createIncompleteFuture(); try { - CompletableFutureUtils.orTimeout(cf, 1, TimeUnit.MILLISECONDS).get(); + orTimeout(cf, 1, TimeUnit.MILLISECONDS).get(); } catch (ExecutionException expected) { assertEquals(TimeoutException.class, expected.getCause().getClass()); } - cf = new CompletableFuture<>(); - assertEquals(n, CompletableFutureUtils.completeOnTimeout(cf, n, 1, TimeUnit.MILLISECONDS).get()); + cf = createIncompleteFuture(); + assertEquals(n, completeOnTimeout(cf, n, 1, TimeUnit.MILLISECONDS).get()); + + cf = CompletableFuture.completedFuture(n); + assertEquals(n, completeOnTimeout(cf, anotherN, 1, TimeUnit.MILLISECONDS).get()); + + cf = CompletableFuture.completedFuture(n); + assertEquals(n, orTimeout(cf, 1, TimeUnit.MILLISECONDS).get()); } @Test void test_exceptionallyCompose() throws Exception { - CompletableFuture cf = failedFuture(rte); + CompletableFuture completed = CompletableFuture.completedFuture(n); + CompletableFuture failed = failedFuture(rte); - assertEquals(n, CompletableFutureUtils.exceptionallyCompose(cf, + assertEquals(n, exceptionallyCompose(failed, ex -> CompletableFuture.completedFuture(n)).get()); - assertEquals(n, CompletableFutureUtils.exceptionallyComposeAsync(cf, + assertEquals(n, exceptionallyCompose(completed, + ex -> CompletableFuture.completedFuture(anotherN)).get()); + + assertEquals(n, exceptionallyComposeAsync(failed, ex -> CompletableFuture.completedFuture(n)).get()); + assertEquals(n, exceptionallyComposeAsync(completed, + ex -> CompletableFuture.completedFuture(anotherN)).get()); } @Test + @SuppressWarnings({"ResultOfMethodCallIgnored", "ThrowableNotThrown"}) void test_read() { - CompletableFuture cf = CompletableFuture.completedFuture(n); - CompletableFuture ff = failedFuture(rte); - assertEquals(n, CompletableFutureUtils.cffuJoin(cf, 1, TimeUnit.MILLISECONDS)); - assertEquals(n, CompletableFutureUtils.resultNow(cf)); - assertSame(rte, CompletableFutureUtils.exceptionNow(ff)); - assertSame(CffuState.SUCCESS, CompletableFutureUtils.cffuState(cf)); - assertSame(CffuState.FAILED, CompletableFutureUtils.cffuState(ff)); + final CompletableFuture completed = CompletableFuture.completedFuture(n); + + assertEquals(n, cffuJoin(completed, 1, TimeUnit.MILLISECONDS)); + assertEquals(n, resultNow(completed)); + try { + exceptionNow(completed); + fail(); + } catch (IllegalStateException expected) { + if (expected.getMessage() != null) assertEquals("Task completed with a result", expected.getMessage()); + } + assertSame(CffuState.SUCCESS, cffuState(completed)); + + //////////////////////////////////////// + + final CompletableFuture failed = failedFuture(rte); + + try { + cffuJoin(failed, 1, TimeUnit.MILLISECONDS); + fail(); + } catch (CompletionException expected) { + assertSame(rte, expected.getCause()); + } + try { + resultNow(failed); + fail(); + } catch (IllegalStateException expected) { + if (expected.getMessage() != null) assertEquals("Task completed with exception", expected.getMessage()); + } + assertSame(rte, exceptionNow(failed)); + assertSame(CffuState.FAILED, cffuState(failed)); + + //////////////////////////////////////// + + CompletableFuture cancelled = createCancelledFuture(); + try { + resultNow(cancelled); + fail(); + } catch (IllegalStateException expected) { + if (expected.getMessage() != null) assertEquals("Task was cancelled", expected.getMessage()); + } + try { + exceptionNow(cancelled); + fail(); + } catch (IllegalStateException expected) { + if (expected.getMessage() != null) assertEquals("Task was cancelled", expected.getMessage()); + } + assertSame(CffuState.CANCELLED, cffuState(cancelled)); + + //////////////////////////////////////// + + final CompletableFuture incomplete = createIncompleteFuture(); + + try { + cffuJoin(incomplete, 1, TimeUnit.MILLISECONDS); + fail(); + } catch (CompletionException expected) { + assertTrue(expected.getCause() instanceof TimeoutException); + } + try { + resultNow(incomplete); + fail(); + } catch (IllegalStateException expected) { + if (expected.getMessage() != null) assertEquals("Task has not completed", expected.getMessage()); + } + try { + exceptionNow(incomplete); + fail(); + } catch (IllegalStateException expected) { + if (expected.getMessage() != null) assertEquals("Task has not completed", expected.getMessage()); + } + assertSame(CffuState.RUNNING, cffuState(incomplete)); + + // Incomplete Future -> join before timeout + CompletableFuture later = createFutureCompleteLater(n); + assertEquals(n, cffuJoin(later, 3, TimeUnit.SECONDS)); } @Test void test_write() throws Exception { - CompletableFuture cf = new CompletableFuture<>(); - assertEquals(n, CompletableFutureUtils.completeAsync(cf, () -> n).get()); + assertEquals(n, completeAsync(createIncompleteFuture(), () -> n).get()); + assertEquals(n, completeAsync(createIncompleteFuture(), () -> n, commonPool()).get()); + try { + completeAsync(createIncompleteFuture(), () -> { + throw rte; + }).get(); + fail(); + } catch (ExecutionException expected) { + assertSame(rte, expected.getCause()); + } + + CompletableFuture completed = CompletableFuture.completedFuture(n); + assertEquals(n, completeAsync(completed, () -> anotherN).get()); } @Test @@ -747,8 +839,8 @@ void test_re_config() throws Exception { CompletionStage mf = minimalCompletionStage(CompletableFuture.completedFuture(n)); assertEquals(n, mf.toCompletableFuture().get()); - CompletableFuture cf = new CompletableFuture<>(); - CompletableFutureUtils.copy(cf).complete(n); + CompletableFuture cf = createIncompleteFuture(); + copy(cf).complete(n); assertFalse(cf.isDone()); CompletableFuture incomplete = newIncompleteFuture(cf); @@ -758,9 +850,14 @@ void test_re_config() throws Exception { } @Test - void test_getter() { + void test_executor() { Executor executor = defaultExecutor(); assertIsDefaultExecutor(executor); + + assertIsDefaultExecutor(screenExecutor(commonPool())); + + ExecutorService e = Executors.newCachedThreadPool(); + assertSame(e, screenExecutor(e)); } //////////////////////////////////////////////////////////////////////////////// @@ -778,16 +875,4 @@ private static void assertIsDefaultExecutor(Executor executor) { } private static final String testName = "CompletableFutureUtilsTest"; - - private static ExecutorService executorService; - - @BeforeAll - static void beforeAll() { - executorService = TestThreadPoolManager.createThreadPool(testName); - } - - @AfterAll - static void afterAll() { - TestThreadPoolManager.shutdownExecutorService(executorService); - } } diff --git a/cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.kt b/cffu-utils/src/test/java/io/foldright/cffu/tuple/TupleTest.kt similarity index 100% rename from cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.kt rename to cffu-utils/src/test/java/io/foldright/cffu/tuple/TupleTest.kt diff --git a/cffu-utils/src/test/java/io/foldright/test_utils/TestUtils.kt b/cffu-utils/src/test/java/io/foldright/test_utils/TestUtils.kt new file mode 100644 index 00000000..45cac97c --- /dev/null +++ b/cffu-utils/src/test/java/io/foldright/test_utils/TestUtils.kt @@ -0,0 +1,45 @@ +@file:JvmName("TestUtils") + +package io.foldright.test_utils + +import java.util.concurrent.CompletableFuture + + +//////////////////////////////////////////////////////////////////////////////// +// constants for testing +//////////////////////////////////////////////////////////////////////////////// + +const val n = 42 +const val anotherN = 424242 +const val s = "S42" +const val d = 42.1 + +@JvmField +val rte = RuntimeException("Bang") + +@JvmField +val anotherRte = RuntimeException("AnotherBang") + +//////////////////////////////////////////////////////////////////////////////// +// util methods for testing +//////////////////////////////////////////////////////////////////////////////// + +fun createIncompleteFuture(): CompletableFuture = CompletableFuture() + +@JvmOverloads +fun createFutureCompleteLater(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) +} diff --git a/pom.xml b/pom.xml index 147bc8a3..1e7bc39e 100644 --- a/pom.xml +++ b/pom.xml @@ -48,8 +48,11 @@ + + cffu-utils cffu-core - + cffu-kotlin + cffu-ttl-executor-wrapper @@ -143,7 +146,7 @@ + com.alibaba transmittable-thread-local @@ -361,6 +364,13 @@ org.apache.maven.plugins maven-jar-plugin 3.3.0 + + + + test-jar + + + org.apache.maven.plugins @@ -709,11 +719,10 @@ 1.6.13 true @@ -721,11 +730,9 @@ https://s01.oss.sonatype.org/ true ${maven.deploy.skip} diff --git a/scripts/gen_CffuApiCompatibilityTest.sh b/scripts/gen_CffuApiCompatibilityTest.sh index 01323ac5..3f719b8f 100755 --- a/scripts/gen_CffuApiCompatibilityTest.sh +++ b/scripts/gen_CffuApiCompatibilityTest.sh @@ -23,12 +23,12 @@ sed -r '/^import /,${ /@EnabledForJreRange\(.*\)$/d # replace CompatibilityTestHelper methods - s/\bTestUtils\.(\w*)CompletableFuture(\w*)/TestUtils\.\1Cffu\2/g + s/\b((Core)?TestUtils)\.(\w*)CompletableFuture(\w*)/\1\.\3Cffu\4/g # replace CompletableFuture constructor to cffuFactory.newIncompleteCffu() methods s/\bnew\s+CompletableFuture<>/cffuFactory.newIncompleteCffu/g - # replace static methds of CompletableFuture + # replace static methods of CompletableFuture s/\bCompletableFuture\./cffuFactory\./g # replace CompletableFuture class name