1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
from __future__ import annotations
import os
import socket
import sys
from errno import ENODEV, EPERM, ESRCH
from pathlib import Path
from typing import TYPE_CHECKING
import pytest
from filelock import SoftFileLock
if TYPE_CHECKING:
from unittest.mock import MagicMock
from pytest_mock import MockerFixture
unix_only = pytest.mark.skipif(sys.platform == "win32", reason="unix-only stale lock detection")
def test_lock_writes_pid_and_hostname(tmp_path: Path) -> None:
lock_path = tmp_path / "test.lock"
lock = SoftFileLock(lock_path)
with lock:
content = lock_path.read_text(encoding="utf-8")
assert content == f"{os.getpid()}\n{socket.gethostname()}\n"
@unix_only
def test_stale_lock_broken_when_process_dead(tmp_path: Path, mocker: MockerFixture) -> None:
lock_path = tmp_path / "test.lock"
dead_pid = 2**22 + 1
lock_path.write_text(f"{dead_pid}\n{socket.gethostname()}\n", encoding="utf-8")
mocker.patch("filelock._soft.os.kill", side_effect=OSError(ESRCH, "No such process"))
lock = SoftFileLock(lock_path, timeout=1)
with lock:
assert lock.is_locked
@unix_only
def test_stale_lock_not_broken_when_process_alive(tmp_path: Path) -> None:
lock_path = tmp_path / "test.lock"
lock_path.write_text(f"{os.getpid()}\n{socket.gethostname()}\n", encoding="utf-8")
lock = SoftFileLock(lock_path, timeout=0.1)
with pytest.raises(TimeoutError):
lock.acquire()
@unix_only
def test_stale_lock_not_broken_different_hostname(tmp_path: Path) -> None:
lock_path = tmp_path / "test.lock"
dead_pid = 2**22 + 1
lock_path.write_text(f"{dead_pid}\nother-host.example.com\n", encoding="utf-8")
lock = SoftFileLock(lock_path, timeout=0.1)
with pytest.raises(TimeoutError):
lock.acquire()
@unix_only
def test_stale_lock_not_broken_when_eperm(tmp_path: Path, mocker: MockerFixture) -> None:
lock_path = tmp_path / "test.lock"
lock_path.write_text(f"{99999}\n{socket.gethostname()}\n", encoding="utf-8")
mocker.patch("filelock._soft.os.kill", side_effect=OSError(EPERM, "Operation not permitted"))
lock = SoftFileLock(lock_path, timeout=0.1)
with pytest.raises(TimeoutError):
lock.acquire()
@unix_only
def test_stale_lock_empty_file_ignored(tmp_path: Path) -> None:
lock_path = tmp_path / "test.lock"
lock_path.write_text("", encoding="utf-8")
lock = SoftFileLock(lock_path, timeout=0.1)
with pytest.raises(TimeoutError):
lock.acquire()
@unix_only
def test_stale_lock_malformed_content_ignored(tmp_path: Path) -> None:
lock_path = tmp_path / "test.lock"
lock_path.write_text("not-a-pid\n", encoding="utf-8")
lock = SoftFileLock(lock_path, timeout=0.1)
with pytest.raises(TimeoutError):
lock.acquire()
@unix_only
def test_stale_lock_rename_race(tmp_path: Path, mocker: MockerFixture) -> None:
lock_path = tmp_path / "test.lock"
dead_pid = 2**22 + 1
lock_path.write_text(f"{dead_pid}\n{socket.gethostname()}\n", encoding="utf-8")
mocker.patch("filelock._soft.os.kill", side_effect=OSError(ESRCH, "No such process"))
mocker.patch.object(Path, "rename", side_effect=FileNotFoundError("already gone"))
lock = SoftFileLock(lock_path, timeout=0.1)
with pytest.raises(TimeoutError):
lock.acquire()
@unix_only
def test_stale_lock_unexpected_kill_error_suppressed(tmp_path: Path, mocker: MockerFixture) -> None:
lock_path = tmp_path / "test.lock"
lock_path.write_text(f"{99999}\n{socket.gethostname()}\n", encoding="utf-8")
mocker.patch("filelock._soft.os.kill", side_effect=OSError(ENODEV, "No such device"))
lock = SoftFileLock(lock_path, timeout=0.1)
with pytest.raises(TimeoutError):
lock.acquire()
@unix_only
def test_stale_detection_errors_suppressed(tmp_path: Path, mocker: MockerFixture) -> None:
lock_path = tmp_path / "test.lock"
lock_path.write_text(f"{os.getpid()}\n{socket.gethostname()}\n", encoding="utf-8")
mock_read: MagicMock = mocker.patch.object(Path, "read_text", side_effect=OSError("read failed"))
lock = SoftFileLock(lock_path, timeout=0.1)
with pytest.raises(TimeoutError):
lock.acquire()
mock_read.assert_called()
def test_write_lock_info_errors_suppressed(tmp_path: Path, mocker: MockerFixture) -> None:
lock_path = tmp_path / "test.lock"
mocker.patch("filelock._soft.os.write", side_effect=OSError("write failed"))
lock = SoftFileLock(lock_path)
with lock:
assert lock.is_locked
assert not lock_path.read_text(encoding="utf-8")
|