Exemple #1
0
async def test_client_secret_credential_async(aad_credential, live_eventhub):
    try:
        from azure.identity.aio import EnvironmentCredential
    except ImportError:
        pytest.skip("No azure identity library")

    credential = EnvironmentCredential()
    producer_client = EventHubProducerClient(host=live_eventhub['hostname'],
                                             event_hub_path=live_eventhub['event_hub'],
                                             credential=credential,
                                             user_agent='customized information')
    consumer_client = EventHubConsumerClient(host=live_eventhub['hostname'],
                                             event_hub_path=live_eventhub['event_hub'],
                                             credential=credential,
                                             user_agent='customized information')

    async with producer_client:
        await producer_client.send(EventData(body='A single message'))

    async def event_handler(partition_context, events):
        assert partition_context.partition_id == '0'
        assert len(events) == 1
        assert list(events[0].body)[0] == 'A single message'.encode('utf-8')

    async with consumer_client:
        task = asyncio.ensure_future(
            consumer_client.receive(event_handler=event_handler, consumer_group='$default', partition_id='0'))
        await asyncio.sleep(2)
        task.cancel()
async def test_client_secret_credential_async(live_eventhub):
    credential = EnvironmentCredential()
    producer_client = EventHubProducerClient(
        fully_qualified_namespace=live_eventhub['hostname'],
        eventhub_name=live_eventhub['event_hub'],
        credential=credential,
        user_agent='customized information')
    consumer_client = EventHubConsumerClient(
        fully_qualified_namespace=live_eventhub['hostname'],
        eventhub_name=live_eventhub['event_hub'],
        consumer_group='$default',
        credential=credential,
        user_agent='customized information')

    async with producer_client:
        batch = await producer_client.create_batch(partition_id='0')
        batch.add(EventData(body='A single message'))
        await producer_client.send_batch(batch)

    def on_event(partition_context, event):
        on_event.called = True
        on_event.partition_id = partition_context.partition_id
        on_event.event = event

    on_event.called = False
    async with consumer_client:
        task = asyncio.ensure_future(
            consumer_client.receive(on_event,
                                    partition_id='0',
                                    starting_position='-1'))
        await asyncio.sleep(13)
    await task
    assert on_event.called is True
    assert on_event.partition_id == "0"
    assert list(on_event.event.body)[0] == 'A single message'.encode('utf-8')
Exemple #3
0
async def test_client_secret_credential_async(aad_credential, live_eventhub):
    try:
        from azure.identity.aio import EnvironmentCredential
    except ImportError:
        pytest.skip("No azure identity library")

    credential = EnvironmentCredential()
    producer_client = EventHubProducerClient(
        fully_qualified_namespace=live_eventhub['hostname'],
        eventhub_name=live_eventhub['event_hub'],
        credential=credential,
        user_agent='customized information')
    consumer_client = EventHubConsumerClient(
        fully_qualified_namespace=live_eventhub['hostname'],
        eventhub_name=live_eventhub['event_hub'],
        consumer_group='$default',
        credential=credential,
        user_agent='customized information')

    async with producer_client:
        await producer_client.send(EventData(body='A single message'),
                                   partition_id='0')

    def on_event(partition_context, event):
        on_event.called = True
        on_event.partition_id = partition_context.partition_id
        on_event.event = event

    on_event.called = False
    async with consumer_client:
        task = asyncio.ensure_future(
            consumer_client.receive(on_event, partition_id='0'))
        await asyncio.sleep(6)
    await task
    assert on_event.called is True
    assert on_event.partition_id == "0"
    assert list(on_event.event.body)[0] == 'A single message'.encode('utf-8')