11"""Tests for API."""
22
33import asyncio
4- import logging
54
65import pytest
76import serial
7+ import zigpy .config
88import zigpy .exceptions
99import zigpy .types as t
1010
1111from zigpy_xbee import api as xbee_api , types as xbee_t , uart
12- import zigpy_xbee .config
1312from zigpy_xbee .exceptions import ATCommandError , ATCommandException , InvalidCommand
1413from zigpy_xbee .zigbee .application import ControllerApplication
1514
1615import tests .async_mock as mock
1716
18- DEVICE_CONFIG = zigpy_xbee .config .SCHEMA_DEVICE (
19- {zigpy_xbee .config .CONF_DEVICE_PATH : "/dev/null" }
17+ DEVICE_CONFIG = zigpy .config .SCHEMA_DEVICE (
18+ {
19+ zigpy .config .CONF_DEVICE_PATH : "/dev/null" ,
20+ zigpy .config .CONF_DEVICE_BAUDRATE : 57600 ,
21+ }
2022)
2123
2224
@@ -38,13 +40,8 @@ async def test_connect(monkeypatch):
3840def test_close (api ):
3941 """Test connection close."""
4042 uart = api ._uart
41- conn_lost_task = mock .MagicMock ()
42- api ._conn_lost_task = conn_lost_task
43-
4443 api .close ()
4544
46- assert api ._conn_lost_task is None
47- assert conn_lost_task .cancel .call_count == 1
4845 assert api ._uart is None
4946 assert uart .close .call_count == 1
5047
@@ -602,51 +599,6 @@ def test_handle_many_to_one_rri(api):
602599 api ._handle_many_to_one_rri (ieee , nwk , 0 )
603600
604601
605- async def test_reconnect_multiple_disconnects (monkeypatch , caplog ):
606- """Test reconnect with multiple disconnects."""
607- api = xbee_api .XBee (DEVICE_CONFIG )
608- connect_mock = mock .AsyncMock (return_value = True )
609- monkeypatch .setattr (uart , "connect" , connect_mock )
610-
611- await api .connect ()
612-
613- caplog .set_level (logging .DEBUG )
614- connect_mock .reset_mock ()
615- connect_mock .side_effect = [OSError , mock .sentinel .uart_reconnect ]
616- api .connection_lost ("connection lost" )
617- await asyncio .sleep (0.3 )
618- api .connection_lost ("connection lost 2" )
619- await asyncio .sleep (0.3 )
620-
621- assert "Cancelling reconnection attempt" in caplog .messages
622- assert api ._uart is mock .sentinel .uart_reconnect
623- assert connect_mock .call_count == 2
624-
625-
626- async def test_reconnect_multiple_attempts (monkeypatch , caplog ):
627- """Test reconnect with multiple attempts."""
628- api = xbee_api .XBee (DEVICE_CONFIG )
629- connect_mock = mock .AsyncMock (return_value = True )
630- monkeypatch .setattr (uart , "connect" , connect_mock )
631-
632- await api .connect ()
633-
634- caplog .set_level (logging .DEBUG )
635- connect_mock .reset_mock ()
636- connect_mock .side_effect = [
637- asyncio .TimeoutError ,
638- OSError ,
639- mock .sentinel .uart_reconnect ,
640- ]
641-
642- with mock .patch ("asyncio.sleep" ):
643- api .connection_lost ("connection lost" )
644- await api ._conn_lost_task
645-
646- assert api ._uart is mock .sentinel .uart_reconnect
647- assert connect_mock .call_count == 3
648-
649-
650602@mock .patch .object (xbee_api .XBee , "_at_command" , new_callable = mock .AsyncMock )
651603@mock .patch .object (uart , "connect" , return_value = mock .MagicMock ())
652604async def test_probe_success (mock_connect , mock_at_cmd ):
@@ -727,3 +679,17 @@ async def test_xbee_new(conn_mck):
727679 assert isinstance (api , xbee_api .XBee )
728680 assert conn_mck .call_count == 1
729681 assert conn_mck .await_count == 1
682+
683+
684+ @mock .patch .object (xbee_api .XBee , "connect" , return_value = mock .MagicMock ())
685+ async def test_connection_lost (conn_mck ):
686+ """Test `connection_lost` propagataion."""
687+ api = await xbee_api .XBee .new (mock .sentinel .application , DEVICE_CONFIG )
688+ await api .connect ()
689+
690+ app = api ._app = mock .MagicMock ()
691+
692+ err = RuntimeError ()
693+ api .connection_lost (err )
694+
695+ app .connection_lost .assert_called_once_with (err )
0 commit comments