|
4 | 4 | import time |
5 | 5 |
|
6 | 6 | from tasktiger import Worker, periodic |
7 | | - |
8 | | -from .tasks_periodic import tiger |
| 7 | +from tasktiger import ( |
| 8 | + Task, |
| 9 | + gen_unique_id, |
| 10 | + serialize_func_name, |
| 11 | + QUEUED, |
| 12 | + SCHEDULED, |
| 13 | +) |
| 14 | + |
| 15 | +from .tasks_periodic import tiger, periodic_task |
9 | 16 | from .test_base import BaseTestCase |
10 | 17 |
|
11 | 18 |
|
@@ -107,3 +114,117 @@ def ensure_run(n): |
107 | 114 | time.sleep(1) |
108 | 115 |
|
109 | 116 | ensure_run(2) |
| 117 | + |
| 118 | + def test_periodic_execution_unique_ids(self): |
| 119 | + """ |
| 120 | + Test that periodic tasks generate the same unique ids |
| 121 | +
|
| 122 | + When a periodic task is scheduled initially as part of worker startup |
| 123 | + vs re-scheduled from within python the unique id generated should be |
| 124 | + the same. If they aren't it could result in duplicate tasks. |
| 125 | + """ |
| 126 | + # Sleep until the next second |
| 127 | + now = datetime.datetime.utcnow() |
| 128 | + time.sleep(1 - now.microsecond / 10.0 ** 6) |
| 129 | + |
| 130 | + # After the first worker run, the periodic task will be queued. |
| 131 | + # Note that since periodic tasks register with the Tiger instance, it |
| 132 | + # must be the same instance that was used to decorate the task. We |
| 133 | + # therefore use `tiger` from the tasks module instead of `self.tiger`. |
| 134 | + self._ensure_queues() |
| 135 | + Worker(tiger).run(once=True) |
| 136 | + self._ensure_queues(scheduled={'periodic': 1}) |
| 137 | + time.sleep(1) |
| 138 | + Worker(tiger).run(once=True) |
| 139 | + self._ensure_queues(queued={'periodic': 1}) |
| 140 | + |
| 141 | + # generate the expected unique id |
| 142 | + expected_unique_id = gen_unique_id( |
| 143 | + serialize_func_name(periodic_task), [], {} |
| 144 | + ) |
| 145 | + |
| 146 | + # pull task out of the queue by id. If found, then the id is correct |
| 147 | + task = Task.from_id(tiger, 'periodic', QUEUED, expected_unique_id) |
| 148 | + assert task is not None |
| 149 | + |
| 150 | + # execute and reschedule the task |
| 151 | + self._ensure_queues(queued={'periodic': 1}) |
| 152 | + Worker(tiger).run(once=True) |
| 153 | + self._ensure_queues(scheduled={'periodic': 1}) |
| 154 | + |
| 155 | + # wait for the task to need to be queued |
| 156 | + time.sleep(1) |
| 157 | + Worker(tiger).run(once=True) |
| 158 | + self._ensure_queues(queued={'periodic': 1}) |
| 159 | + |
| 160 | + # The unique id shouldn't change between executions. Try finding the |
| 161 | + # task by id again |
| 162 | + task = Task.from_id(tiger, 'periodic', QUEUED, expected_unique_id) |
| 163 | + assert task is not None |
| 164 | + |
| 165 | + def test_periodic_execution_unique_ids_manual_scheduling(self): |
| 166 | + """ |
| 167 | + Periodic tasks should have the same unique ids when manually scheduled |
| 168 | +
|
| 169 | + When a periodic task is scheduled initially as part of worker startup |
| 170 | + vs ``.delay``'d manually, the unique id generated should be the same. |
| 171 | + If they aren't it could result in duplicate tasks. |
| 172 | + """ |
| 173 | + # Sleep until the next second |
| 174 | + now = datetime.datetime.utcnow() |
| 175 | + time.sleep(1 - now.microsecond / 10.0 ** 6) |
| 176 | + |
| 177 | + # After the first worker run, the periodic task will be queued. |
| 178 | + # Note that since periodic tasks register with the Tiger instance, it |
| 179 | + # must be the same instance that was used to decorate the task. We |
| 180 | + # therefore use `tiger` from the tasks module instead of `self.tiger`. |
| 181 | + self._ensure_queues() |
| 182 | + Worker(tiger).run(once=True) |
| 183 | + self._ensure_queues(scheduled={'periodic': 1}) |
| 184 | + time.sleep(1) |
| 185 | + Worker(tiger).run(once=True) |
| 186 | + self._ensure_queues(queued={'periodic': 1}) |
| 187 | + |
| 188 | + # schedule the task manually |
| 189 | + periodic_task.delay() |
| 190 | + |
| 191 | + # make sure a duplicate wasn't scheduled |
| 192 | + self._ensure_queues(queued={'periodic': 1}) |
| 193 | + |
| 194 | + def test_periodic_execution_unique_ids_self_correct(self): |
| 195 | + """ |
| 196 | + Test that periodic tasks will self-correct unique ids |
| 197 | + """ |
| 198 | + # Sleep until the next second |
| 199 | + now = datetime.datetime.utcnow() |
| 200 | + time.sleep(1 - now.microsecond / 10.0 ** 6) |
| 201 | + |
| 202 | + # generate the ids |
| 203 | + correct_unique_id = gen_unique_id( |
| 204 | + serialize_func_name(periodic_task), [], {} |
| 205 | + ) |
| 206 | + malformed_unique_id = gen_unique_id( |
| 207 | + serialize_func_name(periodic_task), None, None |
| 208 | + ) |
| 209 | + |
| 210 | + task = Task(tiger, func=periodic_task) |
| 211 | + |
| 212 | + # patch the id to something slightly wrong |
| 213 | + assert task.id == correct_unique_id |
| 214 | + task._data['id'] = malformed_unique_id |
| 215 | + assert task.id == malformed_unique_id |
| 216 | + |
| 217 | + # schedule the task |
| 218 | + task.delay() |
| 219 | + self._ensure_queues(queued={'periodic': 1}) |
| 220 | + |
| 221 | + # pull task out of the queue by the malformed id |
| 222 | + task = Task.from_id(tiger, 'periodic', QUEUED, malformed_unique_id) |
| 223 | + assert task is not None |
| 224 | + |
| 225 | + Worker(tiger).run(once=True) |
| 226 | + self._ensure_queues(scheduled={'periodic': 1}) |
| 227 | + |
| 228 | + # pull task out of the queue by the self-corrected id |
| 229 | + task = Task.from_id(tiger, 'periodic', SCHEDULED, correct_unique_id) |
| 230 | + assert task is not None |
0 commit comments