Commit 1fe238cf authored by Johan Bloemberg's avatar Johan Bloemberg

test celery behaviour for rate limited tasks

parent 5e8a2c36
"""Test assumptions about rate limiting."""
from django.conf import settings
from failmap_admin.celery import rate_limited, waitsome
SAMPLES = settings.CELERY_WORKER_CONCURRENCY
SLEEP = 1
TASK_EXPIRY_TIME = SAMPLES * SLEEP
def test_rate_limits(celery_app, celery_worker):
"""Rate limited tasks should not hold up worker queue for other tasks."""
# fill queue with rate limited tasks
rated_tasks = [rate_limited.apply_async([SLEEP], expires=TASK_EXPIRY_TIME) for _ in range(SAMPLES)]
# add tasks that is not rate limited
task = waitsome.apply_async([SLEEP], expires=TASK_EXPIRY_TIME)
# make sure task is executed before all rate limited tasks are done
assert task.get(timeout=SLEEP * 2)
# for sanity make sure not more than half the rate limited task have been executed in the mean time
# but at least one has executed
PENDING_RATED_TASKS = len([s for s in rated_tasks if s.state == 'PENDING'])
assert PENDING_RATED_TASKS > SAMPLES / 2
assert PENDING_RATED_TASKS < SAMPLES
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment