scala – 在akka中测试计时器事件
发布时间:2020-12-16 18:49:12 所属栏目:安全 来源:网络整理
导读:我有一个函数,它使用scheduleOnce来安排事件在将来的某个时间发生,我想编写一个测试来检查: 事件确实已经安排好了 它安排在正确的时间 当事件最终触发时,系统按预期运行 但是我不希望测试实际上等几分钟什么都不做. 我应该如何最好地测试使用akka的Schedule
我有一个函数,它使用scheduleOnce来安排事件在将来的某个时间发生,我想编写一个测试来检查:
>事件确实已经安排好了 但是我不希望测试实际上等几分钟什么都不做. 我应该如何最好地测试使用akka的Scheduler的代码? 解决方法
下面是@lmm描述的模拟调度程序的示例.在这个例子中,我们真的测试了动作的完整调度和处理,作为两个独立的场景.给出一些条件的第一个测试(在我的例子中接收某种类型的消息)我们将安排一个回调,第二个是处理在该定时器关闭时被激发回来的消息.代码如下:
object TimerExampleActor{ case object ClearState case class ScheduleStateClearing(duration:FiniteDuration) } class TimerExampleActor extends Actor{ import TimerExampleActor._ var state:List[Int] = Nil def receive = { case ScheduleStateClearing(d) => scheduler.scheduleOnce(d,self,ClearState)(context.dispatcher) case ClearState => state = Nil } def scheduler = context.system.scheduler } 然后,使用specs2和mockito,我的测试用例如下: class TimerExampleActorTest extends Specification with Mockito with NoTimeConversions{ import TimerExampleActor._ implicit val system = ActorSystem("test") trait testscope extends Scope{ val mockScheduler = mock[Scheduler] val actor = TestActorRef(new TimerExampleActor{ override def scheduler = mockScheduler }) } "A request to schedule state clearing" should{ "schedule a callback for the message ClearState to self with the supplied duration" in new testscope{ val dur = 1.minute actor ! ScheduleStateClearing(dur) there was one(mockScheduler).scheduleOnce(dur,actor,ClearState)(actor.underlyingActor.context.dispatcher) } } "A ClearState message received by the actor" should{ "clear the interval 'state' List" in new testscope{ actor.underlyingActor.state = List(1,2,3) actor ! ClearState actor.underlyingActor.state mustEqual Nil } } } 你可以看到,当我在测试中创建actor实例时,我覆盖了我创建的方法,以获取调度程序的实例,允许我返回一个模拟.这不是测试这样的东西的唯一方法,但它肯定是你可以考虑的一个选择. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |