@Test
public void cancelDuringExecutionShouldThrowAndEventuallyStopExecution() throws InterruptedException, ExecutionException, IOException {

    CountDownLatch waitForExecution = new CountDownLatch(1);
    CountDownLatch execution = new CountDownLatch(1); // Will be interrupted, not decremented
    CountDownLatch waitForInterruption = new CountDownLatch(1);
    MutableBoolean stopped = new MutableBoolean(false);
    LocalCommandExecutor localCommandExecutor = new AbstractLocalCommandExecutor() {
        @Override
        void nonZeroLog(List<String> cmdLs, int exitCode) {
        }
        @Override
        public int execute(String program, List<String> args, Path outFile, Path errFile, Path workingDir, Map<String, String> env) throws IOException, InterruptedException {
            waitForExecution.countDown();
            execution.await(); //Simulates process running
            return 0;
        }
        @Override
        public void stop(Path workingDir) {
            stopped.setTrue();
            waitForInterruption.countDown();
        }
        @Override
        public void stopForcibly(Path workingDir) {
            stopped.setTrue();
            waitForInterruption.countDown();
        }
    };
    try (ComputationManager computationManager = new LocalComputationManager(config, localCommandExecutor, ForkJoinPool.commonPool())) {
        CompletableFuture<Object> result = computationManager.execute(ExecutionEnvironment.createDefault(), new AbstractExecutionHandler<Object>() {
            @Override
            public List<CommandExecution> before(Path workingDir) {
                return dummyExecutions();
            }
            @Override
            public Object after(Path workingDir, ExecutionReport report) throws IOException {
                fail();
                return super.after(workingDir, report);
            }
        });
        waitForExecution.await();
        result.cancel(true);
        result.get();
        fail("Should not happen: result has been cancelled");
    } catch (CancellationException exc) {
        //OK
    }
    waitForInterruption.await(10, TimeUnit.SECONDS);
    assertTrue(stopped.isTrue());
}