Skip to content

Commit

Permalink
Turn the Waiting state into a sub-state of Auto
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Apr 26, 2024
1 parent 77e3117 commit 1b9718d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
32 changes: 16 additions & 16 deletions src/nextline_schedule/auto/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ def build_state_machine(model=None, graph=False, asyncio=True, markup=False) ->
.-------------->| Off |<--------------.
| '-------------' |
| | turn_on() |
turn_off() | |
| v |
| .-------------. |
|---------------| Waiting | on_raised()
| '-------------' |
| | on_initialized() |
| | on_finished() |
turn_off() | on_raised()
| | |
| | |
| .------------------+------------------. |
| | Auto | | |
| | v | |
| | .-------------. | |
| | | Waiting | | |
| | '-------------' | |
| | | on_initialized() | |
| | | on_finished() | |
| | v | |
| | .-------------. | |
| | | Pulling | | |
'---| '-------------' |---'
| run() | ^ |
Expand All @@ -44,11 +45,11 @@ def build_state_machine(model=None, graph=False, asyncio=True, markup=False) ->
'-------------------------------------'
>>> class Model:
... def on_enter_waiting(self):
... def on_enter_auto_waiting(self):
... print('enter the waiting state')
... self.on_finished()
...
... def on_exit_waiting(self):
... def on_exit_auto_waiting(self):
... print('exit the waiting state')
...
... def on_enter_auto_pulling(self):
Expand Down Expand Up @@ -88,12 +89,14 @@ def build_state_machine(model=None, graph=False, asyncio=True, markup=False) ->

auto_state_conf = {
'name': 'auto',
'states': ['pulling', 'running'],
'states': ['waiting', 'pulling', 'running'],
'transitions': [
['on_initialized', 'waiting', 'pulling'],
['on_finished', 'waiting', 'pulling'],
['run', 'pulling', 'running'],
['on_finished', 'running', 'pulling'],
],
'initial': 'pulling',
'initial': 'waiting',
'queued': True,
'ignore_invalid_triggers': True,
}
Expand All @@ -109,18 +112,15 @@ def build_state_machine(model=None, graph=False, asyncio=True, markup=False) ->
'states': [
'created',
'off',
'waiting',
{'name': 'auto', 'children': auto_state},
],
'transitions': [
['start', 'created', 'off'],
['turn_on', 'off', 'waiting'],
['on_initialized', 'waiting', 'auto'],
['on_finished', 'waiting', 'auto'],
['turn_on', 'off', 'auto'],
['on_raised', 'auto', 'off'],
{
'trigger': 'turn_off',
'source': ['waiting', 'auto'],
'source': 'auto',
'dest': 'off',
'before': 'cancel_task',
},
Expand Down
2 changes: 1 addition & 1 deletion src/nextline_schedule/auto/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, callback: CallbackType):
def subscribe_state(self) -> AsyncIterator[str]:
return self._pubsub_state.subscribe()

async def on_enter_waiting(self) -> None:
async def on_enter_auto_waiting(self) -> None:
task = asyncio.create_task(self._callback.wait())
self._task = task
self._tasks.add(task)
Expand Down
2 changes: 1 addition & 1 deletion tests/auto/test_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def test_one() -> None:

expected = [
'off',
'waiting',
'auto_waiting',
'auto_pulling',
'auto_running',
'auto_pulling',
Expand Down
4 changes: 2 additions & 2 deletions tests/auto/test_auto_on_raised.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def request_statement():
if state == 'off':
break

expected = ['off', 'waiting', 'auto_pulling', 'off']
expected = ['off', 'auto_waiting', 'auto_pulling', 'off']
assert expected == await states


Expand All @@ -61,7 +61,7 @@ async def request_statement():
if state == 'off':
break

expected = ['off', 'waiting', 'auto_pulling', 'auto_running', 'off']
expected = ['off', 'auto_waiting', 'auto_pulling', 'auto_running', 'off']
assert expected == await states


Expand Down
2 changes: 1 addition & 1 deletion tests/auto/test_auto_turn_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def test_turn_off_while_waiting():
trace_no=prompt.trace_no,
)

expected = ['off', 'waiting', 'off']
expected = ['off', 'auto_waiting', 'off']
assert expected == await states


Expand Down
5 changes: 3 additions & 2 deletions tests/test_fsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ def st_paths(draw: st.DrawFn):
'start': {'dest': 'off'},
},
'off': {
'turn_on': {'dest': 'waiting'},
'turn_on': {'dest': 'auto_waiting'},
},
'waiting': {
'auto_waiting': {
'turn_off': {'dest': 'off', 'before': 'cancel_task'},
'on_initialized': {'dest': 'auto_pulling'},
'on_finished': {'dest': 'auto_pulling'},
'on_raised': {'dest': 'off'},
},
'auto_pulling': {
'run': {'dest': 'auto_running'},
Expand Down

0 comments on commit 1b9718d

Please sign in to comment.