回复发件人去死信箱

时间:2016-12-15 19:17:24

标签: scala akka

我有以下单元测试:

  it should "return xml" in new TestScope {
    val testProbe: TestProbe = TestProbe()
    val someActor = system.actorOf(Props[SomeActor])

    testProbe.send(someActor, MakeXmlApiCall())
    testProbe.expectMsgPF() {
      case Success(message) => {
        assert(message == "someMessage")
      }
    }
  }

对于使用此receive方法的演员:

  override def receive: Receive = {
    case MakeXmlApiCall() => {
      val status =  Future {"someMessage"}

      println("In SomeActor")

      status onComplete {
        case Success(message) => {
          sender ! message
        }
      }

    }
  }

打印“ In SomeActor ”,表示控件达到receive方法。

但是,我也收到了这条消息:

[INFO] [12/15/2016 18:42:29.463] [testSystem-akka.actor.default-dispatcher-3] [akka://testSystem/deadLetters] Message [java.lang.String] from Actor[akka://testSystem/user/$a#-1159394947] to Actor[akka://testSystem/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

那么为什么消息会从单元测试发送到dead-letter而不是testProbe

为什么会这样?

1 个答案:

答案 0 :(得分:6)

从未来的回调或操作员引用sender()(或任何参与者状态)是不安全的,因为sender()可能已更改。

您应该将 pipe 的未来转发给发件人:status.pipeTo(sender())

此外,您的expectMsgPF()来电与您SomeActor实际发送的内容不符。您使用字符串回复,而不是Success的实例。