即使抛出,Actor也不会在Test中捕获异常

时间:2015-06-03 02:29:52

标签: scala akka akka-testkit

我的UIColor看起来像

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)

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

我做错了什么?

1 个答案:

答案 0 :(得分:3)

发件人不会处理在actor的接收块中抛出的未捕获异常。它将根据演员的监督层级进行处理。家长将根据其主管策略与孩子打交道。

文档的 Actors and exceptions 部分对此进行了介绍:http://doc.akka.io/docs/akka/2.3.11/scala/actors.html#Actors_and_exceptions