Search in sources :

Example 36 with SuspendExecution

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());
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 37 with SuspendExecution

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;
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) ArrayList(java.util.ArrayList) List(java.util.List)

Example 38 with SuspendExecution

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));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 39 with SuspendExecution

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();
    }
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.anyObject(org.mockito.Matchers.anyObject) Test(org.junit.Test)

Example 40 with SuspendExecution

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());
    }
}
Also used : ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) ArrayList(java.util.ArrayList) GatewayType(de.hpi.bpt.scylla.model.process.node.GatewayType) ScyllaEvent(de.hpi.bpt.scylla.simulation.event.ScyllaEvent) ScyllaValidationException(de.hpi.bpt.scylla.exception.ScyllaValidationException) NodeNotFoundException(de.hpi.bpt.scylla.model.process.graph.exception.NodeNotFoundException) SimulationModel(de.hpi.bpt.scylla.simulation.SimulationModel)

Aggregations

SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)40 Test (org.junit.Test)30 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)24 Fiber (co.paralleluniverse.fibers.Fiber)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Suspendable (co.paralleluniverse.fibers.Suspendable)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Strand (co.paralleluniverse.strands.Strand)3 ArrayList (java.util.ArrayList)3 TimeoutException (java.util.concurrent.TimeoutException)3 Channel (co.paralleluniverse.strands.channels.Channel)2 ExecutionException (java.util.concurrent.ExecutionException)2 Matchers.anyObject (org.mockito.Matchers.anyObject)2 Actor (co.paralleluniverse.actors.Actor)1 ActorRef (co.paralleluniverse.actors.ActorRef)1 BasicActor (co.paralleluniverse.actors.BasicActor)1 LocalActor (co.paralleluniverse.actors.LocalActor)1 MailboxConfig (co.paralleluniverse.actors.MailboxConfig)1 MessageProcessor (co.paralleluniverse.actors.MessageProcessor)1 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)1