| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import time |
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from filelock import SoftFileLease, Timeout |
| 10 |
from filelock._identity import process_start_token |
| 11 |
from filelock._marker import OwnerRecord, encode_marker |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
from pytest_mock import MockerFixture |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.parametrize( |
| 20 |
"record", |
| 21 |
[ |
| 22 |
pytest.param("filelock/1\npid=1\nhost=h\nmode=lease\n", id="wrong-protocol"), |
| 23 |
pytest.param("filelock/2\npid=1\nhost=h\n", id="no-mode"), |
| 24 |
pytest.param("filelock/2\npid=1\nmode=lease\n", id="no-host"), |
| 25 |
pytest.param("filelock/2\npid=1\nhost=\nmode=lease\n", id="empty-host"), |
| 26 |
pytest.param("filelock/2\nhost=h\nmode=lease\n", id="no-pid"), |
| 27 |
pytest.param("filelock/2\npid=nine\nhost=h\nmode=lease\n", id="pid-not-a-number"), |
| 28 |
pytest.param("filelock/2\npid=0\nhost=h\nmode=lease\n", id="pid-below-range"), |
| 29 |
pytest.param("filelock/2\npid=99999999999\nhost=h\nmode=lease\n", id="pid-above-range"), |
| 30 |
pytest.param("filelock/2\npid=1\nhost=h\nmode=lease\nbare-line\n", id="field-without-value"), |
| 31 |
pytest.param("filelock/2\npid=1\nhost=h\nmode=lease\nduration=5\n", id="lease-without-token"), |
| 32 |
pytest.param("filelock/2\npid=1\nhost=h\nmode=lease\ntoken=t\n", id="lease-without-duration"), |
| 33 |
pytest.param("filelock/2\npid=1\nhost=h\nmode=lease\ntoken=t\nduration=0\n", id="lease-duration-zero"), |
| 34 |
pytest.param( |
| 35 |
"filelock/2\npid=1\nhost=h\nmode=lease\ntoken=t\nduration=soon\n", id="lease-duration-not-a-number" |
| 36 |
), |
| 37 |
pytest.param("filelock/2\npid=1\nhost=h\nmode=lease\ntoken=t\nduration=nan\n", id="lease-duration-nan"), |
| 38 |
pytest.param("filelock/2\npid=1\nhost=h\nmode=lease\ntoken=t\nduration=inf\n", id="lease-duration-inf"), |
| 39 |
pytest.param("filelock/2\npid=1\nhost=h\nmode=lease\ntoken=t\nduration=5\nstart=later\n", id="start-nan"), |
| 40 |
], |
| 41 |
) |
| 42 |
def test_unreadable_record_names_no_owner(tmp_path: Path, record: str) -> None: |
| 43 |
marker = tmp_path / "a.lock" |
| 44 |
marker.write_text(record, encoding="utf-8") |
| 45 |
|
| 46 |
assert SoftFileLease(str(marker), lease_duration=1).owner is None |
| 47 |
|
| 48 |
|
| 49 |
def test_encode_marker_omits_absent_lease_fields() -> None: |
| 50 |
# A non-lease owner carries no token or duration, so those lines never appear in the rendered marker. |
| 51 |
rendered = encode_marker(OwnerRecord(pid=7, hostname="h", mode="unknown")) |
| 52 |
|
| 53 |
assert rendered == b"filelock/2\npid=7\nhost=h\nmode=unknown\n" |
| 54 |
assert b"token=" not in rendered |
| 55 |
assert b"duration=" not in rendered |
| 56 |
|
| 57 |
|
| 58 |
def test_record_start_token_reads_back(tmp_path: Path) -> None: |
| 59 |
marker = tmp_path / "a.lock" |
| 60 |
record = "filelock/2\npid=4242\nhost=somehost\nmode=lease\ntoken=t\nduration=5\nstart=987654\n" |
| 61 |
marker.write_text(record, encoding="utf-8") |
| 62 |
|
| 63 |
owner = SoftFileLease(str(marker), lease_duration=1).owner |
| 64 |
|
| 65 |
assert owner is not None |
| 66 |
assert (owner.pid, owner.hostname, owner.mode, owner.start) == (4242, "somehost", "lease", 987654) |
| 67 |
|
| 68 |
|
| 69 |
def test_record_from_a_newer_filelock_still_names_its_owner(tmp_path: Path) -> None: |
| 70 |
# An unknown key is a field this version does not model yet; the owner it publishes must still read back. |
| 71 |
marker = tmp_path / "a.lock" |
| 72 |
record = "filelock/2\npid=4242\nhost=somehost\nmode=lease\ntoken=t\nduration=5\nstart-id=17\n" |
| 73 |
marker.write_text(record, encoding="utf-8") |
| 74 |
|
| 75 |
owner = SoftFileLease(str(marker), lease_duration=1).owner |
| 76 |
|
| 77 |
assert owner is not None |
| 78 |
assert (owner.pid, owner.hostname, owner.mode) == (4242, "somehost", "lease") |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.parametrize("published", ["strict", "banana"]) |
| 82 |
def test_record_with_an_unknown_mode_names_its_owner(tmp_path: Path, published: str) -> None: |
| 83 |
# A mode this version does not implement states a contract a newer filelock published, not a corrupt record. |
| 84 |
marker = tmp_path / "a.lock" |
| 85 |
marker.write_text(f"filelock/2\npid=4242\nhost=somehost\nmode={published}\n", encoding="utf-8") |
| 86 |
|
| 87 |
owner = SoftFileLease(str(marker), lease_duration=1).owner |
| 88 |
|
| 89 |
assert owner is not None |
| 90 |
assert (owner.pid, owner.hostname, owner.mode) == (4242, "somehost", "unknown") |
| 91 |
|
| 92 |
|
| 93 |
def test_held_lease_records_the_process_start_token(tmp_path: Path) -> None: |
| 94 |
marker = tmp_path / "a.lock" |
| 95 |
with SoftFileLease(str(marker), lease_duration=5) as lease: |
| 96 |
assert lease.owner is not None |
| 97 |
assert lease.owner.start == process_start_token(os.getpid()) |
| 98 |
|
| 99 |
|
| 100 |
def test_marker_without_start_token_omits_it(tmp_path: Path, mocker: MockerFixture) -> None: |
| 101 |
# A platform without a proven start time publishes no start field, and the record still reads back cleanly. |
| 102 |
mocker.patch("filelock._marker.process_start_token", return_value=None) |
| 103 |
marker = tmp_path / "a.lock" |
| 104 |
with SoftFileLease(str(marker), lease_duration=5) as lease: |
| 105 |
assert lease.owner is not None |
| 106 |
assert lease.owner.start is None |
| 107 |
assert "start=" not in marker.read_text(encoding="utf-8") |
| 108 |
|
| 109 |
|
| 110 |
def test_lease_treats_an_unreadable_record_as_contention(tmp_path: Path) -> None: |
| 111 |
# Only a peer that published a lease agreed to be superseded by age, so an unreadable record is never reclaimed. |
| 112 |
marker = tmp_path / "a.lock" |
| 113 |
marker.write_text("filelock/2\npid=1\nhost=h\nmode=banana\n", encoding="utf-8") |
| 114 |
|
| 115 |
with pytest.raises(Timeout): |
| 116 |
SoftFileLease(str(marker), lease_duration=0.3, heartbeat_interval=0.1, timeout=0.5).acquire() |
| 117 |
|
| 118 |
assert marker.exists() |
| 119 |
|
| 120 |
|
| 121 |
def test_lease_never_ages_out_an_unknown_contract(tmp_path: Path) -> None: |
| 122 |
# A marker naming a contract this version cannot interpret must outlive the malformed grace window: aging it out |
| 123 |
# would delete a lock whose owner never agreed to expire. |
| 124 |
marker = tmp_path / "a.lock" |
| 125 |
marker.write_text("filelock/2\npid=999999\nhost=nowhere\nmode=fenced\n", encoding="utf-8") |
| 126 |
stale = time.time() - 3600 |
| 127 |
os.utime(marker, (stale, stale)) |
| 128 |
|
| 129 |
with pytest.raises(Timeout): |
| 130 |
SoftFileLease(str(marker), lease_duration=0.3, heartbeat_interval=0.1, timeout=0.5).acquire() |
| 131 |
|
| 132 |
assert marker.exists() |
| 133 |
|
| 134 |
|
| 135 |
def test_lease_ages_out_a_malformed_record(tmp_path: Path) -> None: |
| 136 |
# The self-heal still applies to a record that states no contract at all, so corruption cannot wedge contenders. |
| 137 |
marker = tmp_path / "a.lock" |
| 138 |
marker.write_text("filelock/2\nnot-a-record\n", encoding="utf-8") |
| 139 |
stale = time.time() - 3600 |
| 140 |
os.utime(marker, (stale, stale)) |
| 141 |
|
| 142 |
with SoftFileLease(str(marker), lease_duration=5, timeout=1) as lease: |
| 143 |
assert lease.is_lock_held_by_us |
| 144 |
|
| 145 |
|
| 146 |
def test_marker_pid_and_owner_are_none_without_marker(tmp_path: Path) -> None: |
| 147 |
lease = SoftFileLease(tmp_path / "a") |
| 148 |
assert (lease.pid, lease.owner, lease.is_lock_held_by_us) == (None, None, False) |
| 149 |
|
| 150 |
|
| 151 |
def test_marker_force_break_removes_the_marker(tmp_path: Path) -> None: |
| 152 |
# force_break clears a marker whose holder is gone, so the file is on disk but not held open — the one case where |
| 153 |
# an unlink also succeeds on Windows, which refuses to remove a descriptor another handle still holds. |
| 154 |
marker = tmp_path / "a" |
| 155 |
marker.write_text("filelock/2\npid=1\nhost=h\nmode=lease\n", encoding="utf-8") |
| 156 |
assert marker.exists() |
| 157 |
|
| 158 |
SoftFileLease(marker).force_break() |
| 159 |
|
| 160 |
assert not marker.exists() |