Ejemplo n.º 1
0
async def test_query(tmp_path, create_event_server):
    backend_conf = {
        'module': 'hat.event.server.backends.sqlite',
        'db_path': str(tmp_path / 'event.db'),
        'query_pool_size': 1
    }
    modules_conf = []

    with create_event_server(backend_conf, modules_conf) as srv:
        srv.wait_active(5)

        client = await hat.event.client.connect(srv.address)

        resp0 = await client.query(common.QueryData(event_types=[('*', )]))
        assert len(resp0) == 1

        resp1 = await client.register_with_response(
            [common.RegisterEvent(('a', ), common.now(), None)])
        result = await client.query(common.QueryData(event_types=[('*', )]))
        assert resp1 + resp0 == result

        resp2 = await client.register_with_response(
            [common.RegisterEvent(('b', ), common.now(), None)])
        result = await client.query(common.QueryData(event_types=[('*', )]))
        assert resp2 + resp1 + resp0 == result

        await client.async_close()
Ejemplo n.º 2
0
async def test_register_with_response(create_event_server):
    backend_conf = {'module': 'hat.event.server.backends.dummy'}
    modules_conf = [{
        'module': 'test_sys.test_event.modules.copier',
        'subscriptions': [['*']]
    }]

    register_events = [
        common.RegisterEvent(event_type=(f'a{i}', ),
                             source_timestamp=common.now(),
                             payload=common.EventPayload(
                                 type=common.EventPayloadType.BINARY,
                                 data=b'123')) for i in range(10)
    ]

    with create_event_server(backend_conf, modules_conf) as srv:
        srv.wait_active(5)

        client = await hat.event.client.connect(srv.address)

        resp = await client.register_with_response(register_events)

        assert (len(resp) == 10)
        for reg_event, event in zip(register_events, resp):
            assert reg_event.event_type == event.event_type
            assert reg_event.source_timestamp == event.source_timestamp
            assert reg_event.payload == event.payload
        assert all(resp[0].timestamp == e.timestamp for e in resp)

        await client.async_close()
Ejemplo n.º 3
0
async def test_register(create_event_server):
    backend_conf = {'module': 'hat.event.server.backends.dummy'}
    modules_conf = []

    register_event = common.RegisterEvent(
        event_type=('a', ),
        source_timestamp=common.now(),
        payload=common.EventPayload(type=common.EventPayloadType.BINARY,
                                    data=b'123'))

    with create_event_server(backend_conf, modules_conf) as srv:
        srv.wait_active(5)

        client = await hat.event.client.connect(srv.address)

        client.register([register_event])
        resp = await client.register_with_response([register_event])

        assert len(resp) == 1
        assert resp[0].event_type == register_event.event_type
        assert resp[0].source_timestamp == register_event.source_timestamp
        assert resp[0].payload == register_event.payload

        assert not client.is_closed
        await client.async_close()
Ejemplo n.º 4
0
async def test_multiple_clients(create_event_server, client_count):
    backend_conf = {'module': 'hat.event.server.backends.dummy'}
    modules_conf = []

    with create_event_server(backend_conf, modules_conf) as srv:
        srv.wait_active(5)

        clients = []
        for _ in range(client_count):
            client = await hat.event.client.connect(srv.address, [('a', )])
            clients.append(client)

        for i, sender in enumerate(clients):
            client.register([
                common.RegisterEvent(
                    ('a', ), None,
                    common.EventPayload(type=common.EventPayloadType.JSON,
                                        data=i))
            ])
            for receiver in clients:
                evts = await asyncio.wait_for(receiver.receive(), 1)
                assert evts[0].payload.data == i

        for client in clients:
            await client.async_close()
Ejemplo n.º 5
0
async def test_client_register(server_address):
    register_events = [
        common.RegisterEvent(event_type=('a', 'b', 'c'),
                             source_timestamp=common.now(),
                             payload=common.EventPayload(
                                 common.EventPayloadType.JSON, i))
        for i in range(10)
    ]

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

    client.register([])
    msg = await conn.receive()
    assert msg.first is True
    assert msg.last is True
    assert msg.data == chatter.Data('HatEvent', 'MsgRegisterReq', [])

    client.register(register_events)
    msg = await conn.receive()
    assert msg.first is True
    assert msg.last is True
    assert msg.data == chatter.Data(
        'HatEvent', 'MsgRegisterReq',
        [common.register_event_to_sbs(i) for i in register_events])

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

    with pytest.raises(ConnectionError):
        client.register(register_events)
Ejemplo n.º 6
0
async def _register(args, client, printer):
    register_event = common.RegisterEvent(
        event_type=args.event_type,
        source_timestamp=args.source_timestamp,
        payload=(common.EventPayload(type=common.EventPayloadType.JSON,
                                     data=args.payload)
                 if args.payload is not None else None))
    events = await client.register_with_response([register_event])
    printer.print(events)
