akka.nettestkit

Akka.NET TestKit synchronous behavior


I have the following C# code to test an AKKA.NET actor's behavior. The statement productActor.Tell(new ChangeActiveStatus(true)); is expected to be a blocking call (TestKit makes it a synchronous call as per this blog) but I'm seeing it returns immediately. As a result, the second test fails although the ActiveStatus will be changed later.

[TestMethod]
public void ProductActorUnitTests_CheckFor_ActiveStatusChanged()
{
    var productActor = ActorOfAsTestActorRef<ProductActor>();

    Assert.IsTrue(ProductActor.UnderlyingActor.ActiveStatus == false, "The initial ActiveStatus is expected to be FALSE.");

    productActor.Tell(new ChangeActiveStatus(true));

    Assert.IsTrue(productActor.UnderlyingActor.ActiveStatus == true, "The new ActiveStatus is expected to be TRUE.");
}

****** UPDATE *****

The following code with a Thread.Sleep(10000) succeeds:

[TestMethod]
public void ProductActorUnitTests_CheckFor_ActiveStatusChanged()
{
    var productActor = ActorOfAsTestActorRef<ProductActor>();

    Assert.IsTrue(ProductActor.UnderlyingActor.ActiveStatus == false, "The initial ActiveStatus is expected to be FALSE.");

    productActor.Tell(new ChangeActiveStatus(true));

    Thread.Sleep(10000);

    Assert.IsTrue(productActor.UnderlyingActor.ActiveStatus == true, "The new ActiveStatus is expected to be TRUE.");
}

Solution

  • Most of the AKKA.NET unit tests that I have seen use only two actors. The blog that shows the example (in the original question) has only two actors, so it will work because the operations are synchronous. If there are multiple actors in the system, for example the Actor A messages Actor B which messages Actor C which messages Actor A again, the messaging happen asynchronously, so have to wait until all the operations are complete.