|
10 | 10 | from ..node import Node |
11 | 11 | from .. import mesh_pb2 |
12 | 12 | from ..__init__ import LOCAL_ADDR, BROADCAST_ADDR |
| 13 | +from ..radioconfig_pb2 import RadioConfig |
| 14 | +from ..util import Timeout |
13 | 15 |
|
14 | 16 |
|
15 | 17 | @pytest.mark.unit |
@@ -164,6 +166,22 @@ def test_sendPosition(reset_globals, caplog): |
164 | 166 | assert re.search(r'p.time:', caplog.text, re.MULTILINE) |
165 | 167 |
|
166 | 168 |
|
| 169 | +@pytest.mark.unit |
| 170 | +def test_close_with_heartbeatTimer(reset_globals, caplog): |
| 171 | + """Test close() with heartbeatTimer""" |
| 172 | + iface = MeshInterface(noProto=True) |
| 173 | + anode = Node('foo', 'bar') |
| 174 | + radioConfig = RadioConfig() |
| 175 | + radioConfig.preferences.phone_timeout_secs = 10 |
| 176 | + anode.radioConfig = radioConfig |
| 177 | + iface.localNode = anode |
| 178 | + assert iface.heartbeatTimer is None |
| 179 | + with caplog.at_level(logging.DEBUG): |
| 180 | + iface._startHeartbeat() |
| 181 | + assert iface.heartbeatTimer is not None |
| 182 | + iface.close() |
| 183 | + |
| 184 | + |
167 | 185 | @pytest.mark.unit |
168 | 186 | def test_handleFromRadio_empty_payload(reset_globals, caplog): |
169 | 187 | """Test _handleFromRadio""" |
@@ -543,3 +561,70 @@ def test_getOrCreateByNum(capsys, reset_globals, iface_with_nodes): |
543 | 561 | iface.myInfo.my_node_num = 2475227164 |
544 | 562 | tmp = iface._getOrCreateByNum(2475227164) |
545 | 563 | assert tmp['num'] == 2475227164 |
| 564 | + |
| 565 | + |
| 566 | +@pytest.mark.unit |
| 567 | +def test_enter(): |
| 568 | + """Test __enter__()""" |
| 569 | + iface = MeshInterface(noProto=True) |
| 570 | + assert iface == iface.__enter__() |
| 571 | + |
| 572 | + |
| 573 | +@pytest.mark.unit |
| 574 | +def test_exit_with_exception(caplog): |
| 575 | + """Test __exit__()""" |
| 576 | + iface = MeshInterface(noProto=True) |
| 577 | + with caplog.at_level(logging.ERROR): |
| 578 | + iface.__exit__('foo', 'bar', 'baz') |
| 579 | + assert re.search(r'An exception of type foo with value bar has occurred', caplog.text, re.MULTILINE) |
| 580 | + assert re.search(r'Traceback: baz', caplog.text, re.MULTILINE) |
| 581 | + |
| 582 | + |
| 583 | +@pytest.mark.unit |
| 584 | +def test_showNodes_exclude_self(capsys, caplog, reset_globals, iface_with_nodes): |
| 585 | + """Test that we hit that continue statement""" |
| 586 | + with caplog.at_level(logging.DEBUG): |
| 587 | + iface = iface_with_nodes |
| 588 | + iface.localNode.nodeNum = 2475227164 |
| 589 | + iface.showNodes() |
| 590 | + iface.showNodes(includeSelf=False) |
| 591 | + capsys.readouterr() |
| 592 | + |
| 593 | + |
| 594 | +@pytest.mark.unit |
| 595 | +def test_waitForConfig(caplog, capsys): |
| 596 | + """Test waitForConfig()""" |
| 597 | + iface = MeshInterface(noProto=True) |
| 598 | + # override how long to wait |
| 599 | + iface._timeout = Timeout(0.01) |
| 600 | + with pytest.raises(Exception) as pytest_wrapped_e: |
| 601 | + iface.waitForConfig() |
| 602 | + assert pytest_wrapped_e.type == Exception |
| 603 | + out, err = capsys.readouterr() |
| 604 | + assert re.search(r'Exception: Timed out waiting for interface config', err, re.MULTILINE) |
| 605 | + assert out == '' |
| 606 | + |
| 607 | + |
| 608 | +@pytest.mark.unit |
| 609 | +def test_waitConnected_raises_an_exception(caplog, capsys): |
| 610 | + """Test waitConnected()""" |
| 611 | + iface = MeshInterface(noProto=True) |
| 612 | + with pytest.raises(Exception) as pytest_wrapped_e: |
| 613 | + iface.failure = "warn about something" |
| 614 | + iface._waitConnected(0.01) |
| 615 | + assert pytest_wrapped_e.type == Exception |
| 616 | + out, err = capsys.readouterr() |
| 617 | + assert re.search(r'warn about something', err, re.MULTILINE) |
| 618 | + assert out == '' |
| 619 | + |
| 620 | + |
| 621 | +@pytest.mark.unit |
| 622 | +def test_waitConnected_isConnected_timeout(caplog, capsys): |
| 623 | + """Test waitConnected()""" |
| 624 | + with pytest.raises(Exception) as pytest_wrapped_e: |
| 625 | + iface = MeshInterface() |
| 626 | + iface._waitConnected(0.01) |
| 627 | + assert pytest_wrapped_e.type == Exception |
| 628 | + out, err = capsys.readouterr() |
| 629 | + assert re.search(r'warn about something', err, re.MULTILINE) |
| 630 | + assert out == '' |
0 commit comments