Akka EventHandler永远旋转。我该怎么办呢?

时间:2011-11-29 01:09:01

标签: scala event-handling akka

我认为我发现了EventHandler的问题。以下规范将永远运行。 基本上EventHandler.info()会导致这种情况。我尝试使用After spec调用EventHandler.shutdown()。但是,没有运气。你觉得我错过了什么吗?

Akka:1.3-RC1

class EventHandlerProblem extends Specification {

  def is =
    "This describes a possible problem with the EventHandler" ^
      p ^
      "The EventHandler should" ^
      "not keep spinning forever...." ! e1

  end

  def e1 = {
    // no need to start the actor
    val ac = TestActorRef[PrintMessageActor]

    true must beTrue
  }
}

class PrintMessageActor extends Actor {
  EventHandler.info(this, "Printer actor is starting up...")

  def receive = {
    case msg => {
      println("Recieved: " + msg)
    }
  }
}

2 个答案:

答案 0 :(得分:2)

在我的Akka Actor测试中,我有一个特殊的特性,在运行所有片段后调用registry.shutdownAll。这样,虽然您仍然需要小心,您的测试可以并行运行而不会互相踩踏,但在所有测试运行后,事情都会得到清理。这是特质:

import org.specs2.Specification
import org.specs2.specification.{Step,Fragments}
import akka.actor.Actor.registry
trait AkkaSpec extends Specification {
  override def map(fs: => Fragments) = fs ^ Step(registry.shutdownAll)
}

class EventHandlerProblem extends AkkaSpec {

  def is =
    "This describes a possible problem with the EventHandler" ^
      p ^
      "The EventHandler should" ^
      "not keep spinning forever...." ! e1

  end

  def e1 = {
    // no need to start the actor
    val ac = TestActorRef[PrintMessageActor]

    true must beTrue
  }
}

class PrintMessageActor extends Actor {
  EventHandler.info(this, "Printer actor is starting up...")

  def receive = {
    case msg => {
      println("Recieved: " + msg)
    }
  }
}

答案 1 :(得分:1)

我尝试使用EventHandler.shutdown()并且它正常工作(没有挂断)。 输出结果如下:

Testing started at 11:03 ...
[INFO]    [29/11/11 11:03] [specs2.DefaultExecutionStrategy1] [PrintMessageActor] Printer actor is starting up...
[INFO]    [29/11/11 11:03] [specs2.DefaultExecutionStrategy2] [PrintMessageActor] Printer actor is starting up...
Process finished with exit code 0

代码:

import akka.testkit.TestActorRef
import org.specs2.Specification
import akka.event.EventHandler

class EventHandlerProblemSpec extends Specification {

  def is =
    "This describes a possible problem with the EventHandler" ^
      p ^
      "The EventHandler should" ^
      "not keep spinning forever...." ! e1 ^
      "not keep spinning forever 2...." ! e2 ^
      end

  def e1 = {
    {
      val ac = TestActorRef[PrintMessageActor]
      true must beTrue
    } after {
      EventHandler.shutdown()
    }
  }

  def e2 = {
    try {
      val ac = TestActorRef[PrintMessageActor]
      true must beTrue
    } finally {
      EventHandler.shutdown()
    }
  }
}