diff --git a/meshtastic/tests/conftest.py b/meshtastic/tests/conftest.py index c863888a7..369b7bc22 100644 --- a/meshtastic/tests/conftest.py +++ b/meshtastic/tests/conftest.py @@ -1,6 +1,7 @@ """Common pytest code (place for fixtures).""" import argparse +import threading from unittest.mock import MagicMock import pytest @@ -17,6 +18,23 @@ ) from .fw_helpers import set_region + +# Reader threads that loop on their transport; over a MagicMock transport they spin and record every call. +READER_THREAD_NAMES = frozenset({"stream reader", "BLEReceive"}) + + +@pytest.fixture(autouse=True) +def no_leaked_reader_threads(): + """Fail a test that leaves an interface reader thread running. + + Over a mocked transport the reader spins without sleeping and every MagicMock call is recorded, + so a leaked reader grows memory for the rest of the session until the CI runner is killed.""" + before = set(threading.enumerate()) + yield + leaked = [t.name for t in threading.enumerate() if t.name in READER_THREAD_NAMES and t not in before] + assert not leaked, f"test left reader thread(s) {leaked} running; close() the interface it created" + + # Use a different base port for the single-node fixture so it doesn't # conflict with the multi-node mesh fixture. SINGLE_NODE_BASE_PORT = DEFAULT_BASE_PORT + 100 diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index a25c0cc9c..fdde5bd95 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -3588,18 +3588,21 @@ def my_sleep(amount): serialInterface = SerialInterface(noProto=True) - with caplog.at_level(logging.DEBUG): - with patch("meshtastic.serial_interface.SerialInterface", return_value=serialInterface): - with patch("time.sleep", side_effect=my_sleep): - with pytest.raises(SystemExit) as pytest_wrapped_e: - tunnelMain() - mock_platform_system.assert_called() - assert pytest_wrapped_e.type == SystemExit - assert pytest_wrapped_e.value.code == 3 - assert re.search(r"Not starting Tunnel", caplog.text, re.MULTILINE) - out, err = capsys.readouterr() - assert re.search(r"Connected to radio", out, re.MULTILINE) - assert err == "" + try: + with caplog.at_level(logging.DEBUG): + with patch("meshtastic.serial_interface.SerialInterface", return_value=serialInterface): + with patch("time.sleep", side_effect=my_sleep): + with pytest.raises(SystemExit) as pytest_wrapped_e: + tunnelMain() + mock_platform_system.assert_called() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 3 + assert re.search(r"Not starting Tunnel", caplog.text, re.MULTILINE) + out, err = capsys.readouterr() + assert re.search(r"Connected to radio", out, re.MULTILINE) + assert err == "" + finally: + serialInterface.close() @pytest.mark.unit diff --git a/meshtastic/tests/test_tcp_interface.py b/meshtastic/tests/test_tcp_interface.py index f9e03d565..5d09d1d07 100644 --- a/meshtastic/tests/test_tcp_interface.py +++ b/meshtastic/tests/test_tcp_interface.py @@ -152,10 +152,13 @@ def test_TCPInterface_writeBytes_reconnects(): iface = TCPInterface(hostname="localhost", noProto=True) iface.socket.sendall.side_effect = OSError("Broken pipe") - with patch.object(iface, "_reconnect") as mock_reconnect: - with pytest.raises(OSError, match="Broken pipe"): - iface._writeBytes(b"some data") - mock_reconnect.assert_called_once() + try: + with patch.object(iface, "_reconnect") as mock_reconnect: + with pytest.raises(OSError, match="Broken pipe"): + iface._writeBytes(b"some data") + mock_reconnect.assert_called_once() + finally: + iface.close() @pytest.mark.unit diff --git a/meshtastic/tests/test_tunnel.py b/meshtastic/tests/test_tunnel.py index e2e917e5b..d6ff3f72b 100644 --- a/meshtastic/tests/test_tunnel.py +++ b/meshtastic/tests/test_tunnel.py @@ -24,9 +24,12 @@ def test_Tunnel_on_non_linux_system(mock_platform_system): a_mock.return_value = "notLinux" mock_platform_system.side_effect = a_mock with patch("socket.socket") as mock_socket: - with pytest.raises(Tunnel.TunnelError) as pytest_wrapped_e: - iface = TCPInterface(hostname="localhost", noProto=True) - Tunnel(iface) + iface = TCPInterface(hostname="localhost", noProto=True) + try: + with pytest.raises(Tunnel.TunnelError) as pytest_wrapped_e: + Tunnel(iface) + finally: + iface.close() assert pytest_wrapped_e.type == Tunnel.TunnelError assert mock_socket.called