async def test_check_connection(app, client, mocker):
    sim = connection_sim.fget(app)
    ctrl = controller.fget(app)
    cmder = commander.fget(app)

    s_noop = mocker.spy(cmder, 'noop')
    s_reconnect = mocker.spy(cmder, 'start_reconnect')

    await ctrl.noop()
    await ctrl._check_connection()
    assert s_noop.await_count == 2
    assert s_reconnect.await_count == 0

    cmder._timeout = 0.1
    with pytest.raises(exceptions.CommandTimeout):
        sim.next_error.append(None)
        await ctrl.noop()

    await asyncio.sleep(0.01)
    assert s_noop.await_count == 4

    with pytest.raises(exceptions.CommandTimeout):
        sim.next_error += [None, ErrorCode.INSUFFICIENT_HEAP]
        await ctrl.noop()

    await asyncio.sleep(0.01)
    assert s_reconnect.await_count == 1

    # Should be a noop if not connected
    await sim.end()
    await ctrl._check_connection()
    assert s_noop.await_count == 6
 def __init__(self, app: web.Application):
     super().__init__(app)
     self.name = app['config']['name']
     self.controller = controller.fget(app)
     self.listeners = {
         '/create': self._create,
         '/write': self._write,
         '/patch': self._patch,
         '/delete': self._delete,
     }
async def test_resolve_data_ids(app, client, store):
    store['eeney', 9001] = dict()
    store['miney', 9002] = dict()
    store['moo', 9003] = dict()

    def create_data():
        return {
            'testval': 1,
            'input<ProcessValueInterface>': 'eeney',
            'output<ProcessValueInterface>': 'miney',
            'nested': {
                'flappy<>': 'moo',
                'meaning_of_life': 42,
                'mystery<EdgeCase>': None
            },
            'listed': [{
                'flappy<SetpointSensorPairInterface>': 'moo'
            }],
            'metavalue': {
                '__bloxtype': 'Link',
                'id': 'eeney',
            },
        }

    ctrl = controller.fget(app)
    data = create_data()
    controller.resolve_data_ids(data, ctrl._find_nid)

    assert data == {
        'testval': 1,
        'input<ProcessValueInterface>': 9001,
        'output<ProcessValueInterface>': 9002,
        'nested': {
            'flappy<>': 9003,
            'meaning_of_life': 42,
            'mystery<EdgeCase>': 0,
        },
        'listed': [
            {
                'flappy<SetpointSensorPairInterface>': 9003
            },
        ],
        'metavalue': {
            '__bloxtype': 'Link',
            'id': 9001,
        },
    }

    controller.resolve_data_ids(data, ctrl._find_sid)
    assert data == create_data()

    controller.resolve_data_ids(data, ctrl._find_nid)
    data['input<ProcessValueInterface>'] = 'eeney'
    with pytest.raises(exceptions.DecodeException):
        controller.resolve_data_ids(data, ctrl._find_sid)
async def test_to_block(app, client, store):
    store['alias', 123] = dict()
    store['4-2', 24] = dict()

    ctrl = controller.fget(app)

    assert ctrl._to_block(FirmwareBlock(nid=123, type='',
                                        data={})).id == 'alias'

    # Service ID not found: create placeholder
    generated = ctrl._to_block(FirmwareBlock(nid=456, type='', data={}))
    assert generated.id.startswith(const.GENERATED_ID_PREFIX)
def test_main(mocker, app):
    mocker.patch(TESTED + '.service.run')
    mocker.patch(TESTED + '.service.create_app').return_value = app

    main.main()

    assert None not in [
        commander.fget(app),
        service_store.fget(app),
        block_store.fget(app),
        controller.fget(app),
        mqtt.handler(app),
        broadcaster.fget(app)
    ]
async def test_to_firmware_block(app, client, store):
    store['alias', 123] = dict()
    store['4-2', 24] = dict()

    ctrl = controller.fget(app)

    assert ctrl._to_firmware_block(Block(id='alias', type='',
                                         data={})).nid == 123
    assert ctrl._to_firmware_block(Block(nid=840, type='', data={})).nid == 840

    assert ctrl._to_firmware_block_identity(
        BlockIdentity(id='alias')).nid == 123
    assert ctrl._to_firmware_block_identity(BlockIdentity(nid=840)).nid == 840

    # When both present, NID takes precedence
    assert ctrl._to_firmware_block(Block(id='alias', nid=444, type='',
                                         data={})).nid == 444

    with pytest.raises(exceptions.UnknownId):
        ctrl._to_firmware_block(Block(type='', data={}))

    with pytest.raises(exceptions.UnknownId):
        ctrl._to_firmware_block_identity(BlockIdentity())
async def test_validate_sid_error(sid, app, client):
    with pytest.raises(exceptions.InvalidId):
        controller.fget(app)._validate_sid(sid)
async def test_validate_sid(sid, app, client):
    controller.fget(app)._validate_sid(sid)
Exemple #9
0
 def __init__(self, request: web.Request) -> None:
     super().__init__(request)
     self.app = request.app
     self.controller = controller.fget(request.app)