为什么这个akka-http路由测试永远不会成功完成?

时间:2017-12-04 15:38:44

标签: scala akka akka-stream akka-http

我有一条简单的路线和一些单独成功的测试,但总是因超时而失败。知道为什么吗?

val route = (requestHandler: ActorRef @@ Web) => {
   get {
     pathPrefix("apps") {
       pathEndOrSingleSlash {
         completeWith(implicitly[ToEntityMarshaller[List[String]]]) { callback =>
           requestHandler ! GetAppsRequest(callback)
         }
       } ~ path("stats") {
         completeWith(implicitly[ToEntityMarshaller[List[Stats]]]) { callback =>
           requestHandler ! GetStatsRequest(callback)
         }
       }
     } ~ path("apps" / Segment / "stats") { app =>
       completeWith(implicitly[ToEntityMarshaller[Stats]]) { callback =>
         requestHandler ! GetStatsForOneRequest(app, callback)
       }
     }
   }
 }

和测试:

val testProbe = TestProbe()
val testProbeActor = testProbe.ref
  .taggedWith[Web]

val timeout = 1.minute

"Route" should "respond to get apps request" in {
  implicit val routeTestTimout = RouteTestTimeout(timeout.dilated)
  Get("/apps") ~> route(testProbeActor) ~> check {

    testProbe.receiveOne(timeout) match {
      case GetAppsRequest(callback) => {
        callback(k8SProperties.apps)
      }
    }
    entityAs[List[String]] should contain("test")
  }
  testProbe.expectNoMessage(timeout)
}

it should "respond to get stats request for all apps" in {
  implicit val routeTestTimout = RouteTestTimeout(timeout.dilated)
  val app = "test"
  Get("/apps/stats") ~> route(testProbeActor) ~> check {

    testProbe.receiveOne(timeout) match {
      case GetStatsRequest(callback) => {
        callback(List(Stats(app, ChronoUnit.SECONDS, Nil)))
      }
      case other => fail(s"Unexpected message $other.")
    }
    entityAs[List[Stats]].size shouldBe (1)
    entityAs[List[Stats]].head.app shouldBe (app)
  }
  testProbe.expectNoMessage(timeout)
}

it should "respond to get stats request for one app" in {
  implicit val routeTestTimout = RouteTestTimeout(timeout.dilated)
  val app = "test"
  Get(s"/apps/$app/stats") ~> route(testProbeActor) ~> check {

    testProbe.receiveOne(timeout) match {
      case GetStatsForOneRequest(app, callback) => {
        callback(Stats(app, ChronoUnit.SECONDS, Nil))
      }
      case other => fail(s"Unexpected message $other.")
    }
    entityAs[Stats].app shouldBe (app)
  }
  testProbe.expectNoMessage(timeout)
}

修改: 已打开https://github.com/akka/akka-http/issues/1615

2 个答案:

答案 0 :(得分:2)

问题是您在所有三个测试中使用单个TestProbe。 TestProbe是一个单独的actor,因此接收来自所有三个测试的消息。如果您只是在测试体内移动测试探针创建和配置,它应该按预期工作;特别是这两行:

val testProbe = TestProbe()
val testProbeActor = testProbe.ref
  .taggedWith[Web]

答案 1 :(得分:0)

工作代码,谢谢我。

"Routes" should "respond to get apps request" in {
  testProbe.setAutoPilot((_: ActorRef, msg: Any) => {
    msg match {
      case GetAppsRequest(callback) => callback(List("test")); TestActor.KeepRunning
      case _ => TestActor.NoAutoPilot
    }
  })

  Get("/apps") ~> handler ~> check {
    entityAs[List[String]] should contain("test")
  }
}

TestProbe放在check内永远会挂起,因为它会造成死锁; test等待调用回调,并且在执行主体之前不会调用回调。

使用自动驾驶仪设置了一个可以在以后实现的“期望”,从而避免了死锁。