Search in sources :

Example 1 with ActorSystem

use of actor4j.core.ActorSystem in project actor4j-core by relvaner.

the class StatelessActorFeature method test.

@Test(timeout = 10000)
public void test() {
    ActorSystem system = new ActorSystem();
    system.setParallelismFactor(2);
    CountDownLatch testDone = new CountDownLatch(system.getParallelismMin() * system.getParallelismFactor());
    ActorGroup group = new ActorGroupSet();
    system.setAlias(system.addActor(() -> new StatelessActor(group) {

        protected boolean first = true;

        @Override
        public void receive(ActorMessage<?> message) {
            logger().debug(String.format("from thread %s of actor %s%n", Thread.currentThread().getName(), self()));
            if (first) {
                testDone.countDown();
                first = false;
            }
        }
    }, system.getParallelismMin() * system.getParallelismFactor()), "instances");
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            system.sendViaAlias(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, null), "instances");
        }
    }, 0, 50);
    system.start();
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    timer.cancel();
    system.shutdownWithActors(true);
}
Also used : ActorSystem(actor4j.core.ActorSystem) Timer(java.util.Timer) TimerTask(java.util.TimerTask) ActorGroup(actor4j.core.utils.ActorGroup) StatelessActor(actor4j.core.actors.StatelessActor) ActorGroupSet(actor4j.core.utils.ActorGroupSet) ActorMessage(actor4j.core.messages.ActorMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ActorSystem

use of actor4j.core.ActorSystem in project actor4j-core by relvaner.

the class PrimarySecondaryActorFeature method test.

@Test(timeout = 2000)
public void test() {
    ActorSystem system = new ActorSystem();
    CountDownLatch testDone = new CountDownLatch(system.getParallelismMin() * system.getParallelismFactor());
    AtomicBoolean primaryReceivedFromSystem = new AtomicBoolean(false);
    AtomicInteger secondaryReceived = new AtomicInteger(0);
    AtomicBoolean primaryReceived = new AtomicBoolean(false);
    ConcurrentActorGroup group = new ConcurrentActorGroup();
    UUID primary = system.addActor(() -> new PrimaryActor("primary", group, "instances", (id) -> () -> new SecondaryActor(group, id) {

        @Override
        public void receive(ActorMessage<?> message) {
            if (message.source == primary) {
                secondaryReceived.incrementAndGet();
                testDone.countDown();
            } else if (message.source == system.SYSTEM_ID)
                publish(message);
        }
    }, system.getParallelismMin() * system.getParallelismFactor() - 1) {

        @Override
        public void preStart() {
            super.preStart();
            group.addAll(hub.getPorts());
        }

        @Override
        public void receive(ActorMessage<?> message) {
            if (message.source == system.SYSTEM_ID) {
                primaryReceivedFromSystem.set(true);
                publish(message);
            } else if (message.tag == 101) {
                primaryReceived.set(true);
                testDone.countDown();
            }
        }
    });
    system.start();
    system.send(new ActorMessage<>(null, 0, system.SYSTEM_ID, primary));
    system.send(new ActorMessage<>(null, 101, system.SYSTEM_ID, group.peek()));
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(primaryReceivedFromSystem.get());
    assertEquals(3, secondaryReceived.get());
    assertTrue(primaryReceived.get());
    system.shutdownWithActors(true);
}
Also used : ActorSystem(actor4j.core.ActorSystem) PrimaryActor(actor4j.core.actors.PrimaryActor) SecondaryActor(actor4j.core.actors.SecondaryActor) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentActorGroup(actor4j.core.utils.ConcurrentActorGroup) ActorSystem(actor4j.core.ActorSystem) PrimaryActor(actor4j.core.actors.PrimaryActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) UUID(java.util.UUID) Assert(org.junit.Assert) ActorMessage(actor4j.core.messages.ActorMessage) ConcurrentActorGroup(actor4j.core.utils.ConcurrentActorGroup) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecondaryActor(actor4j.core.actors.SecondaryActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with ActorSystem

use of actor4j.core.ActorSystem in project actor4j-core by relvaner.

the class PersistenceFeature method test.

@Test(timeout = 10000)
public void test() {
    CountDownLatch testDone = new CountDownLatch(2);
    ActorSystem system = new ActorSystem();
    AtomicBoolean first = new AtomicBoolean(true);
    UUID id = system.addActor(() -> new PersistentActor<MyState, MyEvent>("example") {

        @Override
        public void receive(ActorMessage<?> message) {
            MyEvent event1 = new MyEvent("I am the first event!");
            MyEvent event2 = new MyEvent("I am the second event!");
            saveSnapshot(null, null, new MyState("I am a state!"));
            persist((s) -> logger().debug(String.format("Event: %s", s)), (e) -> logger().error(String.format("Error: %s", e.getMessage())), event1, event2);
            if (first.getAndSet(false))
                tell(null, Actor.RESTART, self());
        }

        @Override
        public void recover(String json) {
            if (!Recovery.isError(json)) {
                logger().debug(String.format("Recovery: %s", json));
                Recovery<MyState, MyEvent> obj = Recovery.convertValue(json, new TypeReference<Recovery<MyState, MyEvent>>() {
                });
                logger().debug(String.format("Recovery: %s", obj.toString()));
                if (first.get())
                    assertEquals("{\"state\":{}}", json);
                else {
                    assertEquals("I am a state!", obj.state.title);
                    assertTrue(obj.events.size() == 2);
                    assertEquals("I am the first event!", obj.events.get(0).title);
                    assertEquals("I am the second event!", obj.events.get(1).title);
                }
                testDone.countDown();
            } else
                logger().error(String.format("Error: %s", Recovery.getErrorMsg(json)));
        }

        @Override
        public UUID persistenceId() {
            /* e.g. https://www.uuidgenerator.net/ */
            return UUID.fromString("60f086af-27d3-44e9-8fd7-eb095c98daed");
        }
    });
    // Drop database
    MongoClient client = new MongoClient("localhost", 27017);
    client.dropDatabase("actor4j-test");
    client.close();
    system.persistenceMode("localhost", 27017, "actor4j-test");
    system.start();
    system.sendWhenActive(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, id));
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    system.shutdownWithActors(true);
}
Also used : ActorSystem(actor4j.core.ActorSystem) ActorSystem(actor4j.core.ActorSystem) ActorPersistenceObject(actor4j.core.persistence.ActorPersistenceObject) Actor(actor4j.core.actors.Actor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) UUID(java.util.UUID) Recovery(actor4j.core.persistence.Recovery) ActorLogger(actor4j.core.utils.ActorLogger) PersistentActor(actor4j.core.actors.PersistentActor) ActorMessage(actor4j.core.messages.ActorMessage) CountDownLatch(java.util.concurrent.CountDownLatch) MongoClient(com.mongodb.MongoClient) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Assert(org.junit.Assert) CountDownLatch(java.util.concurrent.CountDownLatch) Recovery(actor4j.core.persistence.Recovery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MongoClient(com.mongodb.MongoClient) ActorPersistenceObject(actor4j.core.persistence.ActorPersistenceObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with ActorSystem

use of actor4j.core.ActorSystem in project actor4j-core by relvaner.

the class BehaviourFeature method before.

@Before
public void before() {
    system = new ActorSystem();
    system.setParallelismMin(1);
}
Also used : ActorSystem(actor4j.core.ActorSystem) Before(org.junit.Before)

Example 5 with ActorSystem

use of actor4j.core.ActorSystem 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

ActorSystem (actor4j.core.ActorSystem)10 CountDownLatch (java.util.concurrent.CountDownLatch)6 Test (org.junit.Test)6 ActorMessage (actor4j.core.messages.ActorMessage)5 UUID (java.util.UUID)5 Actor (actor4j.core.actors.Actor)4 Before (org.junit.Before)4 Assert (org.junit.Assert)3 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ActorTimer (actor4j.core.ActorTimer)1 EmbeddedActor (actor4j.core.actors.EmbeddedActor)1 PersistentActor (actor4j.core.actors.PersistentActor)1 PrimaryActor (actor4j.core.actors.PrimaryActor)1 PseudoActor (actor4j.core.actors.PseudoActor)1 SecondaryActor (actor4j.core.actors.SecondaryActor)1 StatelessActor (actor4j.core.actors.StatelessActor)1 ActorPersistenceObject (actor4j.core.persistence.ActorPersistenceObject)1