如何在测试期间等待Akka演员开始?

时间:2017-09-21 23:37:03

标签: scala akka

我有一个测试,需要对演员EventFilter.debug(start = "started", occurrences = 1).assertDone(10.seconds) 期间发生的事情作出断言,但我还没弄清楚如何等到这种情况发生,有时它不会#39; t发生在断言之前(有时它确实发生)。我试过这个:

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

但使用时收到错误消息:

@Column(name="parentEntity", updatable=false, insertable=false) private Integer parentEntityId;

1 个答案:

答案 0 :(得分:2)

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

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"
  }
}

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

[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钩子中的调试语句。