async def test_example_eventhub_async_send_and_receive(live_eventhub_config):
    # [START create_eventhub_client_async]
    from azure.eventhub.aio import EventHubClient
    import os
    connection_str = "Endpoint=sb://{}/;SharedAccessKeyName={};SharedAccessKey={};EntityPath={}".format(
        os.environ['EVENT_HUB_HOSTNAME'],
        os.environ['EVENT_HUB_SAS_POLICY'],
        os.environ['EVENT_HUB_SAS_KEY'],
        os.environ['EVENT_HUB_NAME'])    
    client = EventHubClient.from_connection_string(connection_str)
    # [END create_eventhub_client_async]

    from azure.eventhub import EventData, EventPosition

    # [START create_eventhub_client_async_sender]
    client = EventHubClient.from_connection_string(connection_str)
    # Create an async producer.
    producer = client.create_producer(partition_id="0")
    # [END create_eventhub_client_async_sender]

    # [START create_eventhub_client_async_receiver]
    client = EventHubClient.from_connection_string(connection_str)
    # Create an async consumer.
    receiver = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EventPosition('@latest'))
    # Create an exclusive async consumer.
    receiver = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EventPosition('@latest'), owner_level=1)
    # [END create_eventhub_client_async_receiver]

    client = EventHubClient.from_connection_string(connection_str)
    producer = client.create_producer(partition_id="0")
    consumer = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EventPosition('@latest'))

    await consumer.receive(timeout=1)

    # [START eventhub_client_async_send]
    async with producer:
        event_data = EventData(b"A single event")
        await producer.send(event_data)
    # [END eventhub_client_async_send]

        await asyncio.sleep(1)

    # [START eventhub_client_async_receive]
    logger = logging.getLogger("azure.eventhub")
    async with consumer:
        received = await consumer.receive(timeout=5)
        for event_data in received:
            logger.info("Message received:{}".format(event_data.body_as_str()))
    # [END eventhub_client_async_receive]
        assert len(received) > 0
        assert received[0].body_as_str() == "A single event"
        assert list(received[-1].body)[0] == b"A single event"
async def test_receive_batch_with_app_prop_async(connstr_senders):
    connection_str, senders = connstr_senders
    app_prop_key = "raw_prop"
    app_prop_value = "raw_value"
    app_prop = {app_prop_key: app_prop_value}

    def batched():
        for i in range(10):
            ed = EventData("Event Data {}".format(i))
            ed.application_properties = app_prop
            yield ed
        for i in range(10, 20):
            ed = EventData("Event Data {}".format(i))
            ed.application_properties = app_prop
            yield ed

    client = EventHubClient.from_connection_string(connection_str, network_tracing=False)
    receiver = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EventPosition('@latest'), prefetch=500)
    async with receiver:
        received = await receiver.receive(timeout=5)
        assert len(received) == 0

        senders[0].send(batched())

        await asyncio.sleep(1)

        received = await receiver.receive(max_batch_size=15, timeout=5)
        assert len(received) == 15

    for index, message in enumerate(received):
        assert list(message.body)[0] == "Event Data {}".format(index).encode('utf-8')
        assert (app_prop_key.encode('utf-8') in message.application_properties) \
            and (dict(message.application_properties)[app_prop_key.encode('utf-8')] == app_prop_value.encode('utf-8'))
Пример #3
0
async def test_send_batch_with_app_prop_async(connstr_receivers):
    connection_str, receivers = connstr_receivers
    app_prop_key = "raw_prop"
    app_prop_value = "raw_value"
    app_prop = {app_prop_key: app_prop_value}

    def batched():
        for i in range(10):
            ed = EventData("Event number {}".format(i))
            ed.application_properties = app_prop
            yield ed
        for i in range(10, 20):
            ed = EventData("Event number {}".format(i))
            ed.application_properties = app_prop
            yield ed

    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    sender = client.create_producer()
    async with sender:
        await sender.send(batched())

    time.sleep(1)

    received = []
    for r in receivers:
        received.extend(r.receive(timeout=3))

    assert len(received) == 20
    for index, message in enumerate(received):
        assert list(
            message.body)[0] == "Event number {}".format(index).encode('utf-8')
        assert (app_prop_key.encode('utf-8') in message.application_properties) \
            and (dict(message.application_properties)[app_prop_key.encode('utf-8')] == app_prop_value.encode('utf-8'))
Пример #4
0
async def test_exclusive_receiver_after_non_exclusive_receiver_async(
        connstr_senders):
    connection_str, senders = connstr_senders
    senders[0].send(EventData(b"Receiving only a single event"))

    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    receiver1 = client.create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition("-1"),
                                       prefetch=10)
    receiver2 = client.create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition("-1"),
                                       owner_level=15,
                                       prefetch=10)
    try:
        await pump(receiver1)
        output2 = await pump(receiver2)
        with pytest.raises(ConnectionLostError):
            await receiver1.receive(timeout=1)
        assert output2 == 1
    finally:
        await receiver1.close()
        await receiver2.close()
