|
| 1 | +import unittest |
| 2 | +import contextlib |
| 3 | +from typing import Any, Mapping, Iterator |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import fakeredis |
| 7 | +from django_lightweight_queue import task |
| 8 | +from django_lightweight_queue.types import QueueName, WorkerNumber |
| 9 | +from django_lightweight_queue.utils import get_path, get_backend |
| 10 | +from django_lightweight_queue.backends.redis import RedisBackend |
| 11 | + |
| 12 | +from . import settings |
| 13 | + |
| 14 | +QUEUE = QueueName('dummy-queue') |
| 15 | + |
| 16 | + |
| 17 | +@task(str(QUEUE)) |
| 18 | +def dummy_task(num: int) -> None: |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +class TaskTests(unittest.TestCase): |
| 23 | + longMessage = True |
| 24 | + prefix = settings.LIGHTWEIGHT_QUEUE_REDIS_PREFIX |
| 25 | + |
| 26 | + @contextlib.contextmanager |
| 27 | + def mock_workers(self, workers: Mapping[str, int]) -> Iterator[None]: |
| 28 | + with unittest.mock.patch( |
| 29 | + 'django_lightweight_queue.utils._accepting_implied_queues', |
| 30 | + new=False, |
| 31 | + ), unittest.mock.patch.dict( |
| 32 | + 'django_lightweight_queue.app_settings.WORKERS', |
| 33 | + workers, |
| 34 | + ): |
| 35 | + yield |
| 36 | + |
| 37 | + def setUp(self) -> None: |
| 38 | + super().setUp() |
| 39 | + |
| 40 | + get_backend.cache_clear() |
| 41 | + |
| 42 | + with mock.patch('redis.StrictRedis', fakeredis.FakeStrictRedis): |
| 43 | + self.backend = RedisBackend() |
| 44 | + |
| 45 | + # Mock get_backend. Unfortunately due to the nameing of the 'task' |
| 46 | + # decorator class being the same as its containing module and it being |
| 47 | + # exposed as the symbol at django_lightweight_queue.task, we cannot mock |
| 48 | + # this in the normal way. Instead we mock get_path (which get_backend |
| 49 | + # calls) and intercept the our dummy value. |
| 50 | + def mocked_get_path(path: str) -> Any: |
| 51 | + if path == 'test-backend': |
| 52 | + return lambda: self.backend |
| 53 | + return get_path(path) |
| 54 | + |
| 55 | + patch = mock.patch( |
| 56 | + 'django_lightweight_queue.app_settings.BACKEND', |
| 57 | + new='test-backend', |
| 58 | + ) |
| 59 | + patch.start() |
| 60 | + self.addCleanup(patch.stop) |
| 61 | + patch = mock.patch( |
| 62 | + 'django_lightweight_queue.utils.get_path', |
| 63 | + side_effect=mocked_get_path, |
| 64 | + ) |
| 65 | + patch.start() |
| 66 | + self.addCleanup(patch.stop) |
| 67 | + |
| 68 | + def tearDown(self) -> None: |
| 69 | + super().tearDown() |
| 70 | + get_backend.cache_clear() |
| 71 | + |
| 72 | + def test_enqueues_job(self) -> None: |
| 73 | + self.assertEqual(0, self.backend.length(QUEUE)) |
| 74 | + |
| 75 | + dummy_task(42) |
| 76 | + |
| 77 | + job = self.backend.dequeue(QUEUE, WorkerNumber(0), 5) |
| 78 | + # Plain assert to placate mypy |
| 79 | + assert job is not None, "Failed to get a job after enqueuing one" |
| 80 | + |
| 81 | + self.assertEqual( |
| 82 | + { |
| 83 | + 'path': 'tests.test_task.dummy_task', |
| 84 | + 'args': [42], |
| 85 | + 'kwargs': {}, |
| 86 | + 'timeout': None, |
| 87 | + 'sigkill_on_stop': False, |
| 88 | + 'created_time': mock.ANY, |
| 89 | + }, |
| 90 | + job.as_dict(), |
| 91 | + ) |
| 92 | + |
| 93 | + def test_bulk_enqueues_jobs(self) -> None: |
| 94 | + self.assertEqual(0, self.backend.length(QUEUE)) |
| 95 | + |
| 96 | + with dummy_task.bulk_enqueue() as enqueue: |
| 97 | + enqueue(13) |
| 98 | + enqueue(num=42) |
| 99 | + |
| 100 | + job = self.backend.dequeue(QUEUE, WorkerNumber(0), 5) |
| 101 | + # Plain assert to placate mypy |
| 102 | + assert job is not None, "Failed to get a job after enqueuing one" |
| 103 | + |
| 104 | + self.assertEqual( |
| 105 | + { |
| 106 | + 'path': 'tests.test_task.dummy_task', |
| 107 | + 'args': [13], |
| 108 | + 'kwargs': {}, |
| 109 | + 'timeout': None, |
| 110 | + 'sigkill_on_stop': False, |
| 111 | + 'created_time': mock.ANY, |
| 112 | + }, |
| 113 | + job.as_dict(), |
| 114 | + "First job", |
| 115 | + ) |
| 116 | + |
| 117 | + job = self.backend.dequeue(QUEUE, WorkerNumber(0), 5) |
| 118 | + # Plain assert to placate mypy |
| 119 | + assert job is not None, "Failed to get a job after enqueuing one" |
| 120 | + |
| 121 | + self.assertEqual( |
| 122 | + { |
| 123 | + 'path': 'tests.test_task.dummy_task', |
| 124 | + 'args': [], |
| 125 | + 'kwargs': {'num': 42}, |
| 126 | + 'timeout': None, |
| 127 | + 'sigkill_on_stop': False, |
| 128 | + 'created_time': mock.ANY, |
| 129 | + }, |
| 130 | + job.as_dict(), |
| 131 | + "Second job", |
| 132 | + ) |
| 133 | + |
| 134 | + def test_bulk_enqueues_jobs_batch_size_boundary(self) -> None: |
| 135 | + self.assertEqual(0, self.backend.length(QUEUE), "Should initially be empty") |
| 136 | + |
| 137 | + with dummy_task.bulk_enqueue(batch_size=3) as enqueue: |
| 138 | + enqueue(1) |
| 139 | + enqueue(2) |
| 140 | + enqueue(3) |
| 141 | + enqueue(4) |
| 142 | + |
| 143 | + jobs = [ |
| 144 | + self.backend.dequeue(QUEUE, WorkerNumber(0), 5) |
| 145 | + for _ in range(4) |
| 146 | + ] |
| 147 | + |
| 148 | + self.assertEqual(0, self.backend.length(QUEUE), "Should be empty after dequeuing all jobs") |
| 149 | + |
| 150 | + args = [x.args for x in jobs if x is not None] |
| 151 | + |
| 152 | + self.assertEqual( |
| 153 | + [[1], [2], [3], [4]], |
| 154 | + args, |
| 155 | + "Wrong jobs bulk enqueued", |
| 156 | + ) |
0 commit comments