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);
}
Aggregations