Adds tests for task chains

adds several small improvements based on the problems that surfaced during writing the tests
This commit is contained in:
Ilan Steemers
2015-10-17 18:49:45 +02:00
parent 6dcead7310
commit 2df29b908c
4 changed files with 70 additions and 17 deletions
+4
View File
@@ -38,5 +38,9 @@ def get_user_id(user):
return user.id
def hello():
return 'hello'
def result(obj):
print('RESULT HOOK {} : {}'.format(obj.name, obj.result))
+32 -3
View File
@@ -1,11 +1,11 @@
from multiprocessing import Event, Queue, Value
import pytest
from django_q.cluster import pusher, worker, monitor
from django_q.cluster import pusher, worker, monitor
from django_q.conf import Conf
from django_q.tasks import async, result, fetch, count_group, result_group, fetch_group, delete_group, delete_cached, \
async_iter
async_iter, Chain, async_chain
from django_q.brokers import get_broker
@@ -96,9 +96,38 @@ def test_iter(broker):
result_t = result(t)
assert result_t is not None
task_t = fetch(t)
assert task_t. __unicode__ is not None
assert task_t.result == result_t
assert result(t2) is not None
assert result(t3) is not None
assert result(t4)[0] == 1
# test cached iter result
@pytest.mark.django_db
def test_chain(broker):
broker.purge_queue()
broker.cache.clear()
task_chain = Chain(sync=True)
task_chain.append('math.floor', 1)
task_chain.append('math.copysign', 1, -1)
task_chain.append('math.floor', 2)
assert task_chain.length() == 3
assert task_chain.current() is None
task_chain.run()
r = task_chain.result(wait=1000)
assert task_chain.current() == task_chain.length()
assert len(r) == task_chain.length()
t = task_chain.fetch()
assert len(t) == task_chain.length()
task_chain.cached = True
task_chain.append('math.floor', 3)
assert task_chain.length() == 4
task_chain.run()
r = task_chain.result(wait=1000)
assert task_chain.current() == task_chain.length()
assert len(r) == task_chain.length()
t = task_chain.fetch()
assert len(t) == task_chain.length()
# test single
rid = async_chain(['django_q.tests.tasks.hello', 'django_q.tests.tasks.hello'], sync=True, cached=True)
assert result_group(rid, cached=True) == ['hello', 'hello']