Reputation: 19854
My Akka
actor system has some tests to verify message content
myEventActor.expectMsgPF() {
verifyEventPF(id)
}
def verifyEventPF(id: String): PartialFunction[Any, Any] = {
case e : MyEvent if e.id == id => e.otherID
}
For example, we use this partial function to check that the id on the event is correct. But how do I go about getting the result of the partial function e.g. if I want to achieve the following
myEventActor.expectMsgPF() {
var otherID = verifyEventPF(id) // How do I achieve this?
}
Is this possible?
(I am aware I don't need to use a partial function here and could use Akka TestProbe.receiveOne()
but I'd like to understand how this is possible)
Upvotes: 1
Views: 59
Reputation: 3965
Calling verfiyEventPF(id)
returns a PartialFunction[Any, Any]
. You have to also call the returned function (giving it input, of course): verifyEventPF(id)(event)
.
This is the same as the following:
val getId: PartialFunction[Any, Any] = verifyEventPF(id)
getId(event)
If you're concerned about the partial funciton not being defined for a particular input, you can check if the function is defined for a given value:
if (getId.isDefinedAt(event)) {
getId(event)
}
Or you can lift the partial function into a total function that returns an Option
:
val totalGetId: (Any => Option[Any]) = getId.lift
val result: Option[Any] = totalGetId(event)
Upvotes: 2