Approve the order of messages received with Akka TestProbe

We have an actor over whom we write unit tests, and as part of the tests, we want to argue that certain messages are sent to another actor in a specific order. In our unit tests, the message-receiving actor is represented by Akka TestProbe, which is introduced into the actor being tested when it is created.

There is no problem to claim that the messages were sent for a test study, however we tried our best to find a way to say that they were sent in the correct order (we could not find suitable methods for this in the documentation ). Any ideas on how we achieve this?

Below is a minimal implementation that emphasizes the problem.

Implementation

case class Message(message: String)
case class ForwardedMessage(message: String)

class ForwardingActor(forwardTo: ActorRef) extends Actor {
  def receive = {
    case Message(message) =>
      forwardTo ! ForwardedMessage(message)
  }
}

Unit test

class ForwardMessagesInOrderTest extends TestKit(ActorSystem("testSystem"))
                                 with WordSpecLike
                                 with MustMatchers {

  "A forwarding actor" must {
    val forwardingReceiver = TestProbe()
    val forwardingActor = system.actorOf(Props(new ForwardingActor(forwardingReceiver.ref)))

    "forward messages in the order they are received" in {
      forwardingActor ! Message("First message")
      forwardingActor ! Message("Second message")

      // This is the closest way we have found of achieving what we are looking for, it asserts
      // that both messages were received, but doesn't assert correct order. The test will pass
      // regardless which way round we put the messages below.
      forwardingReceiver.expectMsgAllOf(
          ForwardedMessage("Second message"),
          ForwardedMessage("First message"))
    }
  }
}
+4
1

. -, TestActorRef :

val forwardingActor = TestActorRef(new ForwardingActor(forwardingReceiver.ref))

TestActorRef , CallingThreadDispatcher, ( ). :

forwardingReceiver.expectMsg(ForwardedMessage("First message"))
forwardingReceiver.expectMsg(ForwardedMessage("Second message"))

In-Order, , , . .

+7

All Articles