|
2 | 2 | # pylint: disable=protected-access |
3 | 3 |
|
4 | 4 | import asyncio |
| 5 | +from collections.abc import AsyncIterable |
5 | 6 | from pathlib import Path |
6 | | -from typing import AsyncIterable |
| 7 | +from typing import Any, Final |
7 | 8 | from unittest.mock import Mock |
8 | 9 |
|
9 | 10 | import aioprocessing |
|
17 | 18 | from simcore_service_dynamic_sidecar.modules.outputs._event_handler import ( |
18 | 19 | EventHandlerObserver, |
19 | 20 | _EventHandlerProcess, |
| 21 | + _PortKeysEventHandler, |
20 | 22 | ) |
21 | 23 | from simcore_service_dynamic_sidecar.modules.outputs._manager import OutputsManager |
| 24 | +from watchdog.events import ( |
| 25 | + DirModifiedEvent, |
| 26 | + FileClosedEvent, |
| 27 | + FileCreatedEvent, |
| 28 | + FileMovedEvent, |
| 29 | + FileSystemEvent, |
| 30 | +) |
22 | 31 |
|
23 | 32 |
|
24 | 33 | @pytest.fixture |
@@ -124,3 +133,111 @@ async def test_event_handler_observer_health_degraded( |
124 | 133 | await asyncio.sleep(observer_monitor.wait_for_heart_beat_interval_s * 3) |
125 | 134 | await observer_monitor.stop() |
126 | 135 | assert outputs_manager.set_all_ports_for_upload.call_count >= 1 |
| 136 | + |
| 137 | + |
| 138 | +_STATE_PATH: Final[Path] = Path("/some/random/fake/path/for/state/") |
| 139 | + |
| 140 | + |
| 141 | +@pytest.fixture |
| 142 | +def mock_state_path() -> Path: |
| 143 | + return _STATE_PATH |
| 144 | + |
| 145 | + |
| 146 | +class _MockAioQueue: |
| 147 | + def __init__(self) -> None: |
| 148 | + self.items: list[Any] = [] |
| 149 | + |
| 150 | + def put(self, item: Any) -> None: |
| 151 | + self.items.append(item) |
| 152 | + |
| 153 | + def get(self) -> Any | None: |
| 154 | + try: |
| 155 | + return self.items.pop() |
| 156 | + except IndexError: |
| 157 | + return None |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.parametrize( |
| 161 | + "event, expected_port_key", |
| 162 | + [ |
| 163 | + pytest.param( |
| 164 | + FileCreatedEvent(src_path=f"{_STATE_PATH}/untitled.txt", dest_path=""), |
| 165 | + None, |
| 166 | + id="file_create_outside", |
| 167 | + ), |
| 168 | + pytest.param( |
| 169 | + FileCreatedEvent( |
| 170 | + src_path=f"{_STATE_PATH}/output_1/untitled1.txt", |
| 171 | + dest_path="", |
| 172 | + ), |
| 173 | + "output_1", |
| 174 | + id="file_create_inside_monitored_port", |
| 175 | + ), |
| 176 | + pytest.param( |
| 177 | + FileCreatedEvent( |
| 178 | + src_path=f"{_STATE_PATH}/output_9/untitled1.txt", |
| 179 | + dest_path="", |
| 180 | + ), |
| 181 | + None, |
| 182 | + id="file_create_inside_not_monitored_port", |
| 183 | + ), |
| 184 | + pytest.param( |
| 185 | + FileMovedEvent( |
| 186 | + src_path=f"{_STATE_PATH}/untitled.txt", |
| 187 | + dest_path=f"{_STATE_PATH}/asdsadsasad.txt", |
| 188 | + ), |
| 189 | + None, |
| 190 | + id="move_outside_any_port", |
| 191 | + ), |
| 192 | + pytest.param( |
| 193 | + FileMovedEvent( |
| 194 | + src_path=f"{_STATE_PATH}/asdsadsasad.txt", |
| 195 | + dest_path=f"{_STATE_PATH}/output_1/asdsadsasad.txt", |
| 196 | + ), |
| 197 | + "output_1", |
| 198 | + id="move_to_monitored_port", |
| 199 | + ), |
| 200 | + pytest.param( |
| 201 | + FileMovedEvent( |
| 202 | + src_path=f"{_STATE_PATH}/asdsadsasad.txt", |
| 203 | + dest_path=f"{_STATE_PATH}/output_9/asdsadsasad.txt", |
| 204 | + ), |
| 205 | + None, |
| 206 | + id="move_outside_monitored_port", |
| 207 | + ), |
| 208 | + pytest.param( |
| 209 | + DirModifiedEvent(src_path=f"{_STATE_PATH}/output_1", dest_path=""), |
| 210 | + None, |
| 211 | + id="modified_port_dir_does_nothing", |
| 212 | + ), |
| 213 | + pytest.param( |
| 214 | + DirModifiedEvent(src_path=f"{_STATE_PATH}", dest_path=""), |
| 215 | + None, |
| 216 | + id="modified_outer_dir_does_nothing", |
| 217 | + ), |
| 218 | + pytest.param( |
| 219 | + FileClosedEvent(src_path=f"{_STATE_PATH}/untitled.txt", dest_path=""), |
| 220 | + None, |
| 221 | + id="close_file_outside_does_nothing", |
| 222 | + ), |
| 223 | + pytest.param( |
| 224 | + FileClosedEvent( |
| 225 | + src_path=f"{_STATE_PATH}/output_1/asdsadsasad.txt", dest_path="" |
| 226 | + ), |
| 227 | + "output_1", |
| 228 | + id="close_file_inside_triggers_event", |
| 229 | + ), |
| 230 | + ], |
| 231 | +) |
| 232 | +def test_port_keys_event_handler_triggers_for_events( |
| 233 | + mock_state_path: Path, event: FileSystemEvent, expected_port_key: str | None |
| 234 | +) -> None: |
| 235 | + |
| 236 | + queue = _MockAioQueue() |
| 237 | + |
| 238 | + event_handler = _PortKeysEventHandler(mock_state_path, queue) |
| 239 | + event_handler.handle_set_outputs_port_keys(outputs_port_keys={"output_1"}) |
| 240 | + event_handler.handle_toggle_event_propagation(is_enabled=True) |
| 241 | + |
| 242 | + event_handler.event_handler(event) |
| 243 | + assert queue.get() == expected_port_key |
0 commit comments