Spaces:
Running
Running
Download test/services/test_concurrent_task_delete.py from kodnuke/youtube-bot: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/kodnuke/youtube-bot/resolve/main/test/services/test_concurrent_task_delete.py
- Command line
-
hf download hf://spaces/kodnuke/youtube-bot/test/services/test_concurrent_task_delete.py
-
curl -L -o test_concurrent_task_delete.py https://huggingface.co/spaces/kodnuke/youtube-bot/resolve/main/test/services/test_concurrent_task_delete.py
3.1 kB
| import shutil | |
| from concurrent.futures import ThreadPoolExecutor | |
| from pathlib import Path | |
| from tempfile import TemporaryDirectory | |
| from threading import Barrier, Lock | |
| from unittest.mock import patch | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from app import asgi | |
| from app.config import config | |
| from app.controllers.v1 import video as controller | |
| from app.models import const | |
| from app.services.state import MemoryState | |
| def test_concurrent_task_deletions_do_not_return_server_error(): | |
| state = MemoryState() | |
| state.update_task('task-1', state=const.TASK_STATE_COMPLETE) | |
| with TemporaryDirectory() as directory: | |
| target = Path(directory, 'task-1') | |
| target.mkdir() | |
| (target / 'final.mp4').write_bytes(b'fixture') | |
| barrier = Barrier(2) | |
| remove_lock = Lock() | |
| exists = controller.os.path.exists | |
| remove = shutil.rmtree | |
| def synchronized_exists(path): | |
| result = exists(path) | |
| if Path(path) == target: | |
| barrier.wait(timeout=5) | |
| return result | |
| def sequential_removal(path, *args, **kwargs): | |
| # Both requests passed exists(); serialize the actual removals so | |
| # the second deterministically sees the missing task directory. | |
| with remove_lock: | |
| return remove(path, *args, **kwargs) | |
| with ( | |
| patch.dict(config.app, {'api_key': ''}), | |
| patch.object(controller.sm, 'state', state), | |
| patch.object(controller.utils, 'task_dir', return_value=directory), | |
| patch.object(controller.os.path, 'exists', side_effect=synchronized_exists), | |
| patch.object(controller.shutil, 'rmtree', side_effect=sequential_removal), | |
| ThreadPoolExecutor(max_workers=2) as executor, | |
| ): | |
| client = TestClient(asgi.get_application(), raise_server_exceptions=False) | |
| responses = list(executor.map(lambda _: client.delete('/api/v1/tasks/task-1'), range(2))) | |
| assert sorted(response.status_code for response in responses) == [200, 200] | |
| assert not target.exists() | |
| assert state.get_task('task-1') is None | |
| def test_delete_preserves_state_when_directory_still_exists(error): | |
| state = MemoryState() | |
| state.update_task('task-1', state=const.TASK_STATE_COMPLETE) | |
| with TemporaryDirectory() as directory: | |
| target = Path(directory, 'task-1') | |
| target.mkdir() | |
| with ( | |
| patch.dict(config.app, {'api_key': ''}), | |
| patch.object(controller.sm, 'state', state), | |
| patch.object(controller.utils, 'task_dir', return_value=directory), | |
| patch.object(controller.shutil, 'rmtree', side_effect=error), | |
| ): | |
| response = TestClient(asgi.get_application(), raise_server_exceptions=False).delete('/api/v1/tasks/task-1') | |
| assert response.status_code == 500 | |
| assert target.is_dir() | |
| assert state.get_task('task-1') is not None | |