1

我正在使用 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)

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

我还看到完全相同的问题 - 在这里以更概括的形式提出- 没有得到回答。这难道不是 Akka FSM 和 Testkit 语义上的一个缺口,值得解决吗?:-P

4

0 回答 0