Search in sources :

Example 76 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class FiberTest method testCancel1.

@Test
public void testCancel1() throws Exception {
    final AtomicBoolean started = new AtomicBoolean();
    final AtomicBoolean terminated = new AtomicBoolean();
    Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution {
            started.set(true);
            try {
                Fiber.sleep(100);
                fail("InterruptedException not thrown");
            } catch (InterruptedException e) {
            }
            terminated.set(true);
        }
    });
    fiber.start();
    Thread.sleep(20);
    fiber.cancel(true);
    fiber.join(5, TimeUnit.MILLISECONDS);
    assertThat(started.get(), is(true));
    assertThat(terminated.get(), is(true));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 77 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class FiberTest method testThreadLocals.

@Test
public void testThreadLocals() throws Exception {
    final ThreadLocal<String> tl1 = new ThreadLocal<>();
    final InheritableThreadLocal<String> tl2 = new InheritableThreadLocal<>();
    tl1.set("foo");
    tl2.set("bar");
    Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(tl1.get(), is(nullValue()));
            assertThat(tl2.get(), is("bar"));
            tl1.set("koko");
            tl2.set("bubu");
            assertThat(tl1.get(), is("koko"));
            assertThat(tl2.get(), is("bubu"));
            Fiber.sleep(100);
            assertThat(tl1.get(), is("koko"));
            assertThat(tl2.get(), is("bubu"));
        }
    });
    fiber.start();
    fiber.join();
    assertThat(tl1.get(), is("foo"));
    assertThat(tl2.get(), is("bar"));
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 78 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class InstrumentationOptimizerTest method testDontSkipForwardsWithLoopBefore.

@Test
public void testDontSkipForwardsWithLoopBefore() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            dontSkipForwardsWithLoopBefore();
        }
    }).start().join();
    assertFalse(isOptimized("skipForwardsWithLoopBefore"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 79 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class InstrumentationOptimizerTest method testSkipForwardsWithLoopAfter.

@Test
public void testSkipForwardsWithLoopAfter() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            skipForwardsWithLoopAfter();
        }
    }).start().join();
    assertTrue(isOptimized("skipForwardsWithLoopAfter"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 80 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class InstrumentationOptimizerTest method testSkipForwardsToSuspendableLong.

@Test
public void testSkipForwardsToSuspendableLong() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            skipForwardsToSuspendableLong();
        }
    }).start().join();
    assertTrue(isOptimized("skipForwardsToSuspendableLong"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Aggregations

SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)90 Test (org.junit.Test)82 Fiber (co.paralleluniverse.fibers.Fiber)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Strand (co.paralleluniverse.strands.Strand)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Ignore (org.junit.Ignore)3 Condition (co.paralleluniverse.strands.Condition)2 SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)2 Timeout (co.paralleluniverse.strands.Timeout)2 Channel (co.paralleluniverse.strands.channels.Channel)2 IntChannel (co.paralleluniverse.strands.channels.IntChannel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CheckedCallable (co.paralleluniverse.common.util.CheckedCallable)1 Function2 (co.paralleluniverse.common.util.Function2)1 Pair (co.paralleluniverse.common.util.Pair)1 VerifyInstrumentationException (co.paralleluniverse.fibers.VerifyInstrumentationException)1