File: _decorators_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 (91 lines) | stat: -rw-r--r-- 3,357 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
import asyncio
import functools
from typing import Callable, Any

from azure.core.exceptions import HttpResponseError

from devtools_testutils import is_live, is_live_and_not_recording, trim_kwargs_from_test_function
from azure.communication.jobrouter._shared.utils import parse_connection_str


class RouterPreparersAsync(object):
    @staticmethod
    def before_test_execute_async(
        method_name,  # type: str
        **kwargs  # type: Any
    ):
        def __decorator__(func):
            @functools.wraps(func)
            async def wrapper(self, *args, **kwargs):
                trim_kwargs_from_test_function(func, kwargs)
                first_method = getattr(self, method_name)
                await first_method()
                return await func(self, *args, **kwargs)

            return wrapper

        return __decorator__

    @staticmethod
    def after_test_execute_async(
        method_name,  # type: str
        **kwargs  # type: Any
    ):
        def __decorator__(func):
            @functools.wraps(func)
            async def wrapper(self, *args, **kwargs):
                trim_kwargs_from_test_function(func, kwargs)
                first_method = getattr(self, method_name)
                try:
                    return await func(self, *args, **kwargs)
                except Exception as e:
                    raise e
                finally:
                    try:
                        await first_method()
                    except:
                        print("")  # Consume exceptions

            return wrapper

        return __decorator__

    @staticmethod
    def router_test_decorator_async(func: Callable[[], object], **kwargs: Any):
        async def wrapper(self, *args, **kwargs):
            if is_live() or is_live_and_not_recording():
                self.connection_string = os.getenv("COMMUNICATION_LIVETEST_DYNAMIC_CONNECTION_STRING")
                endpoint, _ = parse_connection_str(self.connection_string)
                self.resource_name = endpoint.split(".")[0]
            else:
                self.connection_string = "endpoint=https://sanitized.communication.azure.net/;accesskey=fake==="
                self.resource_name = "sanitized"

            EXPONENTIAL_BACKOFF = 1.5
            RETRY_COUNT = 0
            MAX_RETRY = 10

            try:
                return await func(self, *args, **kwargs)
            except HttpResponseError as exc:
                if exc.status_code != 429:
                    raise
                print("Retrying: {} {}".format(RETRY_COUNT, EXPONENTIAL_BACKOFF))
                while RETRY_COUNT < MAX_RETRY:
                    if is_live():
                        await asyncio.sleep(EXPONENTIAL_BACKOFF)
                    try:
                        return await func(self, *args, **kwargs)
                    except HttpResponseError as exc:
                        print("Retrying: {} {}".format(RETRY_COUNT, EXPONENTIAL_BACKOFF))
                        EXPONENTIAL_BACKOFF **= 2
                        RETRY_COUNT += 1
                        if exc.status_code != 429 or RETRY_COUNT >= MAX_RETRY:
                            raise

        return wrapper