diff --git a/run_tests.py b/run_tests.py index 2bb443a..58448a8 100644 --- a/run_tests.py +++ b/run_tests.py @@ -45,7 +45,7 @@ import jcl if __name__ == '__main__': logger = logging.getLogger() logger.addHandler(logging.StreamHandler()) - logger.setLevel(logging.INFO) + logger.setLevel(logging.CRITICAL) component_suite = unittest.makeSuite(JCLComponent_TestCase, "test") feeder_component_suite = unittest.makeSuite(FeederComponent_TestCase, "test") @@ -58,7 +58,7 @@ if __name__ == '__main__': jcl_suite = unittest.TestSuite() # 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((presence_account_suite)) jcl_suite = unittest.TestSuite((component_suite, \ diff --git a/src/jcl/jabber/component.py b/src/jcl/jabber/component.py index db33468..5c8197a 100644 --- a/src/jcl/jabber/component.py +++ b/src/jcl/jabber/component.py @@ -118,20 +118,14 @@ class JCLComponent(Component, object): name = "TimerThread") timer_thread.start() try: - try: - while (self.running and self.stream \ - and not self.stream.eof \ - and self.stream.socket is not None): - self.stream.loop_iter(JCLComponent.timeout) - if self.queue.qsize(): - 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 + while (self.running and self.stream \ + and not self.stream.eof \ + and self.stream.socket is not None): + self.stream.loop_iter(JCLComponent.timeout) + if self.queue.qsize(): + raise self.queue.get(0) finally: -# self.running = False + self.running = False if self.stream and not self.stream.eof \ and self.stream.socket is not None: current_user_jid = None @@ -190,7 +184,6 @@ class JCLComponent(Component, object): self.wait_event.wait(self.time_unit) except Exception, exception: self.queue.put(exception) - raise self.__logger.info("Timer thread terminated...") def authenticated(self): diff --git a/tests/jcl/jabber/test_component.py b/tests/jcl/jabber/test_component.py index 28e6402..16f3a20 100644 --- a/tests/jcl/jabber/test_component.py +++ b/tests/jcl/jabber/test_component.py @@ -115,8 +115,8 @@ class MockStreamNoConnect(MockStream): self.eof = True class MockStreamRaiseException(MockStream): - def connect(self): - raise Error("Test error") + def loop_iter(self, timeout): + raise Exception("in loop error") class JCLComponent_TestCase(unittest.TestCase): ########################################################################### @@ -191,38 +191,35 @@ class JCLComponent_TestCase(unittest.TestCase): def test_run_unhandled_error(self): """Test main loop unhandled error from a component handler""" - # TODO + def do_nothing(): + return self.comp.time_unit = 1 - self.comp.stream = MockStreamNoConnect() - self.comp.stream_class = MockStreamNoConnect - self.comp.run() - self.assertTrue(self.comp.stream.connection_started) - threads = threading.enumerate() - self.assertEquals(len(threads), 1) - self.assertTrue(self.comp.stream.connection_stopped) - if self.comp.queue.qsize(): - raise self.comp.queue.get(0) + self.comp.stream = MockStreamRaiseException() + self.comp.stream_class = MockStreamRaiseException + self.comp.handle_tick = do_nothing + + try: + self.comp.run() + except Exception, e: + threads = threading.enumerate() + 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): """Test JCLComponent 'NotImplemented' error from handle_tick method""" self.comp.time_unit = 1 self.comp.stream = MockStream() self.comp.stream_class = MockStream - self.saved_time_handler = self.comp.time_handler - self.comp.time_handler = self.__comp_time_handler - run_thread = threading.Thread(target = self.__comp_run, \ - 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() - 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)) + try: + self.comp.run() + except NotImplementedError, e: + threads = threading.enumerate() + self.assertEquals(len(threads), 1) + self.assertTrue(self.comp.stream.connection_stopped) + return + self.fail("No exception caught") def test_run_go_offline(self): """Test main loop send offline presence when exiting"""