async def test_connect(monkeypatch): api = xbee_api.XBee() dev = mock.MagicMock() monkeypatch.setattr( uart, 'connect', mock.MagicMock(side_effect=asyncio.coroutine(mock.MagicMock()))) await api.connect(dev, 115200)
async def test_reconnect_multiple_disconnects(monkeypatch, caplog): api = xbee_api.XBee(DEVICE_CONFIG) connect_mock = mock.AsyncMock(return_value=True) monkeypatch.setattr(uart, "connect", connect_mock) await api.connect() caplog.set_level(logging.DEBUG) connect_mock.reset_mock() connect_mock.side_effect = [OSError, mock.sentinel.uart_reconnect] api.connection_lost("connection lost") await asyncio.sleep(0.3) api.connection_lost("connection lost 2") await asyncio.sleep(0.3) assert "Cancelling reconnection attempt" in caplog.messages assert api._uart is mock.sentinel.uart_reconnect assert connect_mock.call_count == 2
async def test_reconnect_multiple_attempts(monkeypatch, caplog): api = xbee_api.XBee(DEVICE_CONFIG) connect_mock = mock.AsyncMock(return_value=True) monkeypatch.setattr(uart, "connect", connect_mock) await api.connect() caplog.set_level(logging.DEBUG) connect_mock.reset_mock() connect_mock.side_effect = [ asyncio.TimeoutError, OSError, mock.sentinel.uart_reconnect, ] with mock.patch("asyncio.sleep"): api.connection_lost("connection lost") await api._conn_lost_task assert api._uart is mock.sentinel.uart_reconnect assert connect_mock.call_count == 3
def api(): api = xbee_api.XBee() api._uart = mock.MagicMock() return api
async def test_connect(monkeypatch): api = xbee_api.XBee(DEVICE_CONFIG) monkeypatch.setattr(uart, "connect", mock.AsyncMock()) await api.connect()
def api(): api = xbee_api.XBee(DEVICE_CONFIG) api._uart = mock.MagicMock() return api