-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2322] Added single-run enforcement the task, added test
- Loading branch information
Bart van der Schoor
committed
May 13, 2024
1 parent
317f7f6
commit e87ed3e
Showing
8 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .celery import app as celery_app # noqa | ||
from .celery import app as celery_app | ||
|
||
__all__ = ("celery_app",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import logging | ||
|
||
from django.core.management import call_command | ||
|
||
from open_inwoner.celery import QueueOnce, app | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@app.task(base=QueueOnce, once={"keys": []}) | ||
def import_zgw_data(): | ||
logger.info(f"starting import_zgw_data() task") | ||
|
||
call_command("zgw_import_data") | ||
|
||
logger.info(f"finished import_zgw_data() task") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import uuid | ||
from unittest.mock import Mock, patch | ||
|
||
from django.test import TestCase | ||
|
||
from celery_once.tasks import AlreadyQueued | ||
|
||
from open_inwoner.celery import app as celery_app | ||
from open_inwoner.openzaak.tasks import import_zgw_data | ||
from open_inwoner.utils.test import ClearCachesMixin | ||
|
||
|
||
class ZGWImportTest(ClearCachesMixin, TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
# manually patch the conf: it is a dynamic object that is hard to patch | ||
cls._old_eager = celery_app.conf.task_always_eager | ||
celery_app.conf.task_always_eager = False | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
super().tearDownClass() | ||
celery_app.conf.task_always_eager = cls._old_eager | ||
|
||
@patch("open_inwoner.openzaak.tasks.call_command") | ||
def test_task_calls_command(self, mock_call: Mock): | ||
import_zgw_data() | ||
mock_call.assert_called_once_with("zgw_import_data") | ||
|
||
@patch("open_inwoner.celery.app.send_task") | ||
def test_task_runs_once(self, mock_send: Mock): | ||
# manually patch the task because it is not a normal class/method | ||
key = str(uuid.uuid4()) | ||
|
||
def get_key(self, args=None, kwargs=None): | ||
return key | ||
|
||
import_zgw_data.get_key = get_key | ||
|
||
# add manual cleanup so we don't litter locks | ||
def cleanup(): | ||
import_zgw_data.once_backend.clear_lock(key) | ||
|
||
self.addCleanup(cleanup) | ||
|
||
# actual test | ||
import_zgw_data.apply_async() | ||
with self.assertRaises(AlreadyQueued): | ||
import_zgw_data.apply_async() | ||
|
||
mock_send.assert_called_once() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters