How to test ReceiveTimeout in Akka typed

298 views
Skip to first unread message

Urs Peter

unread,
Jan 2, 2020, 6:48:02 AM1/2/20
to Akka User List
Hi,

I'm struggling with testing Behaviour/Actor termination in Akka typed. The examples below should be self explanatory:
- test actor termination in the classic way works as expected

However, I could not figure out how to do the same test with Akka typed, especially:
- testing behaviour/ typed actor termination with the ActorTestKit
- testing behaviour/ typed actor termination with the BehaviorTestKit

Help would be much appreciated!

Urs

import akka.actor.testkit.typed.Effect.ReceiveTimeoutSet
import akka.actor.testkit.typed.javadsl.BehaviorTestKit
import akka.actor.{Actor, ActorSystem, PoisonPill, Props, ReceiveTimeout}
import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.actor.typed.{Behavior, Terminated}
import akka.actor.typed.scaladsl.Behaviors
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.duration._

class ReceiveTimeoutSpec extends AnyWordSpecLike with Matchers with BeforeAndAfterAll {


"Works fine: Classic ReceiveTimeout" should {

"terminate classic actor - works as expected" in {
val classic = ActorSystem("classic")
val classicTestKit = new TestKit(classic)
class TimeoutActor extends Actor {
context.setReceiveTimeout(500 millis)

override def receive: Receive = {
case ReceiveTimeout => self ! PoisonPill
}
}
val timeoutActor = classic.actorOf(Props(new TimeoutActor()))
classicTestKit.watch(timeoutActor)
classicTestKit.expectTerminated(timeoutActor)
}
}


"Questions: ??? Typed ReceiveTimeout ???" should {
val timeoutBehavior: Behavior[String] = Behaviors.setup[String] { context =>
context.setReceiveTimeout(500 millis, "terminated")
Behaviors.receiveMessage {
case "terminated" => Behaviors.stopped
}
}
"??? terminate typed actor with ActorTestKit - but how ???" in {
val actorTestKit = ActorTestKit()
val timeout = actorTestKit.spawn(timeoutBehavior)
//how to watch that behaviour has terminated???
}
"??? terminate typed actor with BehaviorTestKit - but how ???" in {
val behaviorTestKit = BehaviorTestKit.create(timeoutBehavior)
//works ...
behaviorTestKit.expectEffect(ReceiveTimeoutSet(500 millis, "terminated"))
//...but how to intercept the actual termination (-> Stopped) without sending a message???


}
}
}

Urs Peter

unread,
Jan 2, 2020, 10:12:15 AM1/2/20
to Akka User List
Answering part of my own question: 

I figured out how to watch termination of a typed actor, relying on the classic converters which requires the following import import akka.actor.typed.scaladsl.adapter._:

"!!! terminate typed actor with ActorTestKit - that's how !!!" in {
  import akka.actor.typed.scaladsl.adapter._
val actorTestKit = ActorTestKit()
implicit val classicActorSystem = actorTestKit.system.toClassic
val probe = TestProbe()
val timeout = actorTestKit.spawn(timeoutBehavior)
probe.watch(timeout.toClassic)
probe.expectTerminated(timeout.toClassic)
}

I assume that's the way to go. If not please let me know. 

Concerning the BehaviorTestKit approach I would still look for an answer.

Roland Kuhn

unread,
Jan 4, 2020, 6:03:30 AM1/4/20
to akka-user
Hi Urs,

you can use isAlive to check whether the behavior in the BehaviorTestKit has terminated or not. You’ll have to manually feed the actor the "terminated" message (making the test timing-independent as a bonus), then you can check for termination.

Regards,

Roland

-- 
*****************************************************************************************************
** New discussion forum: https://discuss.akka.io/ replacing akka-user google-group soon.
** This group will soon be put into read-only mode, and replaced by discuss.akka.io
** More details: https://akka.io/blog/news/2018/03/13/discuss.akka.io-announced
*****************************************************************************************************
>>>>>>>>>> 
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/akka-user/936b3f29-1a25-42d3-85f5-e8a68179cfd9%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages