|
| 1 | +from contextlib import contextmanager |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from django.test import TransactionTestCase |
| 5 | +from django.contrib.auth.models import User |
| 6 | + |
| 7 | +from apps.ifc_validation_models.models import ( |
| 8 | + ValidationRequest, |
| 9 | + ValidationTask, |
| 10 | + set_user_context, |
| 11 | +) |
| 12 | + |
| 13 | +from ..tasks.configs import task_registry |
| 14 | +from ..tasks import schema_validation_subtask |
| 15 | +import apps.ifc_validation.tasks.task_runner as task_runner |
| 16 | + |
| 17 | + |
| 18 | +@contextmanager |
| 19 | +def _noop_lock(*args, **kwargs): |
| 20 | + # Replaces the redis-backed user task lock so these tests stay hermetic. |
| 21 | + yield None |
| 22 | + |
| 23 | + |
| 24 | +@mock.patch.object(task_runner, "acquire_user_lock", _noop_lock) |
| 25 | +class ProgressUpdateTaskTestCase(TransactionTestCase): |
| 26 | + """Progress must advance only after a subtask's work finishes, so a request |
| 27 | + never reports 100% while a task is still running.""" |
| 28 | + |
| 29 | + TASK_TYPE = ValidationTask.Type.SCHEMA # increment = 10 |
| 30 | + |
| 31 | + def setUp(self): |
| 32 | + user, _ = User.objects.get_or_create( |
| 33 | + id=1, defaults={"username": "SYSTEM", "is_active": True} |
| 34 | + ) |
| 35 | + set_user_context(user) |
| 36 | + self.config = task_registry[self.TASK_TYPE] |
| 37 | + self.increment = self.config.increment |
| 38 | + |
| 39 | + def _make_request(self, progress=0): |
| 40 | + request = ValidationRequest.objects.create( |
| 41 | + file_name="valid_file.ifc", file="valid_file.ifc", size=280 |
| 42 | + ) |
| 43 | + request.mark_as_initiated() # progress -> 0 |
| 44 | + if progress: |
| 45 | + ValidationRequest.objects.filter(pk=request.id).update(progress=progress) |
| 46 | + return request |
| 47 | + |
| 48 | + def _run(self, request): |
| 49 | + schema_validation_subtask( |
| 50 | + prev_result={"is_valid": True, "reason": "test"}, |
| 51 | + id=request.id, |
| 52 | + file_name=request.file_name, |
| 53 | + ) |
| 54 | + |
| 55 | + def test_progress_advances_only_after_task_work_completes(self): |
| 56 | + request = self._make_request() |
| 57 | + seen = {} |
| 58 | + |
| 59 | + def fake_check(context): |
| 60 | + seen["during_check"] = ValidationRequest.objects.get(pk=request.id).progress |
| 61 | + return context |
| 62 | + |
| 63 | + def fake_process(context): |
| 64 | + seen["during_process"] = ValidationRequest.objects.get(pk=request.id).progress |
| 65 | + return "ok" |
| 66 | + |
| 67 | + with mock.patch.object(self.config, "check_program", side_effect=fake_check), \ |
| 68 | + mock.patch.object(self.config, "process_results", side_effect=fake_process): |
| 69 | + self._run(request) |
| 70 | + |
| 71 | + # During the task body, progress must not have advanced (the bug: it was |
| 72 | + # bumped before the work ran). |
| 73 | + self.assertEqual(seen["during_check"], 0) |
| 74 | + self.assertEqual(seen["during_process"], 0) |
| 75 | + |
| 76 | + # It advances by the increment only after the task finishes. |
| 77 | + request.refresh_from_db() |
| 78 | + self.assertEqual(request.progress, self.increment) |
| 79 | + |
| 80 | + def test_progress_is_clamped_to_100(self): |
| 81 | + request = self._make_request(progress=95) # 95 + 10 would overflow to 105 |
| 82 | + |
| 83 | + with mock.patch.object(self.config, "check_program", return_value=None), \ |
| 84 | + mock.patch.object(self.config, "process_results", return_value="ok"): |
| 85 | + self._run(request) |
| 86 | + |
| 87 | + request.refresh_from_db() |
| 88 | + self.assertEqual(request.progress, 100) |
| 89 | + |
| 90 | + def test_failed_task_does_not_advance_progress(self): |
| 91 | + request = self._make_request() |
| 92 | + |
| 93 | + with mock.patch.object(self.config, "check_program", side_effect=RuntimeError("boom")): |
| 94 | + self._run(request) |
| 95 | + |
| 96 | + request.refresh_from_db() |
| 97 | + self.assertEqual(request.progress, 0) |
| 98 | + |
| 99 | + task = ValidationTask.objects.filter( |
| 100 | + request_id=request.id, type=self.TASK_TYPE |
| 101 | + ).first() |
| 102 | + self.assertEqual(task.status, ValidationTask.Status.FAILED) |
0 commit comments