Ejemplo n.º 7
0
async def test_dummy(client):
    result = await client.query(common.QueryData(event_types=[('*',)]))
    assert result == []

    resp = await client.register_with_response(
        [common.RegisterEvent(('a',), None, None)])
    assert len(resp) == 1

    result = await client.query(common.QueryData(event_types=[('*',)]))
    assert result == []
Ejemplo n.º 8
0
async def test_payload(client, payload_type, payload_data):
    resp = await client.register_with_response([
        common.RegisterEvent(('a', ), None,
                             common.EventPayload(payload_type, payload_data))
    ])
    result = await client.query(common.QueryData(event_types=[('a', )]))

    assert resp == result
    assert len(result) == 1
    assert result[0].payload == common.EventPayload(payload_type, payload_data)
Ejemplo n.º 9
0
async def test_multiple_writes(client, register_count, register_size):
    for i in range(register_count):
        resp = await client.register_with_response([
            common.RegisterEvent(('a', ), None, None)
            for _ in range(register_size)
        ])
        assert len(resp) == register_size

        result = await client.query(common.QueryData(event_types=[('a', )]))
        assert len(result) == (i + 1) * register_size
Ejemplo n.º 10
0
 async def run_cb(client):
     client.register([
         common.RegisterEvent(event_type=('a', ),
                              source_timestamp=common.now(),
                              payload=None)
     ])
     try:
         await asyncio.Future()
     finally:
         await client.register_with_response([
             common.RegisterEvent(event_type=('b'),
                                  source_timestamp=common.now(),
                                  payload=None)
         ])
         client.register([
             common.RegisterEvent(event_type=('c'),
                                  source_timestamp=common.now(),
                                  payload=None)
         ])
Ejemplo n.º 11
0
async def test_client_register_with_response(server_address):
    register_events = [
        common.RegisterEvent(event_type=('a', 'b', 'c'),
                             source_timestamp=common.now(),
                             payload=common.EventPayload(
                                 common.EventPayloadType.JSON, i))
        for i in range(10)
    ]

    events = [
        common.Event(event_id=common.EventId(1, 2),
                     event_type=register_event.event_type,
                     timestamp=common.now(),
                     source_timestamp=register_event.source_timestamp,
                     payload=register_event.payload)
        for register_event in register_events
    ]

    events = [event if i % 2 else None for i, event in enumerate(events)]

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

    register_future = asyncio.ensure_future(client.register_with_response([]))
    msg = await conn.receive()
    assert msg.first is True
    assert msg.last is False
    assert msg.data == chatter.Data('HatEvent', 'MsgRegisterReq', [])
    assert not register_future.done()
    conn.send_register_res(msg.conv, [])
    received = await register_future
    assert received == []

    register_future = asyncio.ensure_future(
        client.register_with_response(register_events))
    msg = await conn.receive()
    assert msg.first is True
    assert msg.last is False
    assert msg.data == chatter.Data(
        'HatEvent', 'MsgRegisterReq',
        [common.register_event_to_sbs(i) for i in register_events])
    assert not register_future.done()
    conn.send_register_res(msg.conv, events)
    received = await register_future
    assert received == events

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

    with pytest.raises(ConnectionError):
        await client.register_with_response(register_events)
Ejemplo n.º 12
0
async def test_subscribe(create_event_server):
    backend_conf = {'module': 'hat.event.server.backends.dummy'}
    modules_conf = []

    with create_event_server(backend_conf, modules_conf) as srv:
        srv.wait_active(5)

        client = await hat.event.client.connect(srv.address, [('a', '*')])

        client.register([common.RegisterEvent(('a', ), None, None)])
        evts = await asyncio.wait_for(client.receive(), 0.1)
        assert evts[0].event_type == ('a', )

        client.register([common.RegisterEvent(('b', ), None, None)])
        with pytest.raises(asyncio.TimeoutError):
            await asyncio.wait_for(client.receive(), 0.1)

        client.register([common.RegisterEvent(('a', 'b', 'c'), None, None)])
        evts = await asyncio.wait_for(client.receive(), 0.1)
        assert evts[0].event_type == ('a', 'b', 'c')

        await client.async_close()
Ejemplo n.º 13
0
async def test_register_simple(duration, create_server, create_client,
                               event_count):
    with create_server(1) as server:
        client = await create_client(server.address)
        register_events = [
            common.RegisterEvent(['a'], None, None) for _ in range(event_count)
        ]

        message = f"bulk register {event_count} minimal events"

        with duration(f"{message} (first time)"):
            await client.register_with_response(register_events)

        with duration(f"{message} (second time)"):
            await client.register_with_response(register_events)

        await client.async_close()