|
3 | 3 | import json |
4 | 4 | import logging |
5 | 5 | import re |
| 6 | +import time |
| 7 | +from types import SimpleNamespace |
6 | 8 | from unittest.mock import patch |
7 | 9 |
|
8 | 10 | import pytest |
@@ -236,20 +238,111 @@ def test_remove_keys_from_dict_nested(): |
236 | 238 | assert remove_keys_from_dict(keys, adict) == exp |
237 | 239 |
|
238 | 240 |
|
239 | | -@pytest.mark.unitslow |
240 | | -def test_Timeout_not_found(): |
241 | | - """Test Timeout()""" |
242 | | - to = Timeout(0.2) |
243 | | - attrs = "foo" |
244 | | - to.waitForSet("bar", attrs) |
| 241 | +@pytest.mark.unit |
| 242 | +def test_Timeout_waitForSet_found(): |
| 243 | + """waitForSet returns True when all required attrs are truthy on target.""" |
| 244 | + to = Timeout(0.01) |
| 245 | + target = SimpleNamespace(foo=1, bar="ok") |
| 246 | + assert to.waitForSet(target, ("foo", "bar")) is True |
245 | 247 |
|
246 | 248 |
|
247 | | -@pytest.mark.unitslow |
248 | | -def test_Timeout_found(): |
249 | | - """Test Timeout()""" |
250 | | - to = Timeout(0.2) |
251 | | - attrs = () |
252 | | - to.waitForSet("bar", attrs) |
| 249 | +@pytest.mark.unit |
| 250 | +@patch("meshtastic.util.time.sleep") |
| 251 | +def test_Timeout_waitForSet_not_found(mock_sleep): # pylint: disable=unused-argument |
| 252 | + """waitForSet returns False when attrs remain falsy until timeout.""" |
| 253 | + to = Timeout(0.01) |
| 254 | + target = SimpleNamespace(foo=None, bar=0) |
| 255 | + assert to.waitForSet(target, ("foo", "bar")) is False |
| 256 | + |
| 257 | + |
| 258 | +@pytest.mark.unit |
| 259 | +def test_Timeout_waitForSet_empty_attrs(): |
| 260 | + """waitForSet returns True immediately when attrs is empty (vacuous truth).""" |
| 261 | + to = Timeout(0.01) |
| 262 | + assert to.waitForSet(object(), ()) is True |
| 263 | + |
| 264 | + |
| 265 | +@pytest.mark.unit |
| 266 | +def test_Timeout_reset(): |
| 267 | + """reset() sets expireTime to now + expireTimeout.""" |
| 268 | + to = Timeout(maxSecs=5) |
| 269 | + before = time.time() |
| 270 | + to.reset() |
| 271 | + assert to.expireTime == pytest.approx(before + 5, abs=0.1) |
| 272 | + |
| 273 | + |
| 274 | +@pytest.mark.unit |
| 275 | +def test_Timeout_reset_custom(): |
| 276 | + """reset(expireTimeout) overrides the stored expireTimeout.""" |
| 277 | + to = Timeout(maxSecs=5) |
| 278 | + before = time.time() |
| 279 | + to.reset(expireTimeout=10) |
| 280 | + assert to.expireTime == pytest.approx(before + 10, abs=0.1) |
| 281 | + |
| 282 | + |
| 283 | +@pytest.mark.unit |
| 284 | +def test_Timeout_waitForAckNak_found(): |
| 285 | + """waitForAckNak returns True when any acknowledgment attr is set, and clears it.""" |
| 286 | + to = Timeout(0.01) |
| 287 | + ack = Acknowledgment() |
| 288 | + ack.receivedAck = True |
| 289 | + assert to.waitForAckNak(ack) is True |
| 290 | + assert ack.receivedAck is False # cleared after detection |
| 291 | + |
| 292 | + |
| 293 | +@pytest.mark.unit |
| 294 | +@patch("meshtastic.util.time.sleep") |
| 295 | +def test_Timeout_waitForAckNak_not_found(mock_sleep): # pylint: disable=unused-argument |
| 296 | + """waitForAckNak returns False when no acknowledgment attr is set.""" |
| 297 | + to = Timeout(0.01) |
| 298 | + ack = Acknowledgment() |
| 299 | + assert to.waitForAckNak(ack) is False |
| 300 | + |
| 301 | + |
| 302 | +@pytest.mark.unit |
| 303 | +def test_Timeout_waitForTraceRoute_found(): |
| 304 | + """waitForTraceRoute returns True on receivedTraceRoute, and clears it.""" |
| 305 | + to = Timeout(0.01) |
| 306 | + ack = Acknowledgment() |
| 307 | + ack.receivedTraceRoute = True |
| 308 | + assert to.waitForTraceRoute(3.0, ack) is True |
| 309 | + assert ack.receivedTraceRoute is False |
| 310 | + |
| 311 | + |
| 312 | +@pytest.mark.unit |
| 313 | +@patch("meshtastic.util.time.sleep") |
| 314 | +def test_Timeout_waitForTraceRoute_not_found(mock_sleep): # pylint: disable=unused-argument |
| 315 | + """waitForTraceRoute returns False on timeout.""" |
| 316 | + to = Timeout(0.01) |
| 317 | + ack = Acknowledgment() |
| 318 | + assert to.waitForTraceRoute(3.0, ack) is False |
| 319 | + |
| 320 | + |
| 321 | +@pytest.mark.unit |
| 322 | +def test_Timeout_waitForTelemetry_found(): |
| 323 | + """waitForTelemetry returns True when receivedTelemetry is set.""" |
| 324 | + to = Timeout(0.01) |
| 325 | + ack = Acknowledgment() |
| 326 | + ack.receivedTelemetry = True |
| 327 | + assert to.waitForTelemetry(ack) is True |
| 328 | + |
| 329 | + |
| 330 | +@pytest.mark.unit |
| 331 | +def test_Timeout_waitForPosition_found(): |
| 332 | + """waitForPosition returns True when receivedPosition is set.""" |
| 333 | + to = Timeout(0.01) |
| 334 | + ack = Acknowledgment() |
| 335 | + ack.receivedPosition = True |
| 336 | + assert to.waitForPosition(ack) is True |
| 337 | + |
| 338 | + |
| 339 | +@pytest.mark.unit |
| 340 | +def test_Timeout_waitForWaypoint_found(): |
| 341 | + """waitForWaypoint returns True when receivedWaypoint is set.""" |
| 342 | + to = Timeout(0.01) |
| 343 | + ack = Acknowledgment() |
| 344 | + ack.receivedWaypoint = True |
| 345 | + assert to.waitForWaypoint(ack) is True |
253 | 346 |
|
254 | 347 |
|
255 | 348 | @pytest.mark.unitslow |
|
0 commit comments