0

有两个演员 -ProducerActorConsumerActor。Producer 有一个调度器,"Tick"每个调度器都向自己发送消息2000 ms。之后生产者向"Hello"消费者发送消息:

class ProducerActor(consumer: ActorRef) extends Actor {
    override def receive: Receive = {
      case "Tick" =>
          ...
          // some code which takes < 1 ms
          ...
          consumer ! "Hello"
    }

    override def preStart: Unit = 
      context.system
         .scheduler
         .schedule(2000 milliseconds, 2000 milliseconds, self, "Tick")
}

当消费者参与者每个都收到“Hello”消息时,是否可以测试案例2000 ms?因此,如果在周期内收到消息,< 2000 ms或者> 2000 ms测试将断言错误。

例如:

"Consumer test" should {
    "receive message each 2000 ms" in {
       ...
       val consumer = TestProbe()
       val producer = system.actorOf(Props(new ProducerActor(consumer.ref))
       ...
       consumer.howToExpectDelayedMessage(minDelay = 2000 millis, "Hello")

    }
}

--- UPD ---

我想出了以下对我来说很好的解决方案:

"Consumer test" should {
    "receive message each 2000 ms" in {

       val consumer = TestProbe()
       val producer = system.actorOf(Props(new ProducerActor(consumer.ref))

       // check the first ten periods
       (0 to 10) foreach { _ =>
         consumer.expectNoMsg(2000 millis)
         consumer.expectMsg("Hello")
       }

    }
}

但是,如果这项任务有更好的解决方案,如果你能分享,那就太好了。

4

0 回答 0