From f951507ec65c10b4a7779f593f8c80392184648c Mon Sep 17 00:00:00 2001 From: Gagan Trivedi Date: Tue, 24 Oct 2023 12:41:12 +0530 Subject: [PATCH] add tests --- api/task_processor/decorators.py | 4 +- .../test_unit_task_processor_decorators.py | 42 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/api/task_processor/decorators.py b/api/task_processor/decorators.py index 067e090c166e..2f7e5482415d 100644 --- a/api/task_processor/decorators.py +++ b/api/task_processor/decorators.py @@ -57,6 +57,8 @@ def delay( task = Task.create( task_identifier=task_identifier, scheduled_for=delay_until or timezone.now(), + priority=priority, + queue_size=queue_size, args=args, kwargs=kwargs, ) @@ -64,7 +66,7 @@ def delay( logger.warning(e) return - task.save(priority=priority) + task.save() return task def run_in_thread(*, args: typing.Tuple = (), kwargs: typing.Dict = None): diff --git a/api/tests/unit/task_processor/test_unit_task_processor_decorators.py b/api/tests/unit/task_processor/test_unit_task_processor_decorators.py index 8137e32cf2de..4b730ff8f176 100644 --- a/api/tests/unit/task_processor/test_unit_task_processor_decorators.py +++ b/api/tests/unit/task_processor/test_unit_task_processor_decorators.py @@ -10,7 +10,7 @@ register_task_handler, ) from task_processor.exceptions import InvalidArgumentsError -from task_processor.models import RecurringTask +from task_processor.models import RecurringTask, Task, TaskPriority from task_processor.task_registry import get_task from task_processor.task_run_method import TaskRunMethod @@ -131,3 +131,43 @@ class NonSerializableObj: # When with pytest.raises(InvalidArgumentsError): my_function.delay(args=(NonSerializableObj(),)) + + +def test_delay_returns_none_if_task_queue_is_full(settings, db): + # Given + settings.TASK_RUN_METHOD = TaskRunMethod.TASK_PROCESSOR + + @register_task_handler(queue_size=1) + def my_function(*args, **kwargs): + pass + + for _ in range(10): + Task.objects.create( + task_identifier="test_unit_task_processor_decorators.my_function" + ) + + # When + task = my_function.delay() + + # Then + assert task is None + + +def test_can_create_task_with_priority(settings, db): + # Given + settings.TASK_RUN_METHOD = TaskRunMethod.TASK_PROCESSOR + + @register_task_handler(priority=TaskPriority.HIGH) + def my_function(*args, **kwargs): + pass + + for _ in range(10): + Task.objects.create( + task_identifier="test_unit_task_processor_decorators.my_function" + ) + + # When + task = my_function.delay() + + # Then + assert task.priority == TaskPriority.HIGH