-
Notifications
You must be signed in to change notification settings - Fork 414
/
flow_tests.py
214 lines (172 loc) · 6.83 KB
/
flow_tests.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
Tests for flow producer class.
https://bbc.github.io/cloudfit-public-docs/asyncio/testing.html
"""
from asyncio import Future
from bullmq import Queue, Job, FlowProducer, Worker
from uuid import uuid4
import unittest
queue_name = f"__test_queue__{uuid4().hex}"
class TestJob(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
print("Setting up test queue")
# Delete test queue
queue = Queue(queue_name)
await queue.pause()
await queue.obliterate()
await queue.close()
async def test_should_process_children_before_parent(self):
child_job_name = 'child-job'
children_data = [
{"bar": 'something'},
{"baz": 'something'},
{"qux": 'something'}
]
parent_queue_name = f"__test_parent_queue__{uuid4().hex}"
processing_children = Future()
processed_children = 0
async def process1(job: Job, token: str):
nonlocal processed_children
processed_children+=1
if processed_children == len(children_data):
processing_children.set_result(None)
return children_data[job.data.get("idx")]
processing_parent = Future()
async def process2(job: Job, token: str):
processing_parent.set_result(None)
return 1
parent_worker = Worker(parent_queue_name, process2)
children_worker = Worker(queue_name, process1)
flow = FlowProducer()
await flow.add(
{
"name": 'parent-job',
"queueName": parent_queue_name,
"data": {},
"children": [
{"name": child_job_name, "data": {"idx": 0, "foo": 'bar'}, "queueName": queue_name},
{"name": child_job_name, "data": {"idx": 1, "foo": 'baz'}, "queueName": queue_name},
{"name": child_job_name, "data": {"idx": 2, "foo": 'qux'}, "queueName": queue_name}
]
}
)
await processing_children
await processing_parent
await parent_worker.close()
await children_worker.close()
await flow.close()
parent_queue = Queue(parent_queue_name)
await parent_queue.pause()
await parent_queue.obliterate()
await parent_queue.close()
async def test_addBulk_should_process_children_before_parent(self):
child_job_name = 'child-job'
children_data = [
{"idx": 0, "bar": 'something'},
{"idx": 1, "baz": 'something'}
]
parent_queue_name = f"__test_parent_queue__{uuid4().hex}"
processing_children = Future()
processed_children = 0
async def process1(job: Job, token: str):
nonlocal processed_children
processed_children+=1
if processed_children == len(children_data):
processing_children.set_result(None)
return children_data[job.data.get("idx")]
processing_parents = Future()
processed_parents = 0
async def process2(job: Job, token: str):
nonlocal processed_parents
processed_parents+=1
if processed_parents == 2:
processing_parents.set_result(None)
return 1
parent_worker = Worker(parent_queue_name, process2)
children_worker = Worker(queue_name, process1)
flow = FlowProducer()
await flow.addBulk([
{
"name": 'parent-job-1',
"queueName": parent_queue_name,
"data": {},
"children": [
{"name": child_job_name, "data": {"idx": 0, "foo": 'bar'}, "queueName": queue_name}
]
},
{
"name": 'parent-job-2',
"queueName": parent_queue_name,
"data": {},
"children": [
{"name": child_job_name, "data": {"idx": 1, "foo": 'baz'}, "queueName": queue_name}
]
}
])
await processing_children
await processing_parents
await parent_worker.close()
await children_worker.close()
await flow.close()
parent_queue = Queue(parent_queue_name)
await parent_queue.pause()
await parent_queue.obliterate()
await parent_queue.close()
async def test_get_children_values(self):
child_job_name = 'child-job'
children_data = [
{"bar": None},
{"baz": 12.93},
{"qux": "string value"}
]
parent_queue_name = f"__test_parent_queue__{uuid4().hex}"
processing_children = Future()
processed_children = 0
async def process1(job: Job, token: str):
nonlocal processed_children
processed_children+=1
if processed_children == len(children_data):
processing_children.set_result(None)
return children_data[job.data.get("idx")]
processing_parent = Future()
async def process2(job: Job, token: str):
children_values = await job.getChildrenValues()
processing_parent.set_result(children_values)
return 1
parent_worker = Worker(parent_queue_name, process2)
children_worker = Worker(queue_name, process1)
flow = FlowProducer()
await flow.add(
{
"name": 'parent-job',
"queueName": parent_queue_name,
"data": {},
"children": [
{"name": child_job_name, "data": {"idx": 0, "foo": 'bar'}, "queueName": queue_name},
{"name": child_job_name, "data": {"idx": 1, "foo": 'baz'}, "queueName": queue_name},
{"name": child_job_name, "data": {"idx": 2, "foo": 'qux'}, "queueName": queue_name}
]
}
)
await processing_children
await processing_parent
def on_parent_processed(future):
self.assertIn(children_data[0], future.result().values())
self.assertIn(children_data[1], future.result().values())
self.assertIn(children_data[2], future.result().values())
processing_parent.add_done_callback(on_parent_processed)
await parent_worker.close()
await children_worker.close()
await flow.close()
parent_queue = Queue(parent_queue_name)
await parent_queue.pause()
await parent_queue.obliterate()
await parent_queue.close()
async def test_get_children_values_on_simple_jobs(self):
queue = Queue(queue_name)
job = await queue.add("test", {"foo": "bar"}, {"delay": 1500})
children_values = await job.getChildrenValues()
self.assertEqual(children_values, {})
await queue.close()
if __name__ == '__main__':
unittest.main()