【问题标题】:Why is Future not returned from actor to ScalaTest's whenReady?为什么 Future 没有从 actor 返回到 ScalaTest 的 whenReady?
【发布时间】:2015-01-07 02:21:31
【问题描述】:

我遇到了 Future 的问题,该问题不是从使用 whenReady 的 ScalaTest 案例中的参与者返回的。

这是测试用例

"A PolicyHolderDAO Actor" must { 
  "return a failure if given a policy holder to update without an id" in { 
    val updatePolicyHolderFailureAny : Future[Any] = policyHolderDAOActor ? PoliHolderDAO.Update(Future(policyHolder))
    val updatePolicyHolderFailure : Future[PolicyHolderDAO.Failure] = updatePolicyHolderFailureAny.mapTo[PolicyHolderDAO.Failure]
    whenReady(updatePolicyHolderFailure, timeout(5 seconds), interval(5 millis)) { failure => 
      failure.error.getMessage must be ("When updating a policy holder, we must have an id")
    }
  }
}

这里是actor的receive方法:

override def receive = {
  case PolicyHolderDAO.Update(policyHolder) => 
    val s = sender
    policyHolder map { p =>
      p match {
        case _ if p.id.isDefined => s ! update(policyHolder)
        case _  => println("HERE"); s ! PolicyHolderDAO.Failure(throw new IllegalArgumentException("When updating a policy holder, we must have an id"), None)
      }
    }
}

Update的case类如下

case class Update(policyHolder : Future[PolicyHolder]

我要做的是检查保单持有人是否有id。如果它有id,则将其传递给update 方法,如果它没有id,则返回失败。快乐的道路运行良好,而悲伤的道路似乎也运行良好,我的意思是 println("HERE") 被执行。但是,我的测试用例似乎在悲伤的路径场景中没有收到演员的回复。

这是错误信息

[info] A PolicyHolderDAO Actor
HERE
[info] - must return a failure if given a policy holder to update without an id *** FAILED ***
[info]   A timeout occurred waiting for a future to complete. Queried 980 times, sleeping 5000000 nanoseconds between each query. (PolicyHolderDAOSystemTest.scala:103)

【问题讨论】:

    标签: scala akka


    【解决方案1】:
    PolicyHolderDAO.Failure(throw new IllegalArgumentException("When updating a policy holder, we must have an id"), None)
    

    意味着您将抛出 IllegalArgumentException,因此不会构造任何消息,并且错误将被发送到 actor 主管。

    您的意思是创建异常并在消息中返回它,对吗?

    PolicyHolderDAO.Failure(new IllegalArgumentException("When updating a policy holder, we must have an id"), None)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 2020-08-15
      • 1970-01-01
      相关资源
      最近更新 更多