From b21be478e40e16e17d64a61c524d7c5458c77e8d Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 18 Aug 2019 16:03:30 +0100 Subject: [PATCH] issue #627: add test and tweak Reaper behaviour. --- mitogen/parent.py | 4 ++-- tests/reaper_test.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/reaper_test.py diff --git a/mitogen/parent.py b/mitogen/parent.py index 6e99bb66..1c3e1874 100644 --- a/mitogen/parent.py +++ b/mitogen/parent.py @@ -2607,9 +2607,9 @@ class Reaper(object): if not self.kill: pass - elif self._tries == 1: + elif self._tries == 2: self._signal_child(signal.SIGTERM) - elif self._tries == 5: # roughly 4 seconds + elif self._tries == 6: # roughly 4 seconds self._signal_child(signal.SIGKILL) diff --git a/tests/reaper_test.py b/tests/reaper_test.py new file mode 100644 index 00000000..e78fdbf2 --- /dev/null +++ b/tests/reaper_test.py @@ -0,0 +1,54 @@ + +import signal +import unittest2 +import testlib +import mock + +import mitogen.parent + + +class ReaperTest(testlib.TestCase): + @mock.patch('os.kill') + def test_calc_delay(self, kill): + broker = mock.Mock() + proc = mock.Mock() + proc.poll.return_value = None + reaper = mitogen.parent.Reaper(broker, proc, True, True) + self.assertEquals(50, int(1000 * reaper._calc_delay(0))) + self.assertEquals(86, int(1000 * reaper._calc_delay(1))) + self.assertEquals(147, int(1000 * reaper._calc_delay(2))) + self.assertEquals(254, int(1000 * reaper._calc_delay(3))) + self.assertEquals(437, int(1000 * reaper._calc_delay(4))) + self.assertEquals(752, int(1000 * reaper._calc_delay(5))) + self.assertEquals(1294, int(1000 * reaper._calc_delay(6))) + + @mock.patch('os.kill') + def test_reap_calls(self, kill): + broker = mock.Mock() + proc = mock.Mock() + proc.poll.return_value = None + + reaper = mitogen.parent.Reaper(broker, proc, True, True) + + reaper.reap() + self.assertEquals(0, kill.call_count) + + reaper.reap() + self.assertEquals(1, kill.call_count) + + reaper.reap() + reaper.reap() + reaper.reap() + self.assertEquals(1, kill.call_count) + + reaper.reap() + self.assertEquals(2, kill.call_count) + + self.assertEquals(kill.mock_calls, [ + mock.call(proc.pid, signal.SIGTERM), + mock.call(proc.pid, signal.SIGKILL), + ]) + + +if __name__ == '__main__': + unittest2.main()