Skip to content

Commit

Permalink
test: clean test codes 🧹💕
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Aug 10, 2024
1 parent e2899f8 commit 8d61e50
Show file tree
Hide file tree
Showing 22 changed files with 896 additions and 909 deletions.
122 changes: 54 additions & 68 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
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.TestUtils;
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;
Expand All @@ -17,20 +16,21 @@
import java.util.concurrent.*;
import java.util.function.Supplier;

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.cffu.DefaultExecutorTestUtils.unwrapMadeExecutor;
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;
import static org.junit.jupiter.api.Assertions.*;


/**
* see io.foldright.compatibility_test.CffuApiCompatibilityTest
* @see io.foldright.compatibility_test.CffuApiCompatibilityTest
*/
@SuppressWarnings("RedundantThrows")
@SuppressWarnings({"RedundantThrows", "DataFlowIssue", "JavadocReference"})
class CffuFactoryTest {
////////////////////////////////////////////////////////////////////////////////
// region# Constructor Method
Expand All @@ -53,7 +53,7 @@ void test_newIncompleteCffu() {

@Test
void test_mRun() throws Exception {
final Runnable runnable = () -> sleep(100);
final Runnable runnable = TestUtils::snoreZzz;

final long tick = System.currentTimeMillis();
@SuppressWarnings("unchecked")
Expand All @@ -73,7 +73,7 @@ void test_mRun() throws Exception {
@Test
void test_mSupply() throws Exception {
final Supplier<Integer> supplier = () -> {
sleep(100);
snoreZzz();
return n;
};

Expand Down Expand Up @@ -120,24 +120,24 @@ void test_mSupply() throws Exception {
@Test
void test_tupleMSupplyAsync() throws Exception {
final Supplier<Integer> supplier_n = () -> {
sleep(100);
snoreZzz();
return n;
};
final Supplier<String> supplier_s = () -> {
sleep(100);
snoreZzz();
return s;
};

final Supplier<Double> supplier_d = () -> {
sleep(100);
snoreZzz();
return d;
};
final Supplier<Integer> supplier_an = () -> {
sleep(100);
snoreZzz();
return anotherN;
};
final Supplier<Integer> supplier_nn = () -> {
sleep(100);
snoreZzz();
return n + n;
};

Expand Down Expand Up @@ -172,24 +172,24 @@ void test_tupleMSupplyAsync() throws Exception {
@Test
void test_tupleMSupplyMostSuccessAsync() throws Exception {
final Supplier<Integer> supplier_n = () -> {
sleep(10);
nap();
return n;
};
final Supplier<String> supplier_s = () -> {
sleep(10);
nap();
return s;
};

final Supplier<Double> supplier_d = () -> {
sleep(10);
nap();
return d;
};
final Supplier<Integer> supplier_an = () -> {
sleep(10);
nap();
return anotherN;
};
final Supplier<Integer> supplier_nn = () -> {
sleep(10);
nap();
return n + n;
};
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get());
Expand Down Expand Up @@ -312,8 +312,8 @@ void test_mostOf() throws Exception {
final Cffu<Integer> completed = cffuFactory.completedFuture(n);
final CompletionStage<Integer> completedStage = cffuFactory.completedStage(n);
final Cffu<Integer> failed = cffuFactory.failedFuture(rte);
final Cffu<Integer> cancelled = cffuFactory.toCffu(createCancelledFuture());
final Cffu<Integer> incomplete = cffuFactory.toCffu(createIncompleteFuture());
final Cffu<Integer> cancelled = cffuFactory.toCffu(cancelledFuture());
final Cffu<Integer> incomplete = cffuFactory.toCffu(incompleteCf());

assertEquals(0, cffuFactory.mostSuccessResultsOf(null, 10, TimeUnit.MILLISECONDS).get().size());

Expand All @@ -340,9 +340,9 @@ void test_mostOf() throws Exception {
////////////////////////////////////////////////////////////////////////////////

@Test
void test_anyOf() throws Exception {
void test_anyOf_anySuccessOf() throws Exception {
assertEquals(n, cffuFactory.anyOf(
createIncompleteFuture(),
incompleteCf(),
completedFuture(n)
).get());
assertEquals(n, cffuFactory.anyOf(
Expand All @@ -361,7 +361,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());
Expand Down Expand Up @@ -404,16 +404,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());
}

Expand All @@ -424,7 +424,7 @@ void test_anySuccessOf__trivial_case() throws Exception {
cffuFactory.newIncompleteCffu(),
cffuFactory.newIncompleteCffu(),
cffuFactory.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
cffuFactory.completedFuture(n)
Expand All @@ -434,7 +434,7 @@ void test_anySuccessOf__trivial_case() throws Exception {
cffuFactory.newIncompleteCffu(),
cffuFactory.newIncompleteCffu(),
cffuFactory.supplyAsync(() -> {
sleep(300);
snoreZzz();
throw rte;
}),
cffuFactory.completedFuture(n)
Expand All @@ -443,11 +443,11 @@ void test_anySuccessOf__trivial_case() throws Exception {
// all success
assertEquals(n, cffuFactory.anySuccessOf(
cffuFactory.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
cffuFactory.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
cffuFactory.completedFuture(n)
Expand All @@ -457,20 +457,20 @@ void test_anySuccessOf__trivial_case() throws Exception {

// success then success
assertEquals(n, cffuFactory.anySuccessOf(
createIncompleteFuture(),
createIncompleteFuture(),
incompleteCf(),
incompleteCf(),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
completedFuture(n)
).get());
// success then failed
assertEquals(n, cffuFactory.anySuccessOf(
createIncompleteFuture(),
createIncompleteFuture(),
incompleteCf(),
incompleteCf(),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
throw rte;
}),
completedFuture(n)
Expand All @@ -479,11 +479,11 @@ void test_anySuccessOf__trivial_case() throws Exception {
// all success
assertEquals(n, cffuFactory.anySuccessOf(
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
completedFuture(n)
Expand Down Expand Up @@ -639,8 +639,8 @@ void test_mostSuccessTupleOf() throws Exception {
final Cffu<Integer> completed = cffuFactory.completedFuture(n);
final CompletionStage<String> anotherCompleted = cffuFactory.completedStage(s);
final Cffu<Integer> failed = cffuFactory.failedFuture(rte);
final Cffu<Integer> cancelled = cffuFactory.toCffu(createCancelledFuture());
final Cffu<Integer> incomplete = cffuFactory.toCffu(createIncompleteFuture());
final Cffu<Integer> cancelled = cffuFactory.toCffu(cancelledFuture());
final Cffu<Integer> incomplete = cffuFactory.toCffu(incompleteCf());

assertEquals(Tuple2.of(n, s), cffuFactory.mostSuccessTupleOf(
10, TimeUnit.MILLISECONDS, completed, anotherCompleted
Expand Down Expand Up @@ -807,15 +807,15 @@ void test_toCffu() throws Exception {
assertEquals(n, cf.get());
shouldNotBeMinimalStage(cf);

final Cffu<Integer> cffu_in = cffuFactory.completedFuture(42);
final Cffu<Integer> cffu_in = cffuFactory.completedFuture(n);

CffuFactory fac = CffuFactory.builder(anotherExecutorService).forbidObtrudeMethods(true).build();
CffuFactory fac = CffuFactory.builder(dummyExecutor).forbidObtrudeMethods(true).build();
Cffu<Integer> 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));
Expand Down Expand Up @@ -920,12 +920,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());
Expand All @@ -940,7 +940,7 @@ void test_forbidObtrudeMethods_property() {
Cffu<Object> 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()
Expand All @@ -959,31 +959,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;
}
30 changes: 0 additions & 30 deletions cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.java

This file was deleted.

26 changes: 26 additions & 0 deletions cffu-core/src/test/java/io/foldright/cffu/CffuStateTest.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading

0 comments on commit 8d61e50

Please sign in to comment.