Files
DCCONSTRUCTIONS fa8bde584f feat(video): recover camera previews and reduce encoding and playout cost
Improve camera recovery, shared preview lifecycle and receiver playout diagnostics. Preserve qualified X4 runtime changes and restore async admission in loopback media tests.
2026-10-29 09:10:01 +03:00

153 lines
5.7 KiB
Python

"""Original transfer bounds, idempotency and camera-control exclusion; fake SDK."""
import hashlib
import json
import sys
import tempfile
import threading
import time
import unittest
from contextlib import nullcontext
from pathlib import Path
from unittest.mock import Mock, patch
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from runtime import archive # noqa: E402
ID = "file_" + "a" * 32
SOURCE = "/DCIM/Camera01/VID_20261014_120000_00_001.insv"
class ArchiveTests(unittest.TestCase):
def setUp(self):
self.folder = tempfile.TemporaryDirectory(dir=ROOT / "build")
self.addCleanup(self.folder.cleanup)
self.gate = threading.Lock()
self.camera = Mock()
self.manager = archive.Archive(self.camera, Path(self.folder.name), self.gate)
self.life = patch.object(archive, "acquisition", return_value=nullcontext())
self.space = patch.object(
archive.shutil, "disk_usage", return_value=Mock(free=16 * 1024**3)
)
self.life.start()
self.space.start()
self.addCleanup(patch.stopall)
def wait(self):
deadline = time.monotonic() + 3
while self.manager.busy() and time.monotonic() < deadline:
time.sleep(0.005)
self.assertFalse(self.manager.busy())
return self.manager.status(ID)
def complete(self, source, destination, progress):
# SDK transports may announce (0, 0) before receiving Content-Length.
progress(0, 0)
destination.write_bytes(b"original")
progress(8, 8)
return True
def test_verified_original_chunks_and_same_identity_do_not_repeat_sdk(self):
self.camera.download.side_effect = self.complete
self.manager.start(ID, SOURCE)
status = self.wait()
self.assertEqual(status["state"], "complete")
self.assertEqual(status["sha256"], hashlib.sha256(b"original").hexdigest())
self.assertEqual(self.manager.read(ID, 2), b"iginal")
self.assertEqual(self.manager.start(ID, SOURCE), status)
self.camera.download.assert_called_once()
self.assertTrue(self.gate.acquire(blocking=False))
self.gate.release()
with self.assertRaises(ValueError):
self.manager.start(ID, SOURCE.replace("001", "002"))
with self.assertRaises(ValueError):
self.manager.read(ID, 8)
def test_busy_transfer_owns_gate_until_sdk_really_returns(self):
entered, release = threading.Event(), threading.Event()
def slow(*args):
entered.set()
release.wait(2)
return self.complete(*args)
self.camera.download.side_effect = slow
try:
self.manager.start(ID, SOURCE)
self.assertTrue(entered.wait(1))
self.assertFalse(self.gate.acquire(blocking=False))
with self.assertRaises(RuntimeError):
self.manager.start("file_" + "b" * 32, SOURCE)
with self.assertRaises(ValueError):
self.manager.read(ID, 0)
finally:
release.set()
self.wait()
def test_failed_and_interrupted_files_are_retained_and_not_restarted(self):
def failed(source, destination, progress):
destination.write_bytes(b"part")
progress(4, 8)
return False
self.camera.download.side_effect = failed
self.manager.start(ID, SOURCE)
self.assertEqual(self.wait()["state"], "error")
path = self.manager.folder(ID)
self.assertEqual((path / "original.insv").read_bytes(), b"part")
value = json.loads((path / "receipt.json").read_text())
value["state"] = "running"
(path / "receipt.json").write_text(json.dumps(value))
self.assertEqual(self.manager.start(ID, SOURCE)["state"], "interrupted")
self.camera.download.assert_called_once()
def test_timeout_requests_cancel_without_releasing_live_sdk_ownership(self):
def slow(source, destination, progress):
time.sleep(0.15)
self.assertTrue(self.manager.busy())
return self.complete(source, destination, progress)
self.camera.download.side_effect = slow
with patch.object(archive, "TIMEOUT", 0):
self.manager.start(ID, SOURCE)
self.assertEqual(self.wait()["state"], "error")
self.camera.cancel_download.assert_called_once()
def test_oversized_or_inconsistent_progress_cannot_mark_file_complete(self):
for downloaded, total in ((8, archive.MAX_BYTES + 1), (8, 0), (7, 8)):
ident = "file_" + f"{total + 1:032x}"
def invalid(source, destination, progress, downloaded=downloaded, total=total):
destination.write_bytes(b"original")
progress(downloaded, total)
return True
self.camera.download.side_effect = invalid
self.manager.start(ident, SOURCE)
deadline = time.monotonic() + 2
while self.manager.busy() and time.monotonic() < deadline:
time.sleep(0.005)
self.assertEqual(self.manager.status(ident)["state"], "error")
def test_paths_and_space_reject_before_sdk_or_file_creation(self):
for source in (
"/etc/passwd",
"http://camera/file.insv",
"/DCIM/../x.insv",
"/DCIM/a\\b.insv",
):
with self.assertRaises(ValueError):
self.manager.start(ID, source)
with (
patch.object(archive.shutil, "disk_usage", return_value=Mock(free=0)),
self.assertRaises(RuntimeError),
):
self.manager.start(ID, SOURCE)
self.assertFalse(self.manager.folder(ID).exists())
self.camera.download.assert_not_called()
if __name__ == "__main__":
unittest.main()