0
我有以下演員:如果有不清楚的未來超時或立即失敗
class SomeActor extends Actor with ActorLogging {
override def receive: Receive = {
case StartMessage =>
//check if the job is still running
val address = url("http://mywebsite.com")
val status: Future[String] = Await.ready(Http(address OK as.String), 1 second)
val now = Calendar.getInstance().getTime()
println("TIME NOW: " + now)
status onComplete {
case Success(message) => sender ! SomeMessage(message)
case Failure(_) => {
val then = Calendar.getInstance().getTime()
sender ! SomeMessage("Not running")
println("TIME THEN: " + then)
}
}
而且我測試它像這樣:
it should "check the id of a submitted job" in new Scope {
myActorRef ! StartMessage
expectMsg(SomeMessage("Not running"))
}
的問題是,我得到
java.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsg while waiting for SomeMessage(Not running)
所以看起來我的未來會超時,因爲我只等了1秒就不應該這樣。
但是,這也是印刷。
TIME NOW: Tue Dec 06 13:39:13 GMT 2016
TIME THEN: Tue Dec 06 13:39:13 GMT 2016
由此看來,未來似乎未能立即啓動。
那它是哪一個呢?
,但請不要'Actor'內等待。更好的辦法是避免在'Actor'中嵌套異步'未來'代碼。不好的事情總是發生。 – wheaties