Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

During testing do not postpone execution of the tasks #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions django_atomic_celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import threading
from collections import defaultdict
from celery.task import task as base_task, Task
from django.conf import settings
from django.dispatch import receiver
from django.db import DEFAULT_DB_ALIAS
from functools import partial
Expand All @@ -27,8 +28,8 @@ def schedule(self):
"""Schedule the conditional task.
"""

self.task_cls.original_apply_async(*self.task_args,
**self.task_kwargs)
return self.task_cls.original_apply_async(*self.task_args,
**self.task_kwargs)

@property
def name(self):
Expand Down Expand Up @@ -91,7 +92,8 @@ def apply_async(cls, args=(), kwargs=None, *a, **kw):

t = ConditionalTask(cls, args, kwargs, *a, **kw)

if task_queue_stack:
if task_queue_stack and not getattr(settings,
'CELERY_ALWAYS_EAGER', False):
logger.debug('Scheduling task %s if transaction block is '
'successful' % (t.description))
task_queue_stack[-1].append(t)
Expand Down
5 changes: 5 additions & 0 deletions tests/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from django_atomic_celery import task


@task
def returning_task(sentinel):
return sentinel


@task
def task(*arg, **kwargs):
pass
19 changes: 18 additions & 1 deletion tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.conf import settings
from django.test import TestCase
from django_atomic_celery.testing import DjangoAtomicCeleryTestCaseMixin
from .tasks import task
from .tasks import returning_task, task


class TaskTestCase(DjangoAtomicCeleryTestCaseMixin, TestCase):
Expand Down Expand Up @@ -132,3 +132,20 @@ def test_apply_async(self):
self._test_behavior(task.apply_async)
self._test_behavior(task.apply_async, args=())
self._test_behavior(task.apply_async, kwargs={})


class EagerTestCase(TestCase):
"""
Check here behaviour of django_atomic_celery related to
CELERY_ALWAYS_EAGER settings
"""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get some docstrings on this for good meassure.

def test_celery_always_eager(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get some docstrings on this for good meassure.

"""
task.apply_async() should return immediately
because CELERY_ALWAYS_EAGER is True
"""
with self.settings(CELERY_ALWAYS_EAGER=True):
sentinel = object()
self.assertTrue(
returning_task.apply_async(args=(sentinel,)).get() is sentinel)