Debian Codegraph @

test/util/test_pipeline.py

raw
1 """Test the "pipeline.py" restricted parallel programming library."""
2
3 import unittest
4
5 import pytest
6
7 from beets.util import pipeline
8
9
10 # Some simple pipeline stages for testing.
11 def _produce(num=5):
12 yield from range(num)
13
14
15 def _work():
16 i = None
17 while True:
18 i = yield i
19 i *= 2
20
21
22 def _consume(result):
23 while True:
24 i = yield
25 result.append(i)
26
27
28 # Pipeline stages that raise an exception.
29 class PipelineError(Exception):
30 pass
31
32
33 def _exc_produce(num=5):
34 yield from range(num)
35 raise PipelineError()
36
37
38 def _exc_work(num=3):
39 i = None
40 while True:
41 i = yield i
42 if i == num:
43 raise PipelineError()
44 i *= 2
45
46
47 def _exc_consume(result, num=4):
48 while True:
49 i = yield
50 if i == num:
51 raise PipelineError()
52 result.append(i)
53
54
55 # A worker that yields a bubble.
56 def _bub_work(num=3):
57 i = None
58 while True:
59 i = yield i
60 if i == num:
61 i = pipeline.BUBBLE
62 else:
63 i *= 2
64
65
66 # Yet another worker that yields multiple messages.
67 def _multi_work():
68 i = None
69 while True:
70 i = yield i
71 i = pipeline.multiple([i, -i])
72
73
74 class SimplePipelineTest(unittest.TestCase):
75 def setUp(self):
76 self.result = []
77 self.pl = pipeline.Pipeline(
78 (_produce(), _work(), _consume(self.result))
79 )
80
81 def test_run_sequential(self):
82 self.pl.run_sequential()
83 assert self.result == [0, 2, 4, 6, 8]
84
85 def test_run_parallel(self):
86 self.pl.run_parallel()
87 assert self.result == [0, 2, 4, 6, 8]
88
89 def test_pull(self):
90 pl = pipeline.Pipeline((_produce(), _work()))
91 assert list(pl.pull()) == [0, 2, 4, 6, 8]
92
93 def test_pull_chain(self):
94 pl = pipeline.Pipeline((_produce(), _work()))
95 pl2 = pipeline.Pipeline((pl.pull(), _work()))
96 assert list(pl2.pull()) == [0, 4, 8, 12, 16]
97
98
99 class ParallelStageTest(unittest.TestCase):
100 def setUp(self):
101 self.result = []
102 self.pl = pipeline.Pipeline(
103 (_produce(), (_work(), _work()), _consume(self.result))
104 )
105
106 def test_run_sequential(self):
107 self.pl.run_sequential()
108 assert self.result == [0, 2, 4, 6, 8]
109
110 def test_run_parallel(self):
111 self.pl.run_parallel()
112 # Order possibly not preserved; use set equality.
113 assert set(self.result) == {0, 2, 4, 6, 8}
114
115 def test_pull(self):
116 pl = pipeline.Pipeline((_produce(), (_work(), _work())))
117 assert list(pl.pull()) == [0, 2, 4, 6, 8]
118
119
120 class ExceptionTest(unittest.TestCase):
121 def setUp(self):
122 self.result = []
123
124 def run_sequential(self, *stages):
125 pl = pipeline.Pipeline(stages)
126 with pytest.raises(PipelineError):
127 pl.run_sequential()
128
129 def run_parallel(self, *stages):
130 pl = pipeline.Pipeline(stages)
131 with pytest.raises(PipelineError):
132 pl.run_parallel()
133
134 def test_run_sequential(self):
135 """Test that exceptions from various stages of the pipeline are
136 properly propagated when running sequentially.
137 """
138 self.run_sequential(_exc_produce(), _work(), _consume(self.result))
139 self.run_sequential(_produce(), _exc_work(), _consume(self.result))
140 self.run_sequential(_produce(), _work(), _exc_consume(self.result))
141
142 def test_run_parallel(self):
143 """Test that exceptions from various stages of the pipeline are
144 properly propagated when running in parallel.
145 """
146 self.run_parallel(_exc_produce(), _work(), _consume(self.result))
147 self.run_parallel(_produce(), _exc_work(), _consume(self.result))
148 self.run_parallel(_produce(), _work(), _exc_consume(self.result))
149
150 def test_pull(self):
151 pl = pipeline.Pipeline((_produce(), _exc_work()))
152 pull = pl.pull()
153 for i in range(3):
154 next(pull)
155 with pytest.raises(PipelineError):
156 next(pull)
157
158
159 class ParallelExceptionTest(unittest.TestCase):
160 def setUp(self):
161 self.result = []
162 self.pl = pipeline.Pipeline(
163 (_produce(), (_exc_work(), _exc_work()), _consume(self.result))
164 )
165
166 def test_run_parallel(self):
167 with pytest.raises(PipelineError):
168 self.pl.run_parallel()
169
170
171 class ConstrainedThreadedPipelineTest(unittest.TestCase):
172 def setUp(self):
173 self.result = []
174
175 def test_constrained(self):
176 # Do a "significant" amount of work...
177 self.pl = pipeline.Pipeline(
178 (_produce(1000), _work(), _consume(self.result))
179 )
180 # ... with only a single queue slot.
181 self.pl.run_parallel(1)
182 assert self.result == [i * 2 for i in range(1000)]
183
184 def test_constrained_exception(self):
185 # Raise an exception in a constrained pipeline.
186 self.pl = pipeline.Pipeline(
187 (_produce(1000), _exc_work(), _consume(self.result))
188 )
189 with pytest.raises(PipelineError):
190 self.pl.run_parallel(1)
191
192 def test_constrained_parallel(self):
193 self.pl = pipeline.Pipeline(
194 (_produce(1000), (_work(), _work()), _consume(self.result))
195 )
196 self.pl.run_parallel(1)
197 assert set(self.result) == {i * 2 for i in range(1000)}
198
199
200 class BubbleTest(unittest.TestCase):
201 def setUp(self):
202 self.result = []
203 self.pl = pipeline.Pipeline(
204 (_produce(), _bub_work(), _consume(self.result))
205 )
206
207 def test_run_sequential(self):
208 self.pl.run_sequential()
209 assert self.result == [0, 2, 4, 8]
210
211 def test_run_parallel(self):
212 self.pl.run_parallel()
213 assert self.result == [0, 2, 4, 8]
214
215 def test_pull(self):
216 pl = pipeline.Pipeline((_produce(), _bub_work()))
217 assert list(pl.pull()) == [0, 2, 4, 8]
218
219
220 class MultiMessageTest(unittest.TestCase):
221 def setUp(self):
222 self.result = []
223 self.pl = pipeline.Pipeline(
224 (_produce(), _multi_work(), _consume(self.result))
225 )
226
227 def test_run_sequential(self):
228 self.pl.run_sequential()
229 assert self.result == [0, 0, 1, -1, 2, -2, 3, -3, 4, -4]
230
231 def test_run_parallel(self):
232 self.pl.run_parallel()
233 assert self.result == [0, 0, 1, -1, 2, -2, 3, -3, 4, -4]
234
235 def test_pull(self):
236 pl = pipeline.Pipeline((_produce(), _multi_work()))
237 assert list(pl.pull()) == [0, 0, 1, -1, 2, -2, 3, -3, 4, -4]
238
239
240 class StageDecoratorTest(unittest.TestCase):
241 def test_stage_decorator(self):
242 @pipeline.stage
243 def add(n, i):
244 return i + n
245
246 pl = pipeline.Pipeline([iter([1, 2, 3]), add(2)])
247 assert list(pl.pull()) == [3, 4, 5]
248
249 def test_mutator_stage_decorator(self):
250 @pipeline.mutator_stage
251 def setkey(key, item):
252 item[key] = True
253
254 pl = pipeline.Pipeline(
255 [iter([{"x": False}, {"a": False}]), setkey("x")]
256 )
257 assert list(pl.pull()) == [{"x": True}, {"a": False, "x": True}]