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)
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_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()
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 _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)
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 _query(args, client, printer): data = common.QueryData( event_ids=args.event_ids, event_types=args.event_types, t_from=args.t_from, t_to=args.t_to, source_t_from=args.source_t_from, source_t_to=args.source_t_to, payload=(common.EventPayload(type=common.EventPayloadType.JSON, data=json.encode(args.payload)) if args.payload is not None else None), order=args.order, order_by=args.order_by, unique_type=args.unique_type, max_results=args.max_results) events = await client.query(data) printer.print(events)
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())