【问题标题】:Akka Scala TestKit test PoisonPill messageAkka Scala TestKit 测试 PoisonPill 消息
【发布时间】:2016-01-26 14:13:00
【问题描述】:

鉴于我有一个注入了child 演员的Supervisor 演员,我如何向孩子发送 PoisonPill 消息并使用 TestKit 进行测试?

这是我的主管。

class Supervisor(child: ActorRef) extends Actor {

  ...
  child ! "hello"
  child ! PoisonPill
}

这是我的测试代码

val probe = TestProbe()
val supervisor = system.actorOf(Props(classOf[Supervisor], probe.ref))
probe.expectMsg("hello")
probe.expectMsg(PoisonPill)

问题是没有收到PoisonPill 消息。 可能是因为探测被PoisonPill 消息终止?

断言失败

java.lang.AssertionError: assertion failed: timeout (3 seconds) 
during expectMsg while waiting for PoisonPill

【问题讨论】:

    标签: scala testing akka testkit


    【解决方案1】:

    我认为这个Testing Actor Systems 应该回答你的问题:

    通过 Probes 观察其他演员

    TestProbe 可以为任何其他参与者的 DeathWatch 注册自己:

    val probe = TestProbe()
    probe watch target
    target ! PoisonPill
    probe.expectTerminated(target)
    

    【讨论】:

    • 我使用了这个,但最后更改了我的代码,以便孩子在完成后自行停止。
    【解决方案2】:

    在扩展了 testkit 的测试用例中,您可以使用以下代码:

    "receives ShutDown" must {
      "sends PosionPill to other actor" in {
        val other = TestProbe("Other")
        val testee = TestActorRef(new Testee(actor.ref))
    
        testee ! Testee.ShutDown
    
        watch(other.ref)
        expectTerminated(other.ref)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 2015-10-25
      • 2018-05-25
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多