Threads exceptions handling

pass exceptions throw Queue so MainThread can propagate exceptions to make tests failed when needed

darcs-hash:20061009173419-86b55-9197a944aa629ab43f55d56662b8f873036cc60e.gz
This commit is contained in:
David Rousselie
2006-10-09 19:34:19 +02:00
parent 9e28468b81
commit cb016a5406
2 changed files with 27 additions and 9 deletions

View File

@@ -102,6 +102,8 @@ class JCLComponent(Component):
self.set_account_class(Account) self.set_account_class(Account)
self.version = VERSION self.version = VERSION
self.accounts = [] self.accounts = []
self.time_unit = 60
self.queue = Queue(100)
self.disco_info.add_feature("jabber:iq:version") self.disco_info.add_feature("jabber:iq:version")
self.disco_info.add_feature("jabber:iq:register") self.disco_info.add_feature("jabber:iq:register")
@@ -134,11 +136,13 @@ class JCLComponent(Component):
and not self.stream.eof and self.stream.socket is not None): and not self.stream.eof and self.stream.socket is not None):
try: try:
self.stream.loop_iter(JCLComponent.timeout) self.stream.loop_iter(JCLComponent.timeout)
except (KeyboardInterrupt, SystemExit, FatalStreamError, \ if self.queue.qsize():
StreamError): raise self.queue.get(0)
raise except Exception, e:
except:
self.__logger.exception("Exception cought:") self.__logger.exception("Exception cought:")
# put Exception in queue to be use by unit tests
self.queue.put(e)
raise
finally: finally:
if self.stream: if self.stream:
# TODO : send unavailble from transport and all account to users # TODO : send unavailble from transport and all account to users
@@ -190,10 +194,14 @@ class JCLComponent(Component):
"""Timer thread handler """Timer thread handler
""" """
self.__logger.info("Timer thread started...") self.__logger.info("Timer thread started...")
try:
while self.running: while self.running:
self.handle_tick() self.handle_tick()
self.__logger.debug("Resetting alarm signal") self.__logger.debug("Resetting alarm signal")
time.sleep(60) time.sleep(self.time_unit)
except Exception, e:
self.queue.put(e)
raise
def authenticated(self): def authenticated(self):
"""Override authenticated Component event handler """Override authenticated Component event handler

View File

@@ -127,6 +127,7 @@ class JCLComponent_TestCase(unittest.TestCase):
del account.hub.threadConnection del account.hub.threadConnection
def test_run(self): def test_run(self):
self.comp.time_unit = 1
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = MockStream self.comp.stream_class = MockStream
run_thread = threading.Thread(target = self.comp.run, \ run_thread = threading.Thread(target = self.comp.run, \
@@ -151,6 +152,15 @@ class JCLComponent_TestCase(unittest.TestCase):
if self.max_tick_count == 0: if self.max_tick_count == 0:
self.comp.running = False self.comp.running = False
def test_time_handler(self):
self.comp.time_unit = 1
self.max_tick_count = 2
self.comp.handle_tick = self.__handle_tick_test_time_handler
self.comp.running = True
self.comp.time_handler()
self.assertEquals(self.max_tick_count, 0)
self.assertFalse(self.comp.running)
def test_authenticated_handler(self): def test_authenticated_handler(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.authenticated() self.comp.authenticated()