Merge "Add FSM.is_stable() method"

This commit is contained in:
Jenkins 2015-12-14 03:09:30 +00:00 committed by Gerrit Code Review
commit 02fc79e5a6
2 changed files with 22 additions and 1 deletions

View File

@ -65,6 +65,18 @@ class FSM(machines.FiniteMachine):
def target_state(self):
return self._target_state
def is_stable(self, state):
"""Is the state stable?
:param state: the state of interest
:raises: InvalidState if the state is invalid
:returns True if it is a stable state; False otherwise
"""
try:
return self._states[state]['stable']
except KeyError:
raise excp.InvalidState(_("State '%s' does not exist") % state)
@_translate_excp
def add_state(self, state, on_enter=None, on_exit=None,
target=None, terminal=None, stable=False):
@ -110,7 +122,7 @@ class FSM(machines.FiniteMachine):
if target not in self._states:
raise excp.InvalidState(
_("Target state '%s' does not exist") % target)
if not self._states[target]['stable']:
if not self.is_stable(target):
raise excp.InvalidState(
_("Target state '%s' is not a 'stable' state") % target)

View File

@ -30,6 +30,15 @@ class FSMTest(base.TestCase):
m.add_transition('wakeup', 'working', 'walk')
self.fsm = m
def test_is_stable(self):
self.assertTrue(self.fsm.is_stable('working'))
def test_is_stable_not(self):
self.assertFalse(self.fsm.is_stable('daydream'))
def test_is_stable_invalid_state(self):
self.assertRaises(excp.InvalidState, self.fsm.is_stable, 'foo')
def test_target_state_stable(self):
# Test to verify that adding a new state with a 'target' state pointing
# to a 'stable' state does not raise an exception