def test_client_secret_credential(aad_credential, live_eventhub): try: from azure.identity import ClientSecretCredential except ImportError: pytest.skip("No azure identity library") client_id, secret, tenant_id = aad_credential credential = ClientSecretCredential(client_id=client_id, secret=secret, tenant_id=tenant_id) client = EventHubClient(host=live_eventhub['hostname'], event_hub_path=live_eventhub['event_hub'], credential=credential, user_agent='customized information') sender = client.create_producer(partition_id='0') receiver = client.create_consumer(consumer_group="$default", partition_id='0', event_position=EventPosition("@latest")) with receiver: received = receiver.receive(timeout=1) assert len(received) == 0 with sender: event = EventData(body='A single message') sender.send(event) time.sleep(1) received = receiver.receive(timeout=1) assert len(received) == 1 assert list(received[0].body)[0] == 'A single message'.encode('utf-8')
def test_long_running_receive(connection_str): parser = argparse.ArgumentParser() parser.add_argument("--duration", help="Duration in seconds of the test", type=int, default=30) parser.add_argument("--consumer", help="Consumer group name", default="$default") parser.add_argument("--partitions", help="Comma seperated partition IDs") parser.add_argument("--offset", help="Starting offset", default="-1") parser.add_argument("--conn-str", help="EventHub connection string", default=connection_str) parser.add_argument("--eventhub", help="Name of EventHub") parser.add_argument("--address", help="Address URI to the EventHub entity") parser.add_argument( "--sas-policy", help="Name of the shared access policy to authenticate with") parser.add_argument("--sas-key", help="Shared access key") args, _ = parser.parse_known_args() if args.conn_str: client = EventHubClient.from_connection_string( args.conn_str, event_hub_path=args.eventhub, network_tracing=False) elif args.address: client = EventHubClient(host=args.address, event_hub_path=args.eventhub, credential=EventHubSharedKeyCredential( args.sas_policy, args.sas_key), auth_timeout=240, network_tracing=False) else: try: import pytest pytest.skip("Must specify either '--conn-str' or '--address'") except ImportError: raise ValueError("Must specify either '--conn-str' or '--address'") if args.partitions: partitions = args.partitions.split(",") else: partitions = client.get_partition_ids() threads = [] for pid in partitions: consumer = client.create_consumer(consumer_group="$default", partition_id=pid, event_position=EventPosition( args.offset), prefetch=300) thread = threading.Thread(target=pump, args=(consumer, args.duration)) thread.start() threads.append(thread) for thread in threads: thread.join()
USER = os.environ['EVENT_HUB_SAS_POLICY'] KEY = os.environ['EVENT_HUB_SAS_KEY'] EVENT_POSITION = EventPosition("-1") PARTITION = "0" total = 0 last_sn = -1 last_offset = "-1" client = EventHubClient(host=HOSTNAME, event_hub_path=EVENT_HUB, credential=EventHubSharedKeyCredential(USER, KEY), network_tracing=False) consumer = client.create_consumer(consumer_group="$default", partition_id=PARTITION, event_position=EVENT_POSITION, prefetch=5000) with consumer: start_time = time.time() batch = consumer.receive(timeout=5) while batch: for event_data in batch: last_offset = event_data.offset last_sn = event_data.sequence_number print("Received: {}, {}".format(last_offset, last_sn)) print(event_data.body_as_str()) total += 1 batch = consumer.receive(timeout=5) print("Received {} messages in {} seconds".format(total, time.time() - start_time))
import examples logger = examples.get_logger(logging.INFO) HOSTNAME = os.environ.get('EVENT_HUB_HOSTNAME') # <mynamespace>.servicebus.windows.net EVENT_HUB = os.environ.get('EVENT_HUB_NAME') USER = os.environ.get('EVENT_HUB_SAS_POLICY') KEY = os.environ.get('EVENT_HUB_SAS_KEY') EVENT_POSITION = EventPosition("-1") class PartitionConsumerThread(Thread): def __init__(self, consumer): Thread.__init__(self) self.consumer = consumer def run(self): for item in self.consumer: print(item) client = EventHubClient(host=HOSTNAME, event_hub_path=EVENT_HUB, credential=EventHubSharedKeyCredential(USER, KEY), network_tracing=False) consumer = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EVENT_POSITION) with consumer: thread = PartitionConsumerThread(consumer) thread.start()
KEY = os.environ['EVENT_HUB_SAS_KEY'] EVENT_POSITION = EventPosition("-1") PARTITION = "0" total = 0 last_sn = -1 last_offset = "-1" client = EventHubClient(host=HOSTNAME, event_hub_path=EVENT_HUB, credential=EventHubSharedKeyCredential(USER, KEY), network_tracing=False) consumer = client.create_consumer(consumer_group="$default", partition_id=PARTITION, event_position=EVENT_POSITION, prefetch=5000, track_last_enqueued_event_properties=True) with consumer: start_time = time.time() batch = consumer.receive(timeout=5) for event_data in batch: last_offset = event_data.offset last_sn = event_data.sequence_number print("Received: {}, {}".format(last_offset, last_sn)) print(event_data.body_as_str()) total += 1 batch = consumer.receive(timeout=5) print("Consumer last enqueued event properties: {}.".format( consumer.last_enqueued_event_properties)) print("Received {} messages in {} seconds".format(total,