浏览代码

full test coverage for supervisord.py

Chris McDonough 10 年之前
父节点
当前提交
29458331f6
共有 3 个文件被更改,包括 82 次插入7 次删除
  1. 6 6
      supervisor/supervisord.py
  2. 3 0
      supervisor/tests/base.py
  3. 73 1
      supervisor/tests/test_supervisord.py

+ 6 - 6
supervisor/supervisord.py

@@ -314,7 +314,7 @@ def timeslice(period, when):
     return int(when - (when % period))
     return int(when - (when % period))
 
 
 # profile entry point
 # profile entry point
-def profile(cmd, globals, locals, sort_order, callers):
+def profile(cmd, globals, locals, sort_order, callers): # pragma: no cover
     try:
     try:
         import cProfile as profile
         import cProfile as profile
     except ImportError:
     except ImportError:
@@ -353,16 +353,16 @@ def main(args=None, test=False):
             go(options)
             go(options)
         if test or (options.mood < SupervisorStates.RESTARTING):
         if test or (options.mood < SupervisorStates.RESTARTING):
             break
             break
-        options.close_httpservers()
-        options.close_logger()
-        first = False
+        options.close_httpservers() # pragma: no cover
+        options.close_logger() # pragma: no cover
+        first = False # pragma: no cover
 
 
-def go(options):
+def go(options): # pragma: no cover
     d = Supervisor(options)
     d = Supervisor(options)
     try:
     try:
         d.main()
         d.main()
     except asyncore.ExitNow:
     except asyncore.ExitNow:
         pass
         pass
 
 
-if __name__ == "__main__":
+if __name__ == "__main__": # pragma: no cover
     main()
     main()

+ 3 - 0
supervisor/tests/base.py

@@ -1043,6 +1043,9 @@ class DummyProcessGroup(object):
     def __eq__(self, other):
     def __eq__(self, other):
         return self.config.priority == other.config.priority
         return self.config.priority == other.config.priority
 
 
+    def reopenlogs(self):
+        self.logs_reopened = True
+
 class DummyFCGIProcessGroup(DummyProcessGroup):
 class DummyFCGIProcessGroup(DummyProcessGroup):
 
 
     def __init__(self, config):
     def __init__(self, config):

+ 73 - 1
supervisor/tests/test_supervisord.py

@@ -142,6 +142,27 @@ class SupervisordTests(unittest.TestCase):
         supervisord.reap(once=True)
         supervisord.reap(once=True)
         self.assertEqual(process.finished, (1,1))
         self.assertEqual(process.finished, (1,1))
 
 
+    def test_reap_recursionguard(self):
+        options = DummyOptions()
+        supervisord = self._makeOne(options)
+        result = supervisord.reap(once=True, recursionguard=100)
+        self.assertEqual(result, None)
+
+    def test_reap_more_than_once(self):
+        options = DummyOptions()
+        options.waitpid_return = 1, 1
+        pconfig = DummyPConfig(options, 'process', 'process', '/bin/process1')
+        process = DummyProcess(pconfig)
+        process.drained = False
+        process.killing = 1
+        process.laststop = None
+        process.waitstatus = None, None
+        options.pidhistory = {1:process}
+        supervisord = self._makeOne(options)
+
+        supervisord.reap(recursionguard=99)
+        self.assertEqual(process.finished, (1,1))
+
     def test_reap_unknown_pid(self):
     def test_reap_unknown_pid(self):
         options = DummyOptions()
         options = DummyOptions()
         options.waitpid_return = 2, 0 # pid, status
         options.waitpid_return = 2, 0 # pid, status
@@ -219,11 +240,14 @@ class SupervisordTests(unittest.TestCase):
         options.process_group_configs = DummyPGroupConfig(
         options.process_group_configs = DummyPGroupConfig(
             options, 'foo',
             options, 'foo',
             pconfigs=pconfigs)
             pconfigs=pconfigs)
+        dummypgroup = DummyProcessGroup(options)
+        supervisord.process_groups = {None:dummypgroup}
         supervisord.handle_signal()
         supervisord.handle_signal()
         self.assertEqual(supervisord.options.mood, 1)
         self.assertEqual(supervisord.options.mood, 1)
         self.assertEqual(options.logs_reopened, True)
         self.assertEqual(options.logs_reopened, True)
         self.assertEqual(options.logger.data[0],
         self.assertEqual(options.logger.data[0],
                          'received SIGUSR2 indicating log reopen request')
                          'received SIGUSR2 indicating log reopen request')
