我一直在尝试Akka团队建议的reaper pattern。它在理论上听起来不错,但提供的单元测试不会传入sbt或Eclipse的JUnit运行程序。
import scala.concurrent.duration._
import akka.util.Timeout
import akka.actor._
import akka.testkit.{TestKit, ImplicitSender, TestProbe}
import org.scalatest.{WordSpec, BeforeAndAfterAll}
import org.scalatest.matchers.MustMatchers
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
@RunWith(classOf[JUnitRunner])
class ReaperSpec extends TestKit(ActorSystem("ReaperSpec"))
with ImplicitSender with WordSpec with BeforeAndAfterAll with MustMatchers
{
import Reaper._
override def afterAll() {
system.shutdown()
}
// Our test reaper. Sends the snooper a message when all the souls have been reaped
private class TestReaper(snooper: ActorRef) extends Reaper {
def allSoulsReaped(): Unit = snooper ! "Dead"
}
"Reaper" should {
"work" in {
// Set up some dummy Actors
val a = TestProbe()
val b = TestProbe()
val c = TestProbe()
val d = TestProbe()
// Build our reaper
val reaper = system.actorOf(Props(new TestReaper(testActor)))
// Watch a couple
reaper ! WatchMe(a.ref)
reaper ! WatchMe(d.ref)
// Stop them
system.stop(a.ref)
system.stop(d.ref)
// Make sure we've been called
within (1 seconds, 5 seconds) {
expectMsg("Dead")
}
}
}
}
在向演员添加日志后,我注意到Terminated消息最终出现在死信框中。所以收割者演员从来没有得到终结的信息。
[ERROR] [08/05/2014 11:10:27.244] [ReaperSpec-akka.actor.default-dispatcher-4]
[akka://ReaperSpec/user/$a] Monitored actor [Actor[akka://ReaperSpec/system/testActor2#-1060476766]] terminated (akka.actor.DeathPactException)
[INFO] [08/05/2014 11:10:27.248] [ReaperSpec-akka.actor.default-dispatcher-2]
[akka://ReaperSpec/user/$a] Message [akka.actor.Terminated] from Actor[akka://ReaperSpec/system/testActor5#2029818496] to Actor[akka://ReaperSpec/user/$a#1787786349] was not delivered.
[1] dead letters encountered, no more dead letters will be logged. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
我认为system.stop(watchedActorRef)
可以保证向正在观看watchedActorRef
的所有演员发送终止消息。我错过了什么?