summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThom Nichols <tmnichols@gmail.com>2010-06-07 13:43:37 -0400
committerThom Nichols <tmnichols@gmail.com>2010-06-07 13:43:37 -0400
commit47f1fb16909d3baaec5822b1dcbca89491d0d18c (patch)
treef60363662ece056558deb7e846d2f5177ac115d3
parent66cf0c20218657622a5d98aee155df1ea37d9a89 (diff)
downloadslixmpp-47f1fb16909d3baaec5822b1dcbca89491d0d18c.tar.gz
slixmpp-47f1fb16909d3baaec5822b1dcbca89491d0d18c.tar.bz2
slixmpp-47f1fb16909d3baaec5822b1dcbca89491d0d18c.tar.xz
slixmpp-47f1fb16909d3baaec5822b1dcbca89491d0d18c.zip
context manager now returns a boolean 'result' as the context variable to indicate whether the transition timed out or if you are actually locked when entering the context body
-rw-r--r--sleekxmpp/xmlstream/statemachine.py4
-rw-r--r--tests/test_statemachine.py15
2 files changed, 11 insertions, 8 deletions
diff --git a/sleekxmpp/xmlstream/statemachine.py b/sleekxmpp/xmlstream/statemachine.py
index 51b4aaed..b1ab7574 100644
--- a/sleekxmpp/xmlstream/statemachine.py
+++ b/sleekxmpp/xmlstream/statemachine.py
@@ -182,12 +182,12 @@ class _StateCtx:
if time.time() >= start + self.wait:
logging.debug('StateMachine timeout while waiting for state: %s', self.from_state )
self._timeout = True # to indicate we should not transition
- break
+ return False
self.state_machine.lock.wait(self.wait)
logging.debug('StateMachine entered context in state: %s',
self.state_machine.current_state() )
- return self.state_machine
+ return True
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val is not None:
diff --git a/tests/test_statemachine.py b/tests/test_statemachine.py
index 4cfb50de..0173ff09 100644
--- a/tests/test_statemachine.py
+++ b/tests/test_statemachine.py
@@ -223,11 +223,12 @@ class testStateMachine(unittest.TestCase):
s = sm.StateMachine(('one','two','three'))
- with s.transition_ctx('two','three') as _s:
- self.assertTrue( _s['one'] )
- self.failIf( _s.current_state in ('two','three') )
+ with s.transition_ctx('two','three') as result:
+ self.failIf( result )
+ self.assertTrue( s['one'] )
+ self.failIf( s.current_state in ('two','three') )
- self.assertTrue( _s['one'] )
+ self.assertTrue( s['one'] )
def r1():
print 'thread 1 started'
@@ -237,10 +238,12 @@ class testStateMachine(unittest.TestCase):
def r2():
print 'thread 2 started'
self.failIf( s['two'] )
- with s.transition_ctx('two','three', 10) as _s:
- self.assertTrue( _s['two'] )
+ with s.transition_ctx('two','three', 10) as result:
+ self.assertTrue( result )
+ self.assertTrue( s['two'] )
print 'thread 2 will transition on exit from the context manager...'
self.assertTrue( s['three'] )
+ print 'transitioned to %s' % s.current_state()
t1 = threading.Thread(target=r1)
t2 = threading.Thread(target=r2)