Skip to content

Commit 82b4701

Browse files
author
Andrei Neagu
committed
revert changes
1 parent d4d5fd9 commit 82b4701

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

services/dynamic-sidecar/tests/unit/test_modules_outputs_event_filter.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# pylint:disable=redefined-outer-name
22
# pylint:disable=unused-argument
33

4-
from collections.abc import AsyncIterator
4+
import asyncio
55
from pathlib import Path
6-
from typing import Any, Final
6+
from typing import AsyncIterator, Iterator
77
from unittest.mock import AsyncMock
88

99
import pytest
@@ -24,12 +24,12 @@
2424
from tenacity.stop import stop_after_delay
2525
from tenacity.wait import wait_fixed
2626

27-
_TENACITY_RETRY_PARAMS: Final[dict[str, Any]] = {
28-
"reraise": True,
29-
"retry": retry_if_exception_type(AssertionError),
30-
"stop": stop_after_delay(10),
31-
"wait": wait_fixed(0.01),
32-
}
27+
_TENACITY_RETRY_PARAMS = dict(
28+
reraise=True,
29+
retry=retry_if_exception_type(AssertionError),
30+
stop=stop_after_delay(10),
31+
wait=wait_fixed(0.01),
32+
)
3333

3434
# FIXTURES
3535

@@ -75,11 +75,11 @@ async def outputs_manager(
7575
@pytest.fixture
7676
def mocked_port_key_content_changed(
7777
mocker: MockerFixture, outputs_manager: OutputsManager
78-
) -> AsyncMock:
78+
) -> Iterator[AsyncMock]:
7979
async def _mock_upload_outputs(*args, **kwargs) -> None:
8080
pass
8181

82-
return mocker.patch.object(
82+
yield mocker.patch.object(
8383
outputs_manager, "port_key_content_changed", side_effect=_mock_upload_outputs
8484
)
8585

@@ -101,8 +101,8 @@ def get_wait_interval(self, dir_size: NonNegativeInt) -> NonNegativeFloat:
101101

102102

103103
@pytest.fixture
104-
def mock_get_directory_total_size(mocker: MockerFixture) -> AsyncMock:
105-
return mocker.patch(
104+
def mock_get_directory_total_size(mocker: MockerFixture) -> Iterator[AsyncMock]:
105+
yield mocker.patch(
106106
"simcore_service_dynamic_sidecar.modules.outputs._event_filter.get_directory_total_size",
107107
return_value=1,
108108
)
@@ -120,6 +120,17 @@ async def event_filter(
120120
await event_filter.shutdown()
121121

122122

123+
# UTILS
124+
125+
126+
async def _wait_for_event_to_trigger(event_filter: EventFilter) -> None:
127+
await asyncio.sleep(event_filter.delay_policy.get_min_interval() * 5)
128+
129+
130+
async def _wait_for_event_to_trigger_big_directory(event_filter: EventFilter) -> None:
131+
await asyncio.sleep(event_filter.delay_policy.get_wait_interval(1) * 2)
132+
133+
123134
# TESTS
124135

125136

@@ -130,16 +141,13 @@ async def test_event_triggers_once(
130141
):
131142
# event triggers once
132143
await event_filter.enqueue(port_key_1)
133-
134-
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
135-
with attempt:
136-
assert mocked_port_key_content_changed.call_count == 1
144+
await _wait_for_event_to_trigger(event_filter)
145+
assert mocked_port_key_content_changed.call_count == 1
137146

138147
# event triggers a second time
139148
await event_filter.enqueue(port_key_1)
140-
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
141-
with attempt:
142-
assert mocked_port_key_content_changed.call_count == 2
149+
await _wait_for_event_to_trigger(event_filter)
150+
assert mocked_port_key_content_changed.call_count == 2
143151

144152

145153
async def test_trigger_once_after_event_chain(
@@ -149,9 +157,8 @@ async def test_trigger_once_after_event_chain(
149157
):
150158
for _ in range(100):
151159
await event_filter.enqueue(port_key_1)
152-
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
153-
with attempt:
154-
assert mocked_port_key_content_changed.call_count == 1
160+
await _wait_for_event_to_trigger(event_filter)
161+
assert mocked_port_key_content_changed.call_count == 1
155162

156163

157164
async def test_always_trigger_after_delay(
@@ -163,9 +170,8 @@ async def test_always_trigger_after_delay(
163170
# event trigger after correct interval delay correctly
164171
for expected_call_count in range(1, 10):
165172
await event_filter.enqueue(port_key_1)
166-
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
167-
with attempt:
168-
assert mocked_port_key_content_changed.call_count == expected_call_count
173+
await _wait_for_event_to_trigger_big_directory(event_filter)
174+
assert mocked_port_key_content_changed.call_count == expected_call_count
169175

170176

171177
async def test_minimum_amount_of_get_directory_total_size_calls(
@@ -177,12 +183,14 @@ async def test_minimum_amount_of_get_directory_total_size_calls(
177183
await event_filter.enqueue(port_key_1)
178184
# wait a bit for the vent to be picked up
179185
# by the workers and processed
186+
await _wait_for_event_to_trigger(event_filter)
180187
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
181188
with attempt:
182189
assert mock_get_directory_total_size.call_count == 1
183190
assert mocked_port_key_content_changed.call_count == 0
184191

185192
# event finished processing and was dispatched
193+
await _wait_for_event_to_trigger_big_directory(event_filter)
186194
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
187195
with attempt:
188196
assert mock_get_directory_total_size.call_count == 2
@@ -198,27 +206,24 @@ async def test_minimum_amount_of_get_directory_total_size_calls_with_continuous_
198206
await event_filter.enqueue(port_key_1)
199207
# wait a bit for the vent to be picked up
200208
# by the workers and processed
201-
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
202-
with attempt:
203-
assert mock_get_directory_total_size.call_count == 1
204-
assert mocked_port_key_content_changed.call_count == 0
209+
await _wait_for_event_to_trigger(event_filter)
210+
assert mock_get_directory_total_size.call_count == 1
211+
assert mocked_port_key_content_changed.call_count == 0
205212

206213
# while changes keep piling up, keep extending the duration
207214
# no event will trigger
208215
# size of directory will not be computed
209216
VERY_LONG_EVENT_CHAIN = 1000
210217
for _ in range(VERY_LONG_EVENT_CHAIN):
211218
await event_filter.enqueue(port_key_1)
212-
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
213-
with attempt:
214-
assert mock_get_directory_total_size.call_count == 1
215-
assert mocked_port_key_content_changed.call_count == 0
219+
await _wait_for_event_to_trigger(event_filter)
220+
assert mock_get_directory_total_size.call_count == 1
221+
assert mocked_port_key_content_changed.call_count == 0
216222

217223
# event finished processing and was dispatched
218-
async for attempt in AsyncRetrying(**_TENACITY_RETRY_PARAMS):
219-
with attempt:
220-
assert mock_get_directory_total_size.call_count == 2
221-
assert mocked_port_key_content_changed.call_count == 1
224+
await _wait_for_event_to_trigger_big_directory(event_filter)
225+
assert mock_get_directory_total_size.call_count == 2
226+
assert mocked_port_key_content_changed.call_count == 1
222227

223228

224229
def test_default_delay_policy():

0 commit comments

Comments
 (0)