Esempio n. 1
0
async def test_start_stop_device(settings, conf_path, ui_path, addr,
                                 patch_autoflush, patch_device_queue):
    conf = {'settings': settings, 'devices': []}
    srv = await hat.manager.server.create_server(conf, conf_path, ui_path)
    conn = juggler.RpcConnection(await juggler.connect(addr))
    data_devices_queue = create_remote_data_change_queue(conn, 'devices')

    data_devices = await data_devices_queue.get()
    assert data_devices == {}

    device_id = await conn.call('add', 'device_type')
    data_devices = await data_devices_queue.get()
    assert device_id in data_devices
    assert data_devices[device_id]['status'] == 'stopped'

    await conn.call('start', device_id)
    data_devices = await data_devices_queue.get()
    assert data_devices[device_id]['status'] == 'starting'
    data_devices = await data_devices_queue.get()
    assert data_devices[device_id]['status'] == 'started'

    await conn.call('stop', device_id)
    data_devices = await data_devices_queue.get()
    assert data_devices[device_id]['status'] == 'stopping'
    data_devices = await data_devices_queue.get()
    assert data_devices[device_id]['status'] == 'stopped'

    await conn.async_close()
    await srv.async_close()
Esempio n. 2
0
async def test_add_remove_device(settings, conf_path, ui_path, addr,
                                 patch_autoflush, patch_device_queue):
    conf = {'settings': settings, 'devices': []}
    srv = await hat.manager.server.create_server(conf, conf_path, ui_path)
    conn = juggler.RpcConnection(await juggler.connect(addr))
    data_devices_queue = create_remote_data_change_queue(conn, 'devices')

    data_devices = await data_devices_queue.get()
    assert data_devices == {}

    device_id = await conn.call('add', 'device_type')
    data_devices = await data_devices_queue.get()
    assert device_id in data_devices
    assert data_devices[device_id]['type'] == 'device_type'

    await conn.call('set_name', device_id, 'abc')
    data_devices = await data_devices_queue.get()
    assert data_devices[device_id]['name'] == 'abc'

    await conn.call('save')
    new_conf = json.decode_file(conf_path)
    assert len(new_conf['devices']) == 1
    assert new_conf['devices'][0]['type'] == 'device_type'
    assert new_conf['devices'][0]['name'] == 'abc'

    await conn.call('remove', device_id)
    data_devices = await data_devices_queue.get()
    assert data_devices == {}

    await conn.async_close()
    await srv.async_close()
Esempio n. 3
0
async def test_set_settings(settings, conf_path, ui_path, addr,
                            patch_autoflush):
    conf = {'settings': settings, 'devices': []}
    srv = await hat.manager.server.create_server(conf, conf_path, ui_path)
    conn = juggler.RpcConnection(await juggler.connect(addr))
    data_settings_queue = create_remote_data_change_queue(conn, 'settings')

    data_settings = await data_settings_queue.get()
    assert data_settings == settings

    await conn.call('save')
    new_conf = json.decode_file(conf_path)
    assert new_conf['settings'] == settings

    await conn.call('set_settings', ['ui', 'address'], 'abc')
    new_settings = json.set_(settings, ['ui', 'address'], 'abc')
    data_settings = await data_settings_queue.get()
    assert data_settings == new_settings

    await conn.call('save')
    new_conf = json.decode_file(conf_path)
    assert new_conf['settings'] == new_settings

    await conn.async_close()
    await srv.async_close()
Esempio n. 4
0
async def test_rpc(server_port):
    async with create_conn_pair(server_port, 0.001) as conn_pair:
        conn1, conn2 = conn_pair
        actions = {'act': lambda x: x + 1}
        rpc1 = juggler.RpcConnection(conn1, actions)
        rpc2 = juggler.RpcConnection(conn2, actions)

        result = await rpc1.call('act', 123)
        assert result == 124

        result = await rpc2.call('act', 321)
        assert result == 322

        with pytest.raises(Exception):
            await rpc1.call('act', '123')

        with pytest.raises(Exception):
            await rpc1.call('not act', 321)
Esempio n. 5
0
 def _on_connection(self, conn):
     conn = juggler.RpcConnection(conn, {
         'set_settings': self._rpc_set_settings,
         'save': self._rpc_save,
         'add': self._rpc_add,
         'remove': self._rpc_remove,
         'start': self._rpc_start,
         'stop': self._rpc_stop,
         'set_name': self._rpc_set_name,
         'set_autostart': self._rpc_set_autostart,
         'execute': self._rpc_execute})
     conn.async_group.spawn(self._connection_loop, conn)
Esempio n. 6
0
async def test_execute(settings, conf_path, ui_path, addr, patch_autoflush,
                       patch_device_queue):
    conf = {'settings': settings, 'devices': []}
    srv = await hat.manager.server.create_server(conf, conf_path, ui_path)
    conn = juggler.RpcConnection(await juggler.connect(addr))
    device_id = await conn.call('add', 'device_type')

    result = await conn.call('execute', device_id, 'abc', 123)
    assert result == {'action': 'abc', 'args': [123]}

    await conn.async_close()
    await srv.async_close()
Esempio n. 7
0
async def test_save(settings, conf_path, ui_path, addr):
    conf = {'settings': settings, 'devices': []}
    srv = await hat.manager.server.create_server(conf, conf_path, ui_path)
    conn = juggler.RpcConnection(await juggler.connect(addr))

    assert conf_path.exists() is False

    await conn.call('save')

    assert conf_path.exists() is True

    new_conf = json.decode_file(conf_path)

    assert new_conf['settings'] == conf['settings']
    assert new_conf['devices'] == conf['devices']

    await conn.async_close()
    await srv.async_close()