async def test_server_master_set_rank(server_conf):
    server = await hat.monitor.server.server.create(server_conf)
    master = common.create_master()
    server.set_master(master)
    server.set_rank(1, 0, 10)
    assert (await master._rank_queue.get()) == (1, 0, 10)
    server.set_rank(3, 2, 5)
    assert (await master._rank_queue.get()) == (3, 2, 5)
    await server.async_close()
    assert server.closed.done()
async def test_server_rank_cache(server_conf, client_conf):
    async def client_connect_info_queue():
        info_queue = aio.Queue()
        client = await hat.monitor.client.connect(client_conf)
        client.register_change_cb(lambda: info_queue.put_nowait(client.info))
        return client, info_queue

    async def get_info_until_not_none(info_queue):
        info = None
        while info is None:
            info = await info_queue.get()
        return info

    server = await hat.monitor.server.server.create(server_conf)
    client, info_queue = await client_connect_info_queue()
    info = await get_info_until_not_none(info_queue)
    assert info.rank == 1
    server.set_rank(info.cid, info.mid, 2)
    assert (await info_queue.get()).rank == 2
    await client.async_close()
    assert client.closed.done()

    client, info_queue = await client_connect_info_queue()
    info = await get_info_until_not_none(info_queue)
    assert info.rank == 2

    master = common.create_master(
        mid=server.mid, components=[server.components[0]._replace(rank=3)])

    server.set_master(master)
    assert (await info_queue.get()).rank == 3
    await client.async_close()
    assert client.closed.done()
    server.set_master(None)

    client, info_queue = await client_connect_info_queue()
    info = await get_info_until_not_none(info_queue)
    assert info.rank == 3

    master = common.create_master(
        mid=server.mid, components=[server.components[0]._replace(rank=4)])

    server.set_master(master)
    assert (await info_queue.get()).rank == 4
    master._set_components([i._replace(rank=5) for i in master.components])
    assert (await info_queue.get()).rank == 5
    await client.async_close()
    assert client.closed.done()
    server.set_master(None)

    client, info_queue = await client_connect_info_queue()
    info = await get_info_until_not_none(info_queue)
    assert info.rank == 5
    await client.async_close()
    assert client.closed.done()
async def test_server_set_rank(server_conf, client_conf):
    components_queue = aio.Queue()
    server = await hat.monitor.server.server.create(server_conf)
    server.register_change_cb(
        lambda: components_queue.put_nowait(server.components))
    client = await hat.monitor.client.connect(client_conf)
    c = (await components_queue.get())[0]
    assert c.rank == 1
    server.set_rank(c.cid, c.mid, 2)
    assert (await components_queue.get())[0].rank == 2
    server.set_rank(c.cid, c.mid, -11)
    assert (await components_queue.get())[0].rank == -11
    await client.async_close()
    assert client.closed.done()
    await server.async_close()
    assert server.closed.done()
Beispiel #4
0
async def test_set_rank(server_address):
    conf = {'address': server_address, 'default_rank': 123}

    msg = common.MsgClient(name='name',
                           group='group',
                           address=None,
                           ready=None)

    changes = aio.Queue()

    def on_change():
        changes.put_nowait(server.local_components)

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

    server.set_rank(123, 321)
    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(changes.get(), 0.001)

    conn = await connect(server_address)

    components = await changes.get()
    assert len(components) == 1
    info = components[0]
    assert info.name is None
    assert info.rank == conf['default_rank']

    server.set_rank(info.cid, 321)

    components = await changes.get()
    assert len(components) == 1
    info = components[0]
    assert info.rank == 321

    await conn.async_close()

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

    conn = await connect(server_address)

    components = await changes.get()
    assert len(components) == 1
    info = components[0]
    assert info.name is None
    assert info.rank == conf['default_rank']

    conn.send(msg)

    components = await changes.get()
    assert len(components) == 1
    info = components[0]
    assert info.name == msg.name
    assert info.rank == conf['default_rank']

    server.set_rank(info.cid, 321)

    components = await changes.get()
    assert len(components) == 1
    info = components[0]
    assert info.rank == 321

    await conn.async_close()

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

    conn = await connect(server_address)

    components = await changes.get()
    assert len(components) == 1
    info = components[0]
    assert info.name is None
    assert info.rank == conf['default_rank']

    conn.send(msg)

    components = await changes.get()
    assert len(components) == 1
    info = components[0]
    assert info.name == msg.name
    assert info.rank == 321

    await conn.async_close()

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

    await server.async_close()
async def test_server_set_rank_nonexistent(server_conf):
    server = await hat.monitor.server.server.create(server_conf)
    server.set_rank(1, server.mid, 2)
    await server.async_close()
    assert server.closed.done()