-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_runner.py
380 lines (305 loc) · 14 KB
/
test_runner.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from concurrent.futures import ThreadPoolExecutor
import time
from typing import Any, Callable, Dict, List
from haystack import Pipeline, component
import pytest
from haystack_extensions.components.concurrent_runner.runner import (
ConcurrentComponentRunner,
NamedComponent,
NamedPipeline,
ConcurrentPipelineRunner,
)
from haystack.components.others import Multiplexer
from haystack.components.retrievers import InMemoryEmbeddingRetriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
@component
class SimpleComponent:
def __init__(self, wait_time: float, callback: Callable) -> None:
self.wait_time = wait_time
self.callback = callback
@component.output_types(number=int)
def run(self, increment: int, number: int = 5) -> Dict[str, int]:
time.sleep(self.wait_time)
self.callback(self)
return {"number": number + increment}
@component
class ComplexInputOutputComponent:
@component.output_types(number=int, as_list=List[int], as_dict_of_lists={str: List[int]})
def run(self, some_dict: Dict[str, List[int]], increment: int, number: int = 5) -> Dict[str, Any]:
return {
"number": number + increment,
"as_list": [number + increment],
"as_dict_of_lists": {key: [number + increment] for key in some_dict.keys()},
}
@component
class SimplePrintAndReturnInputComponent:
@component.output_types(value=Any)
def run(self, value: Any) -> Any:
print(value)
return {"value": value}
class TestConcurrentComponentRunner:
def test_concurrent_component_runner_with_external_executor(self):
with ThreadPoolExecutor(max_workers=3) as executor:
component_call_stack = []
def callback(component):
component_call_stack.append(component)
named_components = [
NamedComponent(name="component1", component=SimpleComponent(wait_time=0.05, callback=callback)),
NamedComponent(name="component2", component=SimpleComponent(wait_time=0.09, callback=callback)),
NamedComponent(name="component3", component=SimpleComponent(wait_time=0.01, callback=callback)),
]
runner = ConcurrentComponentRunner(named_components, executor=executor)
pipe = Pipeline()
pipe.add_component("concurrent_runner", runner)
results = pipe.run(
data={
"concurrent_runner": {
"component1_increment": 1,
"component2_increment": 2,
"component2_number": 10,
"component3_increment": 3,
"component3_number": 20
}
}
)
assert results == {
"concurrent_runner": {
"component1_number": 6,
"component2_number": 12,
"component3_number": 23
}
}
assert len(component_call_stack) == 3
assert component_call_stack[0] == named_components[2].component
assert component_call_stack[1] == named_components[0].component
assert component_call_stack[2] == named_components[1].component
def test_concurrent_component_runner(self):
component_call_stack = []
def callback(component):
component_call_stack.append(component)
named_components = [
NamedComponent(name="c1", component=SimpleComponent(wait_time=0.05, callback=callback)),
NamedComponent(name="c2", component=SimpleComponent(wait_time=0.09, callback=callback)),
NamedComponent(name="c3", component=SimpleComponent(wait_time=0.01, callback=callback)),
]
runner = ConcurrentComponentRunner(named_components)
pipe = Pipeline()
pipe.add_component("concurrent_runner", instance=runner)
results = pipe.run(
data={
"concurrent_runner": {
"c1_increment": 1,
"c2_increment": 2,
"c2_number": 10,
"c3_increment": 3,
"c3_number": 20
}
}
)
assert results == {
"concurrent_runner": {
"c1_number": 6,
"c2_number": 12,
"c3_number": 23,
}
}
assert len(component_call_stack) == 3
assert component_call_stack[0] == named_components[2].component
assert component_call_stack[1] == named_components[0].component
assert component_call_stack[2] == named_components[1].component
def test_concurrent_component_with_output_connections_runner(self):
component_call_stack = []
def callback(component):
component_call_stack.append(component)
named_components = [
NamedComponent(name="component1", component=SimpleComponent(wait_time=0.05, callback=callback)),
NamedComponent(name="component2", component=SimpleComponent(wait_time=0.09, callback=callback)),
NamedComponent(name="component3", component=SimpleComponent(wait_time=0.01, callback=callback)),
]
runner = ConcurrentComponentRunner(named_components)
pipe = Pipeline()
pipe.add_component("concurrent_runner", runner)
pipe.add_component("print_input", SimplePrintAndReturnInputComponent())
pipe.connect("concurrent_runner.component1_number", "print_input")
results = pipe.run(
data={
"concurrent_runner": {
"component1_increment": 1,
"component2_increment": 2,
"component2_number": 10,
"component3_increment": 3,
"component3_number": 20
}
}
)
assert results == {
'concurrent_runner': {
'component2_number': 12,
'component3_number': 23,
},
'print_input': {
'value': 6
}
}
def test_concurrent_component_with_input_connections_runner(self):
component_call_stack = []
def callback(component):
component_call_stack.append(component)
named_components = [
NamedComponent(name="component1", component=SimpleComponent(wait_time=0.05, callback=callback)),
NamedComponent(name="component2", component=SimpleComponent(wait_time=0.09, callback=callback)),
NamedComponent(name="component3", component=SimpleComponent(wait_time=0.01, callback=callback)),
]
runner = ConcurrentComponentRunner(named_components)
pipe = Pipeline()
pipe.add_component("concurrent_runner", runner)
pipe.add_component("multiplexer", Multiplexer(int))
pipe.connect("multiplexer.value", "concurrent_runner.component1_number")
results = pipe.run(
data={
"concurrent_runner": {
"component1_increment": 1,
"component2_increment": 2,
"component2_number": 10,
"component3_increment": 3,
"component3_number": 20,
},
"multiplexer": {
"value": 12
}
}
)
assert results == {
'concurrent_runner': {
'component1_number': 13,
'component2_number': 12,
'component3_number': 23,
}
}
def test_same_component_name_raises_error(self):
component1 = NamedComponent("component", SimpleComponent(wait_time=0.1, callback=lambda x: None))
component2 = NamedComponent("component", SimpleComponent(wait_time=0.1, callback=lambda x: None))
with pytest.raises(ValueError):
ConcurrentComponentRunner([component1, component2])
def test_complex_input_output_working(self):
component = NamedComponent("component", ComplexInputOutputComponent())
runner = ConcurrentComponentRunner([component])
pipeline = Pipeline()
pipeline.add_component("concurrent_runner", runner)
results = pipeline.run(
data={"concurrent_runner": {
"component_some_dict": {"a": [1, 2, 3]},
"component_increment": 1
}
}
)
assert results == {
"concurrent_runner": {
"component_number": 6,
"component_as_list": [6],
"component_as_dict_of_lists": {"a": [6]}
}
}
@pytest.mark.parametrize(
"input",
[
("component", SimpleComponent(wait_time=0.1, callback=lambda x: None)),
{"component": SimpleComponent(wait_time=0.1, callback=lambda x: None)},
],
)
def test_concurrent_runner_raises_error_on_wrong_input(self, input):
component = ("component", SimpleComponent(wait_time=0.1, callback=lambda x: None))
with pytest.raises(ValueError):
ConcurrentComponentRunner([component])
def test_default_values_are_respected(self):
component = NamedComponent("component", SimpleComponent(wait_time=0.1, callback=lambda x: None))
runner = ConcurrentComponentRunner([component])
pipeline = Pipeline()
pipeline.add_component("concurrent_runner", runner)
results = pipeline.run(data={"concurrent_runner":
{"component_increment": 1}
})
assert results == {'concurrent_runner': {'component_number': 6}}
def test_all_retriever_values_copied(self):
retriever = InMemoryEmbeddingRetriever(InMemoryDocumentStore())
concurrent_retriever = ConcurrentComponentRunner([NamedComponent("retriever", retriever)])
concurrent_retriever.__haystack_input__._sockets_dict
assert len(retriever.__haystack_input__._sockets_dict) == len(concurrent_retriever.__haystack_input__._sockets_dict)
class TestConcurrentPipelineRunner:
def test_concurrent_pipeline_with_external_executor(self):
with ThreadPoolExecutor(max_workers=2) as executor:
component_call_stack = []
def callback(component):
component_call_stack.append(component)
simple_component_1 = SimpleComponent(wait_time=0.09, callback=callback)
pipeline1 = Pipeline()
pipeline1.add_component("simple_component", simple_component_1)
simple_component_2 = SimpleComponent(wait_time=0.02, callback=callback)
pipeline2 = Pipeline()
pipeline2.add_component("simple_component", simple_component_2)
concurrent_pipeline_runner = ConcurrentPipelineRunner(
[NamedPipeline("pipeline1", pipeline1), NamedPipeline("pipeline2", pipeline2)], executor=executor
)
overall_pipeline = Pipeline()
overall_pipeline.add_component("concurrent_pipeline_runner", concurrent_pipeline_runner)
results = overall_pipeline.run(
data={
"concurrent_pipeline_runner": {
"pipeline1": {"simple_component": {"increment": 1}},
"pipeline2": {"simple_component": {"increment": 2, "number": 10}},
}
}
)
assert results == {
'concurrent_pipeline_runner': {
'pipeline1': {'simple_component': {'number': 6}},
'pipeline2': {'simple_component': {'number': 12}},
}
}
assert len(component_call_stack) == 2
assert component_call_stack[0] == simple_component_2
assert component_call_stack[1] == simple_component_1
def test_concurrent_pipeline_runner(self):
component_call_stack = []
def callback(component):
component_call_stack.append(component)
simple_component_1 = SimpleComponent(wait_time=0.09, callback=callback)
pipeline1 = Pipeline()
pipeline1.add_component("simple_component", simple_component_1)
simple_component_2 = SimpleComponent(wait_time=0.02, callback=callback)
pipeline2 = Pipeline()
pipeline2.add_component("simple_component", simple_component_2)
concurrent_pipeline_runner = ConcurrentPipelineRunner(
[NamedPipeline("pipeline1", pipeline1), NamedPipeline("pipeline2", pipeline2)]
)
overall_pipeline = Pipeline()
overall_pipeline.add_component("concurrent_pipeline_runner", concurrent_pipeline_runner)
results = overall_pipeline.run(
data={
"concurrent_pipeline_runner": {
"pipeline1": {"simple_component": {"increment": 1}},
"pipeline2": {"simple_component": {"increment": 2, "number": 10}},
}
}
)
assert results == {
'concurrent_pipeline_runner': {
'pipeline1': {'simple_component': {'number': 6}},
'pipeline2': {'simple_component': {'number': 12}},
}
}
assert len(component_call_stack) == 2
assert component_call_stack[0] == simple_component_2
assert component_call_stack[1] == simple_component_1
@pytest.mark.parametrize("input", [("pipeline", Pipeline(), {"pipeline": Pipeline()})])
def test_concurrent_pipeline_runner_raises_error_on_wrong_input(self, input):
pipeline = Pipeline()
with pytest.raises(ValueError):
ConcurrentPipelineRunner([("pipeline", [input])])
def test_same_pipeline_name_raises_error(self):
pipeline1 = NamedPipeline("pipeline", Pipeline())
pipeline2 = NamedPipeline("pipeline", Pipeline())
with pytest.raises(ValueError):
ConcurrentPipelineRunner([pipeline1, pipeline2])
if __name__ == "__main__":
TestConcurrentComponentRunner().test_concurrent_component_with_input_connections_runner