gpt4 book ai didi

scala - 测试依赖于 future 的 AKKA Actor

转载 作者:行者123 更新时间:2023-11-28 21:08:24 25 4
gpt4 key购买 nike

<分区>

我正在尝试为依赖于 Future 的 actor 编写一个简单的测试。这是几乎不言自明的代码

import akka.actor.{ Actor, ActorSystem, Props }
import akka.testkit.{ ImplicitSender, TestKit }
import org.scalatest.{ BeforeAndAfterAll, WordSpecLike }

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

trait Provider {def get(s: String): Future[String] }

class MyActor(provider: Provider) extends Actor {
override def receive: Receive = {
case s: String ⇒
provider.get(s) map { result: String ⇒
sender() ! result
}
}
}

class FutureTest
extends TestKit(ActorSystem("Test"))
with ImplicitSender
with WordSpecLike
with BeforeAndAfterAll {

"MyActor" must {
"wait for the future" in {
val myref = system.actorOf(Props(
new MyActor((s) ⇒ Future { s })
))

myref ! "hello world"

expectMsg("hello world")
}
}
}

Expect msg 不会等待 future 完成并失败并显示以下消息:

断言失败:WAITING hello world 时 expectMsg 超时(3 秒)
java.lang.AssertionError: assertion failed: expectMsg 等待 hello world 期间超时(3 秒)

在我的案例中测试这种行为的最佳实践是什么?

谢谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com