test_forget_printer.py 12 KB

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