Testing Persistent Actors with an initial set of events

Hi All,

First: Great to see thing coming like the Akka-projections. Slowly but surely our ‘own’ DDD framework becomes obsolete :slight_smile:

Now, we still use our framework and did some porting to typed actors.
With the classic actors, we use a single actor as Aggregate Root and test that with a “given”, “when”, “then” kind of approach.
The given, stores a set of events in a store (in old days using the Akka internal memory store), the “when” thereafter sends a command to the actor and the “then” allows you to test the resulting events and state.

This is the structure I’d like to enable for Typed Actors and luckily the persistence testkit is now around saving some “hacking” around the store.

Except: the storage of events that will be replayed by a persistent actor needs a bit of inside knowledge as it seems.

Let say that I have a classic persistent actor with a tag “my-aggregate” and I like to replay to its current state based on events that are already pushed into the event store

I’ll use PersistenceTestKit with persistForRecovery to store events using a string for the persistenceId like my-aggregate|id1 (type pipe aggregate id) as it is passed as a string.
Replay of the typed actor ‘id1’ will thereafter replay these events, right ?

Except that it states: ERROR a.a.SupervisorStrategy - entityId [my-aggregate|id1] contains [|] which is a reserved character

What is the best way to persist events the actor will use to startup in a test ?

And next to that: what is the best way to capture the state so it can be asserted in a test ?

For the full piece of code : https://github.com/cafienne/bounded-framework/blob/typed-support3/bounded-test/src/main/scala/io/cafienne/bounded/test/typed/TestableAggregateRoot.scala

Note that it is under construction in order to get it available next to the existing one for classic actors.

Kind regards,

Olger

Do you have a stack trace or more information about that reserved character error?
You typically create it with

PersistenceId.of(entityTypeHint, entityId)

if you use ofUniqueId it is not supposed to contain the | character.

Hi Patrick,

I found the issue

  EventSourcedBehavior
    .withEnforcedReplies(
      PersistenceId(aggregateRootTag, id),
      SimpleAggregateState(List.empty[String], id),

The PersistenceId is created within creation of the eventsourced behavior. (BTW not with PersistenceId.of(… any pros or cons to use on or another ?)

I pushed the full PersistenceId as a string Into ‘id’ and that should not work indeed.

The Persistence Testkit want me to create the storage id by hand like:

   final val arTestId        = testId(id)
  final val arTestIdInStore = manager.entityTypeKey.name +   persistenceIdSeparator + arTestId

Where the arTestId is the persistence id (without type hint) and the arTestIdInStore is used for
persistenceTestKit.persistForRecovery(arTestIdInStore, evt)

Any reason that persistForRecovery does not take a PersistenceId ?

Sorry, of is for the Java API, but it’s the same as apply

I think it was written for classic initially, but now I see no reason why we couldn’t change that to PersistenceId or support both. Contibutions are welcome.

Ok, clear. I’ll give it a go by adding the option to work with PersistenceId next to the String.

1 Like