def test_stopping_children_of_stopped_process(): """ Check that children exiting between listing and killing are ignored. Given: Executor is running and it's process spawn children, and we requested it's stop, and it's stopped When: At the time of the check for subprocesses they're still active, but before we start killing them, they are already dead. Then: We ignore and skip OsError indicates there's no such process. """ # pylint: disable=protected-access, missing-docstring def raise_os_error(*_, **__): os_error = OSError() os_error.errno = errno.ESRCH raise os_error def processes_with_env_mock(*_, **__): return [1] with patch("mirakuru.base.processes_with_env", new=processes_with_env_mock), patch("os.kill", new=raise_os_error): executor = SimpleExecutor(SLEEP_300) executor._kill_all_kids(executor._stop_signal)
def test_stopping_children_of_stopped_process(): """ Check that children exiting between listing and killing are ignored. Given: Executor is running and it's process spawn children, and we requested it's stop, and it's stopped When: At the time of the check for subprocesses they're still active, but before we start killing them, they are already dead. Then: We ignore and skip OsError indicates there's no such process. """ # pylint: disable=protected-access, missing-docstring def raise_os_error(*_, **__): os_error = OSError() os_error.errno = errno.ESRCH raise os_error def processes_with_env_mock(*_, **__): return [1] with patch( 'mirakuru.base.processes_with_env', new=processes_with_env_mock ), patch('os.kill', new=raise_os_error): executor = SimpleExecutor(SLEEP_300) executor._kill_all_kids(executor._sig_stop)
def test_stopping_children_of_stopped_process(): """ Check that children exiting between listing and killing are ignored. Given: Executor is running and it's process spawn children, and we requested it's stop, and it's stopped When: At the time of the check for subprocesses they're still active, but before we start killing them, they are already dead. Then: We ignore and skip OsError indicates there's no such process. """ def raise_os_error(*args, **kwargs): os_error = OSError() os_error.errno = errno.ESRCH raise os_error def processes_with_env_mock(*args, **kwargs): return [1] with patch('mirakuru.base.processes_with_env', new=processes_with_env_mock), patch('os.kill', new=raise_os_error): executor = SimpleExecutor(sleep_300) executor._kill_all_kids(executor._sig_stop)
def test_kill_custom_signal_kill(): """Start process and shuts it down using signal SIGQUIT passed to kill.""" executor = SimpleExecutor(SLEEP_300) executor.start() assert executor.running() is True executor.kill(sig=signal.SIGQUIT) assert executor.running() is False
def test_custom_signal_kill(): """Start process and shuts it down using signal SIGQUIT.""" executor = SimpleExecutor(sleep_300, sig_kill=signal.SIGQUIT) executor.start() assert executor.running() is True executor.kill() assert executor.running() is False
def test_already_closed(): """Check that the executor cleans after itself after it exited earlier.""" with SimpleExecutor('python') as executor: assert executor.running() os.killpg(executor.process.pid, SIGKILL) def process_stopped(): """Return True only only when self.process is not running.""" return executor.running() is False executor.wait_for(process_stopped) assert executor.process assert not executor.process
def test_already_closed(): """Check that the executor cleans after itself after it exited earlier.""" with pytest.raises(ProcessFinishedWithError) as excinfo: with SimpleExecutor('python') as executor: assert executor.running() os.killpg(executor.process.pid, SIGKILL) def process_stopped(): """Return True only only when self.process is not running.""" return executor.running() is False executor.wait_for(process_stopped) assert executor.process assert excinfo.value.exit_code == -9 assert not executor.process
def test_daemons_killing(): """ Test if all subprocesses of SimpleExecutor can be killed. The most problematic subprocesses are daemons or other services that change the process group ID. This test verifies that daemon process is killed after executor's kill(). """ executor = SimpleExecutor(('python', SAMPLE_DAEMON_PATH), shell=True) executor.start() time.sleep(2) assert executor.running() is not True, \ "Executor should not have subprocess running as it started a daemon." assert SAMPLE_DAEMON_PATH in ps_aux() executor.kill() assert SAMPLE_DAEMON_PATH not in ps_aux()