Testing a PersistentActor

91 views
Skip to first unread message

Brice Figureau

unread,
Sep 16, 2014, 9:43:47 AM9/16/14
to akka...@googlegroups.com
Hi,

I'm porting some code using an homegrown persistence system to
PersistentActor, and I'm facing the following issue: how do you force
the journal to be empty before starting the test actor?

I have some code that is using Testkit to test the actor interactions,
but unfortunately it seems that the journal is replayed (ie we use the
same actor persistent id for every test) before I even have the time to
send something.

I'm aware of this github ticket around the same subject:
https://github.com/akka/akka/issues/15571

but at the moment I'm a bit desperate regarding testability of the
normal behavior of a persistent actor (I'm not yet interested in testing
the persistence itself).

Would it work to create a special journal that throws every event to the
bitbucket and never return anything?
--
Brice Figureau
My Blog: http://www.masterzen.fr/

Michael Pisula

unread,
Sep 16, 2014, 9:48:55 AM9/16/14
to akka...@googlegroups.com, bric...@daysofwonder.com
Hi Brice,
I had a similar problem some time ago. I decided to create a simple in-memory journal to get around that. Worked fine in my case, so I decided to share the journal on github. You can find it here if that is of interest to you: https://github.com/michaelpisula/akka-journal-inmemory

Cheers,
Michael

Brice Figureau

unread,
Sep 16, 2014, 10:13:57 AM9/16/14
to akka...@googlegroups.com
Hi Michael,

On Tue, 2014-09-16 at 06:48 -0700, Michael Pisula wrote:
> Hi Brice,
> I had a similar problem some time ago. I decided to create a simple
> in-memory journal to get around that. Worked fine in my case, so I
> decided to share the journal on github. You can find it here if that
> is of interest to
> you: https://github.com/michaelpisula/akka-journal-inmemory

Thanks!
Isn't it the same thing as the (undocumented) akka 'inmem' journal
implementation?

In my own case, I think my main problem (beside inheriting the journal
from test run to the next test run) is that all my tests (currently
cucumber tests) share the same actor persistent id, so the next test
gets a replay of the previous events, whereas they should be fully
isolated.

Sean Walsh

unread,
Sep 16, 2014, 12:22:47 PM9/16/14
to akka...@googlegroups.com, bric...@daysofwonder.com
If you use something like Cassandra you can specify the journal location in your specs via configuration and delete them in afterEach or afterAll. This also makes your specs more realistic in that you won't be using the in memory journal in production.

Michael Pisula

unread,
Sep 18, 2014, 8:46:56 AM9/18/14
to akka...@googlegroups.com, bric...@daysofwonder.com
Hi Brice,

yes, they are pretty similar. What was important to me was the ability to log the messages, as well as cleaning the journal each time the actor system is shut down. If I remember correctly the inmem journal does not do that.
I start a new actor system for every test, so that gives me a fresh journal for each test. If you are reusing actor systems for several tests the messages will be replayed though. Only way I can think of to support your use-case would be to disable replaying optionally (i.e. when a config setting is enabled replay will not return any messages). Might be worthwhile for a test-only journal... 

As to what Sean suggest, if you have a journal that will not freak when the journal files are deleted between runs, that surely is an option. Still, the journal you want to run in production might not, so you would still have to use a different journal for testing, at which point the in-memory journal is as realistic as the other. Also interacting with files from your test code can be a problem, especially when CI systems, different OS etc. are involved, so personally, I prefer to use the in-memory journal.

Cheers,
Michael
Reply all
Reply to author
Forward
0 new messages