test_upload_quiet_not_stale_3538.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """A printer receiving Bambuddy's upload is quiet, not stale (#3538).
  2. An A1 publishes nothing over MQTT while it receives a large FTPS upload. A
  3. 19 MB file took three minutes, the stale check force-closed the connection
  4. 42 ms before the print command, and the command found the printer
  5. disconnected and failed the job. Silence during an upload, and within
  6. STALE_TIMEOUT after it, no longer counts.
  7. """
  8. import time
  9. from contextlib import ExitStack
  10. from unittest.mock import AsyncMock, MagicMock, patch
  11. import pytest
  12. from backend.app.services.bambu_mqtt import BambuMQTTClient
  13. from backend.app.services.printer_manager import PrinterManager
  14. from backend.tests.unit.test_scheduler_busy_defer_2598 import dispatch_case # noqa: F401 - fixture
  15. def _silent_client(silence: float) -> BambuMQTTClient:
  16. client = BambuMQTTClient(ip_address="10.0.0.1", serial_number="A1", access_code="12345678")
  17. client.state.connected = True
  18. client._last_message_time = time.time() - silence
  19. return client
  20. class TestStaleCheck:
  21. def test_a_silent_printer_is_still_stale(self):
  22. assert _silent_client(BambuMQTTClient.STALE_TIMEOUT + 10).is_stale()
  23. def test_not_stale_while_an_upload_is_running(self):
  24. client = _silent_client(BambuMQTTClient.STALE_TIMEOUT + 120)
  25. client.begin_upload()
  26. assert not client.is_stale()
  27. def test_the_timeout_counts_from_the_end_of_the_upload(self):
  28. client = _silent_client(BambuMQTTClient.STALE_TIMEOUT + 120)
  29. client.begin_upload()
  30. client.end_upload()
  31. assert not client.is_stale(), "the printer gets STALE_TIMEOUT after the upload to speak"
  32. client._last_upload_end = time.time() - BambuMQTTClient.STALE_TIMEOUT - 1
  33. assert client.is_stale(), "silence long after the upload is staleness again"
  34. def test_no_forced_reconnect_under_the_upload(self):
  35. client = _silent_client(BambuMQTTClient.STALE_TIMEOUT + 120)
  36. client.force_reconnect_stale_session = MagicMock()
  37. client._client = MagicMock()
  38. client.begin_upload()
  39. assert client.check_staleness() is True
  40. assert client.state.connected
  41. client._client.disconnect.assert_not_called()
  42. def test_overlapping_uploads_keep_it_quiet_until_the_last_ends(self):
  43. client = _silent_client(BambuMQTTClient.STALE_TIMEOUT + 120)
  44. client.begin_upload()
  45. client.begin_upload()
  46. client.end_upload()
  47. assert client._uploads_in_flight == 1
  48. assert not client.is_stale()
  49. class TestUploadingContext:
  50. def test_marks_the_printers_client_for_the_upload(self):
  51. manager = PrinterManager()
  52. client = _silent_client(0)
  53. manager._clients[7] = client
  54. with manager.uploading(7):
  55. assert client._uploads_in_flight == 1
  56. assert client._uploads_in_flight == 0
  57. assert client._last_upload_end > 0
  58. def test_ends_on_the_client_it_began_on(self):
  59. manager = PrinterManager()
  60. old, new = _silent_client(0), _silent_client(0)
  61. manager._clients[7] = old
  62. with manager.uploading(7):
  63. manager._clients[7] = new # reconnect replaced the client meanwhile
  64. assert old._uploads_in_flight == 0
  65. assert new._uploads_in_flight == 0
  66. def test_ends_when_the_upload_raises(self):
  67. manager = PrinterManager()
  68. client = _silent_client(0)
  69. manager._clients[7] = client
  70. with pytest.raises(RuntimeError), manager.uploading(7):
  71. raise RuntimeError("upload failed")
  72. assert client._uploads_in_flight == 0
  73. def test_an_unknown_printer_is_a_no_op(self):
  74. with PrinterManager().uploading(None), PrinterManager().uploading(99):
  75. pass
  76. @pytest.mark.asyncio
  77. async def test_the_queue_marks_its_upload(dispatch_case):
  78. """The queue's upload runs inside the uploading window of its printer."""
  79. from backend.app.models.print_queue import PrintQueueItem
  80. from backend.app.services.print_scheduler import PrintScheduler, printer_manager
  81. from backend.tests.unit.test_scheduler_busy_defer_2598 import _base_patches
  82. scheduler = PrintScheduler()
  83. client = _silent_client(0)
  84. seen: list[int] = []
  85. async def upload(*args, **kwargs):
  86. seen.append(client._uploads_in_flight)
  87. return True
  88. get_status = MagicMock(return_value=MagicMock(state="IDLE", subtask_id=None, gcode_file=None))
  89. async with dispatch_case.session_maker() as db:
  90. item = await db.get(PrintQueueItem, dispatch_case.ids.item_id)
  91. with ExitStack() as stack:
  92. for p in _base_patches(scheduler, dispatch_case, AsyncMock(side_effect=upload), MagicMock(), get_status):
  93. stack.enter_context(p)
  94. stack.enter_context(patch.dict(printer_manager._clients, {dispatch_case.ids.printer_id: client}))
  95. await scheduler._start_print(db, item)
  96. assert seen == [1]
  97. assert client._uploads_in_flight == 0
  98. @pytest.mark.asyncio
  99. async def test_the_firmware_upload_marks_its_upload():
  100. """The firmware update's upload is quiet for the printer the same way."""
  101. import inspect
  102. from backend.app.services import firmware_update
  103. source = inspect.getsource(firmware_update)
  104. upload_at = source.index("success = await with_ftp_retry(")
  105. window_at = source.rindex("with printer_manager.uploading(printer_id):", 0, upload_at)
  106. assert upload_at - window_at < 200