コード例 #1
0
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)
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
def api():
    api = xbee_api.XBee()
    api._uart = mock.MagicMock()
    return api
コード例 #5
0
async def test_connect(monkeypatch):
    api = xbee_api.XBee(DEVICE_CONFIG)
    monkeypatch.setattr(uart, "connect", mock.AsyncMock())
    await api.connect()
コード例 #6
0
def api():
    api = xbee_api.XBee(DEVICE_CONFIG)
    api._uart = mock.MagicMock()
    return api