diff --git a/pom.xml b/pom.xml index d3ea2e2..c7bae94 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ com.fathzer games-core - 0.0.11-SNAPSHOT + 0.0.12-SNAPSHOT org.junit.jupiter diff --git a/src/test/java/com/fathzer/jchess/uci/util/FutureTricks.java b/src/test/java/com/fathzer/jchess/uci/util/FutureTricks.java new file mode 100644 index 0000000..b4e74d9 --- /dev/null +++ b/src/test/java/com/fathzer/jchess/uci/util/FutureTricks.java @@ -0,0 +1,54 @@ +package com.fathzer.jchess.uci.util; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import org.awaitility.Awaitility; +import org.junit.jupiter.api.Test; + +import com.fathzer.games.util.exec.CustomThreadFactory; + +class FutureTricks { + private Thread currentThread; +// private static class MyCoolTask implements Callable() + + @Test + void test() throws InterruptedException, ExecutionException { + final ThreadFactory threadFactory = new CustomThreadFactory(() -> "executorService", true); + ExecutorService svce = Executors.newSingleThreadExecutor(threadFactory); + Callable task = () -> { + try { + currentThread = Thread.currentThread(); + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return "I was interrupted"; + } + return "Normal"; + }; + + Future future = svce.submit(task); + assertEquals("Normal", future.get()); + + future = svce.submit(task); + Awaitility.await().atLeast(100, TimeUnit.MILLISECONDS); + currentThread.interrupt(); + Awaitility.await().atLeast(100, TimeUnit.MILLISECONDS); + assertEquals("I was interrupted", future.get()); + + future = svce.submit(task); + Awaitility.await().atLeast(100, TimeUnit.MILLISECONDS); + future.cancel(true); + Awaitility.await().atLeast(100, TimeUnit.MILLISECONDS); + assertEquals("I was interrupted", future.get()); + + } + +}