|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import importlib.util |
| 6 | +import os |
6 | 7 | import sys |
| 8 | +import time |
7 | 9 | from pathlib import Path |
8 | 10 |
|
9 | 11 | import pytest |
10 | 12 |
|
11 | 13 | from hypertrain.auditor.replay import pack_state |
12 | | -from hypertrain.challenge.store import ChallengeError |
| 14 | +from hypertrain.challenge.store import ChallengeError, _stage_once |
13 | 15 | from hypertrain.data.store import put_large |
14 | 16 | from hypertrain.protocol.messages_v2 import StartStateV2 |
15 | 17 | from hypertrain.trainer.config import TrainConfig |
@@ -106,3 +108,40 @@ def test_island_job_route_second_get_is_200_same_body(network, monkeypatch): |
106 | 108 | second = network.client.get(network.url + "/rounds/0/job/" + HOT) |
107 | 109 | assert second.status_code == 200, second.text |
108 | 110 | assert second.json() == first.json() |
| 111 | + |
| 112 | + |
| 113 | +def _part(path: Path, data: bytes, age: float = 0.0) -> Path: |
| 114 | + path.write_bytes(data) |
| 115 | + when = time.time() - age |
| 116 | + os.utime(path, (when, when)) |
| 117 | + return path |
| 118 | + |
| 119 | + |
| 120 | +def test_stage_once_purges_stale_part_files_of_its_own_name(tmp_path): |
| 121 | + stale = _part(tmp_path / ".start_state.abc123.part", b"torn", age=3600) |
| 122 | + _stage_once(tmp_path / "start_state", b"x") |
| 123 | + assert not stale.exists() |
| 124 | + assert (tmp_path / "start_state").read_bytes() == b"x" |
| 125 | + |
| 126 | + |
| 127 | +def test_stage_once_keeps_fresh_part_files_and_other_names(tmp_path): |
| 128 | + stale = _part(tmp_path / ".start_state.abc123.part", b"torn", age=3600) |
| 129 | + fresh = _part(tmp_path / ".start_state.def456.part", b"live") # a concurrent emission's temp |
| 130 | + other = _part(tmp_path / ".ef_in.zzz.part", b"other", age=3600) |
| 131 | + _stage_once(tmp_path / "start_state", b"x") |
| 132 | + assert not stale.exists() |
| 133 | + assert fresh.read_bytes() == b"live" |
| 134 | + assert other.read_bytes() == b"other" |
| 135 | + assert (tmp_path / "start_state").read_bytes() == b"x" |
| 136 | + |
| 137 | + |
| 138 | +def test_stage_once_purge_does_not_follow_symlinks(tmp_path): |
| 139 | + target = _part(tmp_path / "outside", b"keep", age=3600) |
| 140 | + job = tmp_path / "job" |
| 141 | + job.mkdir() |
| 142 | + link = job / ".start_state.link.part" |
| 143 | + link.symlink_to(target) |
| 144 | + os.utime(link, (target.stat().st_mtime,) * 2, follow_symlinks=False) # stale link too |
| 145 | + _stage_once(job / "start_state", b"x") |
| 146 | + assert link.is_symlink() |
| 147 | + assert target.read_bytes() == b"keep" |