def test_atomiccmd__terminate_race_condition(temp_folder): cmd = AtomicCmd("true") cmd.run(temp_folder) while cmd._proc.poll() is None: pass cmd.terminate() assert_equal(cmd.join(), [0])
def test_atomiccmd__terminate_race_condition(tmp_path): cmd = AtomicCmd("true") cmd.run(tmp_path) while cmd._proc.poll() is None: pass cmd.terminate() assert cmd.join() == [0]
def test_atomiccmd__run__already_running(tmp_path): cmd = AtomicCmd(("sleep", "10")) cmd.run(tmp_path) with pytest.raises(CmdError): cmd.run(tmp_path) cmd.terminate() cmd.join()
def test_atomiccmd__commit_while_running(tmp_path): cmd = AtomicCmd(("sleep", "10")) cmd.run(tmp_path) with pytest.raises(CmdError): cmd.commit(tmp_path) cmd.terminate() cmd.join()
def test_atomiccmd__terminate(tmp_path): cmd = AtomicCmd(("sleep", "10")) cmd.run(tmp_path) with patch("os.killpg", wraps=os.killpg) as os_killpg: cmd.terminate() assert cmd.join() == ["SIGTERM"] assert os_killpg.mock_calls == [call(cmd._proc.pid, signal.SIGTERM)]
def test_pformat__simple__running__set_cwd(temp_folder): cmd = AtomicCmd(("sleep", "10"), set_cwd=True) cmd.run(temp_folder) assert_equal(pformat(cmd), ("<Command = ['sleep', '10']\n" " Status = Running ...\n" " STDOUT* = 'pipe_sleep_{id}.stdout'\n" " STDERR* = 'pipe_sleep_{id}.stderr'\n" " CWD = '{temp_dir}'>").format(id=id(cmd), temp_dir=temp_folder)) cmd.terminate() cmd.join()
def test_pformat__simple__running__set_cwd(tmp_path): cmd = AtomicCmd(("sleep", "10"), set_cwd=True) cmd.run(tmp_path) assert pformat(cmd) == ("Command = sleep 10\n" "Status = Running\n" "STDOUT* = 'pipe_sleep_{id}.stdout'\n" "STDERR* = 'pipe_sleep_{id}.stderr'\n" "CWD = '{temp_dir}'").format(id=id(cmd), temp_dir=tmp_path) cmd.terminate() cmd.join()
def test_pformat__simple__terminated_by_pipeline(tmp_path): cmd = AtomicCmd(("sleep", "10")) cmd.run(tmp_path) cmd.terminate() assert cmd.join() == ["SIGTERM"] assert pformat(cmd) == ("Command = sleep 10\n" "Status = Automatically terminated by PALEOMIX\n" "STDOUT* = '{temp_dir}/pipe_sleep_{id}.stdout'\n" "STDERR* = '{temp_dir}/pipe_sleep_{id}.stderr'\n" "CWD = '{cwd}'").format(id=id(cmd), temp_dir=tmp_path, cwd=os.getcwd())
def test_pformat__simple__killed(temp_folder): cmd = AtomicCmd(("sleep", "10")) cmd.run(temp_folder) cmd.terminate() assert_equal(cmd.join(), ["SIGTERM"]) assert_equal(pformat(cmd), ("<Command = ['sleep', '10']\n" " Status = Terminated with signal SIGTERM\n" " STDOUT* = '{temp_dir}/pipe_sleep_{id}.stdout'\n" " STDERR* = '{temp_dir}/pipe_sleep_{id}.stderr'\n" " CWD = '{cwd}'>").format(id=id(cmd), temp_dir=temp_folder, cwd=os.getcwd()))
def test_pformat__simple__running(temp_folder): cmd = AtomicCmd(("sleep", "10")) cmd.run(temp_folder) assert_equal(pformat(cmd), ("Command = sleep 10\n" "Status = Running ...\n" "STDOUT* = '{temp_dir}/pipe_sleep_{id}.stdout'\n" "STDERR* = '{temp_dir}/pipe_sleep_{id}.stderr'\n" "CWD = '{cwd}'").format(id=id(cmd), cwd=os.getcwd(), temp_dir=temp_folder)) cmd.terminate() cmd.join()
def test_pformat__simple__terminated_by_pipeline(temp_folder): cmd = AtomicCmd(("sleep", "10")) cmd.run(temp_folder) cmd.terminate() assert_equal(cmd.join(), ["SIGTERM"]) assert_equal(pformat(cmd), ("Command = sleep 10\n" "Status = Automatically terminated by PALEOMIX\n" "STDOUT* = '{temp_dir}/pipe_sleep_{id}.stdout'\n" "STDERR* = '{temp_dir}/pipe_sleep_{id}.stderr'\n" "CWD = '{cwd}'").format(id=id(cmd), temp_dir=temp_folder, cwd=os.getcwd()))
def test_atomiccmd__terminate_exception(tmp_path): killpg = os.killpg cmd = AtomicCmd(("sleep", "10")) cmd.run(tmp_path) def _killpg(pid, sig): killpg(pid, sig) raise OSError("Proccess not found") with patch("os.killpg", wraps=_killpg) as os_killpg: cmd.terminate() assert cmd.join() == ["SIGTERM"] assert os_killpg.mock_calls == [call(cmd._proc.pid, signal.SIGTERM)]
def test_atomicsets__terminate(cls): mock = Mock() cmd_1 = AtomicCmd(["ls"]) cmd_1.terminate = mock.terminate_1 cmd_2 = AtomicCmd(["ls"]) cmd_2.terminate = mock.terminate_2 cmd_3 = AtomicCmd(["ls"]) cmd_3.terminate = mock.terminate_3 cmds = cls((cmd_3, cmd_2, cmd_1)) cmds.terminate() assert mock.mock_calls == [ call.terminate_3(), call.terminate_2(), call.terminate_1(), ]
def _do_test_atomiccmd__terminate(temp_folder, raise_on_terminate): cmd = AtomicCmd(("sleep", "10")) cmd.run(temp_folder) killpg_was_called = [] def _wrap_killpg(pid, sig): assert_equal(pid, cmd._proc.pid) assert_equal(sig, signal.SIGTERM) killpg_was_called.append(True) if raise_on_terminate: raise OSError("KABOOM!") with Monkeypatch("os.killpg", _wrap_killpg): cmd.terminate() cmd.terminate() assert_equal(cmd.join(), ["SIGTERM"]) assert killpg_was_called
def test_atomiccmd__run__invalid_temp(tmp_path): cmd = AtomicCmd(("sleep", "10")) with pytest.raises(CmdError): cmd.run(os.path.join(tmp_path, "foo")) cmd.terminate() cmd.join()
def test_atomiccmd__terminate_after_join(temp_folder): cmd = AtomicCmd("true") cmd.run(temp_folder) assert_equal(cmd.join(), [0]) cmd.terminate() assert_equal(cmd.join(), [0])
def test_atomiccmd__run__exception_on_missing_command(tmp_path): cmd = AtomicCmd(("xyzabcefgh", "10")) with pytest.raises(CmdError): cmd.run(tmp_path) cmd.terminate() cmd.join()
def test_atomiccmd__run__exception_on_missing_command__no_wrap(tmp_path): cmd = AtomicCmd(("xyzabcefgh", "10")) with pytest.raises(OSError): cmd.run(tmp_path, wrap_errors=False) cmd.terminate() cmd.join()
def test_atomiccmd__commit_while_running(temp_folder): cmd = AtomicCmd(("sleep", "10")) cmd.run(temp_folder) assert_raises(CmdError, cmd.commit, temp_folder) cmd.terminate() cmd.join()
def test_atomiccmd__run__already_running(temp_files): cmd = AtomicCmd(("sleep", "10")) cmd.run(temp_files) assert_raises(CmdError, cmd.run, temp_files) cmd.terminate() cmd.join()
def test_atomiccmd__run__invalid_temp(temp_files): cmd = AtomicCmd(("sleep", "10")) assert_raises(CmdError, cmd.run, os.path.join(temp_files, "foo")) cmd.terminate() cmd.join()
def test_atomiccmd__terminate_sigterm(temp_folder): cmd = AtomicCmd(("sleep", "10")) cmd.run(temp_folder) cmd.terminate() assert_equal(cmd.join(), ["SIGTERM"])
def test_atomiccmd__terminate_sigterm(tmp_path): cmd = AtomicCmd(("sleep", "10")) cmd.run(tmp_path) cmd.terminate() assert cmd.join() == ["SIGTERM"]
def test_atomiccmd__run__exception_on_missing_command__no_wrap(temp_files): cmd = AtomicCmd(("xyzabcefgh", "10")) assert_raises(OSError, cmd.run, temp_files, wrap_errors=False) cmd.terminate() cmd.join()
def test_atomiccmd__terminate_after_join(tmp_path): cmd = AtomicCmd("true") cmd.run(tmp_path) assert cmd.join() == [0] cmd.terminate() assert cmd.join() == [0]
def test_atomiccmd__run__exception_on_missing_command(temp_files): cmd = AtomicCmd(("xyzabcefgh", "10")) assert_raises(CmdError, cmd.run, temp_files) cmd.terminate() cmd.join()