示例#1
0
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')
示例#2
0
def init_event_hub():
    with open('config.json', 'r') as json_file:
        config = json.load(json_file)
    client = EventHubClient(host=config["EH_HOST"],
                            event_hub_path=config["EH_NAME"],
                            credential=EventHubSharedKeyCredential(
                                config["EVENT_HUB_SAS_POLICY"],
                                config["EVENT_HUB_SAS_KEY"]),
                            network_tracing=False)

    for i in range(NUM_PARTITIONS):
        event_producer_list.append(client.create_producer(partition_id=str(i)))
def test_long_running_send(connection_str):
    if sys.platform.startswith('darwin'):
        import pytest
        pytest.skip("Skipping on OSX")
    parser = argparse.ArgumentParser()
    parser.add_argument("--duration",
                        help="Duration in seconds of the test",
                        type=int,
                        default=30)
    parser.add_argument("--payload",
                        help="payload size",
                        type=int,
                        default=512)
    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)
    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'")

    try:
        partition_ids = client.get_partition_ids()
        threads = []
        for pid in partition_ids:
            sender = client.create_producer(partition_id=pid)
            thread = threading.Thread(target=send, args=(sender, args))
            thread.start()
            threads.append(thread)
        thread.join()
    except KeyboardInterrupt:
        pass
示例#4
0
An example to show sending individual events to an Event Hub partition.
Although this works, sending events in batches will get better performance.
See 'send_list_of_event_data.py' and 'send_event_data_batch.py' for an example of batching.
"""

# pylint: disable=C0111

import time
import os
from azure.eventhub import EventHubClient, EventData, EventHubSharedKeyCredential

HOSTNAME = os.environ[
    'EVENT_HUB_HOSTNAME']  # <mynamespace>.servicebus.windows.net
EVENT_HUB = os.environ['EVENT_HUB_NAME']
USER = os.environ['EVENT_HUB_SAS_POLICY']
KEY = os.environ['EVENT_HUB_SAS_KEY']

client = EventHubClient(host=HOSTNAME,
                        event_hub_path=EVENT_HUB,
                        credential=EventHubSharedKeyCredential(USER, KEY),
                        network_tracing=False)
producer = client.create_producer(partition_id="0")

start_time = time.time()
with producer:
    for i in range(100):
        ed = EventData("msg")
        print("Sending message: {}".format(i))
        producer.send(ed)
print("Send 100 messages in {} seconds".format(time.time() - start_time))
    'EVENT_HUB_HOSTNAME']  # <mynamespace>.servicebus.windows.net
EVENT_HUB = os.environ['EVENT_HUB_NAME']

USER = os.environ['EVENT_HUB_SAS_POLICY']
KEY = os.environ['EVENT_HUB_SAS_KEY']


def create_batch_data(producer):
    event_data_batch = producer.create_batch(max_size=10000)
    while True:
        try:
            event_data_batch.try_add(
                EventData('Message inside EventBatchData'))
        except ValueError:
            # EventDataBatch object reaches max_size.
            # New EventDataBatch object can be created here to send more data
            break
    return event_data_batch


client = EventHubClient(host=HOSTNAME,
                        event_hub_path=EVENT_HUB,
                        credential=EventHubSharedKeyCredential(USER, KEY),
                        network_tracing=False)
producer = client.create_producer()
start_time = time.time()
with producer:
    event_data_batch = create_batch_data(producer)
    producer.send(event_data_batch)
print("Runtime: {} seconds".format(time.time() - start_time))
示例#6
0
    'proxy_hostname': '127.0.0.1',  # proxy hostname
    'proxy_port': 3128,  # proxy port
    'username': '******',  # username used for proxy authentication if needed
    'password': '******'  # password used for proxy authentication if needed
}

if not HOSTNAME:
    raise ValueError("No EventHubs URL supplied.")

client = EventHubClient(host=HOSTNAME,
                        event_hub_path=EVENT_HUB,
                        credential=EventHubSharedKeyCredential(USER, KEY),
                        network_tracing=False,
                        http_proxy=HTTP_PROXY)
try:
    producer = client.create_producer(partition_id=PARTITION)
    consumer = client.create_consumer(consumer_group="$default",
                                      partition_id=PARTITION,
                                      event_position=EVENT_POSITION)

    consumer.receive(timeout=1)

    event_list = []
    for i in range(20):
        event_list.append(EventData("Event Number {}".format(i)))

    print('Start sending events behind a proxy.')

    producer.send(event_list)

    print('Start receiving events behind a proxy.')