Skip to content

Commit

Permalink
test: adjust wait time for more stable testing
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Sep 4, 2024
1 parent f2e13d4 commit 177eb2b
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 180 deletions.
45 changes: 23 additions & 22 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static io.foldright.test_utils.TestingExecutorUtils.testExecutor;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.concurrent.ForkJoinPool.commonPool;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.function.Function.identity;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -82,8 +83,8 @@ void test_mSupply() throws Exception {
testCffuFac.mSupplyFastFailAsync(testExecutor, supplier, supplier),
testCffuFac.mSupplyAllSuccessAsync(anotherN, supplier, supplier),
testCffuFac.mSupplyAllSuccessAsync(anotherN, testExecutor, supplier, supplier),
testCffuFac.mSupplyMostSuccessAsync(anotherN, 500, TimeUnit.MILLISECONDS, supplier, supplier),
testCffuFac.mSupplyMostSuccessAsync(anotherN, testExecutor, 500, TimeUnit.MILLISECONDS, supplier, supplier),
testCffuFac.mSupplyMostSuccessAsync(anotherN, LONG_WAIT_MS, MILLISECONDS, supplier, supplier),
testCffuFac.mSupplyMostSuccessAsync(anotherN, testExecutor, LONG_WAIT_MS, MILLISECONDS, supplier, supplier),
testCffuFac.mSupplyAsync(supplier, supplier),
testCffuFac.mSupplyAsync(testExecutor, supplier, supplier),
};
Expand Down Expand Up @@ -172,17 +173,17 @@ void test_tupleMSupplyMostSuccessAsync() throws Exception {
nap();
return nnn;
};
assertEquals(Tuple2.of(n, s), testCffuFac.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), testCffuFac.tupleMSupplyMostSuccessAsync(100, MILLISECONDS, supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, MILLISECONDS, supplier_n, supplier_s).get());

assertEquals(Tuple3.of(n, s, d), testCffuFac.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), testCffuFac.tupleMSupplyMostSuccessAsync(100, MILLISECONDS, supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, MILLISECONDS, supplier_n, supplier_s, supplier_d).get());

assertEquals(Tuple4.of(n, s, d, anotherN), testCffuFac.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), testCffuFac.tupleMSupplyMostSuccessAsync(100, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get());

assertEquals(Tuple5.of(n, s, d, anotherN, nnn), testCffuFac.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), testCffuFac.tupleMSupplyMostSuccessAsync(100, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), testCffuFac.tupleMSupplyMostSuccessAsync(testExecutor, 100, MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
}

// endregion
Expand Down Expand Up @@ -295,22 +296,22 @@ void test_mostOf() throws Exception {
final Cffu<Integer> cancelled = testCffuFac.toCffu(cancelledFuture());
final Cffu<Integer> incomplete = testCffuFac.toCffu(incompleteCf());

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

assertEquals(Arrays.asList(n, null, null, null), testCffuFac.mostSuccessResultsOf(
null, 10, TimeUnit.MILLISECONDS, completed, failed, cancelled, incomplete
null, SHORT_WAIT_MS, MILLISECONDS, completed, failed, cancelled, incomplete
).get());
assertEquals(Arrays.asList(n, anotherN, anotherN, anotherN), testCffuFac.mostSuccessResultsOf(
anotherN, 10, TimeUnit.MILLISECONDS, completedStage, failed, cancelled, incomplete
anotherN, SHORT_WAIT_MS, MILLISECONDS, completedStage, failed, cancelled, incomplete
).get());

assertEquals(Arrays.asList(anotherN, anotherN, anotherN), testCffuFac.mostSuccessResultsOf(
anotherN, 10, TimeUnit.MILLISECONDS, failed, cancelled, incomplete
anotherN, SHORT_WAIT_MS, MILLISECONDS, failed, cancelled, incomplete
).get());

// do not wait for failed and cancelled
assertEquals(Arrays.asList(anotherN, anotherN), testCffuFac.mostSuccessResultsOf(
anotherN, 10, TimeUnit.DAYS, failed, cancelled
anotherN, Long.MAX_VALUE, MILLISECONDS, failed, cancelled
).get());
}

Expand Down Expand Up @@ -599,28 +600,28 @@ void test_mostSuccessTupleOf() throws Exception {
final Cffu<Integer> incomplete = testCffuFac.toCffu(incompleteCf());

assertEquals(Tuple2.of(n, s), testCffuFac.mostSuccessTupleOf(
10, TimeUnit.MILLISECONDS, completed, anotherCompleted
SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted
).get());
assertEquals(Tuple2.of(n, null), testCffuFac.mostSuccessTupleOf(
10, TimeUnit.MILLISECONDS, completed, failed
SHORT_WAIT_MS, MILLISECONDS, completed, failed
).get());

assertEquals(Tuple3.of(n, s, null), testCffuFac.mostSuccessTupleOf(
10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled
SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted, cancelled
).get());
assertEquals(Tuple3.of(null, null, s), testCffuFac.mostSuccessTupleOf(
10, TimeUnit.MILLISECONDS, incomplete, failed, anotherCompleted
SHORT_WAIT_MS, MILLISECONDS, incomplete, failed, anotherCompleted
).get());

assertEquals(Tuple4.of(n, s, null, null), testCffuFac.mostSuccessTupleOf(
10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled, incomplete
SHORT_WAIT_MS, MILLISECONDS, completed, anotherCompleted, cancelled, incomplete
).get());
assertEquals(Tuple4.of(null, null, null, null), testCffuFac.mostSuccessTupleOf(
10, TimeUnit.MILLISECONDS, incomplete, failed, cancelled, incomplete
SHORT_WAIT_MS, MILLISECONDS, incomplete, failed, cancelled, incomplete
).get());

assertEquals(Tuple5.of(null, n, s, null, null), testCffuFac.mostSuccessTupleOf(
10, TimeUnit.MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed
SHORT_WAIT_MS, MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed
).get());
}

Expand Down
73 changes: 37 additions & 36 deletions cffu-core/src/test/java/io/foldright/cffu/CffuTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static io.foldright.test_utils.TestingExecutorUtils.testCffuFac;
import static io.foldright.test_utils.TestingExecutorUtils.testExecutor;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.function.Function.identity;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -67,8 +68,8 @@ void test_thenMApply() throws Exception {
completed.thenMApplyFastFailAsync(testExecutor, function_n, function_n),
completed.thenMApplyAllSuccessAsync(anotherN, function_n, function_n),
completed.thenMApplyAllSuccessAsync(anotherN, testExecutor, function_n, function_n),
completed.thenMApplyMostSuccessAsync(100, 500, TimeUnit.MILLISECONDS, function_n, function_n),
completed.thenMApplyMostSuccessAsync(100, testExecutor, 500, TimeUnit.MILLISECONDS, function_n, function_n),
completed.thenMApplyMostSuccessAsync(anotherN, LONG_WAIT_MS, MILLISECONDS, function_n, function_n),
completed.thenMApplyMostSuccessAsync(anotherN, testExecutor, LONG_WAIT_MS, MILLISECONDS, function_n, function_n),
completed.thenMApplyAsync(function_n, function_n),
completed.thenMApplyAsync(testExecutor, function_n, function_n)
};
Expand Down Expand Up @@ -194,14 +195,14 @@ void test_thenTupleMApplyAsync() throws Exception {
assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyAllSuccessAsync(function_n, function_s, function_d, function_an, function_nnn).get());
assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyAllSuccessAsync(testExecutor, function_n, function_s, function_d, function_an, function_nnn).get());

assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s).get());
assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, function_n, function_s).get());
assertEquals(Tuple3.of(n + n, s + n, d + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get());
assertEquals(Tuple3.of(n + n, s + n, d + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get());
assertEquals(Tuple4.of(n + n, s + n, d + n, anotherN + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get());
assertEquals(Tuple4.of(n + n, s + n, d + n, anotherN + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get());
assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nnn).get());
assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nnn).get());
assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyMostSuccessAsync(MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s).get());
assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s).get());
assertEquals(Tuple3.of(n + n, s + n, d + n), completed.thenTupleMApplyMostSuccessAsync(MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d).get());
assertEquals(Tuple3.of(n + n, s + n, d + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d).get());
assertEquals(Tuple4.of(n + n, s + n, d + n, anotherN + n), completed.thenTupleMApplyMostSuccessAsync(MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an).get());
assertEquals(Tuple4.of(n + n, s + n, d + n, anotherN + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an).get());
assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyMostSuccessAsync(MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an, function_nnn).get());
assertEquals(Tuple5.of(n + n, s + n, d + n, anotherN + n, nnn + n), completed.thenTupleMApplyMostSuccessAsync(testExecutor, MEDIAN_WAIT_MS, MILLISECONDS, function_n, function_s, function_d, function_an, function_nnn).get());

assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyAsync(function_n, function_s).get());
assertEquals(Tuple2.of(n + n, s + n), completed.thenTupleMApplyAsync(testExecutor, function_n, function_s).get());
Expand All @@ -221,43 +222,43 @@ void test_thenTupleMApplyAsync() throws Exception {
@Test
void both_fastFail() throws Exception {
Cffu<Integer> cf = testCffuFac.supplyAsync(() -> {
snoreZzz(2_000);
snoreZzz(LONG_WAIT_MS);
return n;
});
final Cffu<Integer> failed = testCffuFac.failedFuture(rte);

final Runnable runnable = () -> {
};
assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.runAfterBothFastFail(failed, runnable).get(1, TimeUnit.MILLISECONDS)
cf.runAfterBothFastFail(failed, runnable).get(1, MILLISECONDS)
).getCause());
assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.runAfterBothFastFailAsync(failed, runnable).get(1, TimeUnit.MILLISECONDS)
cf.runAfterBothFastFailAsync(failed, runnable).get(1, MILLISECONDS)
).getCause());
assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.runAfterBothFastFailAsync(failed, runnable, testExecutor).get(1, TimeUnit.MILLISECONDS)
cf.runAfterBothFastFailAsync(failed, runnable, testExecutor).get(1, MILLISECONDS)
).getCause());

BiConsumer<Integer, Integer> bc = (i1, i2) -> {
};
assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.thenAcceptBothFastFail(failed, bc).get(1, TimeUnit.MILLISECONDS)
cf.thenAcceptBothFastFail(failed, bc).get(1, MILLISECONDS)
).getCause());
assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.thenAcceptBothFastFailAsync(failed, bc).get(1, TimeUnit.MILLISECONDS)
cf.thenAcceptBothFastFailAsync(failed, bc).get(1, MILLISECONDS)
).getCause());
assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.thenAcceptBothFastFailAsync(failed, bc, testExecutor).get(1, TimeUnit.MILLISECONDS)
cf.thenAcceptBothFastFailAsync(failed, bc, testExecutor).get(1, MILLISECONDS)
).getCause());

assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.thenCombineFastFail(failed, Integer::sum).get(1, TimeUnit.MILLISECONDS)
cf.thenCombineFastFail(failed, Integer::sum).get(1, MILLISECONDS)
).getCause());
assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.thenCombineFastFailAsync(failed, Integer::sum).get(1, TimeUnit.MILLISECONDS)
cf.thenCombineFastFailAsync(failed, Integer::sum).get(1, MILLISECONDS)
).getCause());
assertSame(rte, assertThrowsExactly(ExecutionException.class, () ->
cf.thenCombineFastFailAsync(failed, Integer::sum, testExecutor).get(1, TimeUnit.MILLISECONDS)
cf.thenCombineFastFailAsync(failed, Integer::sum, testExecutor).get(1, MILLISECONDS)
).getCause());
}

Expand Down Expand Up @@ -325,32 +326,32 @@ void test_catching() throws Exception {
@Test
void test_timeout() throws Exception {
assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () ->
testCffuFac.newIncompleteCffu().orTimeout(1, TimeUnit.MILLISECONDS).get()
testCffuFac.newIncompleteCffu().orTimeout(1, MILLISECONDS).get()
).getCause());
assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () ->
testCffuFac.newIncompleteCffu().orTimeout(testExecutor, 1, TimeUnit.MILLISECONDS).get()
testCffuFac.newIncompleteCffu().orTimeout(testExecutor, 1, MILLISECONDS).get()
).getCause());
assertInstanceOf(TimeoutException.class, assertThrowsExactly(ExecutionException.class, () ->
testCffuFac.newIncompleteCffu().unsafeOrTimeout(1, TimeUnit.MILLISECONDS).get()
testCffuFac.newIncompleteCffu().unsafeOrTimeout(1, MILLISECONDS).get()
).getCause());

assertEquals(n, testCffuFac.completedFuture(n).orTimeout(1, TimeUnit.MILLISECONDS).get());
assertEquals(n, testCffuFac.completedFuture(n).orTimeout(testExecutor, 1, TimeUnit.MILLISECONDS).get());
assertEquals(n, testCffuFac.completedFuture(n).unsafeOrTimeout(1, TimeUnit.MILLISECONDS).get());
assertEquals(n, testCffuFac.completedFuture(n).orTimeout(1, MILLISECONDS).get());
assertEquals(n, testCffuFac.completedFuture(n).orTimeout(testExecutor, 1, MILLISECONDS).get());
assertEquals(n, testCffuFac.completedFuture(n).unsafeOrTimeout(1, MILLISECONDS).get());

assertEquals(n, testCffuFac.newIncompleteCffu().completeOnTimeout(
n, 1, TimeUnit.MILLISECONDS).get());
n, 1, MILLISECONDS).get());
assertEquals(n, testCffuFac.newIncompleteCffu().completeOnTimeout(
n, testExecutor, 1, TimeUnit.MILLISECONDS).get());
n, testExecutor, 1, MILLISECONDS).get());
assertEquals(n, testCffuFac.newIncompleteCffu().unsafeCompleteOnTimeout(
n, 1, TimeUnit.MILLISECONDS).get());
n, 1, MILLISECONDS).get());

assertEquals(n, testCffuFac.completedFuture(n).completeOnTimeout(
anotherN, 1, TimeUnit.MILLISECONDS).get());
anotherN, 1, MILLISECONDS).get());
assertEquals(n, testCffuFac.completedFuture(n).completeOnTimeout(
anotherN, testExecutor, 1, TimeUnit.MILLISECONDS).get());
anotherN, testExecutor, 1, MILLISECONDS).get());
assertEquals(n, testCffuFac.completedFuture(n).unsafeCompleteOnTimeout(
anotherN, 1, TimeUnit.MILLISECONDS).get());
anotherN, 1, MILLISECONDS).get());
}

// endregion
Expand Down Expand Up @@ -414,18 +415,18 @@ void test_peek() throws Exception {
@Test
void test_cffuJoin() {
// Completed Future
assertEquals(n, testCffuFac.completedFuture(n).join(1, TimeUnit.MILLISECONDS));
assertEquals(n, testCffuFac.completedFuture(n).join(1, MILLISECONDS));

// Incomplete Future -> CompletionException with TimeoutException
Cffu<Object> incomplete = testCffuFac.newIncompleteCffu();
assertInstanceOf(TimeoutException.class, assertThrowsExactly(CompletionException.class, () ->
incomplete.join(1, TimeUnit.MILLISECONDS)
incomplete.join(1, MILLISECONDS)
).getCause());

// Failed Future -> CompletionException
Cffu<Object> failed = testCffuFac.failedFuture(rte);
assertSame(rte, assertThrowsExactly(CompletionException.class, () ->
failed.join(1, TimeUnit.MILLISECONDS)
failed.join(1, MILLISECONDS)
).getCause());

// Incomplete Future -> join before timeout
Expand All @@ -434,7 +435,7 @@ void test_cffuJoin() {
snoreZzz();
return n;
});
assertEquals(n, cffu.join(3, TimeUnit.SECONDS));
assertEquals(n, cffu.join(LONG_WAIT_MS, MILLISECONDS));
}

@Test
Expand Down
Loading

0 comments on commit 177eb2b

Please sign in to comment.