-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestredisqueue.py
83 lines (74 loc) · 2.6 KB
/
testredisqueue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from redisqueue import RedisQueue
from redis import Redis
import unittest
class TestBasicQueueFunctions(unittest.TestCase):
def setUp(self):
self.rq = RedisQueue("testqueue", "testworker01", db=10)
self.r = Redis(db=10)
self.r.delete("testqueue")
self.r.delete(self.rq.workeritem)
def test_add_to_queue(self):
self.rq.push("testitem")
self.assertEqual(len(self.rq), 1)
self.assertEqual(self.rq.pop(), "testitem")
def test_multiple_additions(self):
results = map(self.rq.push, xrange(10))
self.assertEquals(results, [True]*10)
def test_temp_workerqueue(self):
self.rq.push("testitem")
self.rq.push("nextitem")
# worker can pop item, but until it declares complete/fail,
# subsequent pops will return that same item
item = self.rq.pop()
self.assertEqual(item, "testitem")
self.assertEqual(len(self.rq), 1)
item = self.rq.pop()
self.assertEqual(item, "testitem")
self.assertEqual(len(self.rq), 1)
def test_workerqueue_complete(self):
self.rq.push("testitem")
self.rq.push("nextitem")
# worker can pop item, but until it declares complete/fail,
# subsequent pops will return that same item
item = self.rq.pop()
self.assertEqual(item, "testitem")
self.assertEqual(len(self.rq), 1)
item = self.rq.pop()
self.assertEqual(item, "testitem")
self.assertEqual(len(self.rq), 1)
self.rq.task_complete()
item = self.rq.pop()
self.assertEqual(item, "nextitem")
self.assertEqual(len(self.rq), 0)
def test_requeue_on_fail(self):
self.rq.push("testitem")
self.rq.push("nextitem")
# worker can pop item, but until it declares complete/fail,
# subsequent pops will return that same item
item = self.rq.pop()
self.assertEqual(item, "testitem")
self.assertEqual(len(self.rq), 1)
item = self.rq.pop()
self.assertEqual(item, "testitem")
self.assertEqual(len(self.rq), 1)
self.rq.task_failed()
self.assertEqual(len(self.rq), 2)
def test_requeue_to_end_on_fail(self):
self.rq.push("testitem")
self.rq.push("nextitem")
# worker can pop item, but until it declares complete/fail,
# subsequent pops will return that same item
item = self.rq.pop()
self.assertEqual(item, "testitem")
self.assertEqual(len(self.rq), 1)
self.rq.task_failed()
self.assertEqual(len(self.rq), 2)
item = self.rq.pop()
self.assertEqual(item, "nextitem")
self.assertEqual(len(self.rq), 1)
self.rq.task_complete()
item = self.rq.pop()
self.assertEqual(item, "testitem")
self.assertEqual(len(self.rq), 0)
if __name__ == '__main__':
unittest.main()