|
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,62 @@ 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 | + (FileCreatedEvent(src_path=f"{_STATE_PATH}/untitled.txt", dest_path=""), None), |
| 164 | + (DirModifiedEvent(src_path=f"{_STATE_PATH}", dest_path=""), None), |
| 165 | + (FileClosedEvent(src_path=f"{_STATE_PATH}/untitled.txt", dest_path=""), None), |
| 166 | + ( |
| 167 | + FileMovedEvent( |
| 168 | + src_path=f"{_STATE_PATH}/untitled.txt", |
| 169 | + dest_path=f"{_STATE_PATH}/asdsadsasad.txt", |
| 170 | + ), |
| 171 | + None, |
| 172 | + ), |
| 173 | + ( |
| 174 | + FileMovedEvent( |
| 175 | + src_path=f"{_STATE_PATH}/asdsadsasad.txt", |
| 176 | + dest_path=f"{_STATE_PATH}/output_1/asdsadsasad.txt", |
| 177 | + ), |
| 178 | + "output_1", |
| 179 | + ), |
| 180 | + (DirModifiedEvent(src_path=f"{_STATE_PATH}/output_1", dest_path=""), None), |
| 181 | + ], |
| 182 | +) |
| 183 | +def test_port_keys_event_handler_triggers_for_events( |
| 184 | + mock_state_path: Path, event: FileSystemEvent, expected_port_key: str | None |
| 185 | +) -> None: |
| 186 | + |
| 187 | + queue = _MockAioQueue() |
| 188 | + |
| 189 | + event_handler = _PortKeysEventHandler(mock_state_path, queue) |
| 190 | + event_handler.handle_set_outputs_port_keys(outputs_port_keys={"output_1"}) |
| 191 | + event_handler.handle_toggle_event_propagation(is_enabled=True) |
| 192 | + |
| 193 | + event_handler.event_handler(event) |
| 194 | + assert queue.get() == expected_port_key |
0 commit comments