test_vp_mqtt_server.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. """Tests for Virtual Printer MQTT server."""
  2. import ast
  3. import inspect
  4. from backend.app.services.virtual_printer.mqtt_server import SimpleMQTTServer
  5. class TestMQTTServerNoGlobalState:
  6. """Ensure MQTT server doesn't set global asyncio state."""
  7. def test_no_global_exception_handler(self):
  8. """MQTT server must not call set_exception_handler().
  9. set_exception_handler() is global to the event loop. When multiple
  10. VP instances run, each would overwrite the previous handler,
  11. causing lost error context and spurious 'Unhandled exception in
  12. client_connected_cb' messages.
  13. """
  14. source = inspect.getsource(SimpleMQTTServer)
  15. tree = ast.parse(source)
  16. for node in ast.walk(tree):
  17. if isinstance(node, ast.Attribute) and node.attr == "set_exception_handler":
  18. raise AssertionError(
  19. "SimpleMQTTServer must not call set_exception_handler(). "
  20. "It overwrites the global asyncio exception handler, "
  21. "breaking multi-VP setups."
  22. )