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???


    }
  }
}

-- 
*****************************************************************************************************
** 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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/akka-user/b72d74d5-539a-422b-8d20-5a893915dfe6%40googlegroups.com.

Reply via email to