From 5fd436021fa4b27e1452382cdedcba7a715f1a11 Mon Sep 17 00:00:00 2001 From: Ilan Date: Mon, 20 Jul 2015 12:17:12 +0200 Subject: [PATCH] Expanding coverage --- django_q/tests/settings.py | 18 ++++++++++++++++-- django_q/tests/test_cluster.py | 24 ++++++++++++++++++++++++ django_q/tests/test_config.py | 15 +++++++++++++++ django_q/tests/test_monitor.py | 5 ++++- requirements.txt | 2 ++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 django_q/tests/test_config.py diff --git a/django_q/tests/settings.py b/django_q/tests/settings.py index 631ea7dd..8bad66c8 100644 --- a/django_q/tests/settings.py +++ b/django_q/tests/settings.py @@ -24,7 +24,8 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'django_q' + 'django_q', + 'django_redis' ) MIDDLEWARE_CLASSES = ( @@ -101,8 +102,21 @@ STATIC_URL = '/static/' +# Django Redis +CACHES = { + "default": { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": "redis://127.0.0.1:6379/0", + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.DefaultClient", + "PARSER_CLASS": "redis.connection.HiredisParser", + } + } +} + # Django Q specific Q_CLUSTER = {'name': 'django_q_test', 'cpu_affinity': 1, 'testing': True, - 'log_level': 'DEBUG'} + 'log_level': 'DEBUG', + 'django_redis': 'default'} diff --git a/django_q/tests/test_cluster.py b/django_q/tests/test_cluster.py index 2e347fac..3c7a858c 100644 --- a/django_q/tests/test_cluster.py +++ b/django_q/tests/test_cluster.py @@ -13,6 +13,7 @@ from django_q.tasks import fetch, fetch_group, async, result, result_group, count_group, delete_group from django_q.models import Task from django_q.conf import Conf, redis_client +from django_q.monitor import Stat from .tasks import multiply @@ -282,6 +283,29 @@ def test_recycle(r): r.delete(list_key) +@pytest.mark.django_db +def test_bad_secret(r, monkeypatch): + list_key = 'test_bad_secret' + async('math.copysign', 1, -1, list_key=list_key) + stop_event = Event() + stop_event.set() + start_event = Event() + s = Sentinel(stop_event, start_event, list_key=list_key, start=False) + Stat(s).save() + # change the SECRET + monkeypatch.setattr(Conf, "SECRET_KEY", "OOPS") + stat = Stat.get_all(r) + assert len(stat) == 0 + assert Stat.get(s.parent_pid, r) is None + task_queue = Queue() + pusher(task_queue, stop_event, list_key=list_key, r=r) + result_queue = Queue() + task_queue.put('STOP') + worker(task_queue, result_queue, Value('b', -1), ) + assert result_queue.qsize() == 0 + r.delete(list_key) + + @pytest.mark.django_db def assert_result(task): assert task is not None diff --git a/django_q/tests/test_config.py b/django_q/tests/test_config.py new file mode 100644 index 00000000..de5ab71b --- /dev/null +++ b/django_q/tests/test_config.py @@ -0,0 +1,15 @@ +import pytest + +from django_q import conf + + +@pytest.fixture +def r(): + return conf.redis_client + + +def test_django_redis(): + conf.Conf.DJANGO_REDIS = None + assert conf.redis_client.ping() is True + conf.Conf.DJANGO_REDIS = 'default' + assert conf.redis_client.ping() is True diff --git a/django_q/tests/test_monitor.py b/django_q/tests/test_monitor.py index 7c140b69..90ced366 100644 --- a/django_q/tests/test_monitor.py +++ b/django_q/tests/test_monitor.py @@ -1,8 +1,9 @@ from django_q.cluster import Cluster -from django_q.monitor import monitor +from django_q.monitor import monitor, Stat def test_monitor(): + assert Stat.get(0).sentinel == 0 c = Cluster() c.start() stats = monitor(run_once=True) @@ -12,5 +13,7 @@ def test_monitor(): for stat in stats: if stat.cluster_id == c.pid: found_c = True + assert stat.uptime() > 0 + assert stat.empty_queues() is True break assert found_c is True diff --git a/requirements.txt b/requirements.txt index 144d181b..f609487d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,5 +6,7 @@ future==0.14.3 hiredis==0.2.0 redis==2.10.3 psutil==3.1.1 +django-redis==4.2.0 +