0

我的Actor样子

object LogProcessorActor {
  def props(f: () => Unit): Props = Props(new LogProcessorActor(f))
}

class LogProcessorActor(f: () => Unit) extends Actor with ActorLogging {
  def receive = LoggingReceive {
    case StartLogReaderDisruptor => f()
      sender ! StartedLogReaderDisruptor
  }
}

我的test样子

  it must "fail with method throws exception" in {
    val f: () => Unit = () => 2/0
    val logProcessorRef = TestActorRef[LogProcessorActor](LogProcessorActor.props(f), name = "logProcessorSad")
    intercept[ArithmeticException] {
      logProcessorRef ! StartLogReaderDisruptor
    }
  }

我在日志中看到抛出异常

[DEBUG] [06/02/2015 19:25:44.598] [ScalaTest-run] [EventStream(akka://logProcessorActorSystem)] logger log1-Slf4jLogger started
[DEBUG] [06/02/2015 19:25:44.599] [ScalaTest-run] [EventStream(akka://logProcessorActorSystem)] Default Loggers started
02 Jun 2015 19:25:44,601 [DEBUG] [logProcessorActorSystem-akka.actor.default-dispatcher-3] akka.event.EventStream| logger log1-Slf4jLogger started
02 Jun 2015 19:25:44,603 [DEBUG] [logProcessorActorSystem-akka.actor.default-dispatcher-3] akka.event.EventStream| Default Loggers started02 Jun 2015 19:25:44,782 [ERROR] [logProcessorActorSystem-akka.actor.default-dispatcher-3] akka.actor.OneForOneStrategy| / by zero
java.lang.ArithmeticException: / by zero
    at com.shn.lp.LogProcessorActorSpec$$anonfun$3$$anonfun$4.apply$mcV$sp(LogProcessorActorSpec.scala:18)
    at com.shn.lp.LogProcessorActor$$anonfun$receive$1.applyOrElse(LogProcessorActor.scala:16)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:467)
    at com.shn.lp.LogProcessorActor.aroundReceive(LogProcessorActor.scala:14)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)

但我的测试仍然失败

Expected exception java.lang.ArithmeticException to be thrown, but no exception was thrown.
ScalaTestFailureLocation: com.shn.lp.LogProcessorActorSpec$$anonfun$3 at (LogProcessorActorSpec.scala:20)
org.scalatest.exceptions.TestFailedException: Expected exception java.lang.ArithmeticException to be thrown, but no exception was thrown.

我什至尝试了docs中提到的策略,但我仍然得到相同的结果。

我在做什么不正确?

4

1 回答 1

3

在actor的接收块中抛出的未被捕获的异常将不会被发送者处理。它将根据参与者的监督层次结构进行处理。父演员将根据其主管策略处理孩子。

这在文档的演员和例外部分中进行了介绍:http ://doc.akka.io/docs/akka/2.3.11/scala/actors.html#Actors_and_exceptions

于 2015-06-03T05:41:09.193 回答