简体   繁体   中英

How to test akka.net persistent actors

I'am using [Akka.Net 1.3.1] a mix of ReceiveActors and ReceivePersistentActors and now I want to write tests for my actorsystem.

MyPersistentActor inherits from ReceivePersistentActor and MyActor inherits from ReceiveActor.

I also installed Akka.TestKit using version 1.3.1 .

But it seems that only ReceiveActors can be tested by Akka.TestKit. IActorRef myActorRef = this.Sys.ActorOf<MyActor>(); // is fine IActorRef myPersistentActorRef = this.Sys.ActorOf<MyPersistentActor>(); // is a problem

I also found the nuget package Akka.Persistence.TestKit version 1.2.3.43-beta . The beta wasn't changed since three month and only support akka 1.2.2 . Is it still in development or is it dead. I can not find any kind of information regarding that.

How do you test your persistent actors?

Thanks for your help!

Richi

Akka.Persistence.TestKit has been renamed to Akka.Persistence.TCK and it is used only for testing custom event journal and snapshot store implementations for compatibility with Akka.Persistence protocol. It doesn't bring any utilities for testing user actors.

There are no built-in methods to cooperate with journals/snapshot stores for testing purposes beside having implementations of them working in-memory. With that being said, you can actually work with journal/snapshot store just like with any other actor. If you look into implementations of persistence TCK specs like JournalSpec , you may get some insights into how that protocol works.

For example, if you want to initialize your journal with some events before firing the test case, you can do it like following:

void InitWithEvents(string persistenceId, params object[] events)
{
    var probe = CreateTestProbe();
    var writerGuid = Guid.NewGuid().ToString();
    var writes = new AtomicWrite[events.Length];
    for (int i = 0; i < events.Length; i++)
    {
        var e = events[i];
        writes[i] = new AtomicWrite(new Persistent(e, i+1, persistenceId, "", false, ActorRefs.NoSender, writerGuid));
    }
    var journal = Persistence.Instance.Apply(Sys).JournalFor(null);
    journal.Tell(new WriteMessages(writes, probe.Ref, 1));

    probe.ExpectMsg<WriteMessagesSuccessful>();
    for (int i = 0; i < events.Length; i++)
        probe.ExpectMsg<WriteMessageSuccess>();
}

PS: There is clearly a missing part in the persistence TestKit API, any contributions on that field are more than welcome.

I know this is an ols answer, but I couldn't find any better resource. In my tests I am actually only interested if the correct event(s) is (are) persisted after I give my command. Multiple events could be raised by starting a saga. Most of the time I am only interested in the last persisted event.

If somebody is hitting the same issue as me, this is how I fixed getting the last message, based on Bartosz initWithEvents.

        private void InitWithEvents(string persistenceId, IList<object> events)
        {
            var probe = CreateTestProbe();
            var writerGuid = Guid.NewGuid().ToString();

            var writes = new AtomicWrite[events.Count];
            for (int i = 0; i < events.Count; i++)
            {
                var e = events[i];
                writes[i] = new AtomicWrite(new Persistent(e, i+1, persistenceId, "", false, ActorRefs.NoSender, writerGuid));
            }
            journal = Persistence.Instance.Apply(Sys).JournalFor(null);
            journal.Tell(new WriteMessages(writes, probe.Ref, 1));

            probe.ExpectMsg<WriteMessagesSuccessful>();
            for (int i = 0; i < events.Count; i++)
                probe.ExpectMsg<WriteMessageSuccess>();
        }

        private object GetLastPersistedMessageFromJournal(string persistenceId)
        {
            var repointable = journal as RepointableActorRef;
            var underlying = repointable.Underlying as ActorCell;

            PropertyInfo prop = typeof(ActorCell).GetProperty("Actor", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodInfo getter = prop.GetGetMethod(nonPublic: true);
            MemoryJournal jrnl = getter.Invoke(underlying, null) as MemoryJournal;

            var read = jrnl?.Read(persistenceId, 0, Int64.MaxValue, Int64.MaxValue);
            return read?.Last().Payload;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM