Akka FSM Testkit无法正确捕获*起始状态*

时间:2016-11-08 03:20:58

标签: akka akka-testkit akka-fsm

我正在使用Akka FSM来模拟移动的电梯(你要去了:不再!:-))并试图使用常规的Testkit功能来测试FSM,但是在我的理解或发布中似乎存在差距FSM(或两者)的行为。

以下是代码的相关部分:

class LiftCarriageWithMovingState (val movingStateSimulator: ActorRef) extends Actor
  with LoggingFSM[LiftState,LiftData]
  with ActorLogging
{
  val mxTimeToWaitStopping = 250 milliseconds
  private var pendingPassengerRequests: Vector[NextStop] = Vector.empty
  private var currentFloorID = 0 // Always start at Ground Floor

  val timeToReachNextFloor = 1000 millis  // up  or down, same time taken

  private val dontCare: StateFunction = {
    // ..
  }

  private val powerYourselfOff: StateFunction = {
    case Event(InstructedToPowerOff,_) =>
      stay
  }

  private val powerYourselfOn: StateFunction = {
    case Event(InstructedToPowerOn,_)  =>
      goto (PoweredOn)
  }

  private val beReady: StateFunction = {
    case Event(BeReady,_)          =>
      settleDownAtGroundFloor
      goto (Ready)
  }

  // .. other StateFunctions here

  startWith(PoweredOff,InitialData)

  when (PoweredOff) (powerYourselfOn orElse
                     dontCare)

  when (PoweredOn)  (powerYourselfOff orElse
                     beReady orElse
                     dontCare
  // ...

一个人希望FSM会在创建后立即响应CurrentState消息并调用 startWith 函数,但这不会发生。此测试(部分代码再次)失败:

"A LiftCarriage" must {
    "be ready, when it settles down after being PoweredOn" in {

      val testCarriageFSM = TestFSMRef(new LiftCarriageWithMovingState(movingStateSimulator))

      expectMsgPF() {
        case CurrentState(_,PoweredOff) => true // Line 46, see the stacktrace below
      }

因此:

 A LiftCarriage
[info] - must be ready, when it settles down after being PoweredOn *** FAILED ***
[info]   java.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsg:
[info]   at scala.Predef$.assert(Predef.scala:170)
[info]   at akka.testkit.TestKitBase$class.expectMsgPF(TestKit.scala:356)
[info]   at akka.testkit.TestKit.expectMsgPF(TestKit.scala:718)
[info]   at withExplicitMovingState.LiftCarriageMovingStoppingCorrectlyTest$$anonfun$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(LiftCarriageMovingStoppingCorrectlyTest.scala:46)
[info]   at withExplicitMovingState.LiftCarriageMovingStoppingCorrectlyTest$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(LiftCarriageMovingStoppingCorrectlyTest.scala:42)
[info]   at withExplicitMovingState.LiftCarriageMovingStoppingCorrectlyTest$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(LiftCarriageMovingStoppingCorrectlyTest.scala:42)

我的理解是否有差距?如果是的话,请教育我。

我也看到完全相同的问题 - 以更加概括的形式here提出 - 没有得到答复。难道这不是Akka FSM和Testkit的语义差距,值得解决? :-P

0 个答案:

没有答案