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"));
}
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();
}
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) {
}
}
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));
}
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"));
}
Aggregations