Commit 0c08d44f authored by Philippe Pepiot's avatar Philippe Pepiot
Browse files

Fix running tasks out of scope of the current test

Suppose you have a test that create a task but not run it with run_all_tasks(),
then in a second test run_all_tasks() was running the task of the previous
test...
Fix this by ensuring the task_id (an unique hash generated by celery) is
reachable within the current test database.
parent 31615252fcfa
...@@ -49,7 +49,14 @@ def run_all_tasks(cnx): ...@@ -49,7 +49,14 @@ def run_all_tasks(cnx):
# Tasks can create other tasks, so run them until there is no one left. # Tasks can create other tasks, so run them until there is no one left.
while _TEST_TASKS: while _TEST_TASKS:
task_eid = list(_TEST_TASKS)[0] task_eid = list(_TEST_TASKS)[0]
results[task_eid] = _TEST_TASKS.pop(task_eid).delay() task = _TEST_TASKS.pop(task_eid)
# Ensure current task id is in the scope of the current test
if task.id is not None and not cnx.execute(
'Any X WHERE X is CeleryTask, X task_id %(task_id)s',
{'task_id': task.id}
):
continue
results[task_eid] = task.delay()
if celery.current_app.conf.CELERY_ALWAYS_EAGER: if celery.current_app.conf.CELERY_ALWAYS_EAGER:
for task_eid, result in results.items(): for task_eid, result in results.items():
......
...@@ -89,8 +89,11 @@ class CeleryTaskTC(BaseCeleryTaskTC): ...@@ -89,8 +89,11 @@ class CeleryTaskTC(BaseCeleryTaskTC):
# Task must run even if there is no data on current transaction # Task must run even if there is no data on current transaction
task = celery.signature("success", kwargs={"n": 10}) task = celery.signature("success", kwargs={"n": 10})
task_id = task.freeze().task_id task_id = task.freeze().task_id
cnx.transaction_data['celerytask'] = {42: task} cwtask = cnx.create_entity('CeleryTask', task_name=u'dummy',
StartCeleryTaskOp.get_instance(cnx).add_data(42) task_id=six.text_type(task_id))
cnx.commit()
cnx.transaction_data['celerytask'] = {cwtask.eid: task}
StartCeleryTaskOp.get_instance(cnx).add_data(cwtask.eid)
cnx.commit() cnx.commit()
run_all_tasks(cnx) run_all_tasks(cnx)
result = self.wait_async_task(cnx, task_id) result = self.wait_async_task(cnx, task_id)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment