Better handlers exceptions handling
darcs-hash:20070321072742-86b55-56b0ae25d0a3e8835100a6b570aeb271ad912fe7.gz
This commit is contained in:
@@ -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, \
|
||||||
|
|||||||
@@ -117,7 +117,6 @@ class JCLComponent(Component, object):
|
|||||||
timer_thread = threading.Thread(target = self.time_handler, \
|
timer_thread = threading.Thread(target = self.time_handler, \
|
||||||
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 \
|
||||||
@@ -125,13 +124,8 @@ class JCLComponent(Component, object):
|
|||||||
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):
|
||||||
|
|||||||
@@ -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.handle_tick = do_nothing
|
||||||
|
|
||||||
|
try:
|
||||||
self.comp.run()
|
self.comp.run()
|
||||||
self.assertTrue(self.comp.stream.connection_started)
|
except Exception, e:
|
||||||
threads = threading.enumerate()
|
threads = threading.enumerate()
|
||||||
self.assertEquals(len(threads), 1)
|
self.assertEquals(len(threads), 1)
|
||||||
self.assertTrue(self.comp.stream.connection_stopped)
|
self.assertTrue(self.comp.stream.connection_stopped)
|
||||||
if self.comp.queue.qsize():
|
return
|
||||||
raise self.comp.queue.get(0)
|
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")
|
|
||||||
run_thread.start()
|
|
||||||
time.sleep(1)
|
|
||||||
self.comp.running = False
|
|
||||||
self.assertTrue(self.comp.stream.connection_started)
|
|
||||||
time.sleep(1)
|
|
||||||
threads = threading.enumerate()
|
threads = threading.enumerate()
|
||||||
self.assertEquals(len(threads), 1)
|
self.assertEquals(len(threads), 1)
|
||||||
self.assertTrue(self.comp.stream.connection_stopped)
|
self.assertTrue(self.comp.stream.connection_stopped)
|
||||||
self.assertEquals(self.comp.queue.qsize(), 1)
|
return
|
||||||
self.assertTrue(isinstance(self.comp.queue.get(0), \
|
self.fail("No exception caught")
|
||||||
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"""
|
||||||
|
|||||||
Reference in New Issue
Block a user