如何在测试期间等待 Akka actor 启动? [英] How to wait for Akka actor to start during tests?

查看:30
本文介绍了如何在测试期间等待 Akka actor 启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试需要对演员 preStart() 期间发生的事情做出断言,但我不知道如何等到这种情况发生,有时它没有't 发生在断言之前(有时确实如此).我试过这个:

I have a test that needs to make an assertion on something that happens during an actor's preStart(), but I haven't figured out how to wait until that happens, and sometimes it doesn't happen before the assertion is made (and sometimes it does). I have tried this:

EventFilter.debug(start = "started", occurrences = 1).assertDone(10.seconds)

但我在使用它时收到一条错误消息:

but I get an error message when using it:

java.lang.AssertionError: assertion failed: 1 messages outstanding on DebugFilter(None,Left(started),false)

推荐答案

您可以将actor的创建放在intercept 块:

You could place the creation of the actor inside an intercept block:

import akka.actor._
import akka.testkit.EventFilter
import com.typesafe.config.ConfigFactory

class MyActor extends Actor with ActorLogging {
  override def preStart(): Unit = {
    log.debug("started MyActor...")
  }

  def receive = {
    case m => log.debug(s"Received this message: $m")
  }
}

object MyActor {
  def props() = Props[MyActor]
}

object EventFilterTest extends App {
  implicit val system = ActorSystem("testsystem", ConfigFactory.parseString("""
    akka.loggers = ["akka.testkit.TestEventListener"]
    akka.loglevel = "DEBUG"
  """))

  EventFilter.debug(start = "started", occurrences = 1) intercept {
    val myActor = system.actorOf(MyActor.props)
    myActor ! "cows"
  }
}

运行上面的代码产生以下输出:

Running the above code produces the following output:

[DEBUG] [...] [run-main-0] [EventStream(akka://testsystem)] logger log1-TestEventListener started
[DEBUG] [...] [run-main-0] [EventStream(akka://testsystem)] Default Loggers started
[DEBUG] [...] [testsystem-akka.actor.default-dispatcher-5] [akka://testsystem/user/$a] Received this message: cows

拦截捕获"actor 的preStart 钩子中的调试语句.

The intercept "catches" the debug statement in the actor's preStart hook.

这篇关于如何在测试期间等待 Akka actor 启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