Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Aug 10, 2024
1 parent c05b2fb commit 04afc0a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ void test_anyOf__concurrent() throws Exception {
incompleteCf(),
incompleteCf(),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
completedFuture(n)
Expand All @@ -720,11 +720,11 @@ void test_anyOf__concurrent() throws Exception {
// wait/success then success
assertEquals(n, anyOf(
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
completedFuture(n)
Expand All @@ -735,7 +735,7 @@ void test_anyOf__concurrent() throws Exception {
incompleteCf(),
incompleteCf(),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
throw rte;
}),
completedFuture(n)
Expand All @@ -760,7 +760,7 @@ void test_anyOf__concurrent() throws Exception {
incompleteCf(),
incompleteCf(),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
completedFuture(n)
Expand All @@ -769,11 +769,11 @@ void test_anyOf__concurrent() throws Exception {
// wait/success then success
assertEquals(n, anySuccessOf(
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
return anotherN;
}),
completedFuture(n)
Expand All @@ -784,7 +784,7 @@ void test_anyOf__concurrent() throws Exception {
incompleteCf(),
incompleteCf(),
CompletableFuture.supplyAsync(() -> {
sleep(300);
snoreZzz();
throw rte;
}),
completedFuture(n)
Expand Down Expand Up @@ -1226,10 +1226,7 @@ void test_both() throws Exception {

@Test
void both_fastFail() throws Exception {
CompletableFuture<Integer> cf_n = CompletableFuture.supplyAsync(() -> {
sleep(2_000);
return n;
});
CompletableFuture<Integer> cf_n = completeLaterCf(n);
final CompletableFuture<Integer> failed = failedFuture(rte);
final CompletableFuture<Integer> cf_ee = failedFuture(anotherRte);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import java.util.concurrent.*;

import static io.foldright.cffu.ListenableFutureUtils.*;
import static io.foldright.test_utils.TestUtils.nap;
import static io.foldright.test_utils.TestUtils.snoreZzz;
import static io.foldright.test_utils.TestingConstants.*;
import static io.foldright.test_utils.TestUtils.sleep;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -134,7 +135,7 @@ void test_lf2cf_setCancellationExceptionToCf_cancellationAndPropagation() throws
final CompletableFuture<Integer> cf = toCompletableFuture(lf, executorService, true);

assertTrue(cf.completeExceptionally(new IllegalArgumentException()));
sleep(100);
snoreZzz();
waitForAllCfsToComplete(cf);

assertFalse(lf.isDone());
Expand All @@ -159,7 +160,7 @@ void test_lf2cf_dependentLf() throws Exception {
toCompletableFuture(lf, MoreExecutors.directExecutor(), true));

cf.cancel(false);
sleep(20);
nap();

assertTrue(cf.isCancelled());
assertThrowsExactly(CancellationException.class, cf::get);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,14 @@ class CompletableFutureUsageStudyCaseTest : FunSpec({
}

test("timeout control: normally completed with replacement value").config(enabledIf = java9Plus) {
val f = CompletableFuture.supplyAsync {
sleep(1000)
n
}.completeOnTimeout(anotherN, 1, TimeUnit.MILLISECONDS)
val f = completeLaterCf(n)
.completeOnTimeout(anotherN, 1, TimeUnit.MILLISECONDS)

f.await() shouldBe anotherN
}

test("timeout control: exceptionally completed with java.util.concurrent.TimeoutException").config(enabledIf = java9Plus) {
val f = CompletableFuture
.supplyAsync {
sleep(1000)
n
}
val f = completeLaterCf(n)
.orTimeout(1, TimeUnit.MILLISECONDS)
.exceptionally {
it.shouldBeTypeOf<TimeoutException>()
Expand Down Expand Up @@ -363,10 +357,7 @@ class CompletableFutureUsageStudyCaseTest : FunSpec({
xtest("performance CF then*").config(invocations = 10) {
val times = 1_000_000

var f = CompletableFuture.supplyAsync(
{ sleep(); currentTimeMillis() },
testThreadPoolExecutor
)
var f = completeLaterCf({ currentTimeMillis() }, executor = testForkJoinPoolExecutor)

val tick = currentTimeMillis()
repeat(times) {
Expand Down
15 changes: 15 additions & 0 deletions cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.kotest.matchers.shouldBe
import org.apache.commons.lang3.JavaVersion
import org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast
import java.util.concurrent.*
import java.util.function.Supplier


////////////////////////////////////////////////////////////////////////////////
Expand All @@ -28,11 +29,25 @@ fun <T> completeLaterCf(value: T, millis: Long = 100): CompletableFuture<T> = Co
value
}

@JvmOverloads
fun <T> completeLaterCf(
value: () -> T, millis: Long = 100, executor: Executor = DEFAULT_EXECUTOR
): CompletableFuture<T> {
val action = Supplier {
sleep(millis)
value()
}
return if (executor === DEFAULT_EXECUTOR) CompletableFuture.supplyAsync(action)
else CompletableFuture.supplyAsync(action, executor)
}

@JvmOverloads
fun <T> cancelledFuture(mayInterruptIfRunning: Boolean = false): CompletableFuture<T> = CompletableFuture<T>().apply {
cancel(mayInterruptIfRunning)
}

private val DEFAULT_EXECUTOR: Executor = Executor { /* do nothing */ }

// endregion
////////////////////////////////////////////////////////////////////////////////
// region# Helper functions for API compatibility test
Expand Down

0 comments on commit 04afc0a

Please sign in to comment.