Ejemplo n.º 1
0
async def test_global_components(server_address, conn_count):
    conf = {'address': server_address, 'default_rank': 123}

    components = [
        common.ComponentInfo(cid=i * 3,
                             mid=i * 3 + 1,
                             name=f'name{i}',
                             group=f'group{i}',
                             address=f'address{i}',
                             rank=i * 3 + 2,
                             blessing=i * 3 + 3,
                             ready=i * 3 + 4) for i in range(10)
    ]

    server = await hat.monitor.server.server.create(conf)
    server.update(0, [])

    conns = []
    for _ in range(conn_count):
        conn = await connect(server_address)
        conns.append(conn)

        msg = await conn.receive()
        assert msg.mid == 0
        assert msg.components == []

    server.update(1, components)
    for conn in conns:
        msg = await conn.receive()
        assert msg.mid == 1
        assert msg.components == components

    server.update(2, components)
    for conn in conns:
        msg = await conn.receive()
        assert msg.mid == 2
        assert msg.components == components

    server.update(2, [])
    for conn in conns:
        msg = await conn.receive()
        assert msg.mid == 2
        assert msg.components == []

    server.update(2, [])
    for conn in conns:
        with pytest.raises(asyncio.TimeoutError):
            await asyncio.wait_for(conn.receive(), 0.001)

    await server.async_close()

    while conns:
        conn, conns = conns[0], conns[1:]
        await conn.wait_closed()
Ejemplo n.º 2
0
async def test_global_components(tmp_path, patch_autoflush_delay, ui_address,
                                 ui_juggler_address, conn_count):
    conf = {'address': ui_address}

    info = common.ComponentInfo(cid=1,
                                mid=2,
                                name='name',
                                group='group',
                                address='address',
                                rank=3,
                                blessing=4,
                                ready=5)

    server = Server()
    ui = await hat.monitor.server.ui.create(conf, tmp_path, server)

    conns = []
    for _ in range(conn_count):
        conn = await connect(ui_juggler_address)
        conns.append(conn)

        state = await conn.change_queue.get()
        assert state['global_components'] == []

    server.set_global_components([info])

    for conn in conns:
        state = await conn.change_queue.get()
        assert state['global_components'] == [{
            'cid': info.cid,
            'mid': info.mid,
            'name': info.name,
            'group': info.group,
            'address': info.address,
            'rank': info.rank,
            'blessing': info.blessing,
            'ready': info.ready
        }]

    server.set_global_components([])

    for conn in conns:
        state = await conn.change_queue.get()
        assert state['global_components'] == []

    while conns:
        conn, conns = conns[0], conns[1:]
        await conn.async_close()

    await ui.async_close()
Ejemplo n.º 3
0
async def test_slaves(master_address, slave_count):
    conf = {'address': master_address,
            'default_algorithm': 'BLESS_ALL',
            'group_algorithms': {}}

    infos = [common.ComponentInfo(cid=i * 3,
                                  mid=i * 3 + 1,
                                  name=f'name{i}',
                                  group=f'group{i}',
                                  address=f'address{i}',
                                  rank=i * 3 + 2,
                                  blessing=None,
                                  ready=None)
             for i in range(3)]

    server = Server()
    master = await hat.monitor.server.master.create(conf)
    await master.set_server(server)

    conns = []
    for _ in range(slave_count):
        conn = await connect(master_address)
        conns.append(conn)

        msg = await conn.receive()
        assert msg.mid > 0
        assert msg.components == []

    for i, conn in enumerate(conns):
        msg = common.MsgSlave(infos)
        conn.send(msg)

        for conn in conns:
            msg = await conn.receive()
            assert len(msg.components) == len(infos) * (i + 1)

    assert len(master.components) == len(conns) * len(infos)

    while conns:
        conn, conns = conns[0], conns[1:]
        await conn.async_close()

        for conn in conns:
            msg = await conn.receive()
            assert len(msg.components) == len(infos) * len(conns)

    await master.async_close()
    await server.async_close()
