0

I have parent actor with multiple children, and I want to check that some of them receive same message. As I can understand, TestKit has only one TestActor, so there is no way to achieve this?

Here is an example: Parent actor has Child1, Child2 and Child3 actors, set by configuration message. I need to check that when Parent received Msg1 message, it will send it to Child1 and Child2, but not to Child3.

The only way to do it is to substitute child actors with TestActor and then call ExpectMsg twice. It will tell us that Parent send message to its children, but how to be sure it was send to correct ones?

bonzaster
  • 313
  • 2
  • 12

1 Answers1

0

In short: see TestProbe class, example here

More detailed: TestKit has special class called TestProbe. The TestProbe has all the capabilities of the TestActor and then some. As an added bonus, you can run many probes in parallel.

[Test]
public void FriendsPresenceActor_should_alert_other_users_when_friend_comes_online()
{
    var presence = ActorOfAsTestActorRef<FriendsPresenceActor>("presence");

    // create probes in place of users
    var user1 = CreateTestProbe("user1");
    var user2 = CreateTestProbe("user2");
    var user3 = CreateTestProbe("user3");

    // make those probes send a message I want
    user1.Send(presence, new RegisterUser(user1));
    user2.Send(presence, new RegisterUser(user2));
    user3.Send(presence, new RegisterUser(user3));

    // pre-check we have the right number of subscribers
    Assert.AreEqual(3, presence.UnderlyingActor.Subscribers.Count);

    // trigger by having a probe send msg that I want
    user1.Send(presence, new UserOnline(user1));

    // other user probes should have been informed of new friend presence
    user1.ExpectNoMsg();
    user2.ExpectMsgFrom<UserOnline>(presence, online => online.User.Equals(user1));
    user3.ExpectMsgFrom<UserOnline>(presence, online => online.User.Equals(user1));
}

P.S. Be aware of AutoPilot, which is also available for Akka.Net

Community
  • 1
  • 1
bonzaster
  • 313
  • 2
  • 12