move_detect/tests/test_queue.py
유창욱 9386dfc580 feat(delivery): install-path fixes + cafe24 package + delivery-audit hardening
Three intertwined bodies of work developed and tested together this session.
Full test suite green: python (edge) + phpunit (cloud).

## Edge/Pi install path
- setup_online.sh: apt-lock retry (apt-daily contention), Type=exec unit +
  StartLimitIntervalSec in [Unit], journalctl-on-failure, crash-loop detection.
- onnxruntime is now best-effort (no sdist / no 32-bit-ARM wheel); [6/8] probes the
  real backend and hard-fails if none works.
- Hailo HEF auto-fetch: edge/tools/fetch_hailo_hef.py (pinned sha256, refuses to guess
  8 vs 8L, HAILO_ARCH override for serial-less boards); hailo_pose.py run() timeout fix.
- install_offline.sh: mirrors the backend gate; onnxruntime optional.

## Cafe24 cloud package (cloud/deploy/)
- build_cafe24_package.sh emits move_detect_app/ (private, one folder) + www/move_detect/
  (docroot subdir); no-dev classmap autoloader; preflight.php; hardened .htaccess.
- Subdirectory mount support: Request::baseFrom/pathFor strip the mount prefix; index.php
  walks up to find the app root; views/JS prefix every URL with APP_BASE; session cookie
  scoped to the mount.

## Delivery audit — silent-failure & correctness (docs/delivery-audit.md)
All 4 blockers + all Phase-1 silent-failure + Phase-2 HIGH correctness:
- Edge cloud clients send a custom User-Agent (cafe24 WAF blocks python-requests) and
  allow_redirects=False.
- Daemon fails loud on blank CAFE24_URL/DEVICE_SECRET; run_loop logs swallowed exceptions;
  heartbeat carries real fps + per-camera health; UvcSource reopens a dead camera;
  offline queue is size-capped and drops poison rows; TARGET_FPS/QUEUE_DB validated.
- Zone coordinates normalized 0..1 on both sides (were canvas-px vs camera-px — every zone
  misplaced); pinch zones emit PINCH/HIGH (were downgraded to WARNING).
- Fall detection uses bbox cues when keypoints are low-confidence (occluded fallen person);
  ghost incident not cancelled by a bystander; displacement normalized per-second (FPS-invariant).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019Xmn8KZWqiQUguEMRtn6rW
2026-07-17 09:07:33 +09:00

73 lines
2.6 KiB
Python

from edge.events import Event, EventType, Severity
from edge.queue import OfflineQueue
def _event(n=1):
return Event(EventType.FALL, Severity.HIGH, "cam-01", n, None, float(n), 3.0, {}, None)
class _Client:
def __init__(self, fail=False):
self.fail = fail
self.sent = []
def send(self, event, snapshots):
if self.fail:
return False
self.sent.append(event.track_id)
return True
def test_try_send_success_not_queued(tmp_path):
q = OfflineQueue(str(tmp_path / "q.db"), _Client())
assert q.try_send(_event(1), [b"x"]) is True
assert q.pending() == 0
def test_failure_is_queued(tmp_path):
q = OfflineQueue(str(tmp_path / "q.db"), _Client(fail=True))
assert q.try_send(_event(1), [b"x"]) is False
assert q.pending() == 1
def test_flush_resends_when_recovered(tmp_path):
client = _Client(fail=True)
q = OfflineQueue(str(tmp_path / "q.db"), client)
q.try_send(_event(1), [b"x"])
q.try_send(_event(2), [b"y"])
assert q.pending() == 2
client.fail = False
assert q.flush() == 2
assert q.pending() == 0
assert client.sent == [1, 2]
class _PoisonClient:
"""Rejects one specific event forever (e.g. a 400 the cloud will never accept)."""
def __init__(self, poison_id):
self.poison = poison_id
self.sent = []
def send(self, event, snapshots):
if event.track_id == self.poison:
return False
self.sent.append(event.track_id)
return True
def test_queue_is_bounded_and_keeps_newest(tmp_path):
# An extended outage (or a permanent misconfig) must not fill the SD card. The queue caps
# itself, evicting the OLDEST — a fresh fall alert matters more than a stale one.
q = OfflineQueue(str(tmp_path / "q.db"), _Client(fail=True), max_rows=5)
for n in range(20):
q.try_send(_event(n), [b"x"])
assert q.pending() == 5
q.client.fail = False
q.flush()
assert q.client.sent == [15, 16, 17, 18, 19] # the newest 5 survived
def test_poison_head_is_dropped_and_unblocks_the_queue(tmp_path):
# A permanently-rejected event at the head must not wedge the whole queue forever; after
# max_send_attempts it is dropped so the events behind it can drain.
client = _PoisonClient(99)
q = OfflineQueue(str(tmp_path / "q.db"), client, max_send_attempts=3)
q.enqueue(_event(99), [b"x"]) # poison, at the head
q.enqueue(_event(2), [b"y"]) # good, behind it
for _ in range(3):
q.flush()
assert client.sent == [2] # good event delivered
assert q.pending() == 0 # poison dropped, queue drained