async def test_receive_run_time_metric_async(connstr_senders):
    from uamqp import __version__ as uamqp_version
    from distutils.version import StrictVersion
    if StrictVersion(uamqp_version) < StrictVersion('1.2.3'):
        pytest.skip("Disabled for uamqp 1.2.2. Will enable after uamqp 1.2.3 is released.")
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str, transport_type=TransportType.AmqpOverWebsocket,
                                                   network_tracing=False)
    receiver = client.create_consumer(consumer_group="$default", partition_id="0",
                                      event_position=EventPosition('@latest'), prefetch=500,
                                      track_last_enqueued_event_properties=True)

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

    async with receiver:
        received = await receiver.receive(timeout=5)
        assert len(received) == 0

        senders[0].send(event_list)

        await asyncio.sleep(1)

        received = await receiver.receive(max_batch_size=50, timeout=5)
        assert len(received) == 20
        assert receiver.last_enqueued_event_properties
        assert receiver.last_enqueued_event_properties.get('sequence_number', None)
        assert receiver.last_enqueued_event_properties.get('offset', None)
        assert receiver.last_enqueued_event_properties.get('enqueued_time', None)
        assert receiver.last_enqueued_event_properties.get('retrieval_time', None)
 def __init__(self):
     # This test requires a previusly created Event Hub.
     # In this example the name is "myeventhub", but it could be change below
     connection_string = os.environ["EVENT_HUBS_CONNECTION_STRING"]
     event_hub_name = "myeventhub"
     self.client = EventHubClient.from_connection_string(
         connection_string, event_hub_name)
Пример #7
0
async def test_multiple_receiver_async(connstr_senders):
    connection_str, senders = connstr_senders
    senders[0].send(EventData(b"Receiving only a single event"))

    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    partitions = await client.get_properties()
    assert partitions["partition_ids"] == ["0", "1"]
    receivers = []
    for i in range(2):
        receivers.append(
            client.create_consumer(consumer_group="$default",
                                   partition_id="0",
                                   event_position=EventPosition("-1"),
                                   prefetch=10))
    try:
        more_partitions = await client.get_properties()
        assert more_partitions["partition_ids"] == ["0", "1"]
        outputs = [0, 0]
        outputs[0] = await pump(receivers[0])
        outputs[1] = await pump(receivers[1])
        assert isinstance(outputs[0], int) and outputs[0] == 1
        assert isinstance(outputs[1], int) and outputs[1] == 1
    finally:
        for r in receivers:
            await r.close()
