|
6 | 6 |
|
7 | 7 |
|
8 | 8 | class UpdateHelperTestCase(unittest.TestCase): |
| 9 | + class blockingObject(object): |
| 10 | + terminated = False |
| 11 | + timeout = None |
| 12 | + |
| 13 | + def __init__(self, timeout): |
| 14 | + self.timeout = timeout |
| 15 | + |
| 16 | + def wait(self): |
| 17 | + startTime = time.time() |
| 18 | + # block until waiting == true or we've hit the timeout |
| 19 | + while self.terminated == False and time.time() < startTime + self.timeout: |
| 20 | + time.sleep(0.5) |
| 21 | + |
| 22 | + def poll(self): |
| 23 | + return None |
| 24 | + |
| 25 | + def terminate(self): |
| 26 | + self.terminated = True |
| 27 | + |
| 28 | + pass |
| 29 | + |
9 | 30 | class Object(object): |
10 | 31 | pass |
11 | 32 |
|
12 | | - @mock.patch("haproxy.helper.update_helper.thread.start_new_thread") |
13 | 33 | @mock.patch("haproxy.helper.update_helper.subprocess.Popen") |
14 | | - def test_run_reload_with_old_process(self, mock_popen, mock_new_thread): |
| 34 | + def test_run_graceful_reload_within_timeout(self, mock_popen): |
| 35 | + old_process = UpdateHelperTestCase.blockingObject(2) |
| 36 | + old_process.pid = "old_pid" |
| 37 | + new_process = UpdateHelperTestCase.Object() |
| 38 | + new_process.pid = "new_pid" |
| 39 | + mock_popen.return_value = new_process |
| 40 | + run_reload(old_process, 5) |
| 41 | + self.assertFalse(old_process.terminated) |
| 42 | + |
| 43 | + @mock.patch("haproxy.helper.update_helper.subprocess.Popen") |
| 44 | + def test_run_graceful_reload_exceeding_timeout(self, mock_popen): |
| 45 | + old_process = UpdateHelperTestCase.blockingObject(10) |
| 46 | + old_process.pid = "old_pid" |
| 47 | + new_process = UpdateHelperTestCase.Object() |
| 48 | + new_process.pid = "new_pid" |
| 49 | + mock_popen.return_value = new_process |
| 50 | + run_reload(old_process, 5) |
| 51 | + self.assertTrue(old_process.terminated) |
| 52 | + |
| 53 | + @mock.patch("haproxy.helper.update_helper.threading.Thread") |
| 54 | + @mock.patch("haproxy.helper.update_helper.subprocess.Popen") |
| 55 | + def test_run_graceful_reload_with_old_process(self, mock_popen, mock_new_thread): |
| 56 | + old_process = UpdateHelperTestCase.Object() |
| 57 | + old_process.pid = "old_pid" |
| 58 | + new_process = UpdateHelperTestCase.Object() |
| 59 | + new_process.pid = "new_pid" |
| 60 | + mock_popen.return_value = new_process |
| 61 | + run_reload(old_process, 0) |
| 62 | + mock_popen.assert_called_with(HAPROXY_RUN_COMMAND + ['-sf', 'old_pid']) |
| 63 | + mock_new_thread.assert_called_with(target=wait_pid, args=[old_process, 0]) |
| 64 | + |
| 65 | + @mock.patch("haproxy.helper.update_helper.threading.Thread") |
| 66 | + @mock.patch("haproxy.helper.update_helper.subprocess.Popen") |
| 67 | + def test_run_brutal_reload_with_old_process(self, mock_popen, mock_new_thread): |
15 | 68 | old_process = UpdateHelperTestCase.Object() |
16 | | - old_process.pid = "pid" |
| 69 | + old_process.pid = "old_pid" |
17 | 70 | new_process = UpdateHelperTestCase.Object() |
18 | 71 | new_process.pid = "new_pid" |
19 | 72 | mock_popen.return_value = new_process |
20 | | - mock_popen.return_value = old_process |
21 | | - run_reload(old_process) |
22 | | - mock_popen.assert_called_with(HAPROXY_RUN_COMMAND + ['-sf', 'pid']) |
23 | | - mock_new_thread.caslled_with(wait_pid, (old_process,)) |
| 73 | + run_reload(old_process, -1) |
| 74 | + mock_popen.assert_called_with(HAPROXY_RUN_COMMAND + ['-st', 'old_pid']) |
| 75 | + mock_new_thread.assert_called_with(target=wait_pid, args=[old_process, -1]) |
24 | 76 |
|
25 | | - @mock.patch("haproxy.helper.update_helper.thread.start_new_thread") |
| 77 | + @mock.patch("haproxy.helper.update_helper.threading.Thread") |
26 | 78 | @mock.patch("haproxy.helper.update_helper.subprocess.Popen") |
27 | 79 | def test_run_reload_with_empty_old_process(self, mock_popen, mock_new_thread): |
28 | 80 | new_process = UpdateHelperTestCase.Object() |
|
0 commit comments