-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_utils.py
More file actions
156 lines (125 loc) · 5.25 KB
/
test_utils.py
File metadata and controls
156 lines (125 loc) · 5.25 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import time
from collections.abc import Callable
from typing import Any
import pytest
from apify_shared.consts import WebhookEventType
from apify_client._utils import (
encode_webhook_list_to_base64,
pluck_data,
retry_with_exp_backoff,
retry_with_exp_backoff_async,
to_safe_id,
)
def test__to_safe_id() -> None:
assert to_safe_id('abc') == 'abc'
assert to_safe_id('abc/def') == 'abc~def'
assert to_safe_id('abc~def') == 'abc~def'
def test_pluck_data() -> None:
# works correctly when data is present
assert pluck_data({'data': {}}) == {}
assert pluck_data({'a': 'b', 'data': {'b': 'c'}}) == {'b': 'c'}
# throws the right error when it is not
with pytest.raises(ValueError, match='The "data" property is missing in the response.'):
pluck_data({'a': 'b'})
with pytest.raises(ValueError, match='The "data" property is missing in the response.'):
pluck_data(None)
with pytest.raises(ValueError, match='The "data" property is missing in the response.'):
pluck_data('{"a": "b"}')
def test__retry_with_exp_backoff() -> None:
attempt_counter = 0
class RetryableError(Exception):
pass
class NonRetryableError(Exception):
pass
def returns_on_fifth_attempt(_stop_retrying: Callable, attempt: int) -> Any:
nonlocal attempt_counter
attempt_counter += 1
if attempt == 5:
return 'SUCCESS'
raise RetryableError
def bails_on_third_attempt(stop_retrying: Callable, attempt: int) -> Any:
nonlocal attempt_counter
attempt_counter += 1
if attempt == 3:
stop_retrying()
raise NonRetryableError
else: # noqa: RET506
raise RetryableError
# Returns the correct result after the correct time (should take 100 + 200 + 400 + 800 = 1500 ms)
start = time.time()
result = retry_with_exp_backoff(
returns_on_fifth_attempt, backoff_base_millis=100, backoff_factor=2, random_factor=0
)
elapsed_time_seconds = time.time() - start
assert result == 'SUCCESS'
assert attempt_counter == 5
assert elapsed_time_seconds > 1.4
assert elapsed_time_seconds < 2.0
# Stops retrying when failed for max_retries times
attempt_counter = 0
with pytest.raises(RetryableError):
retry_with_exp_backoff(returns_on_fifth_attempt, max_retries=3, backoff_base_millis=1)
assert attempt_counter == 4
# Bails when the bail function is called
attempt_counter = 0
with pytest.raises(NonRetryableError):
retry_with_exp_backoff(bails_on_third_attempt, backoff_base_millis=1)
assert attempt_counter == 3
async def test__retry_with_exp_backoff_async() -> None:
attempt_counter = 0
class RetryableError(Exception):
pass
class NonRetryableError(Exception):
pass
async def returns_on_fifth_attempt(_stop_retrying: Callable, attempt: int) -> Any:
nonlocal attempt_counter
attempt_counter += 1
if attempt == 5:
return 'SUCCESS'
raise RetryableError
async def bails_on_third_attempt(stop_retrying: Callable, attempt: int) -> Any:
nonlocal attempt_counter
attempt_counter += 1
if attempt == 3:
stop_retrying()
raise NonRetryableError
else: # noqa: RET506
raise RetryableError
# Returns the correct result after the correct time (should take 100 + 200 + 400 + 800 = 1500 ms)
start = time.time()
result = await retry_with_exp_backoff_async(
returns_on_fifth_attempt, backoff_base_millis=100, backoff_factor=2, random_factor=0
)
elapsed_time_seconds = time.time() - start
assert result == 'SUCCESS'
assert attempt_counter == 5
assert elapsed_time_seconds > 1.4
assert elapsed_time_seconds < 2.0
# Stops retrying when failed for max_retries times
attempt_counter = 0
with pytest.raises(RetryableError):
await retry_with_exp_backoff_async(returns_on_fifth_attempt, max_retries=3, backoff_base_millis=1)
assert attempt_counter == 4
# Bails when the bail function is called
attempt_counter = 0
with pytest.raises(NonRetryableError):
await retry_with_exp_backoff_async(bails_on_third_attempt, backoff_base_millis=1)
assert attempt_counter == 3
def test__encode_webhook_list_to_base64() -> None:
assert encode_webhook_list_to_base64([]) == 'W10='
assert (
encode_webhook_list_to_base64(
[
{
'event_types': [WebhookEventType.ACTOR_RUN_CREATED],
'request_url': 'https://example.com/run-created',
},
{
'event_types': [WebhookEventType.ACTOR_RUN_SUCCEEDED],
'request_url': 'https://example.com/run-succeeded',
'payload_template': '{"hello": "world", "resource":{{resource}}}',
},
]
)
== 'W3siZXZlbnRUeXBlcyI6IFsiQUNUT1IuUlVOLkNSRUFURUQiXSwgInJlcXVlc3RVcmwiOiAiaHR0cHM6Ly9leGFtcGxlLmNvbS9ydW4tY3JlYXRlZCJ9LCB7ImV2ZW50VHlwZXMiOiBbIkFDVE9SLlJVTi5TVUNDRUVERUQiXSwgInJlcXVlc3RVcmwiOiAiaHR0cHM6Ly9leGFtcGxlLmNvbS9ydW4tc3VjY2VlZGVkIiwgInBheWxvYWRUZW1wbGF0ZSI6ICJ7XCJoZWxsb1wiOiBcIndvcmxkXCIsIFwicmVzb3VyY2VcIjp7e3Jlc291cmNlfX19In1d' # noqa: E501
)