【问题标题】:Akka Test TCP command failedAkka 测试 TCP 命令失败
【发布时间】:2017-10-26 18:25:07
【问题描述】:

我正在尝试编写我的第一个 Akka 测试并浏览文档。我正在运行它的第一个 example 并收到以下错误:

[错误] [10/26/2017 14:08:55.371] [IngestionWorkerActorSpec-akka.actor.default-dispatcher-4] [akka://IngestionWorkerActorSpec/user/$b] 断言失败:在等待测试消息时,expectMsg 期间超时(3 秒) java.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsg while waiting for Test message

下面是我的测试actor的receive方法:

override def receive: Receive = {
case p: ProducerRecord[_,_] =>
      sendChannel.send(p.value())
case _ => logger.error("Unknown type Producer Record Received.")
}

测试规范:

val uutActor =system.actorOf(IngestionWorkerActor.props(config, KafkaProducer))
"An actor must send " should  {
"send back messages unchanged" in {
  uutActor ! expected
  Thread.sleep(50)
  expectMsg(expected)
}
}

我想测试我的演员是否收到发送给它的消息,后来我想修改它以查看我是否收到特定消息。任何帮助表示赞赏。

【问题讨论】:

    标签: scala akka akka-testkit


    【解决方案1】:

    expectMsg(something) 表示actor会用something回复sender()(在发送消息时由!的隐式参数捕获),它不会内省actor收到的消息。

    最好的做法是避免尝试编写查看参与者内部的测试,而是验证它们在发送消息时的行为是否符合预期。在这种情况下,可能需要在其中放置一个模拟 sendChannel 并验证其上是否发送了正确的值。

    【讨论】:

      猜你喜欢
      • 2014-05-31
      • 2019-06-28
      • 1970-01-01
      • 2020-10-08
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2019-11-13
      相关资源
      最近更新 更多