Correctly trigger 'on_exit' of starting/initial state

Instead of not calling the 'on_exit' of the initialized
and/or starting state we should make an attempt to call
it if a function exists/was provided. This function will
be called on the first event to be processed (which will
cause the state machine to transition out of the starting
state to a new stable state).

Fixes bug 1404124

Change-Id: I037439313f9071af23c0859a62832d735f9abcd8
This commit is contained in:
Joshua Harlow
2014-12-18 22:22:21 -08:00
parent ba62a9c7be
commit dbc890f928
2 changed files with 9 additions and 2 deletions

View File

@@ -304,7 +304,9 @@ class FSMTest(test.TestCase):
m.process_event('fall')
self.assertEqual([('down', 'beat'),
('up', 'jump'), ('down', 'fall')], enter_transitions)
self.assertEqual([('down', 'jump'), ('up', 'fall')], exit_transitions)
self.assertEqual(
[('start', 'beat'), ('down', 'jump'), ('up', 'fall')],
exit_transitions)
def test_run_iter(self):
up_downs = []

View File

@@ -196,7 +196,12 @@ class FSM(object):
if self._states[self._start_state]['terminal']:
raise excp.InvalidState("Can not start from a terminal"
" state '%s'" % (self._start_state))
self._current = _Jump(self._start_state, None, None)
# No on enter will be called, since we are priming the state machine
# and have not really transitioned from anything to get here, we will
# though allow 'on_exit' to be called on the event that causes this
# to be moved from...
self._current = _Jump(self._start_state, None,
self._states[self._start_state]['on_exit'])
def run(self, event, initialize=True):
"""Runs the state machine, using reactions only."""