Search in sources :

Example 1 with Recovery

use of actor4j.core.persistence.Recovery 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)

Aggregations

ActorSystem (actor4j.core.ActorSystem)1 Actor (actor4j.core.actors.Actor)1 PersistentActor (actor4j.core.actors.PersistentActor)1 ActorMessage (actor4j.core.messages.ActorMessage)1 ActorPersistenceObject (actor4j.core.persistence.ActorPersistenceObject)1 Recovery (actor4j.core.persistence.Recovery)1 ActorLogger (actor4j.core.utils.ActorLogger)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 MongoClient (com.mongodb.MongoClient)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1