Search in sources :

Example 6 with SuspendableRunnable

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

the class FiberTest method testInheritThreadLocals.

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

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

Example 7 with SuspendableRunnable

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

the class FiberTest method testDumpStackWaitingFiberWhenCalledFromFiber.

@Test
public void testDumpStackWaitingFiberWhenCalledFromFiber() throws Exception {
    final Condition cond = new SimpleConditionSynchronizer(null);
    final AtomicBoolean flag = new AtomicBoolean(false);
    final Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            foo();
        }

        private void foo() throws InterruptedException, SuspendExecution {
            Object token = cond.register();
            try {
                for (int i = 0; !flag.get(); i++) cond.await(i);
            } finally {
                cond.unregister(token);
            }
        }
    }).start();
    Thread.sleep(200);
    Fiber fiber2 = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            StackTraceElement[] st = fiber.getStackTrace();
            // Strand.printStackTrace(st, System.err);
            assertThat(st[0].getMethodName(), equalTo("park"));
            boolean found = false;
            for (StackTraceElement ste : st) {
                if (ste.getMethodName().equals("foo")) {
                    found = true;
                    break;
                }
            }
            assertThat(found, is(true));
            assertThat(st[st.length - 1].getMethodName(), equalTo("run"));
            assertThat(st[st.length - 1].getClassName(), equalTo(Fiber.class.getName()));
        }
    }).start();
    fiber2.join();
    flag.set(true);
    cond.signalAll();
    fiber.join();
}
Also used : Condition(co.paralleluniverse.strands.Condition) SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 8 with SuspendableRunnable

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

the class FiberTest method testPriority.

@Test
public void testPriority() throws Exception {
    Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution {
        }
    });
    assertThat(fiber.getPriority(), is(Strand.NORM_PRIORITY));
    fiber.setPriority(3);
    assertThat(fiber.getPriority(), is(3));
    try {
        fiber.setPriority(Strand.MAX_PRIORITY + 1);
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        fiber.setPriority(Strand.MIN_PRIORITY - 1);
        fail();
    } catch (IllegalArgumentException e) {
    }
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 9 with SuspendableRunnable

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

the class InheritTest method testInherit.

@Test
public void testInherit() {
    final C dut = new C();
    Fiber c = new Fiber((String) null, null, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution {
            dut.myMethod();
        }
    });
    for (int i = 0; i < 3; i++) {
        exec(c);
    }
    assertEquals(5, dut.result.size());
    assertEquals("a", dut.result.get(0));
    assertEquals("o1", dut.result.get(1));
    assertEquals("o2", dut.result.get(2));
    assertEquals("b", dut.result.get(3));
    assertEquals("b", dut.result.get(4));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 10 with SuspendableRunnable

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

the class InstrumentationOptimizerTest method testDontSkipForwardsWithReflectiveCalls.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            try {
                dontSkipForwardsWithReflectiveCalls();
            } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                throw new RuntimeException(ex);
            }
        }
    }).start().join();
    assertFalse(isOptimized("skipForwardsWithReflectiveCalls"));
}
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