Failing a scalatest when akka actor throws exception outside of the test thread

前端 未结 3 934
既然无缘
既然无缘 2021-01-13 06:36

I\'ve had a situation come up and bite me a few times where I\'m testing an Actor and the Actor throws an exception unexpectedly (due to a bug), but the test still passes.

3条回答
  •  无人共我
    2021-01-13 07:16

    Thinking in Actors there is also another solution: failures travel to the supervisor, so that is the perfect place to catch them and feed them into the test procedure:

    val failures = TestProbe()
    val props = ... // description for the actor under test
    val failureParent = system.actorOf(Props(new Actor {
      val child = context.actorOf(props, "child")
      override val supervisorStrategy = OneForOneStrategy() {
        case f => failures.ref ! f; Stop // or whichever directive is appropriate
      }
      def receive = {
        case msg => child forward msg
      }
    }))
    

    You can send to the actor under test by sending to failureParent and all failures—expected or not—go to the failures probe for inspection.

提交回复
热议问题