在我的项目中,我正在为Scala akka型actor编写测试用例,该actor正在与ZeroMQ交互以进行消息传输,但是在编写测试用例时,我面临的问题是系统抛出断言失败错误,预期消息超时(3秒)。 我正在写测试的演员如下:
object FollowCmdActor {
def create(cmanager: CManager,
zeroMQProtoActor: ActorRef[ZeroMQMessage],
loggerFactory: LoggerFactory): Behavior[ImmediateCommand] =
Behaviors.setup(ctx => FCmdActor(ctx, cmanager, zeroMQProtoActor, loggerFactory))
}
case class FCmdActor(ctx: ActorContext[ImmediateCommand],
cmanager: CManager,
zeroMQProtoActor: ActorRef[ZeroMQMessage],
loggerFactory: LoggerFactory)
extends MutableBehavior[ImmediateCommand] {
private val log: Logger = loggerFactory.getLogger
override def onMessage(msg: ImmediateCommand): Behavior[ImmediateCommand] = {
implicit val duration: Timeout = 20 seconds
implicit val scheduler = ctx.system.scheduler
val response: ZeroMQMessage = Await.result(zeroMQProtoActor ? { ref: ActorRef[ZeroMQMessage] =>
ZeroMQMessage.SubmitCommand(ref, msg.controlCommand)
}, 10.seconds)
response match {
case x: ZeroMQMessage.MCSResponse => {
msg.sender ! ImmediateCommandResponse(x.commandResponse)
}
}
Behavior.stopped
}
}
我的测试用例如下:
class FCmdActorTest extends FunSuite with Matchers with BeforeAndAfterAll {
implicit val untypedSystem: ActorSystem = ActorSystemFactory.remote()
implicit val system: typed.ActorSystem[_] = untypedSystem.toTyped
implicit val testKitSettings: TestKitSettings = TestKitSettings(system)
private val mocks = new FCmdMocks()
private val loggerFactory = mocks.loggerFactory
private val log = mocks.log
test("Test for correct response from ZeroMQ actor"){
val prefix = Prefix("abc")
val setup = Setup(prefix, CommandName("FCmd"), None)
//val zeroMQActor = TestProbe[ZeroMQMessage]()
val inbox = TestInbox[ZeroMQMessage]()
val submitCommand = SubmitCommand(inbox.ref, setup)
//Test1 : ZeroMQ Actor should receive submitCommand message
//zeroMQActor.expectMessage(submitCommand)
val commandHandlerActor = TestProbe[HCommandMessage]()
val immediateCommand : ImmediateCommand = ImmediateCommand(commandHandlerActor.ref,setup)
val behaviorTestKit: BehaviorTestKit[ImmediateCommand] = BehaviorTestKit(FCmdActor.create(mocks.cManager, inbox.ref, mocks.loggerFactory))
behaviorTestKit.run(immediateCommand)
inbox.expectMessage(submitCommand)
}
when(loggerFactory.getLogger).thenReturn(log)
when(loggerFactory.getLogger(any[actor.ActorContext])).thenReturn(log)
when(loggerFactory.getLogger(any[ActorContext[_]])).thenReturn(log)
}
但是我在测试它时遇到以下错误: ssertion失败:在等待SubmitCommand(Actor [akka:// cw-remote-actor-system / system / testProbe-1#-1150646793],Setup(runId = Id(46f52c9f-1c03-4e45 -bd4c-387e8f797bec),paramSet = Set(),source = Prefix(tmt.tcs.asdAssembly-Client),commandName = CommandName(Fcmd),也许ObsId = None)) java.lang.AssertionError:声明失败:在等待SubmitCommand(Actor [akka:// cw-remote-actor-system / system / testProbe-1#-1150646793],Setup(runId = Id (46f52c9f-1c03-4e45-bd4c-387e8f797bec),paramSet = Set(),source = Prefix(tmt.tcs.asdAssembly-Client),commandName = CommandName(Fcmd),也许ObsId = None))