Files
OBLITERATUS/tests/test_campaign_memory_receipts.py

62 lines
2.7 KiB
Python

"""Campaign receipts describe successful memory samples, including absent sampling."""
import io
import sys
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from scripts import qwen38_a100_campaign as campaign
pytestmark = pytest.mark.cpu
SAMPLED = "sampled selected GPU memory.used; includes other processes"
DISABLED = "not sampled; GPU monitoring disabled"
UNAVAILABLE = "not sampled; no selected GPU memory sample collected"
def assert_persisted(root, receipt, label, peak):
assert receipt["memory_measurement"] == label
assert receipt["peak_gpu_memory_mib"] == peak
path = root / "receipts" / "preflight.attempt-1.json"
assert campaign.read_json(path) == receipt
def test_unmonitored_real_child_needs_no_manifest_or_memory_probe(tmp_path, monkeypatch):
sample = Mock(side_effect=AssertionError("unmonitored stage probed GPU"))
monkeypatch.setattr(campaign, "gpu_memory_mib", sample)
receipt = campaign.run_subprocess(
tmp_path, "preflight", [sys.executable, "-c", "print('ready')"],
tmp_path / "preflight", {}, monitor=False,
)
assert receipt["exit_status"] == 0
sample.assert_not_called()
assert not (tmp_path / "run-manifest.json").exists()
assert_persisted(tmp_path, receipt, DISABLED, None)
@pytest.mark.parametrize("memory,label", [(0.0, SAMPLED), (12.5, SAMPLED), (None, UNAVAILABLE)])
def test_monitored_real_child_only_claims_successful_samples(tmp_path, monkeypatch, memory, label):
campaign.write_json(tmp_path / "run-manifest.json", {"locked_inputs": {"gpu": {"uuid": "GPU-test"}}})
sample = Mock(return_value=memory)
monkeypatch.setattr(campaign, "gpu_memory_mib", sample)
receipt = campaign.run_subprocess(
tmp_path, "preflight", [sys.executable, "-c", "import time; time.sleep(0.65)"],
tmp_path / "preflight", {},
)
assert receipt["exit_status"] == 0
assert sample.call_count > 0
assert all(call.args == ("GPU-test",) for call in sample.call_args_list)
assert_persisted(tmp_path, receipt, label, memory)
def test_child_exit_before_first_poll_does_not_claim_a_sample(tmp_path, monkeypatch):
campaign.write_json(tmp_path / "run-manifest.json", {"locked_inputs": {"gpu": {"uuid": "GPU-test"}}})
child = SimpleNamespace(stdout=io.StringIO("done\n"), poll=lambda: 0, returncode=0)
monkeypatch.setattr(campaign.subprocess, "Popen", lambda *args, **kwargs: child)
sample = Mock(side_effect=AssertionError("finished stage probed GPU"))
monkeypatch.setattr(campaign, "gpu_memory_mib", sample)
receipt = campaign.run_subprocess(tmp_path, "preflight", ["stub"], tmp_path / "preflight", {})
sample.assert_not_called()
assert_persisted(tmp_path, receipt, UNAVAILABLE, None)