Skip to content

Commit

Permalink
test: add test case of thread interruption check to `ListenableFuture…
Browse files Browse the repository at this point in the history
…Utils#toCompletableFuture` 🧵
  • Loading branch information
linzee1 authored and oldratlee committed Aug 10, 2024
1 parent 3107b5e commit 655e571
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

import java.time.Duration;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

import static io.foldright.cffu.ListenableFutureUtils.*;
import static io.foldright.test_utils.TestUtils.*;
Expand Down Expand Up @@ -129,6 +131,33 @@ void test_lf2cf_setCancellationExceptionToCf_cancellationAndPropagation() throws
assertTrue(cf.isCancelled());
assertThrowsExactly(CancellationException.class, cf::get);
}
// check interruption happened
{
final AtomicBoolean interrupted = new AtomicBoolean(false);
final ListenableFuture<Integer> lf = Futures.submit(() -> {
try {
Thread.sleep(Duration.ofSeconds(1));
} catch (InterruptedException ex) {
interrupted.set(true);
}
return n;
}, executorService);
final CompletableFuture<Integer> cf = toCompletableFuture(lf, executorService, true);

assertTrue(cf.completeExceptionally(new CancellationException()));
waitForAllCfsToComplete(cf);
waitForAllLfsToComplete(lf);

assertTrue(lf.isCancelled());
assertThrowsExactly(CancellationException.class, lf::get);
assertTrue(cf.isCancelled());
assertThrowsExactly(CancellationException.class, cf::get);

// need nap for interruption check
// it's ok for testing code...
nap();
assertTrue(interrupted.get());
}
{
final ListenableFuture<Integer> lf = SettableFuture.create();
final CompletableFuture<Integer> cf = toCompletableFuture(lf, executorService, true);
Expand Down

0 comments on commit 655e571

Please sign in to comment.