Restart when Jabber server close the connection

darcs-hash:20080821200041-86b55-3564edeb58a6582efd4325c938ae7a9315921082.gz
This commit is contained in:
David Rousselie
2008-08-21 22:00:41 +02:00
parent c7aa435502
commit aab567d4b8
2 changed files with 26 additions and 2 deletions

View File

@@ -673,6 +673,7 @@ class JCLComponent(Component, object):
timer_thread = threading.Thread(target=self.time_handler,
name="TimerThread")
timer_thread.start()
wait_before_restart = 0
try:
try:
while (self.running and self.stream
@@ -685,6 +686,9 @@ class JCLComponent(Component, object):
self.__logger.info("Connection failed, restarting.")
return (True, 5)
finally:
if self.running:
self._restart = True
wait_before_restart = 5
self.running = False
timer_thread.join(JCLComponent.timeout)
self.wait_event.set()
@@ -694,7 +698,7 @@ class JCLComponent(Component, object):
self.send_stanzas(presences)
self.disconnect()
self.__logger.debug("Exitting normally")
return (self._restart, 0)
return (self._restart, wait_before_restart)
def _get_restart(self):
return self._restart

View File

@@ -2967,7 +2967,7 @@ class JCLComponent_run_TestCase(JCLComponent_TestCase):
raise self.comp.queue.get(0)
def test_run_connection_failed(self):
"""Test when connection to Jabber server failed"""
"""Test when connection to Jabber server fails"""
class MockStreamLoopFailed(MockStream):
def connect(self):
self.connection_started = True
@@ -2988,6 +2988,26 @@ class JCLComponent_run_TestCase(JCLComponent_TestCase):
self.assertEquals(len(threads), 1)
self.assertFalse(self.comp.stream.connection_stopped)
def test_run_connection_closed(self):
"""Test when connection to Jabber server is closed"""
def do_nothing():
self.comp.stream.eof = True
return
self.comp.handle_tick = do_nothing
self.comp.time_unit = 1
# Do not loop, handle_tick is virtual
self.comp.stream = MockStreamNoConnect()
self.comp.stream_class = MockStreamNoConnect
self.comp.restart = False
(result, time_to_wait) = self.comp.run()
self.assertEquals(time_to_wait, 5)
self.assertTrue(result)
self.assertFalse(self.comp.running)
self.assertTrue(self.comp.stream.connection_started)
threads = threading.enumerate()
self.assertEquals(len(threads), 1)
self.assertFalse(self.comp.stream.connection_stopped)
def test_run_unhandled_error(self):
"""Test main loop unhandled error from a component handler"""
def do_nothing():