This commit is contained in:
Ben Bierens 2023-02-08 16:15:07 +01:00 committed by Mark Spanbroek
parent 4cf982e327
commit 4978991065
No known key found for this signature in database
GPG Key ID: FBE3E9548D427C00
1 changed files with 19 additions and 18 deletions

View File

@ -5,57 +5,58 @@ import codex/utils/asyncstatemachine
import ../helpers/eventually
type
AsyncTestState = ref object of AsyncState
State1 = ref object of AsyncTestState
State2 = ref object of AsyncTestState
State3 = ref object of AsyncTestState
TestState = ref object of AsyncState
State1 = ref object of TestState
State2 = ref object of TestState
State3 = ref object of TestState
var state1runInvoked = 0
var state2runInvoked = 0
var state3runInvoked = 0
var state1Invoked = 0
var state2Invoked = 0
var state3Invoked = 0
method onMoveToNextStateEvent*(state: AsyncTestState): ?AsyncState {.base.} =
method onMoveToNextStateEvent*(state: TestState): ?AsyncState {.base.} =
discard
method run(state: State1): Future[?AsyncState] {.async.} =
inc state1runInvoked
inc state1Invoked
return some AsyncState(State2.new())
method run(state: State2): Future[?AsyncState] {.async.} =
inc state2runInvoked
inc state2Invoked
method onMoveToNextStateEvent(state: State2): ?AsyncState =
return some AsyncState(State3.new())
method run(state: State3): Future[?AsyncState] {.async.} =
inc state3runInvoked
inc state3Invoked
suite "async state machines":
var machine: AsyncStateMachine
var state1, state2: AsyncState
setup:
state1runInvoked = 0
state2runInvoked = 0
state3runInvoked = 0
state1Invoked = 0
state2Invoked = 0
state3Invoked = 0
machine = AsyncStateMachine.new()
state1 = State1.new()
state2 = State2.new()
test "should call run on start state":
machine.start(state1)
check eventually state1runInvoked == 1
check eventually state1Invoked == 1
test "moves to next state when run completes":
machine.start(state1)
check eventually state2runInvoked == 1
check eventually state2Invoked == 1
test "state2 moves to state3 on event":
machine.start(state2)
proc moveToNextStateEvent(state: AsyncState): ?AsyncState =
AsyncTestState(state).onMoveToNextStateEvent()
TestState(state).onMoveToNextStateEvent()
machine.schedule(Event(moveToNextStateEvent))
check eventually state3runInvoked == 1
check eventually state3Invoked == 1