use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class InterfaceTest method testSuspend.
@Test
public void testSuspend() {
// final I i = new C();
Fiber co = new Fiber((String) null, null, new SuspendableRunnable() {
@Override
public final void run() throws SuspendExecution {
// next line causes an error because of incomplete merge in TypeInterpreter
// SomeInterface i = System.currentTimeMillis() > 0 ? new C() : new C2();
SomeInterface i = new C();
System.out.println("i = " + i);
i.doStuff();
}
});
while (!TestsHelper.exec(co)) System.out.println("State=" + co.getState());
System.out.println("State=" + co.getState());
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class GeneralSelectorTest method fanin.
<Message> Channel<Message> fanin(final ReceivePort<Message>[] ins) {
final Channel<Message> chan = newChannel();
spawn(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
for (; ; ) {
List<SelectAction<Message>> as = new ArrayList<>(ins.length);
for (ReceivePort<Message> c : ins) as.add(receive(c));
SelectAction<Message> sa = select(as);
// System.out.println("Received from " + sa.index());
Message m = sa.message();
// System.out.println("fanin: " + m);
if (m == null) {
chan.close();
break;
} else
chan.send(m);
}
// System.err.println("fanin done");
}
});
return chan;
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ActorTest method whenUnwatchedAfterDeathButBeforeReceiveThenExitMessageIgnored.
@Test
public void whenUnwatchedAfterDeathButBeforeReceiveThenExitMessageIgnored() throws Exception {
final Channel<Object> sync1 = Channels.newChannel(1), sync2 = Channels.newChannel(1);
final Object ping = new Object();
final Actor<Message, Void> actor1 = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {
@Override
protected final Void doRun() throws SuspendExecution, InterruptedException {
sync1.receive();
return null;
}
});
final AtomicBoolean handlerCalled = new AtomicBoolean(false);
final Actor<Message, Void> actor2 = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {
@Override
protected final Void doRun() throws SuspendExecution, InterruptedException {
sync2.receive();
final Message m = receive(200, TimeUnit.MILLISECONDS);
assertThat(m, is(nullValue()));
return null;
}
@Override
protected final Message handleLifecycleMessage(LifecycleMessage m) {
super.handleLifecycleMessage(m);
handlerCalled.set(true);
return null;
}
});
// Watch actor 2
final Object watchId = actor1.watch(actor2.ref());
// Let actor 1 go ahead
sync1.send(ping);
// Wait for actor 1 to terminate
actor1.join();
// Unwatch actor 2
actor1.unwatch(actor2.ref(), watchId);
// Let actor 2 go ahead and check the mailbox
sync2.send(ping);
// Wait for actor 2 to terminate
actor2.join();
assertThat(handlerCalled.get(), is(false));
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ServerTest method whenWatchedActorDiesDuringCallThenExitMessageDeferred.
@Test
public void whenWatchedActorDiesDuringCallThenExitMessageDeferred() throws Exception {
final Actor<Message, Void> a = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {
@Override
protected Void doRun() throws SuspendExecution, InterruptedException {
// noinspection InfiniteLoopStatement
for (; ; ) System.out.println(receive());
}
});
final Server<Message, Integer, Message> s = spawnServer(new AbstractServerHandler<Message, Integer, Message>() {
@Override
public Integer handleCall(ActorRef<?> from, Object id, Message m) throws SuspendExecution {
try {
a.getStrand().interrupt();
Strand.sleep(500);
} catch (InterruptedException ex) {
System.out.println("?????: " + Arrays.toString(ex.getStackTrace()));
}
return 0;
}
});
final AtomicReference<ExitMessage> emr = new AtomicReference<>();
final Actor<Message, Object[]> m = spawnActor(new BasicActor<Message, Object[]>(mailboxConfig) {
private Object watch;
@Override
protected Object[] doRun() throws SuspendExecution, InterruptedException {
return new Object[] { watch = watch(a.ref()), s.call(new Message(3, 4)), receive(100, TimeUnit.MILLISECONDS) };
}
@Override
protected Message handleLifecycleMessage(LifecycleMessage m) {
if (m instanceof ExitMessage) {
final ExitMessage em = (ExitMessage) m;
if (watch.equals(em.watch) && em.actor.equals(a.ref()))
emr.set(em);
}
return super.handleLifecycleMessage(m);
}
});
try {
final Object[] res = m.get();
assertNotNull(res[0]);
assertNotNull(res[1]);
assertNull(res[2]);
assertNotNull(emr.get());
assertEquals(res[1], 0);
assertEquals(res[0], emr.get().watch);
assertEquals(a.ref(), emr.get().actor);
assertNotNull(emr.get().cause);
assertEquals(emr.get().cause.getClass(), InterruptedException.class);
} catch (final Throwable t) {
fail();
}
}
use of co.paralleluniverse.fibers.SuspendExecution in project scylla by bptlab.
the class EventbasedGatewayEventPlugin method eventRoutine.
/**
* Handles a gateway event, if it is an event based gateway
* Changes scheduling behavior by already scheduling the next events of the given gateway event (if event based)
*/
@Override
public void eventRoutine(GatewayEvent desmojEvent, ProcessInstance processInstance) throws ScyllaRuntimeException {
SimulationModel model = (SimulationModel) desmojEvent.getModel();
ProcessModel processModel = processInstance.getProcessModel();
int nodeId = desmojEvent.getNodeId();
GatewayType type = processModel.getGateways().get(nodeId);
try {
Set<Integer> idsOfNextNodes = processModel.getIdsOfNextNodes(nodeId);
if (type == GatewayType.EVENT_BASED && idsOfNextNodes.size() > 1) {
// Schedule all following events
List<ScyllaEvent> nextEvents = new ArrayList<ScyllaEvent>(desmojEvent.getNextEventMap().values());
desmojEvent.scheduleNextEvents();
// and look which is scheduled first.
ScyllaEvent first = nextEvents.get(0);
for (ScyllaEvent e : nextEvents) {
if (TimeInstant.isBefore(e.scheduledNext(), first.scheduledNext())) {
first = e;
}
}
// Cancel all other events except the one that is scheduled first.
nextEvents.remove(first);
for (ScyllaEvent e : nextEvents) {
e.cancel();
}
}
} catch (NodeNotFoundException | ScyllaValidationException | SuspendExecution e) {
e.printStackTrace();
// Critical error (following nodes not found or validation error), abort the instance.
SimulationUtils.abort(model, processInstance, nodeId, desmojEvent.traceIsOn());
}
}
Aggregations