Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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()
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_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_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
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)