Exemplo n.º 1
0
async def test_on_welcome(app, init_app, client, mocker, welcome):
    conn = connection.fget(app)
    status = service_status.fget(app)
    status.set_autoconnecting(True)

    ok_msg = ','.join(welcome)
    nok_welcome = welcome.copy()
    nok_welcome[2] = 'NOPE'
    nok_msg = ','.join(nok_welcome)
    conn._on_event(ok_msg)
    assert status.desc().handshake_info.is_compatible_firmware

    status.service_info.device_id = '1234567f0case'
    conn._on_event(ok_msg)
    assert status.desc().handshake_info.is_valid_device_id

    status.service_info.device_id = '01345'
    with pytest.warns(UserWarning, match='Handshake error'):
        conn._on_event(ok_msg)
    assert not status.desc().handshake_info.is_valid_device_id

    status.service_info.device_id = None
    app['config']['skip_version_check'] = True
    conn._on_event(nok_msg)
    assert status.desc().handshake_info.is_compatible_firmware

    app['config']['skip_version_check'] = False
    with pytest.warns(UserWarning, match='Handshake error'):
        conn._on_event(nok_msg)
    assert not status.desc().handshake_info.is_compatible_firmware
    assert not status.desc().is_synchronized
Exemplo n.º 2
0
async def test_callback(app, init_app, client, m_reader, m_writer):
    service_status.set_autoconnecting(app, True)
    await asyncio.sleep(0.01)
    conn = connection.fget(app)
    m_data_cb = Mock()
    m_data_cb2 = Mock()

    conn.data_callbacks.add(m_data_cb)
    conn.data_callbacks.add(m_data_cb2)

    m_reader.feed_data('<!connected:sensor>bunnies<fluffy>\n'.encode())
    await asyncio.sleep(0.01)
    m_data_cb.assert_called_with('bunnies')
    m_data_cb2.assert_called_with('bunnies')

    # Close it down
    m_reader.feed_data('puppies\n'.encode())
    m_writer.is_closing.return_value = True

    await asyncio.sleep(0.01)
    m_data_cb.assert_called_with('puppies')
    m_data_cb2.assert_called_with('puppies')
    assert conn.active
    assert not conn.connected
    assert not service_status.desc(app).is_connected
async def test_write(app, init_app, client, m_writer):
    service_status.set_autoconnecting(app, True)
    await asyncio.sleep(0.01)
    conn = connection.fget(app)
    assert conn.connected
    assert service_status.desc(app).is_connected
    assert service_status.desc(app).connection_kind == 'usb'

    await conn.write('testey')
    m_writer.write.assert_called_once_with(b'testey\n')
    m_writer.drain.assert_awaited_once()

    m_writer.reset_mock()
    await conn.write(b'testey')
    m_writer.write.assert_called_once_with(b'testey\n')
    m_writer.drain.assert_awaited_once()

    await conn.start_reconnect()
    m_writer.close.assert_called_once()

    m_writer.is_closing.return_value = True
    assert not conn.connected

    await conn.start_reconnect()
    m_writer.close.assert_called_once()

    with pytest.raises(exceptions.NotConnected):
        await conn.write('stuff')
async def test_on_welcome(app, init_app, client, mocker, welcome):
    mocker.patch(TESTED + '.web.GracefulExit', DummyExit)
    conn = connection.fget(app)
    status = service_status.fget(app)
    status.set_autoconnecting(True)

    ok_msg = ','.join(welcome)
    nok_welcome = welcome.copy()
    nok_welcome[2] = 'NOPE'
    nok_msg = ','.join(nok_welcome)
    await conn._on_event(ok_msg)
    assert status.desc().handshake_info.is_compatible_firmware
    status.service_info.device_id = '1234567f0case'
    await conn._on_event(ok_msg)
    assert status.desc().handshake_info.is_valid_device_id

    status.service_info.device_id = '01345'
    with pytest.warns(UserWarning, match='Handshake error'):
        await conn._on_event(ok_msg)
    assert not status.desc().handshake_info.is_valid_device_id

    status.service_info.device_id = None
    app['config']['skip_version_check'] = True
    await conn._on_event(nok_msg)
    assert status.desc().handshake_info.is_compatible_firmware

    app['config']['skip_version_check'] = False
    with pytest.warns(UserWarning, match='Handshake error'):
        await conn._on_event(nok_msg)
    assert not status.desc().handshake_info.is_compatible_firmware
    assert not status.desc().is_synchronized

    with pytest.raises(DummyExit):
        await conn._on_event(connection.SETUP_MODE_PREFIX)
Exemplo n.º 5
0
    def __init__(self, app: web.Application):
        super().__init__(app)

        self._msgid = 0
        self._timeout = app['config']['command_timeout']
        self._active_messages: dict[int, asyncio.Future] = {}
        self._codec = codec.fget(app)
        self._conn = connection.fget(app)
Exemplo n.º 6
0
async def test_error_callback(app, init_app, client, m_reader, m_writer):
    service_status.set_autoconnecting(app, True)
    conn = connection.fget(app)
    m_data_cb = Mock(side_effect=RuntimeError)
    conn.data_callbacks.add(m_data_cb)

    m_reader.feed_data('<!connected:sensor>bunnies<fluffy>\n'.encode())
    await asyncio.sleep(0.01)
    m_data_cb.assert_called_with('bunnies')
    assert conn.connected
Exemplo n.º 7
0
async def test_on_cbox_err(app, init_app, client, cbox_err):
    conn = connection.fget(app)
    status = service_status.fget(app)
    status.set_connected('addr')
    assert status.desc().device_address == 'addr'

    msg = ':'.join(cbox_err)
    conn._on_event(msg)

    # shouldn't fail on non-existent error message
    msg = ':'.join([cbox_err[0], 'ffff'])
    conn._on_event(msg)

    # shouldn't fail on invalid error
    msg = ':'.join([cbox_err[0], 'not hex'])
    conn._on_event(msg)
Exemplo n.º 8
0
 async def startup(self, app: web.Application):
     self._requests.clear()
     self._conn = connection.fget(app)
     self._conn.data_callbacks.add(self.data_callback)
Exemplo n.º 9
0
async def test_on_setup_mode(app, init_app, client, m_exit):
    conn = connection.fget(app)
    with pytest.raises(DummyExit):
        conn._on_event('SETUP_MODE')