示例#1
0
async def test_receive_with_invalid_param_async(live_eventhub_config,
                                                invalid_place):
    eventhub_config = live_eventhub_config.copy()
    if invalid_place != "partition":
        eventhub_config[invalid_place] = "invalid " + invalid_place
    conn_str = live_eventhub_config["connection_str"].format(
        eventhub_config['hostname'], eventhub_config['key_name'],
        eventhub_config['access_key'], eventhub_config['event_hub'])

    client = EventHubConsumerClient.from_connection_string(
        conn_str, consumer_group='$default', retry_total=0)

    async def on_event(partition_context, event):
        pass

    async with client:
        if invalid_place == "partition":
            task = asyncio.ensure_future(
                client.receive(on_event,
                               partition_id=invalid_place,
                               initial_event_position=EventPosition("-1")))
        else:
            task = asyncio.ensure_future(
                client.receive(on_event,
                               partition_id="0",
                               initial_event_position=EventPosition("-1")))
        await asyncio.sleep(10)
        assert len(client._event_processors) == 1
    await task
示例#2
0
def test_receive_with_datetime_sync(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str)
    partitions = client.get_properties()
    assert partitions["partition_ids"] == ["0", "1"]
    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition('@latest'))
    with receiver:
        more_partitions = client.get_properties()
        assert more_partitions["partition_ids"] == ["0", "1"]
        received = receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Data"))
        received = receiver.receive(timeout=5)
        assert len(received) == 1
        offset = received[0].enqueued_time

        assert list(received[0].body) == [b'Data']
        assert received[0].body_as_str() == "Data"

    offset_receiver = client._create_consumer(
        consumer_group="$default",
        partition_id="0",
        event_position=EventPosition(offset))
    with offset_receiver:
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Message after timestamp"))
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 1
    client.close()
示例#3
0
def test_receive_with_offset_sync(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    partitions = client.get_properties()
    assert partitions["partition_ids"] == ["0", "1"]
    receiver = client.create_consumer(consumer_group="$default",
                                      partition_id="0",
                                      event_position=EventPosition('@latest'))
    with receiver:
        more_partitions = client.get_properties()
        assert more_partitions["partition_ids"] == ["0", "1"]

        received = receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Data"))
        received = receiver.receive(timeout=5)
        assert len(received) == 1
        offset = received[0].offset

        assert list(received[0].body) == [b'Data']
        assert received[0].body_as_str() == "Data"

    offset_receiver = client.create_consumer(consumer_group="$default",
                                             partition_id="0",
                                             event_position=EventPosition(
                                                 offset, inclusive=False))
    with offset_receiver:
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Message after offset"))
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 1
示例#4
0
def test_receive_with_inclusive_offset(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'))

    with receiver:
        received = receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Data"))
        time.sleep(1)
        received = receiver.receive(timeout=5)
        assert len(received) == 1
        offset = received[0].offset

        assert list(received[0].body) == [b'Data']
        assert received[0].body_as_str() == "Data"

    offset_receiver = client.create_consumer(consumer_group="$default",
                                             partition_id="0",
                                             event_position=EventPosition(
                                                 offset, inclusive=True))
    with offset_receiver:
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 1
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)
    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=3)
        assert output2 == 1
    finally:
        await receiver1.close()
        await receiver2.close()
    await client.close()
 async def __anext__(self):
     retried_times = 0
     last_exception = None
     while retried_times < self._client._config.max_retries:  # pylint:disable=protected-access
         try:
             await self._open()
             if not self._messages_iter:
                 self._messages_iter = self._handler.receive_messages_iter_async(
                 )
             message = await self._messages_iter.__anext__()
             event_data = EventData._from_message(message)  # pylint:disable=protected-access
             event_data._trace_link_message()  # pylint:disable=protected-access
             self._offset = EventPosition(event_data.offset,
                                          inclusive=False)
             retried_times = 0
             if self._track_last_enqueued_event_properties:
                 self._last_enqueued_event_properties = event_data._get_last_enqueued_event_properties(
                 )  # pylint:disable=protected-access
             return event_data
         except Exception as exception:  # pylint:disable=broad-except
             last_exception = await self._handle_exception(exception)
             await self._client._try_delay(
                 retried_times=retried_times,
                 last_exception=last_exception,  # pylint:disable=protected-access
                 entity_name=self._name)
             retried_times += 1
     log.info("%r operation has exhausted retry. Last exception: %r.",
              self._name, last_exception)
     raise last_exception
