use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ActorTest method testWatchGC.
@Test
public void testWatchGC() throws Exception {
Assume.assumeFalse(Debug.isDebug());
final Actor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {
@Override
protected Void doRun() throws SuspendExecution, InterruptedException {
Fiber.sleep(120000);
return null;
}
});
System.out.println("actor1 is " + actor);
WeakReference wrActor2 = new WeakReference(spawnActor(new BasicActor<Message, Void>(mailboxConfig) {
@Override
protected Void doRun() throws SuspendExecution, InterruptedException {
Fiber.sleep(10);
final Object watch = watch(actor.ref());
// unwatch(actor, watch);
return null;
}
}));
System.out.println("actor2 is " + wrActor2.get());
for (int i = 0; i < 10; i++) {
Thread.sleep(10);
System.gc();
}
Thread.sleep(2000);
assertEquals(null, wrActor2.get());
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ActorTest method testSpawnWithStrandFactory.
@Test
public void testSpawnWithStrandFactory() throws Exception {
final AtomicBoolean run = new AtomicBoolean(false);
Actor<Message, Integer> actor = new BasicActor<Message, Integer>(mailboxConfig) {
@Override
protected Integer doRun() throws SuspendExecution, InterruptedException {
run.set(true);
return 3;
}
};
ActorRef a = actor.spawn(new StrandFactoryBuilder().setFiber(null).setNameFormat("my-fiber-%d").build());
Strand s = LocalActor.getStrand(a);
assertTrue(s.isFiber());
assertThat(s.getName(), equalTo("my-fiber-0"));
assertThat((Integer) LocalActor.get(a), equalTo(3));
assertThat(run.get(), is(true));
run.set(false);
actor = new BasicActor<Message, Integer>(mailboxConfig) {
@Override
protected Integer doRun() throws SuspendExecution, InterruptedException {
run.set(true);
return 3;
}
};
a = actor.spawn(new StrandFactoryBuilder().setThread(false).setNameFormat("my-thread-%d").build());
s = LocalActor.getStrand(a);
assertTrue(!s.isFiber());
assertThat(s.getName(), equalTo("my-thread-0"));
LocalActor.join(a);
assertThat(run.get(), is(true));
run.set(false);
Actor<Message, Integer> actor2 = new BasicActor<Message, Integer>("coolactor", mailboxConfig) {
@Override
protected Integer doRun() throws SuspendExecution, InterruptedException {
run.set(true);
return 3;
}
};
a = actor2.spawn(new StrandFactoryBuilder().setFiber(null).setNameFormat("my-fiber-%d").build());
s = LocalActor.getStrand(a);
assertTrue(s.isFiber());
assertThat(s.getName(), equalTo("coolactor"));
assertThat((Integer) LocalActor.get(a), equalTo(3));
assertThat(run.get(), is(true));
run.set(false);
actor2 = new BasicActor<Message, Integer>("coolactor", mailboxConfig) {
@Override
protected Integer doRun() throws SuspendExecution, InterruptedException {
run.set(true);
return 3;
}
};
a = actor2.spawn(new StrandFactoryBuilder().setThread(false).setNameFormat("my-thread-%d").build());
s = LocalActor.getStrand(a);
assertTrue(!s.isFiber());
assertThat(s.getName(), equalTo("coolactor"));
LocalActor.join(a);
assertThat(run.get(), is(true));
run.set(false);
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ActorTest method testWatch.
@Test
public void testWatch() throws Exception {
Actor<Message, Void> actor1 = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {
@Override
protected Void doRun() throws SuspendExecution, InterruptedException {
Fiber.sleep(100);
return null;
}
});
final AtomicBoolean handlerCalled = new AtomicBoolean(false);
Actor<Message, Void> actor2 = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {
@Override
protected Void doRun() throws SuspendExecution, InterruptedException {
Message m = receive(200, TimeUnit.MILLISECONDS);
assertThat(m, is(nullValue()));
return null;
}
@Override
protected Message handleLifecycleMessage(LifecycleMessage m) {
super.handleLifecycleMessage(m);
handlerCalled.set(true);
return null;
}
});
actor2.watch(actor1.ref());
actor1.join();
actor2.join();
assertThat(handlerCalled.get(), is(true));
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ProxyServerTest method testCallOnVoidMethod.
@Test
public void testCallOnVoidMethod() throws Exception {
final AtomicInteger called = new AtomicInteger(0);
final Server<?, ?, ?> a = spawnServer(true, new A() {
public int foo(String str, int x) {
throw new UnsupportedOperationException();
}
@Suspendable
public void bar(int x) {
try {
Strand.sleep(100);
called.set(x);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (SuspendExecution e) {
throw new AssertionError(e);
}
}
});
((A) a).bar(15);
assertThat(called.get(), is(15));
a.shutdown();
LocalActor.join(a, 100, TimeUnit.MILLISECONDS);
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ServerTest method whenCalledFromThreadThenDeferredResultIsReturned.
@Test
public void whenCalledFromThreadThenDeferredResultIsReturned() throws Exception {
final Server<Message, Integer, Message> s = new ServerActor<Message, Integer, Message>() {
private int a, b;
private ActorRef<?> from;
private Object id;
private boolean received;
@Override
public void init() throws SuspendExecution {
setTimeout(50, TimeUnit.MILLISECONDS);
}
@Override
public Integer handleCall(ActorRef<?> from, Object id, Message m) {
// save for later
this.a = m.a;
this.b = m.b;
this.from = from;
this.id = id;
this.received = true;
return null;
}
@Override
protected void handleTimeout() throws SuspendExecution {
if (received) {
reply(from, id, a + b);
shutdown();
}
}
}.spawn();
int res = s.call(new Message(3, 4));
assertThat(res, is(7));
LocalActor.join(s, 100, TimeUnit.MILLISECONDS);
}
Aggregations