Пример #1
0
async def test_client_change(server_address):
    conf = {
        'name': 'name',
        'group': 'group',
        'monitor_address': server_address,
        'component_address': 'address'
    }

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

    server = await create_server(server_address)
    client = await hat.monitor.client.connect(conf)
    conn = await server.get_connection()

    changes = aio.Queue()
    client.register_change_cb(lambda: changes.put_nowait(
        (client.info, client.components)))

    assert changes.empty()
    assert client.info is None
    assert client.components == []

    msg = common.MsgServer(cid=1, mid=2, components=[])
    conn.send(msg)
    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(changes.get(), 0.001)

    msg = common.MsgServer(cid=1, mid=2, components=[info])
    conn.send(msg)
    change_info, change_components = await changes.get()
    assert change_info == info
    assert change_components == [info]

    msg = common.MsgServer(cid=1, mid=2, components=[info._replace(cid=3)])
    conn.send(msg)
    change_info, change_components = await changes.get()
    assert change_info is None
    assert change_components == [info._replace(cid=3)]

    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(changes.get(), 0.001)

    await client.async_close()
    await conn.wait_closed()
    await server.async_close()
Пример #2
0
async def test_component_exception(server_address):
    conf = {
        'name': 'name',
        'group': 'group',
        'monitor_address': server_address,
        'component_address': 'address'
    }

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

    async def async_run(component):
        raise Exception()

    server = await create_server(server_address)
    client = await hat.monitor.client.connect(conf)
    component = hat.monitor.client.Component(client, async_run)
    component.set_enabled(True)
    conn = await server.get_connection()

    msg = await conn.receive()
    assert msg.ready is None

    msg = common.MsgServer(cid=1,
                           mid=2,
                           components=[info._replace(blessing=123)])
    conn.send(msg)
    msg = await conn.receive()
    assert msg.ready == 123

    msg = common.MsgServer(cid=1,
                           mid=2,
                           components=[info._replace(blessing=123, ready=123)])
    conn.send(msg)
    await component.wait_closed()

    await client.async_close()
    await conn.wait_closed()
    await server.async_close()
Пример #3
0
async def test_component_enable(server_address):
    conf = {
        'name': 'name',
        'group': 'group',
        'monitor_address': server_address,
        'component_address': 'address'
    }

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

    running_queue = aio.Queue()

    async def async_run(component):
        running_queue.put_nowait(True)
        try:
            await asyncio.Future()
        finally:
            running_queue.put_nowait(False)

    server = await create_server(server_address)
    client = await hat.monitor.client.connect(conf)
    component = hat.monitor.client.Component(client, async_run)
    conn = await server.get_connection()

    msg = await conn.receive()
    assert msg.ready is None

    msg = await conn.receive()
    assert msg.ready == 0

    msg = common.MsgServer(cid=1,
                           mid=2,
                           components=[info._replace(blessing=123, ready=0)])
    conn.send(msg)

    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(conn.receive(), 0.001)
    assert running_queue.empty()

    component.set_enabled(True)

    msg = await conn.receive()
    assert msg.ready == 123

    msg = common.MsgServer(cid=1,
                           mid=2,
                           components=[info._replace(blessing=123, ready=123)])
    conn.send(msg)

    running = await running_queue.get()
    assert running is True
    assert running_queue.empty()

    component.set_enabled(False)

    running = await running_queue.get()
    assert running is False
    assert running_queue.empty()

    msg = await conn.receive()
    assert msg.ready == 0

    msg = common.MsgServer(cid=1,
                           mid=2,
                           components=[info._replace(blessing=123, ready=0)])
    conn.send(msg)

    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(conn.receive(), 0.001)
    assert running_queue.empty()

    msg = common.MsgServer(cid=1,
                           mid=2,
                           components=[info._replace(blessing=None, ready=0)])
    conn.send(msg)

    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(conn.receive(), 0.001)
    assert running_queue.empty()

    component.set_enabled(True)

    msg = await conn.receive()
    assert msg.ready is None

    await component.async_close()
    await client.async_close()
    await conn.wait_closed()
    await server.async_close()