コード例 #1
0
async def test_iothub_client_receive_async(live_iothub_config):
    operation = '/messages/events/ConsumerGroups/{}/Partitions/'.format(
        live_iothub_config['consumer_group'])
    auth = authentication.SASLPlain(
        live_iothub_config['hostname'],
        *_build_iothub_amqp_endpoint_from_target(live_iothub_config))
    source = 'amqps://' + live_iothub_config['hostname'] + operation
    log.info("Source: {}".format(source))

    async with uamqp.ConnectionAsync(live_iothub_config['hostname'],
                                     auth,
                                     debug=True) as conn:
        tasks = [
            _receive_mesages(conn, source + '0', auth),
            _receive_mesages(conn, source + '1', auth)
        ]
        results = await asyncio.gather(*tasks)
        redirect = results[0]
        new_auth = authentication.SASLPlain(redirect.hostname,
                                            live_iothub_config['key_name'],
                                            live_iothub_config['access_key'])
        await conn.redirect_async(redirect, new_auth)
        tasks = []
        for t in results:
            tasks.append(_receive_mesages(conn, t.address, auth))
        messages = await asyncio.gather(*tasks)
コード例 #2
0
 async def _create_uamqp_connection(self):
     auth = await create_authentication(self)
     self._connection = uamqp.ConnectionAsync(
         hostname=self.fully_qualified_namespace,
         sasl=auth,
         debug=self._config.logging_enable
     )
async def test_event_hubs_shared_connection_async(live_eventhub_config):
    uri = "sb://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub'])
    sas_auth = authentication.SASTokenAsync.from_shared_access_key(
        uri, live_eventhub_config['key_name'], live_eventhub_config['access_key'])
    source = "amqps://{}/{}/ConsumerGroups/{}/Partitions/".format(
        live_eventhub_config['hostname'],
        live_eventhub_config['event_hub'],
        live_eventhub_config['consumer_group'])

    async with uamqp.ConnectionAsync(live_eventhub_config['hostname'], sas_auth, debug=False) as conn:
        partition_0 = uamqp.ReceiveClientAsync(source + "0", debug=False, auth=sas_auth, timeout=3000, prefetch=10)
        partition_1 = uamqp.ReceiveClientAsync(source + "1", debug=False, auth=sas_auth, timeout=3000, prefetch=10)
        await partition_0.open_async(connection=conn)
        await partition_1.open_async(connection=conn)
        tasks = [
            partition_0.receive_message_batch_async(1),
            partition_1.receive_message_batch_async(1)
        ]
        try:
            messages = await asyncio.gather(*tasks)
            assert len(messages[0]) == 1 and len(messages[1]) == 1
        except:
            raise
        finally:
            await partition_0.close_async()
            await partition_1.close_async()
コード例 #4
0
async def _initiate_event_monitor(target: Target,
                                  enqueued_time_utc,
                                  on_message_received,
                                  timeout=0):
    if not target.partitions:
        logger.debug("No Event Hub partitions found to listen on.")
        return

    coroutines = []

    async with uamqp.ConnectionAsync(
            target.hostname,
            sasl=target.auth,
            debug=DEBUG,
            container_id=_get_container_id(),
            properties=_get_conn_props(),
    ) as conn:
        for p in target.partitions:
            coroutines.append(
                _monitor_events(
                    target=target,
                    connection=conn,
                    partition=p,
                    enqueued_time_utc=enqueued_time_utc,
                    on_message_received=on_message_received,
                    timeout=timeout,
                ))
        return await asyncio.gather(*coroutines, return_exceptions=True)
