Skip to content

Commit

Permalink
🐛 fixes concurrency issue with directory-watcher (#2932)
Browse files Browse the repository at this point in the history
* fix directory-watcher issue

* using a better name

* updated spec

Co-authored-by: Andrei Neagu <neagu@itis.swiss>
  • Loading branch information
GitHK and Andrei Neagu authored Mar 31, 2022
1 parent e26f5c6 commit f070cbc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion services/dynamic-sidecar/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@
"containers"
],
"summary": "Enable/disable directory-watcher event propagation",
"operationId": "disable_directory_watcher_v1_containers_directory_watcher_patch",
"operationId": "toggle_directory_watcher_v1_containers_directory_watcher_patch",
"requestBody": {
"content": {
"application/json": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async def pull_input_ports(
response_class=Response,
status_code=status.HTTP_204_NO_CONTENT,
)
async def disable_directory_watcher(
async def toggle_directory_watcher(
patch_directory_watcher_item: PatchDirectoryWatcherItem,
app: FastAPI = Depends(get_application),
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ async def on_startup() -> None:

app.state.dir_watcher = DirectoryWatcherObservers()
app.state.dir_watcher.observe_directory(mounted_volumes.disk_outputs_path)
app.state.dir_watcher.disable_event_propagation()
app.state.dir_watcher.start()

async def on_shutdown() -> None:
Expand Down
39 changes: 22 additions & 17 deletions services/dynamic-sidecar/tests/unit/test_api_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,22 @@ def _create_network_aliases(network_name: str) -> List[str]:
return [f"alias_{i}_{network_name}" for i in range(10)]


async def _assert_enable_directory_watcher(test_client: TestClient) -> None:
response = await test_client.patch(
f"/{API_VTAG}/containers/directory-watcher", json=dict(is_enabled=True)
)
assert response.status_code == status.HTTP_204_NO_CONTENT, response.text
assert response.text == ""


async def _assert_disable_directory_watcher(test_client: TestClient) -> None:
response = await test_client.patch(
f"/{API_VTAG}/containers/directory-watcher", json=dict(is_enabled=False)
)
assert response.status_code == status.HTTP_204_NO_CONTENT, response.text
assert response.text == ""


# TESTS


Expand Down Expand Up @@ -498,20 +514,6 @@ async def test_container_pull_input_ports(
async def test_directory_watcher_disabling(
test_client: TestClient, mock_dir_watcher_on_any_event: AsyncMock
) -> None:
async def _assert_disable_directory_watcher() -> None:
response = await test_client.patch(
f"/{API_VTAG}/containers/directory-watcher", json=dict(is_enabled=False)
)
assert response.status_code == status.HTTP_204_NO_CONTENT, response.text
assert response.text == ""

async def _assert_enable_directory_watcher() -> None:
response = await test_client.patch(
f"/{API_VTAG}/containers/directory-watcher", json=dict(is_enabled=True)
)
assert response.status_code == status.HTTP_204_NO_CONTENT, response.text
assert response.text == ""

def _create_random_dir_in_inputs() -> int:
mounted_volumes: MountedVolumes = get_mounted_volumes()
dir_name = mounted_volumes.disk_outputs_path / f"{uuid4()}"
Expand All @@ -527,22 +529,23 @@ def _create_random_dir_in_inputs() -> int:

EVENTS_PER_DIR_CREATION = 2

# by default it is enabled
# by default directory-watcher it is disabled
await _assert_enable_directory_watcher(test_client)
assert mock_dir_watcher_on_any_event.call_count == 0
dir_count = _create_random_dir_in_inputs()
assert dir_count == 1
await asyncio.sleep(WAIT_FOR_DIRECTORY_WATCHER)
assert mock_dir_watcher_on_any_event.call_count == EVENTS_PER_DIR_CREATION

# disable and wait for events should have the same count as before
await _assert_disable_directory_watcher()
await _assert_disable_directory_watcher(test_client)
dir_count = _create_random_dir_in_inputs()
assert dir_count == 2
await asyncio.sleep(WAIT_FOR_DIRECTORY_WATCHER)
assert mock_dir_watcher_on_any_event.call_count == EVENTS_PER_DIR_CREATION

# enable and wait for events
await _assert_enable_directory_watcher()
await _assert_enable_directory_watcher(test_client)
dir_count = _create_random_dir_in_inputs()
assert dir_count == 3
await asyncio.sleep(WAIT_FOR_DIRECTORY_WATCHER)
Expand All @@ -554,6 +557,8 @@ async def test_container_create_outputs_dirs(
mock_outputs_labels: Dict[str, ServiceOutput],
mock_dir_watcher_on_any_event: AsyncMock,
) -> None:
# by default directory-watcher it is disabled
await _assert_enable_directory_watcher(test_client)

assert mock_dir_watcher_on_any_event.call_count == 0

Expand Down

0 comments on commit f070cbc

Please sign in to comment.