Пример #8
0
async def test_receive_with_datetime_async(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    receiver = client.create_consumer(consumer_group="$default",
                                      partition_id="0",
                                      event_position=EventPosition('@latest'))
    async with receiver:
        received = await receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Data"))
        received = await receiver.receive(timeout=5)
        assert len(received) == 1
        offset = received[0].enqueued_time

    offset_receiver = client.create_consumer(
        consumer_group="$default",
        partition_id="0",
        event_position=EventPosition(offset))
    async with offset_receiver:
        received = await offset_receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Message after timestamp"))
        time.sleep(1)
        received = await offset_receiver.receive(timeout=5)
        assert len(received) == 1
Пример #9
0
async def test_send_with_partition_key_async(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    sender = client.create_producer()

    async with sender:
        data_val = 0
        for partition in [b"a", b"b", b"c", b"d", b"e", b"f"]:
            partition_key = b"test_partition_" + partition
            for i in range(50):
                data = EventData(str(data_val))
                # data.partition_key = partition_key
                data_val += 1
                await sender.send(data, partition_key=partition_key)

    found_partition_keys = {}
    for index, partition in enumerate(receivers):
        received = partition.receive(timeout=5)
        for message in received:
            try:
                existing = found_partition_keys[message.partition_key]
                assert existing == index
            except KeyError:
                found_partition_keys[message.partition_key] = index
async def test_send_with_forced_conn_close_async(connstr_receivers, sleep):
    pytest.skip("This test is similar to the above one")
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    sender = client.create_producer()
    try:
        await sender.send(EventData(b"A single event"))
        if sleep:
            await asyncio.sleep(300)
        else:
            sender._handler._connection._conn.destroy()
        await sender.send(EventData(b"A single event"))
        await sender.send(EventData(b"A single event"))
        if sleep:
            await asyncio.sleep(300)
        else:
            sender._handler._connection._conn.destroy()
        await sender.send(EventData(b"A single event"))
        await sender.send(EventData(b"A single event"))
    finally:
        await sender.close()

    received = []
    for r in receivers:
        if not sleep:
            r._handler._connection._conn.destroy()
        received.extend(pump(r))
    assert len(received) == 5
    assert list(received[0].body)[0] == b"A single event"
Пример #11
0
async def test_receive_over_websocket_async(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(
        connection_str,
        transport_type=TransportType.AmqpOverWebsocket,
        network_tracing=False)
    receiver = client.create_consumer(consumer_group="$default",
                                      partition_id="0",
                                      event_position=EventPosition('@latest'),
                                      prefetch=500)

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

    async with receiver:
        received = await receiver.receive(timeout=5)
        assert len(received) == 0

        senders[0].send(event_list)

        time.sleep(1)

        received = await receiver.receive(max_batch_size=50, timeout=5)
        assert len(received) == 20
async def test_send_with_invalid_key_async(invalid_key, connstr_receivers):
    _, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(invalid_key,
                                                   network_tracing=False)
    sender = client.create_producer()
    with pytest.raises(AuthenticationError):
        await sender._open()
async def test_non_existing_entity_sender_async(connection_str):
    client = EventHubClient.from_connection_string(connection_str,
                                                   event_hub_path="nemo",
                                                   network_tracing=False)
    sender = client.create_producer(partition_id="1")
    with pytest.raises(AuthenticationError):
        await sender._open()
async def test_receive_with_invalid_policy_async(invalid_policy):
    client = EventHubClient.from_connection_string(invalid_policy,
                                                   network_tracing=False)
    sender = client.create_consumer(consumer_group="$default",
                                    partition_id="0",
                                    event_position=EventPosition("-1"))
    with pytest.raises(AuthenticationError):
        await sender._open()
Пример #15
0
async def test_long_running_receive_async(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")

    loop = asyncio.get_event_loop()
    args, _ = parser.parse_known_args()
    if args.conn_str:
        client = EventHubClient.from_connection_string(
            args.conn_str,
            event_hub_path=args.eventhub,
            auth_timeout=240,
            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 not args.partitions:
        partitions = await client.get_partition_ids()
    else:
        partitions = args.partitions.split(",")
    pumps = []
    for pid in partitions:
        receiver = client.create_consumer(consumer_group="$default",
                                          partition_id=pid,
                                          event_position=EventPosition(
                                              args.offset),
                                          prefetch=300,
                                          loop=loop)
        pumps.append(pump(pid, receiver, args, args.duration))
    await asyncio.gather(*pumps)
async def test_receive_with_invalid_key_async(invalid_key):
    client = EventHubClient.from_connection_string(invalid_key,
                                                   network_tracing=False)
    receiver = client.create_consumer(consumer_group="$default",
                                      partition_id="0",
                                      event_position=EventPosition("-1"))
    with pytest.raises(AuthenticationError):
        await receiver.receive(timeout=3)
    await receiver.close()
async def test_send_to_invalid_partitions_async(connection_str):
    partitions = ["XYZ", "-1", "1000", "-"]
    for p in partitions:
        client = EventHubClient.from_connection_string(connection_str,
                                                       network_tracing=False)
        sender = client.create_producer(partition_id=p)
        with pytest.raises(ConnectError):
            await sender.send(EventData("test data"))
        await sender.close()
async def test_non_existing_entity_receiver_async(connection_str):
    client = EventHubClient.from_connection_string(connection_str,
                                                   event_hub_path="nemo",
                                                   network_tracing=False)
    receiver = client.create_consumer(consumer_group="$default",
                                      partition_id="0",
                                      event_position=EventPosition("-1"))
    with pytest.raises(AuthenticationError):
        await receiver._open()
async def test_send_with_invalid_policy_async(invalid_policy,
                                              connstr_receivers):
    _, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(invalid_policy,
                                                   network_tracing=False)
    sender = client.create_producer()
    with pytest.raises(AuthenticationError):
        await sender.send(EventData("test data"))
    await sender.close()
Пример #20
0
 def __enter__(self):
     self._client = EventHubClient.from_connection_string(
         self._eh_connection_string)
     self._storage_client = ContainerClient.from_connection_string(
         conn_str=self._sa_connection_string,
         container=self._sa_container_name)
     self._partition_manager = BlobPartitionManager(self._storage_client)
     self._event_processor = EventProcessor(self._client, "$default",
                                            self._partition_processor,
                                            self._partition_manager)
async def test_send_null_body_async(connection_str):
    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    sender = client.create_producer()
    try:
        with pytest.raises(ValueError):
            data = EventData(None)
            await sender.send(data)
    finally:
        await sender.close()
async def test_create_batch_with_invalid_hostname_async(invalid_hostname):
    client = EventHubClient.from_connection_string(invalid_hostname,
                                                   network_tracing=False)
    sender = client.create_producer()
    try:
        with pytest.raises(AuthenticationError):
            batch_event_data = await sender.create_batch(max_size=300,
                                                         partition_key="key")
    finally:
        await sender.close()
Пример #23
0
async def test_partition_processor(connstr_senders):
    partition_processor1 = None
    partition_processor2 = None

    class TestPartitionProcessor(PartitionProcessor):
        def __init__(self):
            self.initialize_called = False
            self.error = None
            self.close_reason = None
            self.received_events = []
            self.checkpoint = None

        async def initialize(self, partition_context):
            nonlocal partition_processor1, partition_processor2
            if partition_context.partition_id == "1":
                partition_processor1 = self
            else:
                partition_processor2 = self

        async def process_events(self, events, partition_context):
            self.received_events.extend(events)
            if events:
                offset, sn = events[-1].offset, events[-1].sequence_number
                await partition_context.update_checkpoint(offset, sn)
                self.checkpoint = (offset, sn)

        async def process_error(self, error, partition_context):
            self.error = error
            assert partition_context is not None

        async def close(self, reason, partition_context):
            self.close_reason = reason
            assert partition_context is not None

    connection_str, senders = connstr_senders
    for sender in senders:
        sender.send(EventData("EventProcessor Test"))
    eventhub_client = EventHubClient.from_connection_string(connection_str,
                                                            receive_timeout=3)
    partition_manager = SamplePartitionManager()

    event_processor = EventProcessor(eventhub_client,
                                     "$default",
                                     TestPartitionProcessor,
                                     partition_manager,
                                     polling_interval=1)
    asyncio.ensure_future(event_processor.start())
    await asyncio.sleep(10)
    await event_processor.stop()
    assert partition_processor1 is not None and partition_processor2 is not None
    assert len(partition_processor1.received_events) == 1 and len(
        partition_processor2.received_events) == 1
    assert partition_processor1.checkpoint is not None
    assert partition_processor1.close_reason == CloseReason.SHUTDOWN
    assert partition_processor1.error is None
async def test_send_partition_async(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str, network_tracing=False)
    sender = client.create_producer(partition_id="1")
    async with sender:
        await sender.send(EventData(b"Data"))

    partition_0 = receivers[0].receive(timeout=2)
    assert len(partition_0) == 0
    partition_1 = receivers[1].receive(timeout=2)
    assert len(partition_1) == 1
Пример #25
0
async def test_create_batch_with_none_async(connection_str):
    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    sender = client.create_producer()
    batch_event_data = await sender.create_batch(max_size=300,
                                                 partition_key="key")
    try:
        with pytest.raises(ValueError):
            batch_event_data.try_add(EventData(None))
    finally:
        await sender.close()
async def test_iothub_receive_multiple_async(iot_connection_str):
    pytest.skip("This will get AuthenticationError. We're investigating...")
    partitions = await get_partitions(iot_connection_str)
    client = EventHubClient.from_connection_string(iot_connection_str, network_tracing=False)
    receivers = []
    for p in partitions:
        receivers.append(client.create_consumer(consumer_group="$default", partition_id=p, event_position=EventPosition("-1"), prefetch=10, operation='/messages/events'))
    outputs = await asyncio.gather(*[pump(r) for r in receivers])

    assert isinstance(outputs[0], int) and outputs[0] <= 10
    assert isinstance(outputs[1], int) and outputs[1] <= 10
async def test_receive_from_invalid_partitions_async(connection_str):
    partitions = ["XYZ", "-1", "1000", "-"]
    for p in partitions:
        client = EventHubClient.from_connection_string(connection_str,
                                                       network_tracing=False)
        receiver = client.create_consumer(consumer_group="$default",
                                          partition_id=p,
                                          event_position=EventPosition("-1"))
        with pytest.raises(ConnectError):
            await receiver.receive(timeout=5)
        await receiver.close()
async def test_create_batch_with_too_large_size_async(connection_str):
    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    sender = client.create_producer()
    try:
        with pytest.raises(ValueError):
            batch_event_data = await sender.create_batch(max_size=5 * 1024 *
                                                         1024,
                                                         partition_key="key")
    finally:
        await sender.close()
async def test_receive_end_of_stream_async(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str, network_tracing=False)
    receiver = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EventPosition('@latest'))
    async with receiver:
        received = await receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Receiving only a single event"))
        received = await receiver.receive(timeout=5)
        assert len(received) == 1

        assert list(received[-1].body)[0] == b"Receiving only a single event"
async def test_example_eventhub_async_producer_ops(live_eventhub_config, connection_str):
    from azure.eventhub.aio import EventHubClient
    from azure.eventhub import EventData

    # [START eventhub_client_async_sender_close]
    client = EventHubClient.from_connection_string(connection_str)
    producer = client.create_producer(partition_id="0")
    try:
        await producer.send(EventData(b"A single event"))
    finally:
        # Close down the send handler.
        await producer.close()