コード例 #5
0
async def initiate_event_monitor(
    target,
    consumer_group,
    enqueued_time,
    device_id=None,
    properties=None,
    timeout=0,
    output=None,
    content_type=None,
    devices=None,
    interface_name=None,
    pnp_context=None,
    validate_messages=False,
    simulate_errors=False,
):
    def _get_conn_props():
        properties = {}
        properties["product"] = USER_AGENT
        properties["version"] = VERSION
        properties["framework"] = "Python {}.{}.{}".format(*sys.version_info[0:3])
        properties["platform"] = sys.platform
        return properties

    if not target["partitions"]:
        logger.debug("No Event Hub partitions found to listen on.")
        return

    coroutines = []

    async with uamqp.ConnectionAsync(
        target["endpoint"],
        sasl=target["auth"],
        debug=DEBUG,
        container_id=_get_container_id(),
        properties=_get_conn_props(),
    ) as conn:
        for p in target["partitions"]:
            coroutines.append(
                monitor_events(
                    endpoint=target["endpoint"],
                    connection=conn,
                    path=target["path"],
                    auth=target["auth"],
                    partition=p,
                    consumer_group=consumer_group,
                    enqueuedtimeutc=enqueued_time,
                    properties=properties,
                    device_id=device_id,
                    timeout=timeout,
                    output=output,
                    content_type=content_type,
                    devices=devices,
                    interface_name=interface_name,
                    pnp_context=pnp_context,
                    validate_messages=validate_messages,
                    simulate_errors=simulate_errors,
                )
            )
        return await asyncio.gather(*coroutines, return_exceptions=True)
コード例 #6
0
async def initiate_event_monitor(target,
                                 consumer_group,
                                 enqueued_time,
                                 device_id=None,
                                 properties=None,
                                 timeout=0,
                                 output=None,
                                 content_type=None):
    def _get_conn_props():
        properties = {}
        properties["product"] = "az.cli.iot.extension"
        properties["version"] = VERSION
        properties["framework"] = "Python {}.{}.{}".format(
            *sys.version_info[0:3])
        properties["platform"] = sys.platform
        return properties

    if not target.get('events'):
        endpoint = _build_iothub_amqp_endpoint_from_target(target)
        _, update = await evaluate_redirect(endpoint)
        target['events'] = update['events']
        auth = _build_auth_container(target)
        meta_data = await query_meta_data(target['events']['address'],
                                          target['events']['path'], auth)
        partition_count = meta_data[b'partition_count']
        partition_ids = []
        for i in range(int(partition_count)):
            partition_ids.append(str(i))
        target['events']['partition_ids'] = partition_ids

    partitions = target['events']['partition_ids']

    if not partitions:
        logger.debug('No Event Hub partitions found to listen on.')
        return

    coroutines = []

    auth = _build_auth_container(target)
    async with uamqp.ConnectionAsync(target['events']['endpoint'],
                                     sasl=auth,
                                     debug=DEBUG,
                                     container_id=str(uuid4()),
                                     properties=_get_conn_props()) as conn:
        for p in partitions:
            coroutines.append(
                monitor_events(endpoint=target['events']['endpoint'],
                               connection=conn,
                               path=target['events']['path'],
                               auth=auth,
                               partition=p,
                               consumer_group=consumer_group,
                               enqueuedtimeutc=enqueued_time,
                               properties=properties,
                               device_id=device_id,
                               timeout=timeout,
                               output=output,
                               content_type=content_type))
        await asyncio.gather(*coroutines, return_exceptions=True)
コード例 #7
0
async def initiate_event_monitor(target,
                                 consumer_group,
                                 enqueued_time,
                                 device_id=None,
                                 properties=None,
                                 timeout=0,
                                 output=None,
                                 content_type=None,
                                 devices=None,
                                 interface_id=None,
                                 pnp_context=None):
    def _get_conn_props():
        properties = {}
        properties["product"] = "az.cli.iot.extension"
        properties["version"] = VERSION
        properties["framework"] = "Python {}.{}.{}".format(
            *sys.version_info[0:3])
        properties["platform"] = sys.platform
        return properties

    if not target['partitions']:
        logger.debug('No Event Hub partitions found to listen on.')
        return

    coroutines = []

    async with uamqp.ConnectionAsync(target['endpoint'],
                                     sasl=target['auth'],
                                     debug=DEBUG,
                                     container_id=str(uuid4()),
                                     properties=_get_conn_props()) as conn:
        for p in target['partitions']:
            coroutines.append(
                monitor_events(endpoint=target['endpoint'],
                               connection=conn,
                               path=target['path'],
                               auth=target['auth'],
                               partition=p,
                               consumer_group=consumer_group,
                               enqueuedtimeutc=enqueued_time,
                               properties=properties,
                               device_id=device_id,
                               timeout=timeout,
                               output=output,
                               content_type=content_type,
                               devices=devices,
                               interface_id=interface_id,
                               pnp_context=pnp_context))
        await asyncio.gather(*coroutines, return_exceptions=True)