2015-09-23 23 views
2

作为Akka FSM的学习练习,我模拟了咖啡店的简化订单处理流程。附加的是状态转换图。但是,我写的一个测试用例超时了,我不明白为什么。为什么我的Akka FSM事件超时?

FSM(未示出为了简洁case类):

class OrderSystem extends Actor with ActorLogging with LoggingFSM[State, Data] { 
    startWith(OrderPending, Data(OrderPending, PaymentPending)) 

    when(OrderPending) { 
    case Event(BaristaIsBusy, _) => stay 
    case Event(BaristaIsAvailable(_, PaymentPending), _) => goto(OrderPlaced) using Data(stateName, PaymentPending) 
    case Event(b: BaristaIsAvailable, _) => goto(OrderReady) 
    } 

    val waiting = Data(OrderPlaced, PaymentAccepted) 

    when(OrderPlaced) { 
    case Event(b: BaristaIsAvailable, `waiting`) => println("1"); goto(OrderReady) 
    case Event(b: BaristaIsBusy, `waiting`) => println("2"); goto(OrderPending) using `waiting` 
    case Event(_, Data(_, PaymentDeclined)) => println("3"); goto(OrderClosed) 
    case Event(_, Data(_, PaymentPending)) => println("4"); stay 
    } 

    when(OrderReady) { 
    case Event(HappyWithOrder, _) => goto(OrderClosed) 
    case Event(NotHappyWithOrder, _) => goto(OrderPending) using Data(stateName, PaymentAccepted) 
    } 

    when(OrderClosed) { 
    case _ => stay 
    } 

    whenUnhandled { 
    case Event(e, s) => { 
     // state name is available as 'stateName' 
     log.warning("Received unhandled request {} in state {}/{}", e, stateName, s) 
     stay 
    } 
    } 

    // previous state data is available as 'stateData' and next state data as 'nextStateData' 
    // not necessary as LoggingFSM (if configured) will take care of logging 
    onTransition { 
    case _ -> nextState => log.info("Entering state: {} with payment activity: {} from state: {} with payment activity: {}.", 
     nextState, stateData.paymentActivity, nextStateData.fromState, nextStateData.paymentActivity) 
    } 

    initialize() 
} 

失败测试:

it should "stay in OrderPlaced state as long as customer has not paid" in { 
    val orderSystem = system.actorOf(Props[OrderSystem]) 
    orderSystem ! BaristaIsAvailable(OrderPending, PaymentPending) 

    orderSystem ! SubscribeTransitionCallBack(testActor) 

    expectMsg(CurrentState(orderSystem, OrderPlaced)) 

    orderSystem ! BaristaIsAvailable(OrderPlaced, PaymentPending) 

    expectMsg(CurrentState(orderSystem, OrderPlaced)) 
} 

日志:

2015-09-22 23:29:15.236 [order-system-akka.actor.default-dispatcher-2] [DEBUG] n.a.s.o.OrderSystem - processing Event(BaristaIsAvailable(OrderPending,PaymentPending),Data(OrderPending,PaymentPending)) from Actor[akka://order-system/system/testActor1#-2143558060] 
2015-09-22 23:29:15.238 [order-system-akka.actor.default-dispatcher-2] [INFO ] n.a.s.o.OrderSystem - Entering state: OrderPlaced with payment activity: PaymentPending from state: OrderPending with payment activity: PaymentPending. 
2015-09-22 23:29:15.239 [order-system-akka.actor.default-dispatcher-2] [DEBUG] n.a.s.o.OrderSystem - transition OrderPending -> OrderPlaced 
4 
2015-09-22 23:29:15.242 [order-system-akka.actor.default-dispatcher-2] [DEBUG] n.a.s.o.OrderSystem - processing Event(BaristaIsAvailable(OrderPlaced,PaymentPending),Data(OrderPending,PaymentPending)) from Actor[akka://order-system/system/testActor1#-2143558060] 
[31m- should stay in OrderPlaced state as long as customer has not paid *** FAILED ***[0m 
[31m java.lang.AssertionError: assertion failed: timeout (3 seconds) 

enter image description here

+1

你没有发回任何回信从FSM回来,这是正常的,你expectMsg失败 – kosii

回答

2

SubscribeTransitionCallBack只交付CurrentState一次,然后只有Transition回调。

你可以试着这样做:

it should "stay in OrderPlaced state as long as customer has not paid" in { 

    val orderSystem = TestFSMRef(new OrderSystem) 

    orderSystem ! SubscribeTransitionCallBack(testActor) 
    // fsm first answers with current state 
    expectMsgPF(1.second. s"OrderPending as current state for $orderSystem") { 
    case CurrentState('orderSystem', OrderPending) => ok 
    } 

    // from now on the subscription will yield 'Transition' messages 
    orderSystem ! BaristaIsAvailable(OrderPending, PaymentPending) 
    expectMsgPF(1.second, s"Transition from OrderPending to OrderPlaced for $orderSystem") { 
    case Transition(`orderSystem`, OrderPending, OrderPlaced) => ok 
    } 

    orderSystem ! BaristaIsAvailable(OrderPlaced, PaymentPending) 
    // there is no transition, so there should not be a callback. 
    expectNoMsg(1.second) 

/* 
    // alternatively, if your state data changes, using TestFSMRef, you could check state data blocking for some time 
    awaitCond(
    p = orderSystem.stateData == ???, 
    max = 2.seconds, 
    interval = 200.millis, 
    message = "waiting for expected state data..." 
) 
    // awaitCond will throw an exception if the condition is not met within max timeout 
*/ 
    success 
} 
+0

我见[这](HTTPS使用的探头:// github.com/tombray/akka-fsm-examples/blob/master/src/test/scala/com/tombray/examples/akka/ElevatorActorSpec.scala)测试。这对我的情况会起作用吗? 'val probe = TestProbe(); probe.expectMsg' –

+0

@AbhijitSarkar在您的测试用例中使用'TestProbe'肯定有一种方法可行。但它使事情变得更复杂而不是改善任何事情。 –

+0

嗯。最后一个问题。在上面的日志中,我看到当我发送第二个事件时,它不仅改变了状态,而且还匹配了情况4(在日志中打印为4)。这是预期的行为,如果我使用'goto'来处理没有匹配的事件,它将进入该状态,但不匹配任何'case'语句。如果'goto'与一个匹配的事件一起使用,它会进入新的状态并且匹配'case'? –

相关问题