Ejemplo n.º 4
0
    def _create_client(self, conn):
        self._last_cid += 1
        cid = self._last_cid

        client = _Client(self, conn, cid)
        self.async_group.spawn(client.client_loop)

        info = common.ComponentInfo(cid=cid,
                                    mid=self._mid,
                                    name=None,
                                    group=None,
                                    address=None,
                                    rank=self._default_rank,
                                    blessing=None,
                                    ready=None)
        self._local_components = [*self._local_components, info]
        self._change_cbs.notify()
Ejemplo n.º 5
0
async def test_slave(primary_address):
    info = common.ComponentInfo(cid=1,
                                mid=2,
                                name='name',
                                group='group',
                                address='address',
                                rank=3,
                                blessing=None,
                                ready=None)

    server = Server()
    master = await create_master(primary_address)
    slave = await hat.monitor.server.slave.connect(addresses=[primary_address],
                                                   retry_count=3)
    slave = hat.monitor.server.slave.Slave(server, slave)
    conn = await master.get_connection()

    assert server.mid_queue.empty()
    assert server.global_components_queue.empty()
    assert server.local_components_queue.empty()

    msg = common.MsgMaster(mid=123,
                           components=[info])
    conn.send(msg)

    mid = await server.mid_queue.get()
    assert mid == msg.mid

    components = await server.global_components_queue.get()
    assert components == [info]

    msg = await conn.receive()
    assert msg.components == []

    server.set_local_components([info])

    msg = await conn.receive()
    assert msg.components == [info._replace(mid=123)]

    await master.async_close()
    await slave.wait_closed()
Ejemplo n.º 6
0
async def test_set_server(master_address):
    conf = {'address': master_address,
            'default_algorithm': 'BLESS_ALL',
            'group_algorithms': {}}

    infos = [common.ComponentInfo(cid=i * 3,
                                  mid=i * 3 + 1,
                                  name=f'name{i}',
                                  group=f'group{i}',
                                  address=f'address{i}',
                                  rank=i * 3 + 2,
                                  blessing=None,
                                  ready=None)
             for i in range(10)]

    server = Server()

    changes = aio.Queue()

    def on_change():
        changes.put_nowait(master.components)

    master = await hat.monitor.server.master.create(conf)
    master.register_change_cb(on_change)

    assert not master.active

    await master.set_server(server)

    components = await changes.get()
    assert components == []
    assert master.active
    assert server.global_components == components

    server.set_local_components(infos)

    components = await changes.get()
    assert len(components) == len(infos)
    assert master.active
    assert server.global_components == components
    for info, component in zip(infos, components):
        assert component.mid == 0
        assert component.cid == info.cid
        assert component.name == info.name
        assert component.group == info.group
        assert component.address == info.address
        assert component.rank == info.rank
        assert component.blessing is not None
        assert component.ready == info.ready

    await master.set_server(None)

    components = await changes.get()
    assert components == []
    assert not master.active

    await master.set_server(server)

    components = await changes.get()
    assert components == []
    assert master.active

    components = await changes.get()
    assert len(components) == len(infos)
    assert master.active
    assert server.global_components == components

    await master.async_close()
    await server.async_close()
Ejemplo n.º 7
0
import pytest

from hat.monitor.server import common

infos = [
    common.ComponentInfo(cid=1,
                         mid=2,
                         name='name',
                         group='group',
                         address='address',
                         rank=3,
                         blessing=4,
                         ready=5),
    common.ComponentInfo(cid=1,
                         mid=2,
                         name='name',
                         group='group',
                         address=None,
                         rank=3,
                         blessing=None,
                         ready=None)
]

msg_clients = [
    common.MsgClient(name='name', group='group', address='address', ready=1),
    common.MsgClient(name='name', group='group', address=None, ready=None)
]

msg_servers = [
    common.MsgServer(cid=1, mid=2, components=infos),
    common.MsgServer(cid=1, mid=2, components=[])