Skip to content
This repository was archived by the owner on Dec 15, 2023. It is now read-only.

Commit 52bf57b

Browse files
committed
enable testing with CELERY_ALWAYS_EAGER
1 parent ed9fbf7 commit 52bf57b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

tpasync/resources.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from celery.result import AsyncResult
1+
from celery.result import AsyncResult, EagerResult
2+
from django.conf import settings
23
from django.conf.urls import url
34
from tastypie import resources, http, utils
45

6+
# use for dev/test only!
7+
EAGER_RESULTS={}
58

69
class AsyncResourceMixin(object):
710
def async_get_detail(self, request, **kwargs):
@@ -49,7 +52,10 @@ def async_state(self, request, task_id, **kwargs):
4952
5053
Other methods are forbidden.
5154
"""
52-
task = AsyncResult(task_id)
55+
if not getattr(settings, 'CELERY_ALWAYS_EAGER'):
56+
task = AsyncResult(task_id)
57+
else:
58+
task = EAGER_RESULTS[task_id]
5359
if request.method == 'GET':
5460
data = {
5561
'state': task.state, 'id': task.id,
@@ -82,7 +88,11 @@ def async_result(self, request, task_id, **kwargs):
8288
If request is not ready (or doesn't exist - we can't tell this),
8389
return Http 404 Not Found.
8490
"""
85-
task = AsyncResult(task_id)
91+
# hack to allow local testing
92+
if not getattr(settings, 'CELERY_ALWAYS_EAGER'):
93+
task = AsyncResult(task_id)
94+
else:
95+
task = EAGER_RESULTS[task_id]
8696
if task.ready():
8797
try:
8898
result = task.get()
@@ -163,6 +173,8 @@ def inner(self, request, **kwargs):
163173
return http.HttpNotImplemented()
164174

165175
if isinstance(result, AsyncResult):
176+
if isinstance(result, EagerResult):
177+
EAGER_RESULTS[result.id] = result
166178
response = http.HttpAccepted()
167179
response['Location'] = self._build_reverse_url(
168180
'api_async_state',

0 commit comments

Comments
 (0)