| 1 |
import json |
| 2 |
import stat |
| 3 |
import time |
| 4 |
|
| 5 |
from fenrirscreenreader.core import remoteInstanceRegistry |
| 6 |
|
| 7 |
|
| 8 |
def test_write_instance_creates_shared_sticky_registry_directory( |
| 9 |
tmp_path, monkeypatch |
| 10 |
): |
| 11 |
registry_dir = tmp_path / "instances" |
| 12 |
monkeypatch.setattr( |
| 13 |
remoteInstanceRegistry, "get_registry_dir", lambda: str(registry_dir) |
| 14 |
) |
| 15 |
|
| 16 |
remoteInstanceRegistry.write_instance({"pid": 456}) |
| 17 |
|
| 18 |
mode = stat.S_IMODE(registry_dir.stat().st_mode) |
| 19 |
assert mode == 0o1777 |
| 20 |
|
| 21 |
|
| 22 |
def test_write_instance_prunes_stale_registry_files(tmp_path, monkeypatch): |
| 23 |
monkeypatch.setattr( |
| 24 |
remoteInstanceRegistry, "get_registry_dir", lambda: str(tmp_path) |
| 25 |
) |
| 26 |
monkeypatch.setattr( |
| 27 |
remoteInstanceRegistry, |
| 28 |
"process_exists", |
| 29 |
lambda pid: pid == 456, |
| 30 |
) |
| 31 |
|
| 32 |
stale_instance = tmp_path / "123.json" |
| 33 |
stale_instance.write_text( |
| 34 |
json.dumps( |
| 35 |
{ |
| 36 |
"pid": 123, |
| 37 |
"updated_at": time.time(), |
| 38 |
} |
| 39 |
) |
| 40 |
+ "\n", |
| 41 |
encoding="utf-8", |
| 42 |
) |
| 43 |
invalid_instance = tmp_path / "invalid.json" |
| 44 |
invalid_instance.write_text("not json\n", encoding="utf-8") |
| 45 |
|
| 46 |
remoteInstanceRegistry.write_instance({"pid": 456}) |
| 47 |
|
| 48 |
assert not stale_instance.exists() |
| 49 |
assert not invalid_instance.exists() |
| 50 |
assert (tmp_path / "456.json").exists() |