test_upload_backoff_reconnect_3563.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """A printer that reconnects is not held to its upload backoff (#3563).
  2. An X2D's file service stopped finishing the TLS handshake, so every upload
  3. timed out and the backoff grew to an hour. Restarting the printer fixed it, but
  4. the queue kept waiting out the hour: only the timer ended the backoff, and only
  5. a Bambuddy restart, which forgets it, got the job going.
  6. """
  7. import time
  8. from types import SimpleNamespace
  9. from unittest.mock import AsyncMock, MagicMock, patch
  10. import pytest
  11. from backend.app import main as main_module
  12. from backend.app.services.bambu_ftp import BambuFTPClient
  13. from backend.app.services.print_scheduler import UPLOAD_FAILURE_BACKOFF_SECONDS, PrintScheduler
  14. from backend.tests.unit.test_printer_offline_notification import _reset_edge_state, _spawn_patch, _state
  15. from backend.tests.unit.test_scheduler_upload_requeue_3210 import (
  16. HANDSHAKE,
  17. REFUSING_IP,
  18. _item,
  19. _remaining,
  20. _scheduler,
  21. _upload_refused_by,
  22. farm,
  23. )
  24. __all__ = ["_reset_edge_state", "farm"] # fixtures, used by name
  25. pytestmark = pytest.mark.unit
  26. class TestTheScheduler:
  27. @pytest.mark.asyncio
  28. async def test_a_reconnected_printer_gets_the_job_without_waiting(self, farm):
  29. item_id = await farm.add_item(printer_id=farm.ids.refusing)
  30. scheduler = PrintScheduler()
  31. async with _scheduler(farm, _upload_refused_by(REFUSING_IP, HANDSHAKE), scheduler=scheduler):
  32. await scheduler.check_queue()
  33. assert farm.ids.refusing in scheduler._upload_backoff
  34. scheduler.printer_reconnected(farm.ids.refusing, REFUSING_IP)
  35. async with _scheduler(farm, AsyncMock(return_value=True), scheduler=scheduler):
  36. await scheduler.check_queue()
  37. assert (await _item(farm, item_id)).status == "printing"
  38. @pytest.mark.asyncio
  39. async def test_a_printer_that_still_refuses_gets_the_next_longer_wait_and_no_second_notice(self, farm):
  40. await farm.add_item(printer_id=farm.ids.refusing)
  41. refused = _upload_refused_by(REFUSING_IP, HANDSHAKE)
  42. scheduler = PrintScheduler()
  43. waiting = AsyncMock()
  44. async with _scheduler(farm, refused, scheduler=scheduler, waiting_notify=waiting):
  45. await scheduler.check_queue()
  46. scheduler.printer_reconnected(farm.ids.refusing, REFUSING_IP)
  47. async with _scheduler(farm, refused, scheduler=scheduler, waiting_notify=waiting):
  48. await scheduler.check_queue()
  49. assert _remaining(scheduler, farm.ids.refusing) == pytest.approx(2 * UPLOAD_FAILURE_BACKOFF_SECONDS, abs=5)
  50. assert waiting.await_count == 1
  51. def test_the_ftp_cool_off_from_before_the_reconnect_goes(self):
  52. """Inside it the first retry would fail without reaching the printer."""
  53. scheduler = PrintScheduler()
  54. BambuFTPClient._handshake_blocked_until["10.0.0.50"] = time.monotonic() + 300
  55. BambuFTPClient._handshake_blocked_until["10.0.0.51"] = time.monotonic() + 300
  56. try:
  57. scheduler.printer_reconnected(7, "10.0.0.50")
  58. assert not BambuFTPClient.handshake_blocked("10.0.0.50")
  59. assert BambuFTPClient.handshake_blocked("10.0.0.51")
  60. finally:
  61. BambuFTPClient._handshake_blocked_until.pop("10.0.0.50", None)
  62. BambuFTPClient._handshake_blocked_until.pop("10.0.0.51", None)
  63. def test_only_the_reconnected_printer_is_let_go(self):
  64. scheduler = PrintScheduler()
  65. scheduler._upload_backoff.update({1: time.monotonic() + 600, 2: time.monotonic() + 600})
  66. scheduler._upload_refusals.update({1: 2, 2: 2})
  67. scheduler.printer_reconnected(1, None)
  68. assert 1 not in scheduler._upload_backoff
  69. assert 2 in scheduler._upload_backoff
  70. assert scheduler._upload_refusals == {1: 2, 2: 2}
  71. class TestTheReconnect:
  72. @staticmethod
  73. async def _status(*connected: bool) -> MagicMock:
  74. """Feed the status handler these connection states; return the scheduler stand-in."""
  75. ws_mgr = MagicMock()
  76. ws_mgr.send_printer_status = AsyncMock()
  77. relay = MagicMock()
  78. relay.on_printer_status = AsyncMock()
  79. relay.on_printer_online = AsyncMock()
  80. pm = MagicMock()
  81. pm.get_printer.return_value = SimpleNamespace(name="X2D", serial_number="SN1")
  82. pm.get_model.return_value = ""
  83. pm.is_awaiting_plate_clear.return_value = False
  84. pm.get_client.return_value = SimpleNamespace(ip_address="10.0.0.8")
  85. scheduler = MagicMock()
  86. with (
  87. patch("backend.app.main.ws_manager", ws_mgr),
  88. patch("backend.app.main.mqtt_relay", relay),
  89. patch("backend.app.main.printer_manager", pm),
  90. patch("backend.app.main.print_scheduler", scheduler),
  91. patch("backend.app.main._maybe_notify_printer_offline", new=AsyncMock()),
  92. _spawn_patch(),
  93. patch("backend.app.main.printer_state_to_dict", return_value={}),
  94. ):
  95. for c in connected:
  96. await main_module.on_printer_status_change(1, _state(connected=c))
  97. return scheduler
  98. @pytest.mark.asyncio
  99. async def test_a_printer_back_from_a_dropped_connection_is_let_go(self):
  100. scheduler = await self._status(True, False, True)
  101. scheduler.printer_reconnected.assert_called_once_with(1, "10.0.0.8")
  102. @pytest.mark.asyncio
  103. async def test_bambuddys_own_start_and_further_updates_are_no_reconnect(self):
  104. scheduler = await self._status(True, True, True)
  105. scheduler.printer_reconnected.assert_not_called()