scala – Akka测试 – 发送者是deadLetters
发布时间:2020-12-16 10:07:50 所属栏目:安全 来源:网络整理
导读:我有以下测试规范: 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 Site
我有以下测试规范:
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) } } } 测试用例总是在传递,因为actor类中的orginalSender是deadLetters,而不是对测试发送者的引用. 以下是运行测试后的日志输出: 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'. 为什么会这样? 解决方法
我可以看到的唯一可能性是你在测试过程中在范围内有另一个隐式ActorRef,导致一个模糊的隐式参数,这意味着它们都没有被拾取.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |