async def send_sequence_message(producer): sequence_body = [b'message', 123.456, True] footer = {'footer_key': 'footer_value'} properties = {"subject": "sequence"} application_properties = {"body_type": "sequence"} sequence_message = AmqpAnnotatedMessage( sequence_body=sequence_body, footer=footer, properties=properties, application_properties=application_properties) await producer.send_batch([sequence_message]) print("Message of sequence body sent.")
async def send_value_message(producer): value_body = {b"key": [-123, b'data', False]} header = {"priority": 10} annotations = {"annotation_key": "value"} application_properties = {"body_type": "value"} value_message = AmqpAnnotatedMessage( value_body=value_body, header=header, annotations=annotations, application_properties=application_properties) await producer.send_batch([value_message]) print("Message of value body sent.")
async def send_data_message(producer): data_body = [b'aa', b'bb', b'cc'] application_properties = {"body_type": "data"} delivery_annotations = {"delivery_annotation_key": "value"} data_message = AmqpAnnotatedMessage( data_body=data_body, delivery_annotations=delivery_annotations, application_properties=application_properties) batch = await producer.create_batch() batch.add(data_message) await producer.send_batch(batch) print("Message of data body sent.")
async def test_send_amqp_annotated_message(connstr_receivers): connection_str, receivers = connstr_receivers client = EventHubProducerClient.from_connection_string(connection_str) async with client: sequence_body = [b'message', 123.456, True] footer = {'footer_key': 'footer_value'} prop = {"subject": "sequence"} seq_app_prop = {"body_type": "sequence"} sequence_message = AmqpAnnotatedMessage( sequence_body=sequence_body, footer=footer, properties=prop, application_properties=seq_app_prop) value_body = {b"key": [-123, b'data', False]} header = {"priority": 10} anno = {"ann_key": "ann_value"} value_app_prop = {"body_type": "value"} value_message = AmqpAnnotatedMessage( value_body=value_body, header=header, annotations=anno, application_properties=value_app_prop) data_body = [b'aa', b'bb', b'cc'] data_app_prop = {"body_type": "data"} del_anno = {"delann_key": "delann_value"} data_message = AmqpAnnotatedMessage( data_body=data_body, header=header, delivery_annotations=del_anno, application_properties=data_app_prop) body_ed = """{"json_key": "json_val"}""" prop_ed = {"raw_prop": "raw_value"} cont_type_ed = "text/plain" corr_id_ed = "corr_id" mess_id_ed = "mess_id" event_data = EventData(body_ed) event_data.content_type = cont_type_ed event_data.correlation_id = corr_id_ed event_data.message_id = mess_id_ed batch = await client.create_batch() batch.add(data_message) batch.add(value_message) batch.add(sequence_message) batch.add(event_data) await client.send_batch(batch) await client.send_batch( [data_message, value_message, sequence_message, event_data]) received_count = {} received_count["data_msg"] = 0 received_count["seq_msg"] = 0 received_count["value_msg"] = 0 received_count["normal_msg"] = 0 def check_values(event): raw_amqp_message = event.raw_amqp_message if raw_amqp_message.body_type == AmqpMessageBodyType.DATA: if raw_amqp_message.application_properties and raw_amqp_message.application_properties.get( b'body_type') == b'data': body = [data for data in raw_amqp_message.body] assert data_body == body assert event.body_as_str() == "aabbcc" assert raw_amqp_message.delivery_annotations[ b'delann_key'] == b'delann_value' assert raw_amqp_message.application_properties[ b'body_type'] == b'data' received_count["data_msg"] += 1 else: assert event.body_as_json() == {'json_key': 'json_val'} assert event.correlation_id == corr_id_ed assert event.message_id == mess_id_ed assert event.content_type == cont_type_ed assert event.body_type == AmqpMessageBodyType.DATA received_count["normal_msg"] += 1 elif raw_amqp_message.body_type == AmqpMessageBodyType.SEQUENCE: body = [sequence for sequence in raw_amqp_message.body] assert [sequence_body] == body assert event.body_as_str() == "['message', 123.456, True]" assert raw_amqp_message.footer[b'footer_key'] == b'footer_value' assert raw_amqp_message.properties.subject == b'sequence' assert raw_amqp_message.application_properties[ b'body_type'] == b'sequence' received_count["seq_msg"] += 1 elif raw_amqp_message.body_type == AmqpMessageBodyType.VALUE: assert raw_amqp_message.body == value_body assert event.body_as_str() == "{'key': [-123, 'data', False]}" assert raw_amqp_message.annotations[b'ann_key'] == b'ann_value' assert raw_amqp_message.application_properties[ b'body_type'] == b'value' received_count["value_msg"] += 1 async def on_event(partition_context, event): on_event.received.append(event) on_event.received = [] client = EventHubConsumerClient.from_connection_string( connection_str, consumer_group='$default') async with client: task = asyncio.ensure_future( client.receive(on_event, starting_position="-1")) await asyncio.sleep(15) for event in on_event.received: check_values(event) await task assert len(on_event.received) == 8 assert received_count["data_msg"] == 2 assert received_count["seq_msg"] == 2 assert received_count["value_msg"] == 2 assert received_count["normal_msg"] == 2
def test_amqp_message_str_repr(): data_body = b'A' message = AmqpAnnotatedMessage(data_body=data_body) assert str(message) == 'A' assert 'AmqpAnnotatedMessage(body=A, body_type=data' in repr(message)