def __init__(self, conf: json.Data, logger: common.Logger): self._logger = logger self._async_group = aio.Group() self._run_subgroup = self.async_group.create_subgroup() self._autostart_subgroup = self.async_group.create_subgroup() device_logger = common.Logger() handler = device_logger.register_log_cb(self._log) self._async_group.spawn(aio.call_on_cancel, handler.cancel) self._device = devices.create_device(conf, device_logger) self._data = common.DataStorage({'type': conf['type'], 'name': conf['name'], 'autostart': conf['autostart'], 'status': _Status.STOPPED.value, 'data': self._device.data.data}) on_change = functools.partial(self._data.set, 'data') handler = self._device.data.register_change_cb(on_change) self._async_group.spawn(aio.call_on_cancel, handler.cancel) self._log('device created') self._async_group.spawn(aio.call_on_cancel, self._log, 'removing device') if self._data.data['autostart']: self._run_autostart()
async def test_create(addr, server): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.orchestrator.Device(conf, logger) client = await device.create() assert client.is_open await client.async_close()
async def test_set_address(): conf = {'address': 'addr1'} logger = common.Logger() device = hat.manager.devices.orchestrator.Device(conf, logger) assert device.data.data['address'] == 'addr1' await device.execute('set_address', 'addr2') assert device.data.data['address'] == 'addr2' new_conf = device.get_conf() assert new_conf['address'] == 'addr2'
async def test_create(addr, server): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.event.Device(conf, logger) client = await device.create() assert client.is_open conn = await server.connection_queue.get() assert conn.is_open await client.async_close() await conn.wait_closing()
async def test_stop(addr, server): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.orchestrator.Device(conf, logger) client = await device.create() await device.execute('stop', 123) msg = await server.receive_queue.get() assert msg == {'type': 'stop', 'payload': {'id': 123}} await client.async_close()
async def test_set_rank(addr, server): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.monitor.Device(conf, logger) client = await device.create() await device.execute('set_rank', 1234, 4321) msg = await server.receive_queue.get() assert msg == {'type': 'set_rank', 'payload': {'cid': 1234, 'rank': 4321}} await client.async_close()
async def test_device_data(addr, server): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.monitor.Device(conf, logger) device_queue = aio.Queue() device.data.register_change_cb(device_queue.put_nowait) client = await device.create() await device_queue.get() assert device.data.data == { 'address': addr, 'mid': 0, 'local_components': [], 'global_components': [] } local_components = [{ 'cid': 1, 'name': 'name1', 'group': 'group1', 'address': 'address1', 'rank': 2 }] global_components = [{ 'cid': 3, 'mid': 4, 'name': 'name2', 'group': 'group2', 'address': 'address2', 'rank': 5, 'blessing': 6, 'ready': 7 }] data = { 'mid': 8, 'local_components': local_components, 'global_components': global_components } server.change_data(data) await device_queue.get() assert device.data.data['mid'] == data['mid'] assert device.data.data['local_components'] == data['local_components'] assert device.data.data['global_components'] == data['global_components'] await client.async_close()
async def test_query(addr, server): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.event.Device(conf, logger) latest_queue = create_change_queue(device.data, 'latest') client = await device.create() conn = await server.connection_queue.get() latest = await latest_queue.get() assert latest == [] msg = await conn.receive() assert msg.first is True assert msg.last is True assert msg.data.module == 'HatEvent' assert msg.data.type == 'MsgSubscribe' assert msg.data.data == [['*']] msg = await conn.receive() assert msg.first is True assert msg.last is False assert msg.data.module == 'HatEvent' assert msg.data.type == 'MsgQueryReq' events = [ create_event(('a', 'b', 'c'), 123), create_event(('c', 'b', 'a'), 321) ] data = [hat.event.common.event_to_sbs(event) for event in events] conn.send(chatter.Data('HatEvent', 'MsgQueryRes', data), conv=msg.conv) latest = await latest_queue.get() assert len(events) == len(latest) events = sorted(events, key=lambda i: i.event_type) latest = sorted(latest, key=lambda i: i['event_type']) for event, i in zip(events, latest): assert event.event_id.server == i['event_id']['server'] assert event.event_id.instance == i['event_id']['instance'] assert list(event.event_type) == i['event_type'] assert event.payload.data == i['payload'] await client.async_close()
async def test_register(addr, server, text, register_events, with_source_timestamp): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.event.Device(conf, logger) client = await device.create() conn = await server.connection_queue.get() msg = await conn.receive() assert msg.first is True assert msg.last is True assert msg.data.module == 'HatEvent' assert msg.data.type == 'MsgSubscribe' assert msg.data.data == [['*']] msg = await conn.receive() assert msg.first is True assert msg.last is False assert msg.data.module == 'HatEvent' assert msg.data.type == 'MsgQueryReq' conn.send(chatter.Data('HatEvent', 'MsgQueryRes', []), conv=msg.conv) await device.execute('register', text, with_source_timestamp) msg = await conn.receive() assert msg.first is True assert msg.last is True assert msg.data.module == 'HatEvent' assert msg.data.type == 'MsgRegisterReq' events = [ hat.event.common.register_event_from_sbs(i) for i in msg.data.data ] assert all( ((i.source_timestamp is not None) if with_source_timestamp else ( i.source_timestamp is None)) for i in events) events = [i._replace(source_timestamp=None) for i in events] assert events == register_events await client.async_close()
async def test_components(addr, server, component_count): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.orchestrator.Device(conf, logger) client = await device.create() components_queue = create_change_queue(device.data, 'components') result = await components_queue.get() assert result == [] components = [{'id': i, 'name': f'name{i}', 'delay': 0, 'revive': False, 'status': 'STOPPED'} for i in range(component_count)] server.data.set('components', components) result = await components_queue.get() assert result == components await client.async_close()
async def create_server(conf: json.Data, conf_path: Path, ui_path: Path ) -> 'Server': """Create server Args: conf: configuration defined by ``hat://manager/main.yaml#`` conf_path: configuration file path ui_path: static web ui frontent directory path """ addr = urllib.parse.urlparse(conf['settings']['ui']['address']) server = Server() server._conf_path = conf_path server._devices = {} server._next_device_ids = (str(i) for i in itertools.count(1)) server._data = common.DataStorage({'log': [], 'devices': {}, 'settings': conf['settings']}) server._srv = await juggler.listen(host=addr.hostname, port=addr.port, connection_cb=server._on_connection, static_dir=ui_path, autoflush_delay=autoflush_delay) server._logger = common.Logger() handler = server._logger.register_log_cb(server._on_log) server.async_group.spawn(aio.call_on_cancel, handler.cancel) for device_conf in conf['devices']: server._create_device(device_conf) return server
async def test_changes(addr, server): conf = {'address': addr} logger = common.Logger() device = hat.manager.devices.event.Device(conf, logger) changes_queue = create_change_queue(device.data, 'changes') client = await device.create() conn = await server.connection_queue.get() changes = await changes_queue.get() assert changes == [] msg = await conn.receive() assert msg.first is True assert msg.last is True assert msg.data.module == 'HatEvent' assert msg.data.type == 'MsgSubscribe' assert msg.data.data == [['*']] msg = await conn.receive() assert msg.first is True assert msg.last is False assert msg.data.module == 'HatEvent' assert msg.data.type == 'MsgQueryReq' conn.send(chatter.Data('HatEvent', 'MsgQueryRes', []), conv=msg.conv) events = [create_event(('a', 'b', 'c'), 1)] data = [hat.event.common.event_to_sbs(event) for event in events] conn.send(chatter.Data('HatEvent', 'MsgNotify', data), conv=msg.conv) changes = await changes_queue.get() assert len(changes) == 1 data = changes[0] assert data['event_type'] == ['a', 'b', 'c'] assert data['payload'] == 1 events = [create_event(('c', 'b', 'a'), 2)] data = [hat.event.common.event_to_sbs(event) for event in events] conn.send(chatter.Data('HatEvent', 'MsgNotify', data), conv=msg.conv) changes = await changes_queue.get() assert len(changes) == 2 data = changes[0] assert data['event_type'] == ['c', 'b', 'a'] assert data['payload'] == 2 data = changes[1] assert data['event_type'] == ['a', 'b', 'c'] assert data['payload'] == 1 events = [create_event(('a', 'b', 'c'), 3)] data = [hat.event.common.event_to_sbs(event) for event in events] conn.send(chatter.Data('HatEvent', 'MsgNotify', data), conv=msg.conv) changes = await changes_queue.get() assert len(changes) == 3 data = changes[0] assert data['event_type'] == ['a', 'b', 'c'] assert data['payload'] == 3 data = changes[1] assert data['event_type'] == ['c', 'b', 'a'] assert data['payload'] == 2 data = changes[2] assert data['event_type'] == ['a', 'b', 'c'] assert data['payload'] == 1 await client.async_close()