Better handlers exceptions handling

darcs-hash:20070321072742-86b55-56b0ae25d0a3e8835100a6b570aeb271ad912fe7.gz
This commit is contained in:
David Rousselie
2007-03-21 08:27:42 +01:00
parent 244a82f38a
commit 2791e3cd61
3 changed files with 33 additions and 43 deletions

View File

@@ -45,7 +45,7 @@ import jcl
if __name__ == '__main__': if __name__ == '__main__':
logger = logging.getLogger() logger = logging.getLogger()
logger.addHandler(logging.StreamHandler()) logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO) logger.setLevel(logging.CRITICAL)
component_suite = unittest.makeSuite(JCLComponent_TestCase, "test") component_suite = unittest.makeSuite(JCLComponent_TestCase, "test")
feeder_component_suite = unittest.makeSuite(FeederComponent_TestCase, "test") feeder_component_suite = unittest.makeSuite(FeederComponent_TestCase, "test")
@@ -58,7 +58,7 @@ if __name__ == '__main__':
jcl_suite = unittest.TestSuite() jcl_suite = unittest.TestSuite()
# jcl_suite.addTest(FeederComponent_TestCase('test_handle_tick')) # jcl_suite.addTest(FeederComponent_TestCase('test_handle_tick'))
# jcl_suite.addTest(JCLComponent_TestCase('test_send_error_second')) # jcl_suite.addTest(JCLComponent_TestCase('test_run_unhandled_error'))
# jcl_suite = unittest.TestSuite((component_suite)) # jcl_suite = unittest.TestSuite((component_suite))
# jcl_suite = unittest.TestSuite((presence_account_suite)) # jcl_suite = unittest.TestSuite((presence_account_suite))
jcl_suite = unittest.TestSuite((component_suite, \ jcl_suite = unittest.TestSuite((component_suite, \

View File

@@ -118,20 +118,14 @@ class JCLComponent(Component, object):
name = "TimerThread") name = "TimerThread")
timer_thread.start() timer_thread.start()
try: try:
try: while (self.running and self.stream \
while (self.running and self.stream \ and not self.stream.eof \
and not self.stream.eof \ and self.stream.socket is not None):
and self.stream.socket is not None): self.stream.loop_iter(JCLComponent.timeout)
self.stream.loop_iter(JCLComponent.timeout) if self.queue.qsize():
if self.queue.qsize(): raise self.queue.get(0)
raise self.queue.get(0)
except Exception, exception:
#self.__logger.exception("Exception cought:")
# put Exception in queue to be use by unit tests
self.queue.put(exception)
raise
finally: finally:
# self.running = False self.running = False
if self.stream and not self.stream.eof \ if self.stream and not self.stream.eof \
and self.stream.socket is not None: and self.stream.socket is not None:
current_user_jid = None current_user_jid = None
@@ -190,7 +184,6 @@ class JCLComponent(Component, object):
self.wait_event.wait(self.time_unit) self.wait_event.wait(self.time_unit)
except Exception, exception: except Exception, exception:
self.queue.put(exception) self.queue.put(exception)
raise
self.__logger.info("Timer thread terminated...") self.__logger.info("Timer thread terminated...")
def authenticated(self): def authenticated(self):

View File

@@ -115,8 +115,8 @@ class MockStreamNoConnect(MockStream):
self.eof = True self.eof = True
class MockStreamRaiseException(MockStream): class MockStreamRaiseException(MockStream):
def connect(self): def loop_iter(self, timeout):
raise Error("Test error") raise Exception("in loop error")
class JCLComponent_TestCase(unittest.TestCase): class JCLComponent_TestCase(unittest.TestCase):
########################################################################### ###########################################################################
@@ -191,38 +191,35 @@ class JCLComponent_TestCase(unittest.TestCase):
def test_run_unhandled_error(self): def test_run_unhandled_error(self):
"""Test main loop unhandled error from a component handler""" """Test main loop unhandled error from a component handler"""
# TODO def do_nothing():
return
self.comp.time_unit = 1 self.comp.time_unit = 1
self.comp.stream = MockStreamNoConnect() self.comp.stream = MockStreamRaiseException()
self.comp.stream_class = MockStreamNoConnect self.comp.stream_class = MockStreamRaiseException
self.comp.run() self.comp.handle_tick = do_nothing
self.assertTrue(self.comp.stream.connection_started)
threads = threading.enumerate() try:
self.assertEquals(len(threads), 1) self.comp.run()
self.assertTrue(self.comp.stream.connection_stopped) except Exception, e:
if self.comp.queue.qsize(): threads = threading.enumerate()
raise self.comp.queue.get(0) self.assertEquals(len(threads), 1)
self.assertTrue(self.comp.stream.connection_stopped)
return
self.fail("No exception caught")
def test_run_ni_handle_tick(self): def test_run_ni_handle_tick(self):
"""Test JCLComponent 'NotImplemented' error from handle_tick method""" """Test JCLComponent 'NotImplemented' error from handle_tick method"""
self.comp.time_unit = 1 self.comp.time_unit = 1
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = MockStream self.comp.stream_class = MockStream
self.saved_time_handler = self.comp.time_handler try:
self.comp.time_handler = self.__comp_time_handler self.comp.run()
run_thread = threading.Thread(target = self.__comp_run, \ except NotImplementedError, e:
name = "run_thread") threads = threading.enumerate()
run_thread.start() self.assertEquals(len(threads), 1)
time.sleep(1) self.assertTrue(self.comp.stream.connection_stopped)
self.comp.running = False return
self.assertTrue(self.comp.stream.connection_started) self.fail("No exception caught")
time.sleep(1)
threads = threading.enumerate()
self.assertEquals(len(threads), 1)
self.assertTrue(self.comp.stream.connection_stopped)
self.assertEquals(self.comp.queue.qsize(), 1)
self.assertTrue(isinstance(self.comp.queue.get(0), \
NotImplementedError))
def test_run_go_offline(self): def test_run_go_offline(self):
"""Test main loop send offline presence when exiting""" """Test main loop send offline presence when exiting"""