File: test_websocket_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (61 lines) | stat: -rw-r--r-- 2,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import pytest
import asyncio
import logging

from azure.eventhub._pyamqp.aio import _authentication_async
from azure.eventhub._pyamqp.aio import ReceiveClientAsync, SendClientAsync
from azure.eventhub._pyamqp.constants import TransportType
from azure.eventhub._pyamqp.message import Message
import sys


async def send_message(live_eventhub):
    uri = "sb://{}/{}".format(live_eventhub["hostname"], live_eventhub["event_hub"])
    sas_auth = _authentication_async.SASTokenAuthAsync(
        uri=uri, audience=uri, username=live_eventhub["key_name"], password=live_eventhub["access_key"]
    )

    target = "amqps://{}/{}/Partitions/{}".format(
        live_eventhub["hostname"], live_eventhub["event_hub"], live_eventhub["partition"]
    )

    message = Message(value="Single Message")

    async with SendClientAsync(
        live_eventhub["hostname"], target, auth=sas_auth, debug=True, transport_type=TransportType.Amqp
    ) as send_client:
        await send_client.send_message_async(message)


@pytest.mark.asyncio
async def test_event_hubs_client_web_socket_async(live_eventhub):
    uri = "sb://{}/{}".format(live_eventhub["hostname"], live_eventhub["event_hub"])
    sas_auth = _authentication_async.SASTokenAuthAsync(
        uri=uri, audience=uri, username=live_eventhub["key_name"], password=live_eventhub["access_key"]
    )

    source = "amqps://{}/{}/ConsumerGroups/{}/Partitions/{}".format(
        live_eventhub["hostname"],
        live_eventhub["event_hub"],
        live_eventhub["consumer_group"],
        live_eventhub["partition"],
    )

    await send_message(live_eventhub=live_eventhub)

    async with ReceiveClientAsync(
        live_eventhub["hostname"] + "/$servicebus/websocket/",
        source,
        auth=sas_auth,
        debug=False,
        timeout=500,
        prefetch=1,
        transport_type=TransportType.AmqpOverWebsocket,
    ) as receive_client:
        messages = await receive_client.receive_message_batch_async(max_batch_size=1)
        assert len(messages) > 0