9

i work on a celery beat task within a django project which creates Database entries periodically. I know so beacuse when i set the task up like this :

celery.py:

from __future__ import absolute_import, unicode_literals

import os

from celery import Celery
from celery.schedules import crontab

app = Celery("clock-backend", broker=os.environ.get("RABBITMQ_URL"))

app.config_from_object("django.conf:settings", namespace="CELERY")
app.conf.beat_schedule = {

    'create_reports_monthly': {
        'task': 'project_celery.tasks.create_reports_monthly',
        'schedule': 10.0,
    },
}
app.autodiscover_tasks()

And start my project it really creates an object every 10 seconds.

But what i really want to do is to set it up to run every first day of a month.

To do so i would change "schedule": crontab(0, 0, day_of_month="1").

Here comes my actual problem : How do i test that this really works ?

And by testing i mean actual (unit)tests.

What I've tried is to work with a package called freezegun. A test with this looks like this :

def test_start_of_month_report_creation(self, user_object, contract_object, report_object):
    # set time to the last day of January
    with freeze_time("2019-01-31 23:59:59") as frozen_time:
        # let one second pass
        frozen_time.tick()
        # give the celery task some time
        time.sleep(20)
        # Test Logic to check whether the object was created
        # Example: assert MyModel.objects.count() > 0

But this did not work. I suspect that the celery beat does not use the time set via freezgun/python but the real "hardware" clock.

I've also tried setting the Hardwareclock like here but this did not work in my setup.

I'm thankful for any comments, remarks or help on this topic since i'd really like to implement a test for this.

Chgad
  • 820
  • 2
  • 7
  • 18
  • 2
    Unit tests are not for testing 3rd party libraries. You should possibly use a monitoring solution to make sure that celerybeat is running and that the report is being generated – Iain Shelvington Apr 14 '19 at 17:59
  • 2
    Well I'd like to see whether my task is executed correctly at the beginning of each month. I do not solely want to test the periodic task feature of celery beat. – Chgad Apr 14 '19 at 21:22
  • 1
    maybe just send yourself an email or log a statement to know that your task ran successfully – Aman Garg Apr 16 '19 at 11:43
  • 1
    Here is a solution, how to check test the schedule: https://stackoverflow.com/questions/49586967/how-to-test-celery-periodic-task-in-django/67597941#67597941 – hendrikschneider May 19 '21 at 11:26
  • @hendrikschneider any chance for writing a current solution as an answer? It would be helpful to test the task's crontab was installed correctly by the project CELERY_BEAT_SCHEDULE setting. It doesn't appear that the run_every option is available on tasks anymore – user319862 Jul 13 '21 at 21:43

1 Answers1

0
  1. Unit tests cannot test third-party libraries.
  2. You can set the system log, to keep track.
  3. You can check if your task is already on model PeriodicTask. This model defines a single periodic task to be run. It must be associated with a schedule, which defines how often the task should run.
cảnh nguyễn
  • 528
  • 1
  • 6
  • 6