如何测试Akka演员是在Scala中创建的

如何测试Akka演员是在Scala中创建的

本文介绍了如何测试Akka演员是在Scala中创建的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个测试,以验证下面的演员正在创建heartBeatExpireWorker和heartBeatAccepter,但是我不知道该怎么做。

I'm trying to write a test that will verify that my actor below is creating a heartBeatExpireWorker and a heartBeatAccepter, but I have no idea how to do it.

首先我想我可以使用Mockhito模拟或间谍代替上下文,然后验证我是否调用actorOf,但我想不出一种注入方法在不破坏Akka测试框架的情况下。

First I was thinking I could use Mockhito mock or a spy in place of context and then verify that I called actorOf, but I can't figure out a way to inject the context without breaking the Akka testing framework.

然后,我以为我可以向工作人员发送一个标识消息以验证他们的存在。但是我想到,那也不行,因为Akka TestKit似乎没有创建被测演员的子演员。

Then, I was thinking that I could send an Identify message to the workers to verify that they exist. But it occurred to me that that wouldn't work either because the Akka TestKit doesn't seem to create children actors of an actor under test. It can only take in Testprobes that can stand in for neighboring actors.

class HeartBeatPumpWorker(chatService: ChatService, target: HeartBeatMessageCmd) extends Actor with ActorLogging with
WorkersReference {

  val heartBeatInterval = chatService.getHeartBeatInterval

  val tick = context.system.scheduler.schedule(0 millis, heartBeatInterval millis, self, SendHeartBeat(target))

  override def postStop() = tick.cancel()

  def receive = {
    case SendHeartBeat(command: HeartBeatMessageCmd) =>
      log.debug("Sending heartbeat")
      //Send heartbeat to GWT
      val userTarget = NetworkWorker.buildEventUserTarget(command.getEventCode, command.getUser)

      val uuid: String = UUID.randomUUID().toString
      val freshCommand = new HeartBeatMessageCmd(command.getUser, command.getEventCode, uuid, command.getUserSession)
      networkWorker ! NetworkBroadcast(userTarget, freshCommand)

      val heartBeatId: String = freshCommand.getUuid
      //create expirer
      val heartBeatExpireWorkerRef = context.actorOf(HeartBeatExpireWorker.props(chatService, freshCommand),
        HeartBeatExpireWorker.name(heartBeatId))
      val heartBeatAccepterRef = context
        .actorOf(HeartBeatAcceptWorker.props(chatService, freshCommand), HeartBeatAcceptWorker.name(heartBeatId))

      //record heartbeat
        chatService.saveSentHeartbeat(heartBeatId, freshCommand.getUserSession, freshCommand.getEventCode,
          freshCommand.getUser,
        freshCommand.getTimeCmdGenerated)
    case _ =>
      log.error("Pumper received unknown message.  This shouldn't happen " + sender.path.toString)
      self ! PoisonPill
  }

}


object HeartBeatPumpWorker {
  def name(eventCode: String, user: String, sessionId: String) = f"HeartBeatPumpWorker-$eventCode-$user-$sessionId"

  def path(eventCode: String, user: String, sessionId: String) : String = {
    EventWorker.Path + "/" + name(eventCode, user, sessionId)
  }

  def props(chatService: ChatService, heartBeatMsgCmd: HeartBeatMessageCmd) = {
    Props(classOf[HeartBeatPumpWorker], chatService, heartBeatMsgCmd)
  }
}


推荐答案

在父级 HeartBeatPumpWorker的构造函数中为孩子注入道具(例如 HeartBeatAcceptWorker.props 。通过测试中想要的任何道具。让父母通过提供的道具实例化孩子。与孩子互动。最后一部分取决于您的数据流。例如,如果父母将您与孩子隔离,但将消息委派给他们,则将消息发送给父母。如果孩子彼此交谈,请使用测试探针或类似工具。

Inject Props for the children (e.g. HeartBeatAcceptWorker.props) in the constructor of the parent HeartBeatPumpWorker. Pass any Props you want from the test. Let the parent instantiate the children via provided Props. Interact with the children. The last part is dependent on your data flow. For instance if the parent shields you from the children, but delegates messages to them, send the message to the parent. If children talk to each other use test probes or something similar.

这篇关于如何测试Akka演员是在Scala中创建的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:25