def test_os_exit_didnt_call_when_os_fork_returned_zero_both_times(self): pid = 0 with mock.patch('os.fork', mock.Mock(return_value=pid)): with mock.patch('os.setsid'): with mock.patch('os._exit') as os_exit_mock: utils.daemonize() self.assertEqual(0, os_exit_mock.call_count)
def test_daemonize_child_parent(self): with mock.patch('os.fork', mock.Mock(side_effect=[0, 42])): with mock.patch('os._exit', mock.Mock()) as os_exit: with mock.patch('os.setsid', mock.Mock()): utils.daemonize() os_exit.assert_called_once_with(0)
def test_daemonize_from_child_unsuccess(self, m_exit): pid = 0 with mock.patch('os.fork', mock.Mock(return_value=pid)) as m_fork: with mock.patch('os.setsid', mock.Mock()) as m_setsid: daemonize() m_setsid.assert_called_once_with() self.assertEqual(m_fork.call_count, 2) self.assertFalse(m_exit.called)
def test_os_exit_called_once_when_os_fork_returned_zero_first_time_and_os_fork_return_valid_pid_second_time(self): invalid_pid = 0 valid_pid = 1 with mock.patch('os.fork', mock.Mock(side_effect=[invalid_pid, valid_pid])): with mock.patch('os.setsid'): with mock.patch('os._exit') as os_exit_mock: utils.daemonize() os_exit_mock.assert_called_once_with(0)
def test_daemonize_parent(self): test_pid = 42 with mock.patch('os.fork', mock.Mock(return_value=test_pid)): with mock.patch('os._exit', mock.Mock()) as os_exit: utils.daemonize() os_exit.assert_called_once_with(0)
def test_daemonize_pid_not_0(self): """ pid всегда != 0. os_exit должен вызваться :return: """ with mock.patch('source.lib.utils.try_fork', mock.Mock(return_value=100)): with mock.patch('os._exit', mock.Mock()) as exit: utils.daemonize() assert exit.called
def test_daemonize_pid_not_0_second_time(self): """ pid всегда = 0 при первом вызове и != 0 при втором. os_exit должен вызваться :return: """ with mock.patch('source.lib.utils.try_fork', mock.Mock(side_effect=[0, 1])): with mock.patch('os._exit', mock.Mock()) as exit: utils.daemonize() assert exit.called
def _daemonize(self, m_setsid=Mock(), m_exit=Mock(), m_fork=Mock(return_value=42)): with patch('os.fork', m_fork), patch('os._exit', m_exit) as mock_os_exit,\ mock.patch('os.setsid', m_setsid): utils.daemonize() return mock_os_exit
def test_daemonize_from_child_success(self, m_exit): master_pid = 0 child_pid = 1 with mock.patch('os.fork', mock.Mock(side_effect=[master_pid, child_pid])) as m_fork: with mock.patch('os.setsid', mock.Mock()) as m_setsid: daemonize() m_setsid.assert_called_once_with() self.assertEqual(m_fork.call_count, 2) m_exit.assert_called_once_with(0)
def test_daemonize_parent(self): pid = 12 mock_os_fork = mock.Mock(return_value=pid) mock_os_exit = mock.Mock() with mock.patch('source.notification_pusher.os.fork', mock_os_fork): with mock.patch('source.notification_pusher.os._exit', mock_os_exit): utils.daemonize() mock_os_exit.assert_called_once_with(0) mock_os_fork.assert_called_once_with()
def test_daemonize_parent(self): pid = 12345 with mock.patch('os.fork', mock.Mock(return_value=pid)) as os_fork: with mock.patch('os.setsid', mock.Mock()) as os_setsid: with mock.patch('os._exit', mock.Mock()) as os_exit: utils.daemonize() self.assertEqual(os_fork.call_count, 1) os_exit.assert_called_once_with(0) self.assertEqual(os_setsid.call_count, 0) pass
def test_daemonize_child(self): pid = 12345 with mock.patch('os.fork', mock.Mock(side_effect=[0,pid])) as os_fork: with mock.patch('os.setsid', mock.Mock()) as os_setsid: with mock.patch('os._exit', mock.Mock()) as os_exit: utils.daemonize(); os_exit.assert_called_once_with(0) self.assertEqual(os_fork.call_count, 2) self.assertEqual(os_setsid.call_count, 1) pass
def test_daemonize_parent(self): pid = 12345 with mock.patch('os.fork', mock.Mock(return_value=pid)) as os_fork: with mock.patch('os.setsid', mock.Mock()) as os_setsid: with mock.patch('os._exit', mock.Mock()) as os_exit: utils.daemonize(); self.assertEqual(os_fork.call_count, 1) os_exit.assert_called_once_with(0) self.assertEqual(os_setsid.call_count, 0) pass
def test_daemonize_child_child(self): pid = 0 with mock.patch('os.fork', mock.Mock(return_value=pid)) as os_fork: with mock.patch('os.setsid', mock.Mock()) as os_setsid: with mock.patch('os._exit', mock.Mock()) as os_exit: utils.daemonize(); self.assertEqual(os_fork.call_count, 2) self.assertEqual(os_exit.call_count, 0) self.assertEqual(os_setsid.call_count, 1) pass
def test_daemonize_child(self): pid = 12345 with mock.patch('os.fork', mock.Mock(side_effect=[0, pid])) as os_fork: with mock.patch('os.setsid', mock.Mock()) as os_setsid: with mock.patch('os._exit', mock.Mock()) as os_exit: utils.daemonize() os_exit.assert_called_once_with(0) self.assertEqual(os_fork.call_count, 2) self.assertEqual(os_setsid.call_count, 1) pass
def test_daemonize_child_child(self): pid = 0 with mock.patch('os.fork', mock.Mock(return_value=pid)) as os_fork: with mock.patch('os.setsid', mock.Mock()) as os_setsid: with mock.patch('os._exit', mock.Mock()) as os_exit: utils.daemonize() self.assertEqual(os_fork.call_count, 2) self.assertEqual(os_exit.call_count, 0) self.assertEqual(os_setsid.call_count, 1) pass
def test_daemonize_child(self): test_pid = 0 with mock.patch('os.fork', mock.Mock(return_value=test_pid)) as os_fork: with mock.patch('os._exit', mock.Mock()) as os_exit: with mock.patch('os.setsid', mock.Mock()) as os_setsid: utils.daemonize() os_setsid.assert_called_once_with() self.assertTrue(os_fork.called, 'fork() has not been called') self.assertFalse(os_exit.called, 'exit should not have been called')
def test_daemonize_child_parent(self): pid = 0 mock_os_fork = mock.Mock(side_effect=[pid, 12]) mock_os_exit = mock.Mock() mock_os_setsid = mock.Mock() with mock.patch('source.notification_pusher.os.fork', mock_os_fork): with mock.patch('source.notification_pusher.os._exit', mock_os_exit): with mock.patch('source.notification_pusher.os.setsid', mock_os_setsid): utils.daemonize() mock_os_exit.assert_called_once_with(0) mock_os_setsid.assert_called_once_with() self.assertTrue(mock_os_fork.call_count, 2)
def test_daemonize_child_child(self): pid = 0 mock_os_fork = mock.Mock(return_value=pid) mock_os_exit = mock.Mock() mock_os_setsid = mock.Mock() with mock.patch('source.notification_pusher.os.fork', mock_os_fork): with mock.patch('source.notification_pusher.os._exit', mock_os_exit): with mock.patch('source.notification_pusher.os.setsid', mock_os_setsid): utils.daemonize() self.assertFalse(mock_os_exit.called) mock_os_setsid.assert_called_once_with() self.assertTrue(mock_os_fork.call_count, 2)
def main(argv): """ Точка входа в приложение. В случае возникновения ошибки в приложении, оно засыпает на config.SLEEP_ON_FAIL секунд. :param argv: агрументы командной строки. :type argv: list """ args = parse_cmd_args(argv[1:]) if args.daemon: daemonize() if args.pidfile: create_pidfile(args.pidfile) config = load_config_from_pyfile( os.path.realpath(os.path.expanduser(args.config)) ) patch_all() dictConfig(config.LOGGING) current_thread().name = 'pusher.main' install_signal_handlers() while run: try: main_loop(config) except Exception as exc: logger.error( 'Error in main loop. Go to sleep on {} second(s).'.format(config.SLEEP_ON_FAIL) ) logger.exception(exc) sleep(config.SLEEP_ON_FAIL) logger.info('Stop application loop in main.') return exit_code
def test_os_fork_called_two_times_when_os_fork_returned_zero_first_time(self): pid = 0 with mock.patch('os.fork', mock.Mock(return_value=pid)) as os_fork_mock: with mock.patch('os.setsid'): utils.daemonize() self.assertEqual(2, os_fork_mock.call_count)
def test_daemonize_from_master(self, m_exit): pid = 1 with mock.patch('os.fork', mock.Mock(return_value=pid)): daemonize() m_exit.assert_called_once_with(0)
def test_os_setsid_called_when_os_fork_returned_zero_first_time(self): pid = 0 with mock.patch('os.fork', mock.Mock(return_value=pid)): with mock.patch('os.setsid') as os_setsid_mock: utils.daemonize() os_setsid_mock.assert_calls_once_with()
def test_os_exit_called_once_when_os_fork_returned_valid_pid_first_time(self): pid = 1 with mock.patch('os.fork', mock.Mock(return_value=pid)): with mock.patch('os._exit') as os_exit_mock: utils.daemonize() os_exit_mock.assert_called_once_with(0)