port-ocean 0.14.0__py3-none-any.whl → 0.14.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of port-ocean might be problematic. Click here for more details.
- port_ocean/tests/utils/test_cache.py +189 -0
- port_ocean/utils/cache.py +37 -1
- {port_ocean-0.14.0.dist-info → port_ocean-0.14.1.dist-info}/METADATA +1 -1
- {port_ocean-0.14.0.dist-info → port_ocean-0.14.1.dist-info}/RECORD +7 -6
- {port_ocean-0.14.0.dist-info → port_ocean-0.14.1.dist-info}/LICENSE.md +0 -0
- {port_ocean-0.14.0.dist-info → port_ocean-0.14.1.dist-info}/WHEEL +0 -0
- {port_ocean-0.14.0.dist-info → port_ocean-0.14.1.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
import asyncio
|
|
3
|
+
from port_ocean.utils import cache # Import the module where 'event' is used
|
|
4
|
+
import pytest
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import AsyncGenerator, AsyncIterator, List, TypeVar
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class EventContext:
|
|
11
|
+
attributes: dict[str, Any] = field(default_factory=dict)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.fixture
|
|
15
|
+
def event() -> EventContext:
|
|
16
|
+
return EventContext()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
T = TypeVar("T")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async def collect_iterator_results(iterator: AsyncIterator[List[T]]) -> List[T]:
|
|
23
|
+
results = []
|
|
24
|
+
async for item in iterator:
|
|
25
|
+
results.extend(item)
|
|
26
|
+
return results
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@pytest.mark.asyncio
|
|
30
|
+
async def test_cache_iterator_result(event: EventContext, monkeypatch: Any) -> None:
|
|
31
|
+
monkeypatch.setattr(cache, "event", event)
|
|
32
|
+
|
|
33
|
+
call_count = 0
|
|
34
|
+
|
|
35
|
+
@cache.cache_iterator_result()
|
|
36
|
+
async def sample_iterator(x: int) -> AsyncGenerator[List[int], None]:
|
|
37
|
+
nonlocal call_count
|
|
38
|
+
call_count += 1
|
|
39
|
+
for i in range(x):
|
|
40
|
+
await asyncio.sleep(0.1)
|
|
41
|
+
yield [i]
|
|
42
|
+
|
|
43
|
+
result1 = await collect_iterator_results(sample_iterator(3))
|
|
44
|
+
assert result1 == [0, 1, 2]
|
|
45
|
+
assert call_count == 1
|
|
46
|
+
|
|
47
|
+
result2 = await collect_iterator_results(sample_iterator(3))
|
|
48
|
+
assert result2 == [0, 1, 2]
|
|
49
|
+
assert call_count == 1
|
|
50
|
+
|
|
51
|
+
result3 = await collect_iterator_results(sample_iterator(4))
|
|
52
|
+
assert result3 == [0, 1, 2, 3]
|
|
53
|
+
assert call_count == 2
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@pytest.mark.asyncio
|
|
57
|
+
async def test_cache_iterator_result_with_kwargs(
|
|
58
|
+
event: EventContext, monkeypatch: Any
|
|
59
|
+
) -> None:
|
|
60
|
+
monkeypatch.setattr(cache, "event", event)
|
|
61
|
+
|
|
62
|
+
call_count = 0
|
|
63
|
+
|
|
64
|
+
@cache.cache_iterator_result()
|
|
65
|
+
async def sample_iterator(x: int, y: int = 1) -> AsyncGenerator[List[int], None]:
|
|
66
|
+
nonlocal call_count
|
|
67
|
+
call_count += 1
|
|
68
|
+
for i in range(x * y):
|
|
69
|
+
await asyncio.sleep(0.1)
|
|
70
|
+
yield [i]
|
|
71
|
+
|
|
72
|
+
result1 = await collect_iterator_results(sample_iterator(2, y=2))
|
|
73
|
+
assert result1 == [0, 1, 2, 3]
|
|
74
|
+
assert call_count == 1
|
|
75
|
+
|
|
76
|
+
result2 = await collect_iterator_results(sample_iterator(2, y=2))
|
|
77
|
+
assert result2 == [0, 1, 2, 3]
|
|
78
|
+
assert call_count == 1
|
|
79
|
+
|
|
80
|
+
result3 = await collect_iterator_results(sample_iterator(2, y=3))
|
|
81
|
+
assert result3 == [0, 1, 2, 3, 4, 5]
|
|
82
|
+
assert call_count == 2
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@pytest.mark.asyncio
|
|
86
|
+
async def test_cache_iterator_result_no_cache(
|
|
87
|
+
event: EventContext, monkeypatch: Any
|
|
88
|
+
) -> None:
|
|
89
|
+
monkeypatch.setattr(cache, "event", event)
|
|
90
|
+
|
|
91
|
+
call_count = 0
|
|
92
|
+
|
|
93
|
+
@cache.cache_iterator_result()
|
|
94
|
+
async def sample_iterator(x: int) -> AsyncGenerator[List[int], None]:
|
|
95
|
+
nonlocal call_count
|
|
96
|
+
call_count += 1
|
|
97
|
+
for i in range(x):
|
|
98
|
+
await asyncio.sleep(0.1)
|
|
99
|
+
yield [i]
|
|
100
|
+
|
|
101
|
+
result1 = await collect_iterator_results(sample_iterator(3))
|
|
102
|
+
assert result1 == [0, 1, 2]
|
|
103
|
+
assert call_count == 1
|
|
104
|
+
|
|
105
|
+
event.attributes.clear()
|
|
106
|
+
|
|
107
|
+
result2 = await collect_iterator_results(sample_iterator(3))
|
|
108
|
+
assert result2 == [0, 1, 2]
|
|
109
|
+
assert call_count == 2
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@pytest.mark.asyncio
|
|
113
|
+
async def test_cache_coroutine_result(event: EventContext, monkeypatch: Any) -> None:
|
|
114
|
+
monkeypatch.setattr(cache, "event", event)
|
|
115
|
+
|
|
116
|
+
call_count = 0
|
|
117
|
+
|
|
118
|
+
@cache.cache_coroutine_result()
|
|
119
|
+
async def sample_coroutine(x: int) -> int:
|
|
120
|
+
nonlocal call_count
|
|
121
|
+
call_count += 1
|
|
122
|
+
await asyncio.sleep(0.1)
|
|
123
|
+
return x * 2
|
|
124
|
+
|
|
125
|
+
result1 = await sample_coroutine(2)
|
|
126
|
+
assert result1 == 4
|
|
127
|
+
assert call_count == 1
|
|
128
|
+
|
|
129
|
+
result2 = await sample_coroutine(2)
|
|
130
|
+
assert result2 == 4
|
|
131
|
+
assert call_count == 1
|
|
132
|
+
|
|
133
|
+
result3 = await sample_coroutine(3)
|
|
134
|
+
assert result3 == 6
|
|
135
|
+
assert call_count == 2
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@pytest.mark.asyncio
|
|
139
|
+
async def test_cache_coroutine_result_with_kwargs(
|
|
140
|
+
event: EventContext, monkeypatch: Any
|
|
141
|
+
) -> None:
|
|
142
|
+
monkeypatch.setattr(cache, "event", event)
|
|
143
|
+
|
|
144
|
+
call_count = 0
|
|
145
|
+
|
|
146
|
+
@cache.cache_coroutine_result()
|
|
147
|
+
async def sample_coroutine(x: int, y: int = 1) -> int:
|
|
148
|
+
nonlocal call_count
|
|
149
|
+
call_count += 1
|
|
150
|
+
await asyncio.sleep(0.1)
|
|
151
|
+
return x * y
|
|
152
|
+
|
|
153
|
+
result1 = await sample_coroutine(2, y=3)
|
|
154
|
+
assert result1 == 6
|
|
155
|
+
assert call_count == 1
|
|
156
|
+
|
|
157
|
+
result2 = await sample_coroutine(2, y=3)
|
|
158
|
+
assert result2 == 6
|
|
159
|
+
assert call_count == 1
|
|
160
|
+
|
|
161
|
+
result3 = await sample_coroutine(2, y=4)
|
|
162
|
+
assert result3 == 8
|
|
163
|
+
assert call_count == 2
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@pytest.mark.asyncio
|
|
167
|
+
async def test_cache_coroutine_result_no_cache(
|
|
168
|
+
event: EventContext, monkeypatch: Any
|
|
169
|
+
) -> None:
|
|
170
|
+
monkeypatch.setattr(cache, "event", event)
|
|
171
|
+
|
|
172
|
+
call_count = 0
|
|
173
|
+
|
|
174
|
+
@cache.cache_coroutine_result()
|
|
175
|
+
async def sample_coroutine(x: int) -> int:
|
|
176
|
+
nonlocal call_count
|
|
177
|
+
call_count += 1
|
|
178
|
+
await asyncio.sleep(0.1)
|
|
179
|
+
return x * 2
|
|
180
|
+
|
|
181
|
+
result1 = await sample_coroutine(2)
|
|
182
|
+
assert result1 == 4
|
|
183
|
+
assert call_count == 1
|
|
184
|
+
|
|
185
|
+
event.attributes.clear()
|
|
186
|
+
|
|
187
|
+
result2 = await sample_coroutine(2)
|
|
188
|
+
assert result2 == 4
|
|
189
|
+
assert call_count == 2
|
port_ocean/utils/cache.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import functools
|
|
2
2
|
import hashlib
|
|
3
|
-
from typing import Callable, AsyncIterator, Any
|
|
3
|
+
from typing import Callable, AsyncIterator, Awaitable, Any
|
|
4
4
|
from port_ocean.context.event import event
|
|
5
5
|
|
|
6
6
|
AsyncIteratorCallable = Callable[..., AsyncIterator[list[Any]]]
|
|
7
|
+
AsyncCallable = Callable[..., Awaitable[Any]]
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
def hash_func(function_name: str, *args: Any, **kwargs: Any) -> str:
|
|
@@ -59,3 +60,38 @@ def cache_iterator_result() -> Callable[[AsyncIteratorCallable], AsyncIteratorCa
|
|
|
59
60
|
return wrapper
|
|
60
61
|
|
|
61
62
|
return decorator
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def cache_coroutine_result() -> Callable[[AsyncCallable], AsyncCallable]:
|
|
66
|
+
"""Coroutine version of `cache_iterator_result` from port_ocean.utils.cache
|
|
67
|
+
|
|
68
|
+
Decorator that caches the result of a coroutine function.
|
|
69
|
+
It checks if the result is already in the cache, and if not,
|
|
70
|
+
fetches the result, caches it, and returns the cached value.
|
|
71
|
+
|
|
72
|
+
The cache is stored in the scope of the running event and is
|
|
73
|
+
removed when the event is finished.
|
|
74
|
+
|
|
75
|
+
Usage:
|
|
76
|
+
```python
|
|
77
|
+
@cache_coroutine_result()
|
|
78
|
+
async def my_coroutine_function():
|
|
79
|
+
# Your code here
|
|
80
|
+
```
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
def decorator(func: AsyncCallable) -> AsyncCallable:
|
|
84
|
+
@functools.wraps(func)
|
|
85
|
+
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
86
|
+
cache_key = hash_func(func.__name__, *args, **kwargs)
|
|
87
|
+
|
|
88
|
+
if cache := event.attributes.get(cache_key):
|
|
89
|
+
return cache
|
|
90
|
+
|
|
91
|
+
result = await func(*args, **kwargs)
|
|
92
|
+
event.attributes[cache_key] = result
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
return wrapper
|
|
96
|
+
|
|
97
|
+
return decorator
|
|
@@ -133,18 +133,19 @@ port_ocean/tests/helpers/smoke_test.py,sha256=_9aJJFRfuGJEg2D2YQJVJRmpreS6gEPHHQ
|
|
|
133
133
|
port_ocean/tests/log/test_handlers.py,sha256=bTOGnuj8fMIEXepwYblRvcg0FKqApCdyCBtAQZ2BlXM,2115
|
|
134
134
|
port_ocean/tests/test_smoke.py,sha256=uix2uIg_yOm8BHDgHw2hTFPy1fiIyxBGW3ENU_KoFlo,2557
|
|
135
135
|
port_ocean/tests/utils/test_async_iterators.py,sha256=3PLk1emEXekb8LcC5GgVh3OicaX15i5WyaJT_eFnu_4,1336
|
|
136
|
+
port_ocean/tests/utils/test_cache.py,sha256=GzoS8xGCBDbBcPwSDbdimsMMkRvJATrBC7UmFhdW3fw,4906
|
|
136
137
|
port_ocean/utils/__init__.py,sha256=KMGnCPXZJbNwtgxtyMycapkDz8tpSyw23MSYT3iVeHs,91
|
|
137
138
|
port_ocean/utils/async_http.py,sha256=arnH458TExn2Dju_Sy6pHas_vF5RMWnOp-jBz5WAAcE,1226
|
|
138
139
|
port_ocean/utils/async_iterators.py,sha256=CPXskYWkhkZtAG-ducEwM8537t3z5usPEqXR9vcivzw,3715
|
|
139
|
-
port_ocean/utils/cache.py,sha256=
|
|
140
|
+
port_ocean/utils/cache.py,sha256=RgfN4SjjHrEkbqUChyboeD1mrXomolUUjsJtvbkmr3U,3353
|
|
140
141
|
port_ocean/utils/misc.py,sha256=0q2cJ5psqxn_5u_56pT7vOVQ3shDM02iC1lzyWQ_zl0,2098
|
|
141
142
|
port_ocean/utils/queue_utils.py,sha256=KWWl8YVnG-glcfIHhM6nefY-2sou_C6DVP1VynQwzB4,2762
|
|
142
143
|
port_ocean/utils/repeat.py,sha256=0EFWM9d8lLXAhZmAyczY20LAnijw6UbIECf5lpGbOas,3231
|
|
143
144
|
port_ocean/utils/signal.py,sha256=K-6kKFQTltcmKDhtyZAcn0IMa3sUpOHGOAUdWKgx0_E,1369
|
|
144
145
|
port_ocean/utils/time.py,sha256=pufAOH5ZQI7gXvOvJoQXZXZJV-Dqktoj9Qp9eiRwmJ4,1939
|
|
145
146
|
port_ocean/version.py,sha256=UsuJdvdQlazzKGD3Hd5-U7N69STh8Dq9ggJzQFnu9fU,177
|
|
146
|
-
port_ocean-0.14.
|
|
147
|
-
port_ocean-0.14.
|
|
148
|
-
port_ocean-0.14.
|
|
149
|
-
port_ocean-0.14.
|
|
150
|
-
port_ocean-0.14.
|
|
147
|
+
port_ocean-0.14.1.dist-info/LICENSE.md,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
|
148
|
+
port_ocean-0.14.1.dist-info/METADATA,sha256=wOX1EZAWACc4Y91LXEhR497CEE8Vtk8UPsHZDStMaME,6673
|
|
149
|
+
port_ocean-0.14.1.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
|
150
|
+
port_ocean-0.14.1.dist-info/entry_points.txt,sha256=F_DNUmGZU2Kme-8NsWM5LLE8piGMafYZygRYhOVtcjA,54
|
|
151
|
+
port_ocean-0.14.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|