Search in sources :

Example 1 with CheckedCallable

use of co.paralleluniverse.common.util.CheckedCallable in project quasar by puniverse.

the class FiberAsyncTest method whenCancelRunBlockingInterruptExecutingThread.

@Test
public void whenCancelRunBlockingInterruptExecutingThread() throws Exception {
    final AtomicBoolean started = new AtomicBoolean();
    final AtomicBoolean interrupted = new AtomicBoolean();
    Fiber fiber = new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            FiberAsync.runBlocking(Executors.newSingleThreadExecutor(), new CheckedCallable<Void, RuntimeException>() {

                @Override
                public Void call() throws RuntimeException {
                    started.set(true);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        interrupted.set(true);
                    }
                    return null;
                }
            });
        }
    });
    fiber.start();
    Thread.sleep(100);
    fiber.cancel(true);
    try {
        fiber.join(5, TimeUnit.MILLISECONDS);
        fail("InterruptedException not thrown");
    } catch (ExecutionException e) {
        if (!(e.getCause() instanceof InterruptedException))
            fail("InterruptedException not thrown");
    }
    Thread.sleep(100);
    assertThat(started.get(), is(true));
    assertThat(interrupted.get(), is(true));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CheckedCallable(co.paralleluniverse.common.util.CheckedCallable) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

CheckedCallable (co.paralleluniverse.common.util.CheckedCallable)1 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1