Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions meshtastic/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Common pytest code (place for fixtures)."""

import argparse
import threading
from unittest.mock import MagicMock

import pytest
Expand All @@ -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
Expand Down
27 changes: 15 additions & 12 deletions meshtastic/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions meshtastic/tests/test_tcp_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions meshtastic/tests/test_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down