示例#7
0
async def test_receive_with_offset_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"))
        time.sleep(1)
        received = await receiver.receive(timeout=3)
        assert len(received) == 1
        offset = received[0].offset

    offset_receiver = client.create_consumer(consumer_group="$default",
                                             partition_id="0",
                                             event_position=EventPosition(
                                                 offset, inclusive=False))
    async with offset_receiver:
        received = await offset_receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Message after offset"))
        received = await offset_receiver.receive(timeout=5)
        assert len(received) == 1
示例#8
0
def test_receive_with_sequence_no(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'))

    with receiver:
        received = receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Data"))
        time.sleep(1)
        received = receiver.receive(timeout=5)
        assert len(received) == 1
        offset = received[0].sequence_number

    offset_receiver = client.create_consumer(consumer_group="$default",
                                             partition_id="0",
                                             event_position=EventPosition(
                                                 offset, False))
    with offset_receiver:
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Message next in sequence"))
        time.sleep(1)
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 1
    async def _receive(self, timeout_time=None, max_batch_size=None, **kwargs):
        last_exception = kwargs.get("last_exception")
        data_batch = []

        await self._open()
        remaining_time = timeout_time - time.time()
        if remaining_time <= 0.0:
            if last_exception:
                log.info("%r receive operation timed out. (%r)", self._name,
                         last_exception)
                raise last_exception
            return data_batch

        remaining_time_ms = 1000 * remaining_time
        message_batch = await self._handler.receive_message_batch_async(
            max_batch_size=max_batch_size, timeout=remaining_time_ms)
        for message in message_batch:
            event_data = EventData._from_message(message)  # pylint:disable=protected-access
            data_batch.append(event_data)
            event_data._trace_link_message()  # pylint:disable=protected-access

        if data_batch:
            self._offset = EventPosition(data_batch[-1].offset)

        if self._track_last_enqueued_event_properties and data_batch:
            self._last_enqueued_event_properties = data_batch[
                -1]._get_last_enqueued_event_properties()  # pylint:disable=protected-access

        return data_batch
async def test_receive_with_datetime_async(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str)
    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
    await client.close()
示例#11
0
 def get_init_event_position(self, partition_id, checkpoint):
     checkpoint_offset = checkpoint.get("offset") if checkpoint else None
     if checkpoint_offset:
         initial_event_position = EventPosition(checkpoint_offset)
     elif isinstance(self._initial_event_position, EventPosition):
         initial_event_position = self._initial_event_position
     elif isinstance(self._initial_event_position, dict):
         initial_event_position = self._initial_event_position.get(
             partition_id, EventPosition("-1"))
     else:
         initial_event_position = EventPosition(
             self._initial_event_position)
     return initial_event_position
示例#12
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
示例#13
0
    async def open_clients_async(self):
        """
        Responsible for establishing connection to event hub client
        throws EventHubsException, IOException, InterruptedException, ExecutionException.
        """
        await self.partition_context.get_initial_offset_async()
        # Create event hub client and receive handler and set options
        hostname = "{}.{}".format(self.host.eh_config.sb_name,
                                  self.host.eh_config.namespace_suffix)
        event_hub_path = self.host.eh_config.eh_name
        shared_key_cred = EventHubSharedKeyCredential(
            self.host.eh_config.policy, self.host.eh_config.sas_key)

        self.eh_client = EventHubClient(
            hostname,
            event_hub_path,
            shared_key_cred,
            network_tracing=self.host.eph_options.debug_trace,
            http_proxy=self.host.eph_options.http_proxy)
        self.partition_receive_handler = self.eh_client.create_consumer(
            partition_id=self.partition_context.partition_id,
            consumer_group=self.partition_context.consumer_group_name,
            event_position=EventPosition(self.partition_context.offset),
            prefetch=self.host.eph_options.prefetch_count,
            loop=self.loop)
        self.partition_receiver = PartitionReceiver(self)
示例#14
0
    def __init__(
            self, eventhub_client, consumer_group_name: str,
            event_handler: Callable[[PartitionContext, List[EventData]], None],
            *,
            partition_id: str = None,
            partition_manager: PartitionManager = None,
            initial_event_position=EventPosition("-1"), polling_interval: float = 10.0,
            owner_level=None, prefetch=None, track_last_enqueued_event_properties=False,
            error_handler,
            partition_initialize_handler,
            partition_close_handler
    ):
        self._consumer_group_name = consumer_group_name
        self._eventhub_client = eventhub_client
        self._namespace = eventhub_client._address.hostname  # pylint: disable=protected-access
        self._eventhub_name = eventhub_client.eh_name
        self._partition_id = partition_id
        self._event_handler = event_handler
        self._error_handler = error_handler
        self._partition_initialize_handler = partition_initialize_handler
        self._partition_close_handler = partition_close_handler
        self._partition_manager = partition_manager
        self._initial_event_position = initial_event_position  # will be replaced by reset event position in preview 4
        self._polling_interval = polling_interval
        self._ownership_timeout = self._polling_interval * 2
        self._tasks = {}  # type: Dict[str, asyncio.Task]
        self._partition_contexts = {}  # type: Dict[str, PartitionContext]
        self._owner_level = owner_level
        self._prefetch = prefetch
        self._track_last_enqueued_event_properties = track_last_enqueued_event_properties
        self._last_enqueued_event_properties = {}  # type: Dict[str, Dict[str, Any]]
        self._id = str(uuid.uuid4())
        self._running = False

        self._consumers = {}
 async def run_async(self):
     method_name = self.args.method
     logger = get_logger("{}.log".format(method_name),
                         method_name,
                         level=logging.INFO,
                         print_console=self.args.print_console)
     test_method = globals()[method_name]
     client = self.create_client(EventHubClientAsync)
     self.running = True
     if self.args.partitions.lower() != "all":
         partitions = self.args.partitions.split(",")
     else:
         partitions = await client.get_partition_ids()
     tasks = []
     for pid in partitions:
         if "receive" in method_name:
             worker = client.create_consumer(
                 consumer_group=self.args.consumer,
                 partition_id=pid,
                 event_position=EventPosition(self.args.offset),
                 prefetch=300)
         else:  # "send" in method_name
             worker = client.create_producer(partition_id=pid)
         task = self.run_test_method_async(test_method, worker, logger)
         tasks.append(task)
     await asyncio.gather(*tasks)
示例#16
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')
示例#17
0
def test_receive_with_custom_datetime_sync(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str,
                                                   network_tracing=False)
    for i in range(5):
        senders[0].send(EventData(b"Message before timestamp"))
    time.sleep(65)

    now = datetime.datetime.utcnow()
    offset = datetime.datetime(now.year, now.month, now.day, now.hour,
                               now.minute)
    for i in range(5):
        senders[0].send(EventData(b"Message after timestamp"))

    receiver = client.create_consumer(consumer_group="$default",
                                      partition_id="0",
                                      event_position=EventPosition(offset))
    with receiver:
        all_received = []
        received = receiver.receive(timeout=1)
        while received:
            all_received.extend(received)
            received = receiver.receive(timeout=1)

        assert len(all_received) == 5
        for received_event in all_received:
            assert received_event.body_as_str() == "Message after timestamp"
            assert received_event.enqueued_time > offset
def getEvents(lastOffset):
    try:
        client = EventHubClient.from_connection_string(cfg.connection_str)
        EVENT_POSITION = EventPosition(lastOffset)
        #for a vanilla eventhub replace "custom" consumer_group by "$default"
        consumer = client.create_consumer(consumer_group="custom", partition_id=PARTITION, event_position=EVENT_POSITION, prefetch=100000)
        total=0
        last_offset = -1
        with consumer:
            batched_events = consumer.receive(max_batch_size=100000)
            for event_data in batched_events:
                last_offset = event_data.offset
                last_sn = event_data.sequence_number
                total += 1
                print("Partition {}, Received {}, sn={} offset={}".format(
                    PARTITION,
                    total,
                    last_sn,
                    last_offset))
                print (event_data)
        getEvents(last_offset)
        client.stop()
    except:
        print('got all events for now')
    finally:
        print('got all events for now')
        client.stop()
    def __init__(self, eventhub_client, consumer_group_name, on_event,
                 **kwargs):
        self._consumer_group_name = consumer_group_name
        self._eventhub_client = eventhub_client
        self._namespace = eventhub_client._address.hostname  # pylint: disable=protected-access
        self._eventhub_name = eventhub_client.eh_name
        self._event_handler = on_event
        self._partition_id = kwargs.get("partition_id", None)
        self._error_handler = kwargs.get("on_error", None)
        self._partition_initialize_handler = kwargs.get(
            "on_partition_initialize", None)
        self._partition_close_handler = kwargs.get("on_partition_close", None)
        self._partition_manager = kwargs.get("partition_manager", None)
        self._initial_event_position = kwargs.get("initial_event_position",
                                                  EventPosition("-1"))

        self._polling_interval = kwargs.get("polling_interval", 10.0)
        self._ownership_timeout = self._polling_interval * 2

        self._partition_contexts = {}

        # Receive parameters
        self._owner_level = kwargs.get("owner_level", None)
        self._prefetch = kwargs.get("prefetch", None)
        self._track_last_enqueued_event_properties = kwargs.get(
            "track_last_enqueued_event_properties", False)
        self._last_enqueued_event_properties = {}
        self._id = str(uuid.uuid4())
        self._running = False
        self._lock = threading.RLock()

        self._consumers = {}
示例#20
0
    def send_and_receive_events(self, partitionID):
        with self.client.create_consumer(
                consumer_group="$default",
                partition_id=partitionID,
                event_position=EventPosition(datetime.utcnow()),
        ) as consumer:

            print("Sending events...")
            with self.client.create_producer(
                    partition_id=partitionID) as producer:
                event_list = [
                    EventData(b"Test Event 1 in Python"),
                    EventData(b"Test Event 2 in Python"),
                    EventData(b"Test Event 3 in Python"),
                ]
                producer.send(event_list)
            print("\tdone")

            print("Receiving events...")
            received = consumer.receive(max_batch_size=len(event_list),
                                        timeout=3)
            for event_data in received:
                print("\tEvent Received: " + event_data.body_as_str())

            print("\tdone")

            if len(received) != len(event_list):
                raise Exception(
                    "Error, expecting {0} events, but {1} were received.".
                    format(str(len(event_list)), str(len(received))))
 def run_sync(self):
     method_name = self.args.method
     logger = get_logger("{}.log".format(method_name),
                         method_name,
                         level=logging.INFO,
                         print_console=self.args.print_console)
     test_method = globals()[method_name]
     client = self.create_client(EventHubClient)
     self.running = True
     if self.args.partitions.lower() != "all":
         partitions = self.args.partitions.split(",")
     else:
         partitions = client.get_partition_ids()
     threads = []
     for pid in partitions:
         if "receive" in method_name:
             worker = client.create_consumer(
                 consumer_group=self.args.consumer,
                 partition_id=pid,
                 event_position=EventPosition(self.args.offset),
                 prefetch=300)
         else:  # "send" in method_name
             worker = client.create_producer(partition_id=pid)
         thread = threading.Thread(target=self.run_test_method,
                                   args=(test_method, worker, logger))
         thread.start()
         threads.append(thread)
     for thread in threads:
         thread.join()
示例#22
0
 def _from_iothub_connection_string(cls, conn_str, **kwargs):
     address, policy, key, _ = _parse_conn_str(conn_str)
     hub_name = address.split('.')[0]
     username = "******".format(policy, hub_name)
     password = _generate_sas_token(address, policy, key)
     left_slash_pos = address.find("//")
     if left_slash_pos != -1:
         host = address[left_slash_pos + 2:]
     else:
         host = address
     client = cls(host, "", EventHubSharedKeyCredential(username, password),
                  **kwargs)
     client._auth_config = {  # pylint: disable=protected-access
         'iot_username': policy,
         'iot_password': key,
         'username': username,
         'password': password
     }
     client._is_iothub = True  # pylint: disable=protected-access
     client._redirect_consumer = client.create_consumer(
         consumer_group='$default',  # pylint: disable=protected-access, no-member
         partition_id='0',
         event_position=EventPosition('-1'),
         operation='/messages/events')
     return client
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)
    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()
    await client.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)
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()
示例#26
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)
示例#27
0
def test_receive_with_event_position_sync(connstr_senders, position, inclusive,
                                          expected_result):
    def on_event(partition_context, event):
        assert event.last_enqueued_event_properties.get(
            'sequence_number') == event.sequence_number
        assert event.last_enqueued_event_properties.get(
            'offset') == event.offset
        assert event.last_enqueued_event_properties.get(
            'enqueued_time') == event.enqueued_time
        assert event.last_enqueued_event_properties.get(
            'retrieval_time') is not None

        if position == "offset":
            on_event.event_position = event.offset
        elif position == "sequence":
            on_event.event_position = event.sequence_number
        else:
            on_event.event_position = event.enqueued_time
        on_event.event = event

    on_event.event_position = None
    connection_str, senders = connstr_senders
    senders[0].send(EventData(b"Inclusive"))
    senders[1].send(EventData(b"Inclusive"))
    client = EventHubConsumerClient.from_connection_string(
        connection_str, consumer_group='$default')
    with client:
        thread = threading.Thread(target=client.receive,
                                  args=(on_event, ),
                                  kwargs={
                                      "initial_event_position": "-1",
                                      "track_last_enqueued_event_properties":
                                      True
                                  })
        thread.daemon = True
        thread.start()
        time.sleep(10)
        assert on_event.event_position is not None
    thread.join()
    senders[0].send(EventData(expected_result))
    senders[1].send(EventData(expected_result))
    client2 = EventHubConsumerClient.from_connection_string(
        connection_str, consumer_group='$default')
    with client2:
        thread = threading.Thread(target=client2.receive,
                                  args=(on_event, ),
                                  kwargs={
                                      "initial_event_position":
                                      EventPosition(on_event.event_position,
                                                    inclusive),
                                      "track_last_enqueued_event_properties":
                                      True
                                  })
        thread.daemon = True
        thread.start()
        time.sleep(10)
        assert on_event.event.body_as_str() == expected_result
    thread.join()
示例#28
0
 async def __anext__(self):
     max_retries = self.client.config.max_retries
     retry_count = 0
     while True:
         try:
             await self._open()
             if not self.messages_iter:
                 self.messages_iter = self._handler.receive_messages_iter_async(
                 )
             message = await self.messages_iter.__anext__()
             event_data = EventData._from_message(message)
             self.offset = EventPosition(event_data.offset, inclusive=False)
             retry_count = 0
             return event_data
         except Exception as exception:
             await self._handle_exception(exception, retry_count,
                                          max_retries)
             retry_count += 1
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_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()