test_forget_printer.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. """What Bambuddy keeps per printer id goes when the printer is deleted.
  2. SQLite used to give a deleted printer's id to the next printer created, which
  3. inherited it all: the old print's owner (its prints passed the kill switch and
  4. were billed to that user), the plate-clear gate, pending notifications, the old
  5. camera feed, the old printer's plate-detection references. Ids are no longer
  6. reused, but nothing kept for a printer that is gone should stay.
  7. """
  8. import ast
  9. import asyncio
  10. from pathlib import Path
  11. import pytest
  12. from backend.app import main
  13. pytestmark = pytest.mark.asyncio
  14. GONE, KEPT = 5, 6
  15. # Module-level maps in main.py that are not keyed by a printer, or are kept on
  16. # purpose. Anything else main.py adds must be forgotten with the printer.
  17. NOT_FORGOTTEN = {
  18. "_print_ams_mappings": "keyed by archive (dropped with the printer's expected/active prints)",
  19. "_print_cost_center_ids": "keyed by archive (dropped with the printer's expected/active prints)",
  20. "_print_plate_ids": "keyed by archive (dropped with the printer's expected/active prints)",
  21. "_owned_print_archive_ids": "keyed by archive (dropped with the printer's expected/active prints)",
  22. "_ams_assignment_locks": "a lock carries nothing over; popping one a task holds would let a second be made",
  23. "_fallback_recovery_locks": "a lock carries nothing over (see its comment on popping)",
  24. "_HMS_FAILURE_REASONS": "constant",
  25. "_ACTIVE_PRINT_STATES": "constant",
  26. "_SENSOR_TARGET_KEYS": "constant",
  27. "PUBLIC_API_ROUTES": "constant",
  28. "_HTML_CACHE_HEADERS": "constant",
  29. }
  30. def _module_maps() -> set[str]:
  31. """Names of main.py's module-level dicts and sets."""
  32. tree = ast.parse(Path(main.__file__).read_text())
  33. names = set()
  34. for node in tree.body:
  35. if isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
  36. kind = ast.unparse(node.annotation)
  37. if kind.startswith(("dict", "set")):
  38. names.add(node.target.id)
  39. elif isinstance(node, ast.Assign) and len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
  40. if isinstance(node.value, (ast.Dict, ast.Set)):
  41. names.add(node.targets[0].id)
  42. return names
  43. def _sleeper() -> asyncio.Task:
  44. return asyncio.ensure_future(asyncio.sleep(3600))
  45. def _entries(printer_id: int) -> dict[str, tuple]:
  46. """For each forgotten map: (key, value) for this printer."""
  47. return {
  48. "_active_prints": ((printer_id, "cube.3mf"), 100 + printer_id),
  49. "_expected_prints": ((printer_id, "cube.3mf"), 200 + printer_id),
  50. "_expected_print_creators": ((printer_id, "cube.3mf"), 1),
  51. "_expected_print_registered_at": ((printer_id, "cube.3mf"), 1.0),
  52. "_stage22_finish_frames": (printer_id, b"jpg"),
  53. "_stage22_finish_in_flight": (printer_id, asyncio.Event()),
  54. "_inprint_frame_bank": (printer_id, b"jpg"),
  55. "_inprint_frame_bank_ts": (printer_id, 1.0),
  56. "_printer_reconciled_since_connect": (printer_id, True),
  57. "_printer_kprofiles_primed_since_connect": (printer_id, True),
  58. "_last_progress_milestone": (printer_id, 50),
  59. "_first_layer_notified": (printer_id, True),
  60. "_unauthorized_print_kill_sent": (printer_id, None),
  61. "_kill_switch_notification_tasks": (printer_id, _sleeper()),
  62. "_notified_hms_errors": (printer_id, {"0300_0001": 1.0}),
  63. "_hms_print_state": (printer_id, "RUNNING"),
  64. "_hms_forget_when_gone": (printer_id, {"0300_0001"}),
  65. "_timelapse_baselines": (printer_id, {"video.mp4"}),
  66. "_bed_cool_waiters": (printer_id, {"threshold": 35.0, "filename": "cube", "registered_at": 1.0}),
  67. "_user_stopped_printers": (printer_id, None),
  68. "_kill_switch_stopped": (printer_id, None),
  69. "_printer_last_connected": (printer_id, True),
  70. "_printer_offline_published": (printer_id, None),
  71. "_printer_offline_notify_tasks": (printer_id, _sleeper()),
  72. "_unknown_tag_last_broadcast": (printer_id, {(0, 0): ("uid", "x")}),
  73. "_last_status_broadcast": (printer_id, "hash"),
  74. "_nozzle_count_updated": (printer_id, None),
  75. "_fallback_3mf_retry_tasks": (printer_id, _sleeper()),
  76. "_connection_watchdog_last_attempt": (printer_id, 1.0),
  77. "_ams_index_only_logged": ((printer_id, 0), None),
  78. "_ams_alarm_cooldown": (f"{printer_id}:0:humidity", None),
  79. }
  80. def _put(name: str, key, value) -> None:
  81. target = getattr(main, name)
  82. if isinstance(target, set):
  83. target.add(key)
  84. else:
  85. target[key] = value
  86. def _has(name: str, key) -> bool:
  87. return key in getattr(main, name)
  88. async def test_every_printer_keyed_map_in_main_is_forgotten():
  89. covered = set(_entries(GONE))
  90. unclassified = _module_maps() - covered - set(NOT_FORGOTTEN)
  91. assert not unclassified, (
  92. f"main.py maps neither forgotten with a deleted printer nor listed in NOT_FORGOTTEN: {sorted(unclassified)}"
  93. )
  94. async def test_forget_printer_clears_main_and_keeps_other_printers():
  95. gone, kept = _entries(GONE), _entries(KEPT)
  96. for name in gone:
  97. _put(name, *gone[name])
  98. _put(name, *kept[name])
  99. main._print_ams_mappings[100 + GONE] = [0]
  100. main._print_cost_center_ids[200 + GONE] = 1
  101. main._print_plate_ids[200 + GONE] = 2
  102. pending = {name: gone[name][1] for name in ("_printer_offline_notify_tasks", "_fallback_3mf_retry_tasks")}
  103. try:
  104. await main.forget_printer(GONE)
  105. left = [name for name in gone if _has(name, gone[name][0])]
  106. assert left == []
  107. lost = [name for name in kept if not _has(name, kept[name][0])]
  108. assert lost == []
  109. assert 100 + GONE not in main._print_ams_mappings
  110. assert 200 + GONE not in main._print_cost_center_ids
  111. assert 200 + GONE not in main._print_plate_ids
  112. await asyncio.sleep(0)
  113. assert all(task.cancelled() for task in pending.values())
  114. finally:
  115. for entries in (gone, kept):
  116. for name, (key, value) in entries.items():
  117. target = getattr(main, name)
  118. if isinstance(target, set):
  119. target.discard(key)
  120. else:
  121. target.pop(key, None)
  122. if isinstance(value, asyncio.Task):
  123. value.cancel()
  124. async def test_forget_printer_clears_the_services():
  125. from backend.app.api.routes import camera
  126. from backend.app.services import (
  127. bambu_ftp,
  128. kprofile_drift,
  129. layer_timelapse,
  130. obico_detection,
  131. print_dispatch_context,
  132. usage_tracker,
  133. )
  134. from backend.app.services.printer_manager import printer_manager
  135. printer_manager._current_print_user[GONE] = {"user_id": 1, "username": "alice"}
  136. printer_manager._awaiting_plate_clear.add(GONE)
  137. printer_manager._last_trays[GONE] = {"ams": []}
  138. print_dispatch_context.mark_pending(GONE)
  139. kprofile_drift.note_slot_configured(GONE, 0, 0, -1)
  140. kprofile_drift._watches[(GONE, 0, 1)] = kprofile_drift._SlotWatch()
  141. layer_timelapse._active_sessions[GONE] = type("S", (), {"cleanup": lambda self: None})()
  142. usage_tracker._active_sessions[GONE] = object()
  143. service = obico_detection.obico_detection_service
  144. for attr in ("_states", "_state_keys", "_last_class", "_errors", "_action_fired"):
  145. getattr(service, attr)[GONE] = object()
  146. bambu_ftp._threemf_path_cache[(GONE, "cube")] = Path("/nonexistent/cube.3mf")
  147. camera._last_frames[GONE] = b"jpg"
  148. camera._last_frame_times[GONE] = 1.0
  149. camera._stream_start_times[GONE] = 1.0
  150. camera._active_external_streams[GONE] = 1
  151. camera._external_live_readers[GONE] = 1
  152. from backend.app.api.routes import printers
  153. printers._cover_cache[GONE] = {("Cube", "front"): b"png"}
  154. printers._cover_404_cache[GONE] = {}
  155. from backend.app.services.printer_file_cache import printer_file_cache
  156. printer_file_cache._keep((GONE, "/cache/a.3mf"), b"3mf")
  157. await main.forget_printer(GONE)
  158. assert GONE not in printer_manager._current_print_user
  159. assert GONE not in printer_manager._awaiting_plate_clear
  160. assert GONE not in printer_manager._last_trays
  161. assert not print_dispatch_context.adopt(GONE)
  162. assert (GONE, 0, 0) not in kprofile_drift._default_chosen
  163. assert (GONE, 0, 1) not in kprofile_drift._watches
  164. assert GONE not in layer_timelapse._active_sessions
  165. assert GONE not in usage_tracker._active_sessions
  166. for attr in ("_states", "_state_keys", "_last_class", "_errors", "_action_fired"):
  167. assert GONE not in getattr(service, attr), attr
  168. assert (GONE, "cube") not in bambu_ftp._threemf_path_cache
  169. assert GONE not in camera._last_frames
  170. assert GONE not in camera._last_frame_times
  171. assert GONE not in camera._stream_start_times
  172. assert GONE not in camera._active_external_streams
  173. assert GONE not in camera._external_live_readers
  174. assert GONE not in printers._cover_cache
  175. assert GONE not in printers._cover_404_cache
  176. assert not [key for key in printer_file_cache._entries if key[0] == GONE]
  177. async def test_the_plate_references_of_a_printer_go(tmp_path, monkeypatch):
  178. from backend.app.services import plate_detection
  179. monkeypatch.setattr(plate_detection, "_get_calibration_dir", lambda: tmp_path)
  180. for printer_id in (GONE, KEPT, 55):
  181. (tmp_path / f"printer_{printer_id}_ref_0.jpg").write_bytes(b"jpg")
  182. (tmp_path / f"printer_{printer_id}_metadata.json").write_text("{}")
  183. plate_detection.remove_printer_references(GONE)
  184. assert sorted(p.name for p in tmp_path.iterdir()) == [
  185. "printer_55_metadata.json",
  186. "printer_55_ref_0.jpg",
  187. f"printer_{KEPT}_metadata.json",
  188. f"printer_{KEPT}_ref_0.jpg",
  189. ]
  190. async def test_the_scheduler_and_live_activities_forget_it():
  191. from backend.app.services.notify_live_activities import notify_live_activities
  192. from backend.app.services.print_scheduler import scheduler
  193. scheduler._upload_backoff[GONE] = 1e12
  194. scheduler._upload_refusals[GONE] = 6
  195. scheduler._upload_refusal_notified.add(GONE)
  196. scheduler._wake_failures[GONE] = 1e12
  197. scheduler._dispatch_holds[GONE] = (1.0, "IDLE", None)
  198. scheduler._auto_dry_units[(GONE, 0)] = {}
  199. for attr in ("_snapshots", "_new_fallbacks", "_started_at", "_progress_resets"):
  200. getattr(notify_live_activities, attr)[GONE] = object()
  201. notify_live_activities._wake.clear()
  202. await main.forget_printer(GONE)
  203. assert GONE not in scheduler._upload_backoff
  204. assert GONE not in scheduler._upload_refusals
  205. assert GONE not in scheduler._upload_refusal_notified
  206. assert GONE not in scheduler._wake_failures
  207. assert GONE not in scheduler._dispatch_holds
  208. assert (GONE, 0) not in scheduler._auto_dry_units
  209. for attr in ("_snapshots", "_new_fallbacks", "_started_at", "_progress_resets"):
  210. assert GONE not in getattr(notify_live_activities, attr), attr
  211. # Its tiles are ended at once (the printer is gone), before a new one takes the id.
  212. assert notify_live_activities._wake.is_set()
  213. @pytest.mark.parametrize("ml_fails", [False, True])
  214. async def test_an_obico_check_in_flight_when_the_printer_goes_ends_quietly(ml_fails):
  215. """The check waits up to 30 s for the ML server. A KeyError there stopped the
  216. poll for every printer; an error written back stuck to the next printer given the id."""
  217. from unittest.mock import AsyncMock, MagicMock, patch
  218. from httpx import Request, Response
  219. from backend.app.services.obico_detection import ObicoDetectionService
  220. service = ObicoDetectionService()
  221. async def ml_call(url, **_kwargs):
  222. service.forget_printer(GONE) # deleted meanwhile
  223. if ml_fails:
  224. raise OSError("ML server gone")
  225. return Response(200, json={"detections": []}, request=Request("GET", url))
  226. client = MagicMock()
  227. client.get = AsyncMock(side_effect=ml_call)
  228. client.__aenter__ = AsyncMock(return_value=client)
  229. client.__aexit__ = AsyncMock(return_value=False)
  230. settings = {"sensitivity": "medium", "snapshot_base_url": "http://b", "ml_url": "http://o", "action": "notify"}
  231. with (
  232. patch("backend.app.services.obico_detection.httpx.AsyncClient", return_value=client),
  233. patch.object(service, "_capture_frame", new=AsyncMock(return_value=b"\xff\xd8jpg\xff\xd9")),
  234. ):
  235. await service._check_printer(GONE, MagicMock(state="RUNNING", task_name="job", subtask_name=""), settings)
  236. for attr in ("_states", "_state_keys", "_last_class", "_errors", "_action_fired"):
  237. assert GONE not in getattr(service, attr), attr
  238. assert service._last_error is None