nim-codex/tests/codex/utils/testasyncstatemachine.nim

62 lines
1.6 KiB
Nim
Raw Normal View History

2023-02-08 14:31:59 +01:00
import pkg/asynctest
import pkg/questionable
import pkg/chronos
import codex/utils/asyncstatemachine
import ../helpers/eventually
2023-02-08 14:54:29 +01:00
type
AsyncTestState = ref object of AsyncState
State1 = ref object of AsyncTestState
State2 = ref object of AsyncTestState
State3 = ref object of AsyncTestState
2023-02-08 14:31:59 +01:00
var state1runInvoked = 0
2023-02-08 14:54:29 +01:00
var state2runInvoked = 0
var state3runInvoked = 0
2023-02-08 14:31:59 +01:00
method onMoveToNextStateEvent*(state: AsyncTestState): ?AsyncState {.base.} =
discard
2023-02-08 14:31:59 +01:00
2023-02-08 14:54:29 +01:00
method run(state: State1): Future[?AsyncState] {.async.} =
inc state1runInvoked
2023-02-08 14:54:29 +01:00
return some AsyncState(State2.new())
method run(state: State2): Future[?AsyncState] {.async.} =
inc state2runInvoked
method onMoveToNextStateEvent(state: State2): ?AsyncState =
return some AsyncState(State3.new())
method run(state: State3): Future[?AsyncState] {.async.} =
inc state3runInvoked
2023-02-08 14:31:59 +01:00
suite "async state machines":
var machine: AsyncStateMachine
var state1, state2: AsyncState
2023-02-08 14:31:59 +01:00
setup:
state1runInvoked = 0
2023-02-08 14:54:29 +01:00
state2runInvoked = 0
state3runInvoked = 0
machine = AsyncStateMachine.new()
state1 = State1.new()
state2 = State2.new()
2023-02-08 14:31:59 +01:00
test "should call run on start state":
machine.start(state1)
check eventually state1runInvoked == 1
2023-02-08 14:31:59 +01:00
2023-02-08 14:54:29 +01:00
test "moves to next state when run completes":
machine.start(state1)
check eventually state2runInvoked == 1
2023-02-08 14:54:29 +01:00
test "state2 moves to state3 on event":
machine.start(state2)
2023-02-08 14:54:29 +01:00
proc moveToNextStateEvent(state: AsyncState): ?AsyncState =
AsyncTestState(state).onMoveToNextStateEvent()
machine.schedule(Event(moveToNextStateEvent))
2023-02-08 14:54:29 +01:00
check eventually state3runInvoked == 1