发布测试,在akka中使用scalatest询问未来

时间:2014-12-11 21:38:30

标签: scala akka scalatest

我正在尝试运行一个简单的actor测试,它应该根据请求打印测试。未来还没有完成。

package test

import akka.actor.Actor.Receive
import akka.actor.{Actor, ActorSystem}
import akka.testkit.{TestActorRef, TestProbe, ImplicitSender, TestKit}
import akka.util.Timeout
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Matchers, WordSpecLike}

class TestSpecs(_system:ActorSystem)
  extends TestKit(_system)
  with ImplicitSender
  with WordSpecLike
  with Matchers {
  def this() = this(ActorSystem("test"))

  class TestTestTest extends Actor {
    override def receive: Receive = {
      case "test" =>  "test"
      case _ => "notest"
    }
  }

    val workerFSM = TestActorRef(new TestTestTest)

  "During initialization connection should" should {
    "have initial state as pre initialization" in {
        import scala.concurrent.duration.DurationInt
        implicit val timeout = Timeout(5.seconds)
        import akka.pattern.ask
        import system.dispatcher
        val f = workerFSM ? "test"
        println(f.isCompleted)
        f.map(x => {
          println("Hello")
        })
        expectMsg(2.seconds, "test")

    }
  }
}

在akka测试中阻止未来的性质是否存在问题,或者我错过了什么?

1 个答案:

答案 0 :(得分:2)

当你“问”一个演员某事时,那个演员必须回复或者回到原始来电者的未来永远不会完成。在您的情况下,TestTestTest永远不会回复,因此您测试中的未来f将永远无法完成。