Unit testing an AsyncResult in celery

人盡茶涼 提交于 2019-12-03 02:29:15

When CELERY_ALWAYS_EAGER is True, the call to apply_async() is actually replaced with apply(). The result returned is an EagerResult, which already contains the result of your task.

So, yes, setting ALWAYS_EAGER = True disables the entire AsyncResult functionnality. The entire async process is bypassed, and no task is actually sent to the broker, which is why you cannot retrieve the result through an AsyncResult.

Use CELERY_ALWAYS_EAGER = True when you are testing code path which just need a Celery result, and work the same way with an EagerResult or an AsyncResult.

If needed, there is a way to run tests with AsyncResult too, with CELERY_ALWAYS_EAGER = False, but for this, you'll need to start a worker before calling the task in your test case. The worker will then be able to execute your task, and AsyncResult will work just fine. You can take a look at django-celery-testworker which seems to do just that, although I've not tested it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!