-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest__decorator.py
More file actions
237 lines (185 loc) · 6.96 KB
/
Copy pathtest__decorator.py
File metadata and controls
237 lines (185 loc) · 6.96 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from __future__ import annotations
import asyncio
import time
from collections import defaultdict
from concurrent.futures.thread import ThreadPoolExecutor
from datetime import timedelta
from typing import TYPE_CHECKING, Any
import pytest
from py_cashier import cache
from py_cashier._storages import TTLMapStorage
from py_cashier._storages._ttl_map import TTLMapAsyncStorage
if TYPE_CHECKING:
from collections.abc import Callable
from py_cashier._decorators import PAsyncStorage, PStorage
@pytest.mark.parametrize(
("a", "b", "expected"),
[
(1, 2, 3),
(1, 1, 2),
(5, 10, 15),
],
)
def test_cache(a: int, b: int, expected: int) -> None:
"""Test that the cache decorator works correctly for synchronous functions."""
calls: dict[tuple[int, int], int] = defaultdict(int)
@cache(storage=lambda: TTLMapStorage(max_size=1000, ttl=timedelta(seconds=1)))
def func(x: int, y: int) -> int:
nonlocal calls
calls[(x, y)] += 1
return x + y
# First call should execute the function
assert func(a, b) == expected
# Second call should use the cached result
assert func(a, b) == expected
# The function should only be called once for each unique set of arguments
assert calls[(a, b)] == 1
@pytest.mark.parametrize(
("a", "b", "expected"),
[
(1, 2, 3),
(1, 1, 2),
(5, 10, 15),
],
)
async def test_cache_async(a: int, b: int, expected: int) -> None:
"""Test that the cache decorator works correctly for asynchronous functions."""
calls: dict[tuple[int, int], int] = defaultdict(int)
@cache(storage=lambda: TTLMapAsyncStorage(max_size=1000, ttl=timedelta(seconds=1)))
async def func(x: int, y: int) -> int:
nonlocal calls
calls[(x, y)] += 1
return x + y
# First call should execute the function
assert await func(a, b) == expected
# Second call should use the cached result
assert await func(a, b) == expected
# The function should only be called once for each unique set of arguments
assert calls[(a, b)] == 1
@pytest.mark.parametrize(
("a", "b", "expected", "tasks_count"),
[
(1, 2, 3, 100),
(5, 10, 15, 50),
],
)
async def test_cache_dog_piling_async(a: int, b: int, expected: int, tasks_count: int) -> None:
"""Test dog-piling prevention in an asynchronous context.
Tests the behavior of the caching mechanism to prevent dog-piling in an
asynchronous context. It verifies that concurrent calls to a cached
function with the same arguments at the same time result in only one actual function call,
returning the cached result for the rest.
"""
calls = 0
@cache(storage=lambda: TTLMapAsyncStorage(max_size=1000, ttl=timedelta(seconds=1)))
async def func(x: int, y: int) -> int:
await asyncio.sleep(0.1)
nonlocal calls
calls += 1
return x + y
tasks = [func(a, b) for _ in range(tasks_count)]
results = await asyncio.gather(*tasks)
assert all(result == expected for result in results)
assert calls == 1
@pytest.mark.parametrize(
("a", "b", "expected", "workers_count"),
[
(1, 2, 3, 16),
(5, 10, 15, 8),
],
)
def test_cache_dog_piling_sync(a: int, b: int, expected: int, workers_count: int) -> None:
"""Test that the cache decorator prevents dog-piling in a multithreaded environment.
Dog-piling occurs when multiple threads try to compute the same value simultaneously.
The cache should ensure the function is called only once despite multiple concurrent requests.
"""
calls = 0
@cache(storage=lambda: TTLMapStorage(max_size=1000, ttl=timedelta(seconds=1)))
def func(x: int, y: int) -> int:
time.sleep(0.1)
nonlocal calls
calls += 1
return x + y
# Configure concurrent environment
tasks_count = workers_count * 8
executor = ThreadPoolExecutor(max_workers=workers_count)
# Create input arguments - each call will pass a tuple of (a, b)
input_args = ((a, b) for _ in range(tasks_count))
# Execute a function concurrently and check results
results = executor.map(lambda p: func(*p), input_args)
# Verify all results equal the expected value, and the function was called exactly once
assert all(result == expected for result in results)
assert calls == 1
@pytest.mark.parametrize(
("a", "b", "workers_count"),
[
(1, 2, 16),
(5, 10, 8),
],
)
def test_cache_failing_func_sync(a: int, b: int, workers_count: int) -> None:
"""Test that failing functions are not cached in synchronous context."""
calls = 0
@cache(storage=lambda: TTLMapStorage(max_size=1000, ttl=timedelta(seconds=1)))
def func(x: int, y: int) -> int:
time.sleep(0.03)
nonlocal calls
calls += 1
msg = f"Test error: {x}, {y}"
raise ValueError(msg)
tasks_count = workers_count * 2
executor = ThreadPoolExecutor(max_workers=workers_count)
# Create input arguments - each call will pass the same tuple
input_args = ((a, b) for _ in range(tasks_count))
# Execute a function concurrently and check results
def func2(p: tuple[int, int]) -> int:
try:
return func(*p)
except ValueError:
return 0
results = executor.map(func2, input_args)
assert all(result == 0 for result in results)
# Each call should execute the function (no caching of errors)
assert calls == tasks_count
@pytest.mark.parametrize(
("a", "b", "tasks_count"),
[
(1, 2, 10),
(5, 10, 5),
],
)
async def test_cache_failing_func_async(a: int, b: int, tasks_count: int) -> None:
"""Test that failing functions are not cached in asynchronous context."""
calls = 0
@cache(storage=lambda: TTLMapAsyncStorage(max_size=1000, ttl=timedelta(seconds=1)))
async def func(x: int, y: int) -> int:
await asyncio.sleep(0.1)
nonlocal calls
calls += 1
msg = f"Test error: {x}, {y}"
raise ValueError(msg)
async def func2(x: int, y: int) -> int:
try:
return await func(x, y)
except ValueError:
return 0
tasks = [func2(a, b) for _ in range(tasks_count)]
results = await asyncio.gather(*tasks)
assert all(result == 0 for result in results)
# Each call should execute the function (no caching of errors)
assert calls == tasks_count
async def _af() -> None:
pass
def _f() -> None:
pass
@pytest.mark.parametrize(
("func", "storage"),
[
(_af, lambda: TTLMapStorage(max_size=1000, ttl=timedelta(seconds=1))),
(_f, lambda: TTLMapAsyncStorage(max_size=1000, ttl=timedelta(seconds=1))),
],
)
def test__decorator__invalid_storage(func: Callable[..., Any], storage: PStorage | PAsyncStorage) -> None:
"""Test that the decorator raises TypeError for invalid storage."""
with pytest.raises(TypeError, match="(Async|Regular) function requires a(n async| sync) storage"):
cache(storage=storage)(func)