Search in sources :

Example 1 with PseudoActor

use of actor4j.core.actors.PseudoActor in project actor4j-core by relvaner.

the class ActorSystemImpl method internal_addCell.

protected UUID internal_addCell(ActorCell cell) {
    Actor actor = cell.actor;
    if (actor instanceof PseudoActor)
        pseudoCells.put(cell.id, cell);
    else {
        actor.setCell(cell);
        cells.put(cell.id, cell);
        if (actor instanceof ResourceActor)
            resourceCells.put(cell.id, false);
        if (executerService.isStarted()) {
            messageDispatcher.registerCell(cell);
            /* preStart */
            cell.preStart();
        }
    }
    return cell.id;
}
Also used : ResourceActor(actor4j.core.actors.ResourceActor) ResourceActor(actor4j.core.actors.ResourceActor) Actor(actor4j.core.actors.Actor) PseudoActor(actor4j.core.actors.PseudoActor) PseudoActor(actor4j.core.actors.PseudoActor)

Example 2 with PseudoActor

use of actor4j.core.actors.PseudoActor in project actor4j-core by relvaner.

the class PseudoActorFeature method test.

@Test(timeout = 2000)
public void test() {
    CountDownLatch testDone = new CountDownLatch(10);
    ActorSystem system = new ActorSystem();
    PseudoActor main = new PseudoActor(system, false) {

        @Override
        public void receive(ActorMessage<?> message) {
        }
    };
    final int[] postconditions_numbers = new int[] { 341, 351, 451, 318, 292, 481, 240, 478, 382, 502, 158, 401, 438, 353, 165, 344, 6, 9, 18, 31, 77, 90, 45, 63, 190, 1 };
    UUID numberGenerator = system.addActor(new ActorFactory() {

        @Override
        public Actor create() {
            return new Actor("numberGenerator") {

                protected ActorTimer timer;

                protected int counter = 0;

                @Override
                public void preStart() {
                    timer = system.timer().schedule(() -> new ActorMessage<Integer>(postconditions_numbers[counter++], 0, self(), null), main.getId(), 0, 25);
                }

                @Override
                public void receive(ActorMessage<?> message) {
                    logger().debug(String.format("numberGenerator received a message.tag (%d) from main%n", message.tag));
                    testDone.countDown();
                }

                @Override
                public void postStop() {
                    timer.cancel();
                }
            };
        }
    });
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        protected int i;

        protected int counter = 0;

        @Override
        public void run() {
            main.runWithRx().take(2).forEach(msg -> {
                assertEquals(postconditions_numbers[counter++], msg.valueAsInt());
                logger().debug("-> main received a message.value (" + msg.valueAsInt() + ") from numberGenerator");
            });
            main.send(new ActorMessage<>(null, i++, main.getId(), numberGenerator));
        }
    }, 0, 50);
    system.start();
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    timer.cancel();
    system.shutdownWithActors(true);
}
Also used : ActorSystem(actor4j.core.ActorSystem) ActorSystem(actor4j.core.ActorSystem) Actor(actor4j.core.actors.Actor) Timer(java.util.Timer) Test(org.junit.Test) ActorFactory(actor4j.core.utils.ActorFactory) UUID(java.util.UUID) ActorMessage(actor4j.core.messages.ActorMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActorLogger.logger(actor4j.core.utils.ActorLogger.logger) ActorTimer(actor4j.core.ActorTimer) TimerTask(java.util.TimerTask) Assert(org.junit.Assert) PseudoActor(actor4j.core.actors.PseudoActor) ActorTimer(actor4j.core.ActorTimer) CountDownLatch(java.util.concurrent.CountDownLatch) PseudoActor(actor4j.core.actors.PseudoActor) Timer(java.util.Timer) ActorTimer(actor4j.core.ActorTimer) TimerTask(java.util.TimerTask) Actor(actor4j.core.actors.Actor) PseudoActor(actor4j.core.actors.PseudoActor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Aggregations

Actor (actor4j.core.actors.Actor)2 PseudoActor (actor4j.core.actors.PseudoActor)2 ActorSystem (actor4j.core.ActorSystem)1 ActorTimer (actor4j.core.ActorTimer)1 ResourceActor (actor4j.core.actors.ResourceActor)1 ActorMessage (actor4j.core.messages.ActorMessage)1 ActorFactory (actor4j.core.utils.ActorFactory)1 ActorLogger.logger (actor4j.core.utils.ActorLogger.logger)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1