+        self.assertEqual(dummypgroup.logs_reopened, True)
 
 
     def test_handle_unknown_signal(self):
     def test_handle_unknown_signal(self):
         options = DummyOptions()
         options = DummyOptions()
@@ -234,6 +258,12 @@ class SupervisordTests(unittest.TestCase):
         self.assertEqual(options.logger.data[0],
         self.assertEqual(options.logger.data[0],
                          'received SIGUSR1 indicating nothing')
                          'received SIGUSR1 indicating nothing')
 
 
+    def test_get_state(self):
+        from supervisor.states import SupervisorStates
+        options = DummyOptions()
+        supervisord = self._makeOne(options)
+        self.assertEqual(supervisord.get_state(), SupervisorStates.RUNNING)
+
     def test_diff_add_remove(self):
     def test_diff_add_remove(self):
         options = DummyOptions()
         options = DummyOptions()
         supervisord = self._makeOne(options)
         supervisord = self._makeOne(options)
@@ -467,7 +497,7 @@ class SupervisordTests(unittest.TestCase):
         self.assertEqual(writable.write_event_handled, True)
         self.assertEqual(writable.write_event_handled, True)
         self.assertEqual(error.error_handled, True)
         self.assertEqual(error.error_handled, True)
 
 
-    def test_runforever_select_dispatcher_exitnow(self):
+    def test_runforever_select_dispatcher_exitnow_via_read(self):
         options = DummyOptions()
         options = DummyOptions()
         options.poller.result = [6], []
         options.poller.result = [6], []
         supervisord = self._makeOne(options)
         supervisord = self._makeOne(options)
@@ -481,6 +511,48 @@ class SupervisordTests(unittest.TestCase):
         options.test = True
         options.test = True
         self.assertRaises(asyncore.ExitNow, supervisord.runforever)
         self.assertRaises(asyncore.ExitNow, supervisord.runforever)
 
 
+    def test_runforever_select_dispatcher_exitnow_via_write(self):
+        options = DummyOptions()
+        options.poller.result = [], [6]
+        supervisord = self._makeOne(options)
+        pconfig = DummyPConfig(options, 'foo', '/bin/foo',)
+        gconfig = DummyPGroupConfig(options, pconfigs=[pconfig])
+        pgroup = DummyProcessGroup(gconfig)
+        from supervisor.medusa import asyncore_25 as asyncore
+        exitnow = DummyDispatcher(readable=True, error=asyncore.ExitNow)
+        pgroup.dispatchers = {6:exitnow}
+        supervisord.process_groups = {'foo': pgroup}
+        options.test = True
+        self.assertRaises(asyncore.ExitNow, supervisord.runforever)
+
+    def test_runforever_select_dispatcher_handle_error_via_read(self):
+        options = DummyOptions()
+        options.poller.result = [6], []
+        supervisord = self._makeOne(options)
+        pconfig = DummyPConfig(options, 'foo', '/bin/foo',)
+        gconfig = DummyPGroupConfig(options, pconfigs=[pconfig])
+        pgroup = DummyProcessGroup(gconfig)
+        notimpl = DummyDispatcher(readable=True, error=NotImplementedError)
+        pgroup.dispatchers = {6:notimpl}
+        supervisord.process_groups = {'foo': pgroup}
+        options.test = True
+        supervisord.runforever()
+        self.assertEqual(notimpl.error_handled, True)
+
+    def test_runforever_select_dispatcher_handle_error_via_write(self):
+        options = DummyOptions()
+        options.poller.result = [], [6]
+        supervisord = self._makeOne(options)
+        pconfig = DummyPConfig(options, 'foo', '/bin/foo',)
+        gconfig = DummyPGroupConfig(options, pconfigs=[pconfig])
+        pgroup = DummyProcessGroup(gconfig)
+        notimpl = DummyDispatcher(readable=True, error=NotImplementedError)
+        pgroup.dispatchers = {6:notimpl}
+        supervisord.process_groups = {'foo': pgroup}
+        options.test = True
+        supervisord.runforever()
+        self.assertEqual(notimpl.error_handled, True)
+
     def test_runforever_stopping_emits_events(self):
     def test_runforever_stopping_emits_events(self):
         options = DummyOptions()
         options = DummyOptions()
         supervisord = self._makeOne(options)
         supervisord = self._makeOne(options)