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()
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)
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()
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()
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)
async def test_client_receive(server_address): events = [ common.Event(event_id=common.EventId(1, 2), event_type=('a', 'b', 'c'), timestamp=common.now(), source_timestamp=None, 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() conn.send_notify([]) received = await client.receive() assert received == [] conn.send_notify(events) received = await client.receive() assert received == events await conn.async_close() await client.async_close() await server.async_close() with pytest.raises(ConnectionError): await client.receive()
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) ])
def test_now(): previous_dt = None for _ in range(10): now_dt = common.timestamp_to_datetime(common.now()) assert dt.datetime.now( dt.timezone.utc) - now_dt < dt.timedelta(seconds=1) if previous_dt is not None: assert now_dt >= previous_dt previous_dt = now_dt
async def test_client_query(server_address): events = [ common.Event(event_id=common.EventId(1, 2), event_type=('a', 'b', 'c'), timestamp=common.now(), source_timestamp=None, 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() query_data = common.QueryData() query_future = asyncio.ensure_future(client.query(query_data)) msg = await conn.receive() assert msg.first is True assert msg.last is False assert msg.data == chatter.Data('HatEvent', 'MsgQueryReq', common.query_to_sbs(query_data)) assert not query_future.done() conn.send_query_res(msg.conv, []) received = await query_future assert received == [] query_data = common.QueryData(event_types=[['*']]) query_future = asyncio.ensure_future(client.query(query_data)) msg = await conn.receive() assert msg.first is True assert msg.last is False assert msg.data == chatter.Data('HatEvent', 'MsgQueryReq', common.query_to_sbs(query_data)) assert not query_future.done() conn.send_query_res(msg.conv, events) received = await query_future assert received == events await conn.async_close() await client.async_close() await server.async_close() with pytest.raises(ConnectionError): await client.query(common.QueryData())