Scala Akka测试-发送方是死信
我有以下测试规范:Scala Akka测试-发送方是死信,scala,akka,scalatest,Scala,Akka,Scalatest,我有以下测试规范: class SiteCheckerSpec extends TestKit(ActorSystem("testSystem")) with ImplicitSender with BeforeAndAfterAll with Matchers with WordSpecLike { val sites = L
class SiteCheckerSpec extends TestKit(ActorSystem("testSystem"))
with ImplicitSender
with BeforeAndAfterAll
with Matchers
with WordSpecLike {
val sites = List(
"www.google.com",
"www.apple.com",
"www.gazeta.pl"
)
"Tested SiteChecker actor" must {
"not receive message" in {
val testedActor = system.actorOf(Props(new SiteChecker(sites.size) with TestActorContextCreationSupport {
override def actorRefFactory = system
}))
testedActor ! SiteChecker.CheckSitesTitles(sites)
expectNoMsg()
}
}
override protected def afterAll(): Unit = TestKit.shutdownActorSystem(system)
}
以及以下参与者:
class SiteChecker(workersCount: Int) extends Actor
with ActorLogging
with ActorContextCreationSupport {
val resultMap = mutable.HashMap.empty[String, String]
var originalSender: Option[ActorRef] = None
val workers = (
for (i <- 1 to workersCount) yield createChildren(SiteCheckerWorker.props())
).toList
def actorRefFactory: ActorRefFactory = context
override def receive: Actor.Receive = {
case SiteChecker.CheckSitesTitles(sites) =>
log.info(s"Sites to check $sites")
println(sender)
originalSender = Some(sender)
workers.zip(sites).foreach(
t => t._1 ! SiteCheckerWorker.CheckSiteTitle(t._2)
)
case SiteCheckerWorker.SiteTitle(site, title) =>
log.info(s"Obtained title $title for site $site")
resultMap += (site -> title)
if (resultMap.size == workers.size) {
originalSender.get ! SiteChecker.SitesTitles(resultMap.toMap)
context.children.foreach(context.stop)
}
}
}
为什么会发生这种情况?我能看到的唯一可能性是,在测试过程的作用域中有另一个
隐式ActorRef
,导致了一个模糊的隐式参数,这意味着没有拾取任何一个参数。在我的例子中,发生这种情况是因为我的测试类扩展了TestKit
,但是没有混入ImplicitSender
如何开始调试它?你能给我一些提示,这些多重隐式ActorRef的定义在哪里吗?嗯,不看代码很难说。您还可以尝试将testActor
显式传递为发送方(将!
替换为tell
)。正如您所说,存在多个隐式ActorRef常量。在更改了一些代码之后,一切都开始按预期工作。
Actor[akka://testSystem/deadLetters]
[INFO] [02/13/2014 18:53:59.150] [testSystem-akka.actor.default-dispatcher-2] [akka://testSystem/user/$a] Sites to check List(www.google.com, www.apple.com, www.gazeta.pl)
[INFO] [02/13/2014 18:53:59.154] [testSystem-akka.actor.default-dispatcher-3] [akka://testSystem/user/$a] Obtained title www.gazeta.pl for site www.gazeta.pl
[INFO] [02/13/2014 18:53:59.157] [testSystem-akka.actor.default-dispatcher-3] [akka://testSystem/user/$a] Obtained title www.google.com for site www.google.com
[INFO] [02/13/2014 18:53:59.158] [testSystem-akka.actor.default-dispatcher-5] [akka://testSystem/user/$a] Obtained title www.apple.com for site www.apple.com
[INFO] [02/13/2014 18:53:59.163] [testSystem-akka.actor.default-dispatcher-4] [akka://testSystem/deadLetters] Message [actor.site.SiteChecker$SitesTitles] from Actor[akka://testSystem/user/$a#735871082] 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'.