dnd-music-console/test/test_pidfile.py

36 lines
1013 B
Python
Raw Normal View History

2024-03-10 12:08:45 -07:00
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from croaker import pidfile
2024-03-26 00:51:16 -07:00
@pytest.mark.parametrize(
"pid,terminate,kill_result,broken",
[
("pid", False, None, False), # running proc, no terminate
("pid", True, True, False), # running proc, terminate
("pid", True, ProcessLookupError, True), # stale pid
(None, None, None, False), # no running proc
],
)
2024-03-10 12:08:45 -07:00
def test_pidfile(monkeypatch, pid, terminate, kill_result, broken):
2024-03-26 00:51:16 -07:00
monkeypatch.setattr(
pidfile._pidfile,
"TimeoutPIDLockFile",
MagicMock(
**{
"return_value.read_pid.return_value": pid,
}
),
)
monkeypatch.setattr(
pidfile.os,
"kill",
MagicMock(**{"side_effect": kill_result if type(kill_result) is Exception else [kill_result]}),
)
2024-03-10 12:08:45 -07:00
2024-03-26 00:51:16 -07:00
ret = pidfile.pidfile(pidfile_path=Path("/dev/null"), terminate_if_running=terminate)
2024-03-10 12:08:45 -07:00
assert ret.break_lock.called == broken