Commit | Line | Data |
---|---|---|
d2d857a8 MJ |
1 | # |
2 | # Copyright (C) 2019 EfficiOS Inc. | |
3 | # | |
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; only version 2 | |
7 | # of the License. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | # | |
18 | ||
811644b8 | 19 | import unittest |
811644b8 | 20 | import bt2 |
8e97c333 | 21 | import sys |
6c373cc9 | 22 | from utils import TestOutputPortMessageIterator |
14503fb1 | 23 | from bt2 import port as bt2_port |
811644b8 PP |
24 | |
25 | ||
0a6d7302 SM |
26 | class SimpleSink(bt2._UserSinkComponent): |
27 | # Straightforward sink that creates one input port (`in`) and consumes from | |
28 | # it. | |
811644b8 | 29 | |
0a6d7302 SM |
30 | def __init__(self, params, obj): |
31 | self._add_input_port('in') | |
811644b8 | 32 | |
0a6d7302 SM |
33 | def _user_consume(self): |
34 | next(self._msg_iter) | |
811644b8 | 35 | |
0a6d7302 SM |
36 | def _user_graph_is_configured(self): |
37 | self._msg_iter = self._create_input_port_message_iterator( | |
38 | self._input_ports['in'] | |
39 | ) | |
ca02df0a | 40 | |
ca02df0a | 41 | |
0a6d7302 SM |
42 | def _create_graph(src_comp_cls, sink_comp_cls, flt_comp_cls=None): |
43 | graph = bt2.Graph() | |
ca02df0a | 44 | |
0a6d7302 SM |
45 | src_comp = graph.add_component(src_comp_cls, 'src') |
46 | sink_comp = graph.add_component(sink_comp_cls, 'sink') | |
ca02df0a | 47 | |
0a6d7302 SM |
48 | if flt_comp_cls is not None: |
49 | flt_comp = graph.add_component(flt_comp_cls, 'flt') | |
50 | graph.connect_ports(src_comp.output_ports['out'], flt_comp.input_ports['in']) | |
51 | graph.connect_ports(flt_comp.output_ports['out'], sink_comp.input_ports['in']) | |
52 | else: | |
53 | graph.connect_ports(src_comp.output_ports['out'], sink_comp.input_ports['in']) | |
811644b8 | 54 | |
0a6d7302 SM |
55 | return graph |
56 | ||
57 | ||
58 | class UserMessageIteratorTestCase(unittest.TestCase): | |
811644b8 | 59 | def test_init(self): |
c5f330cd SM |
60 | the_output_port_from_source = None |
61 | the_output_port_from_iter = None | |
62 | ||
5602ef81 | 63 | class MyIter(bt2._UserMessageIterator): |
c5f330cd | 64 | def __init__(self, self_port_output): |
811644b8 | 65 | nonlocal initialized |
c5f330cd | 66 | nonlocal the_output_port_from_iter |
811644b8 | 67 | initialized = True |
c5f330cd | 68 | the_output_port_from_iter = self_port_output |
811644b8 | 69 | |
cfbd7cf3 | 70 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
66964f3f | 71 | def __init__(self, params, obj): |
c5f330cd | 72 | nonlocal the_output_port_from_source |
2e00bc76 | 73 | the_output_port_from_source = self._add_output_port('out', 'user data') |
811644b8 PP |
74 | |
75 | initialized = False | |
0a6d7302 | 76 | graph = _create_graph(MySource, SimpleSink) |
c5f330cd | 77 | graph.run() |
811644b8 | 78 | self.assertTrue(initialized) |
cfbd7cf3 FD |
79 | self.assertEqual( |
80 | the_output_port_from_source.addr, the_output_port_from_iter.addr | |
81 | ) | |
2e00bc76 | 82 | self.assertEqual(the_output_port_from_iter.user_data, 'user data') |
811644b8 | 83 | |
ca02df0a PP |
84 | def test_create_from_message_iterator(self): |
85 | class MySourceIter(bt2._UserMessageIterator): | |
86 | def __init__(self, self_port_output): | |
87 | nonlocal src_iter_initialized | |
88 | src_iter_initialized = True | |
89 | ||
90 | class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter): | |
66964f3f | 91 | def __init__(self, params, obj): |
ca02df0a PP |
92 | self._add_output_port('out') |
93 | ||
94 | class MyFilterIter(bt2._UserMessageIterator): | |
95 | def __init__(self, self_port_output): | |
96 | nonlocal flt_iter_initialized | |
97 | flt_iter_initialized = True | |
98 | self._up_iter = self._create_input_port_message_iterator( | |
99 | self._component._input_ports['in'] | |
100 | ) | |
101 | ||
102 | def __next__(self): | |
103 | return next(self._up_iter) | |
104 | ||
105 | class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): | |
66964f3f | 106 | def __init__(self, params, obj): |
ca02df0a PP |
107 | self._add_input_port('in') |
108 | self._add_output_port('out') | |
109 | ||
110 | src_iter_initialized = False | |
111 | flt_iter_initialized = False | |
0a6d7302 | 112 | graph = _create_graph(MySource, SimpleSink, MyFilter) |
ca02df0a PP |
113 | graph.run() |
114 | self.assertTrue(src_iter_initialized) | |
115 | self.assertTrue(flt_iter_initialized) | |
116 | ||
e803df70 SM |
117 | def test_create_user_error(self): |
118 | # This tests both error handling by | |
119 | # _UserSinkComponent._create_input_port_message_iterator | |
120 | # and _UserMessageIterator._create_input_port_message_iterator, as they | |
121 | # are both used in the graph. | |
122 | class MySourceIter(bt2._UserMessageIterator): | |
123 | def __init__(self, self_port_output): | |
124 | raise ValueError('Very bad error') | |
125 | ||
126 | class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter): | |
127 | def __init__(self, params, obj): | |
128 | self._add_output_port('out') | |
129 | ||
130 | class MyFilterIter(bt2._UserMessageIterator): | |
131 | def __init__(self, self_port_output): | |
132 | # This is expected to raise because of the error in | |
133 | # MySourceIter.__init__. | |
134 | self._create_input_port_message_iterator( | |
135 | self._component._input_ports['in'] | |
136 | ) | |
137 | ||
138 | class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): | |
139 | def __init__(self, params, obj): | |
140 | self._add_input_port('in') | |
141 | self._add_output_port('out') | |
142 | ||
0a6d7302 | 143 | graph = _create_graph(MySource, SimpleSink, MyFilter) |
e803df70 SM |
144 | |
145 | with self.assertRaises(bt2._Error) as ctx: | |
146 | graph.run() | |
147 | ||
148 | exc = ctx.exception | |
149 | cause = exc[0] | |
150 | ||
151 | self.assertIsInstance(cause, bt2._MessageIteratorErrorCause) | |
152 | self.assertEqual(cause.component_name, 'src') | |
153 | self.assertEqual(cause.component_output_port_name, 'out') | |
154 | self.assertIn('ValueError: Very bad error', cause.message) | |
155 | ||
811644b8 | 156 | def test_finalize(self): |
5602ef81 | 157 | class MyIter(bt2._UserMessageIterator): |
6a91742b | 158 | def _user_finalize(self): |
811644b8 PP |
159 | nonlocal finalized |
160 | finalized = True | |
161 | ||
cfbd7cf3 | 162 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
66964f3f | 163 | def __init__(self, params, obj): |
811644b8 PP |
164 | self._add_output_port('out') |
165 | ||
166 | finalized = False | |
0a6d7302 | 167 | graph = _create_graph(MySource, SimpleSink) |
c5f330cd | 168 | graph.run() |
811644b8 PP |
169 | del graph |
170 | self.assertTrue(finalized) | |
171 | ||
172 | def test_component(self): | |
5602ef81 | 173 | class MyIter(bt2._UserMessageIterator): |
c5f330cd | 174 | def __init__(self, self_port_output): |
811644b8 PP |
175 | nonlocal salut |
176 | salut = self._component._salut | |
177 | ||
cfbd7cf3 | 178 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
66964f3f | 179 | def __init__(self, params, obj): |
811644b8 PP |
180 | self._add_output_port('out') |
181 | self._salut = 23 | |
182 | ||
183 | salut = None | |
0a6d7302 | 184 | graph = _create_graph(MySource, SimpleSink) |
c5f330cd | 185 | graph.run() |
811644b8 PP |
186 | self.assertEqual(salut, 23) |
187 | ||
14503fb1 SM |
188 | def test_port(self): |
189 | class MyIter(bt2._UserMessageIterator): | |
190 | def __init__(self_iter, self_port_output): | |
191 | nonlocal called | |
192 | called = True | |
193 | port = self_iter._port | |
194 | self.assertIs(type(self_port_output), bt2_port._UserComponentOutputPort) | |
195 | self.assertIs(type(port), bt2_port._UserComponentOutputPort) | |
196 | self.assertEqual(self_port_output.addr, port.addr) | |
197 | ||
198 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): | |
199 | def __init__(self, params, obj): | |
200 | self._add_output_port('out') | |
201 | ||
202 | called = False | |
0a6d7302 | 203 | graph = _create_graph(MySource, SimpleSink) |
14503fb1 SM |
204 | graph.run() |
205 | self.assertTrue(called) | |
206 | ||
811644b8 | 207 | def test_addr(self): |
5602ef81 | 208 | class MyIter(bt2._UserMessageIterator): |
c5f330cd | 209 | def __init__(self, self_port_output): |
811644b8 PP |
210 | nonlocal addr |
211 | addr = self.addr | |
212 | ||
cfbd7cf3 | 213 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
66964f3f | 214 | def __init__(self, params, obj): |
811644b8 PP |
215 | self._add_output_port('out') |
216 | ||
217 | addr = None | |
0a6d7302 | 218 | graph = _create_graph(MySource, SimpleSink) |
c5f330cd | 219 | graph.run() |
811644b8 PP |
220 | self.assertIsNotNone(addr) |
221 | self.assertNotEqual(addr, 0) | |
222 | ||
d79a8353 SM |
223 | # Test that messages returned by _UserMessageIterator.__next__ remain valid |
224 | # and can be re-used. | |
225 | def test_reuse_message(self): | |
226 | class MyIter(bt2._UserMessageIterator): | |
227 | def __init__(self, port): | |
228 | tc, sc, ec = port.user_data | |
229 | trace = tc() | |
230 | stream = trace.create_stream(sc) | |
231 | packet = stream.create_packet() | |
232 | ||
233 | # This message will be returned twice by __next__. | |
234 | event_message = self._create_event_message(ec, packet) | |
235 | ||
236 | self._msgs = [ | |
237 | self._create_stream_beginning_message(stream), | |
d79a8353 SM |
238 | self._create_packet_beginning_message(packet), |
239 | event_message, | |
240 | event_message, | |
241 | ] | |
242 | ||
243 | def __next__(self): | |
244 | return self._msgs.pop(0) | |
245 | ||
246 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): | |
66964f3f | 247 | def __init__(self, params, obj): |
d79a8353 | 248 | tc = self._create_trace_class() |
26fc5aed | 249 | sc = tc.create_stream_class(supports_packets=True) |
d79a8353 SM |
250 | ec = sc.create_event_class() |
251 | self._add_output_port('out', (tc, sc, ec)) | |
252 | ||
253 | graph = bt2.Graph() | |
254 | src = graph.add_component(MySource, 'src') | |
6c373cc9 | 255 | it = TestOutputPortMessageIterator(graph, src.output_ports['out']) |
d79a8353 SM |
256 | |
257 | # Skip beginning messages. | |
188edac1 | 258 | msg = next(it) |
f0a42b33 | 259 | self.assertIs(type(msg), bt2._StreamBeginningMessageConst) |
188edac1 | 260 | msg = next(it) |
f0a42b33 | 261 | self.assertIs(type(msg), bt2._PacketBeginningMessageConst) |
d79a8353 SM |
262 | |
263 | msg_ev1 = next(it) | |
264 | msg_ev2 = next(it) | |
265 | ||
f0a42b33 FD |
266 | self.assertIs(type(msg_ev1), bt2._EventMessageConst) |
267 | self.assertIs(type(msg_ev2), bt2._EventMessageConst) | |
d79a8353 SM |
268 | self.assertEqual(msg_ev1.addr, msg_ev2.addr) |
269 | ||
0a6d7302 SM |
270 | # Try consuming many times from an iterator that always returns TryAgain. |
271 | # This verifies that we are not missing an incref of Py_None, making the | |
272 | # refcount of Py_None reach 0. | |
273 | def test_try_again_many_times(self): | |
274 | class MyIter(bt2._UserMessageIterator): | |
f00b8d40 | 275 | def __next__(self): |
0a6d7302 | 276 | raise bt2.TryAgain |
f00b8d40 | 277 | |
0a6d7302 | 278 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
66964f3f | 279 | def __init__(self, params, obj): |
0a6d7302 | 280 | self._add_output_port('out') |
f00b8d40 SM |
281 | |
282 | class MyFilterIter(bt2._UserMessageIterator): | |
283 | def __init__(self, port): | |
284 | input_port = port.user_data | |
ca02df0a PP |
285 | self._upstream_iter = self._create_input_port_message_iterator( |
286 | input_port | |
287 | ) | |
f00b8d40 SM |
288 | |
289 | def __next__(self): | |
290 | return next(self._upstream_iter) | |
291 | ||
6a91742b | 292 | def _user_seek_beginning(self): |
f00b8d40 SM |
293 | self._upstream_iter.seek_beginning() |
294 | ||
6a91742b | 295 | def _user_can_seek_beginning(self): |
14cfc8ce | 296 | return self._upstream_iter.can_seek_beginning() |
f00b8d40 SM |
297 | |
298 | class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): | |
66964f3f | 299 | def __init__(self, params, obj): |
f00b8d40 SM |
300 | input_port = self._add_input_port('in') |
301 | self._add_output_port('out', input_port) | |
302 | ||
f00b8d40 SM |
303 | graph = bt2.Graph() |
304 | src = graph.add_component(MySource, 'src') | |
0a6d7302 SM |
305 | it = TestOutputPortMessageIterator(graph, src.output_ports['out']) |
306 | ||
307 | # Three times the initial ref count of `None` iterations should | |
308 | # be enough to catch the bug even if there are small differences | |
309 | # between configurations. | |
310 | none_ref_count = sys.getrefcount(None) * 3 | |
311 | ||
312 | for i in range(none_ref_count): | |
313 | with self.assertRaises(bt2.TryAgain): | |
314 | next(it) | |
315 | ||
316 | ||
317 | def _setup_seek_test(sink_cls, user_seek_beginning=None, user_can_seek_beginning=None): | |
318 | class MySourceIter(bt2._UserMessageIterator): | |
319 | def __init__(self, port): | |
320 | tc, sc, ec = port.user_data | |
321 | trace = tc() | |
322 | stream = trace.create_stream(sc) | |
323 | packet = stream.create_packet() | |
324 | ||
325 | self._msgs = [ | |
326 | self._create_stream_beginning_message(stream), | |
327 | self._create_packet_beginning_message(packet), | |
328 | self._create_event_message(ec, packet), | |
329 | self._create_event_message(ec, packet), | |
330 | self._create_packet_end_message(packet), | |
331 | self._create_stream_end_message(stream), | |
332 | ] | |
333 | self._at = 0 | |
334 | ||
335 | def __next__(self): | |
336 | if self._at < len(self._msgs): | |
337 | msg = self._msgs[self._at] | |
338 | self._at += 1 | |
339 | return msg | |
340 | else: | |
341 | raise StopIteration | |
342 | ||
343 | if user_seek_beginning is not None: | |
344 | MySourceIter._user_seek_beginning = user_seek_beginning | |
345 | ||
346 | if user_can_seek_beginning is not None: | |
14cfc8ce | 347 | MySourceIter._user_can_seek_beginning = user_can_seek_beginning |
0a6d7302 SM |
348 | |
349 | class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter): | |
350 | def __init__(self, params, obj): | |
351 | tc = self._create_trace_class() | |
352 | sc = tc.create_stream_class(supports_packets=True) | |
353 | ec = sc.create_event_class() | |
354 | ||
355 | self._add_output_port('out', (tc, sc, ec)) | |
356 | ||
357 | class MyFilterIter(bt2._UserMessageIterator): | |
358 | def __init__(self, port): | |
359 | self._upstream_iter = self._create_input_port_message_iterator( | |
360 | self._component._input_ports['in'] | |
361 | ) | |
362 | ||
363 | def __next__(self): | |
364 | return next(self._upstream_iter) | |
365 | ||
0a6d7302 | 366 | def _user_can_seek_beginning(self): |
14cfc8ce | 367 | return self._upstream_iter.can_seek_beginning() |
0a6d7302 SM |
368 | |
369 | def _user_seek_beginning(self): | |
370 | self._upstream_iter.seek_beginning() | |
371 | ||
372 | class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): | |
373 | def __init__(self, params, obj): | |
374 | self._add_input_port('in') | |
375 | self._add_output_port('out') | |
376 | ||
377 | return _create_graph(MySource, sink_cls, flt_comp_cls=MyFilter) | |
f00b8d40 | 378 | |
0a6d7302 SM |
379 | |
380 | class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): | |
f00b8d40 | 381 | def test_can_seek_beginning(self): |
6c373cc9 PP |
382 | class MySink(bt2._UserSinkComponent): |
383 | def __init__(self, params, obj): | |
384 | self._add_input_port('in') | |
385 | ||
386 | def _user_graph_is_configured(self): | |
387 | self._msg_iter = self._create_input_port_message_iterator( | |
388 | self._input_ports['in'] | |
389 | ) | |
390 | ||
391 | def _user_consume(self): | |
392 | nonlocal can_seek_beginning | |
14cfc8ce | 393 | can_seek_beginning = self._msg_iter.can_seek_beginning() |
6c373cc9 | 394 | |
6a91742b | 395 | def _user_can_seek_beginning(self): |
6c373cc9 PP |
396 | nonlocal input_port_iter_can_seek_beginning |
397 | return input_port_iter_can_seek_beginning | |
f00b8d40 | 398 | |
0a6d7302 SM |
399 | graph = _setup_seek_test( |
400 | MySink, user_can_seek_beginning=_user_can_seek_beginning | |
401 | ) | |
f00b8d40 | 402 | |
6c373cc9 PP |
403 | input_port_iter_can_seek_beginning = True |
404 | can_seek_beginning = None | |
405 | graph.run_once() | |
7f0c21bb | 406 | self.assertIs(can_seek_beginning, True) |
f00b8d40 | 407 | |
6c373cc9 PP |
408 | input_port_iter_can_seek_beginning = False |
409 | can_seek_beginning = None | |
410 | graph.run_once() | |
7f0c21bb | 411 | self.assertIs(can_seek_beginning, False) |
f00b8d40 | 412 | |
0a6d7302 SM |
413 | def test_no_can_seek_beginning_with_seek_beginning(self): |
414 | # Test an iterator without a _user_can_seek_beginning method, but with | |
415 | # a _user_seek_beginning method. | |
416 | class MySink(bt2._UserSinkComponent): | |
417 | def __init__(self, params, obj): | |
418 | self._add_input_port('in') | |
419 | ||
420 | def _user_graph_is_configured(self): | |
421 | self._msg_iter = self._create_input_port_message_iterator( | |
422 | self._input_ports['in'] | |
423 | ) | |
424 | ||
425 | def _user_consume(self): | |
426 | nonlocal can_seek_beginning | |
14cfc8ce | 427 | can_seek_beginning = self._msg_iter.can_seek_beginning() |
0a6d7302 SM |
428 | |
429 | def _user_seek_beginning(self): | |
430 | pass | |
f00b8d40 | 431 | |
0a6d7302 | 432 | graph = _setup_seek_test(MySink, user_seek_beginning=_user_seek_beginning) |
6c373cc9 PP |
433 | can_seek_beginning = None |
434 | graph.run_once() | |
7f0c21bb | 435 | self.assertIs(can_seek_beginning, True) |
f00b8d40 | 436 | |
0a6d7302 SM |
437 | def test_no_can_seek_beginning(self): |
438 | # Test an iterator without a _user_can_seek_beginning method, without | |
439 | # a _user_seek_beginning method. | |
440 | class MySink(bt2._UserSinkComponent): | |
441 | def __init__(self, params, obj): | |
442 | self._add_input_port('in') | |
443 | ||
444 | def _user_graph_is_configured(self): | |
445 | self._msg_iter = self._create_input_port_message_iterator( | |
446 | self._input_ports['in'] | |
447 | ) | |
448 | ||
449 | def _user_consume(self): | |
450 | nonlocal can_seek_beginning | |
14cfc8ce | 451 | can_seek_beginning = self._msg_iter.can_seek_beginning() |
0a6d7302 SM |
452 | |
453 | graph = _setup_seek_test(MySink) | |
6c373cc9 PP |
454 | can_seek_beginning = None |
455 | graph.run_once() | |
7f0c21bb | 456 | self.assertIs(can_seek_beginning, False) |
f00b8d40 | 457 | |
f2fb1b32 SM |
458 | def test_can_seek_beginning_user_error(self): |
459 | class MySink(bt2._UserSinkComponent): | |
460 | def __init__(self, params, obj): | |
461 | self._add_input_port('in') | |
462 | ||
463 | def _user_graph_is_configured(self): | |
464 | self._msg_iter = self._create_input_port_message_iterator( | |
465 | self._input_ports['in'] | |
466 | ) | |
467 | ||
468 | def _user_consume(self): | |
469 | # This is expected to raise. | |
14cfc8ce | 470 | self._msg_iter.can_seek_beginning() |
f2fb1b32 SM |
471 | |
472 | def _user_can_seek_beginning(self): | |
473 | raise ValueError('moustiquaire') | |
474 | ||
475 | graph = _setup_seek_test( | |
476 | MySink, user_can_seek_beginning=_user_can_seek_beginning | |
477 | ) | |
478 | ||
479 | with self.assertRaises(bt2._Error) as ctx: | |
480 | graph.run_once() | |
481 | ||
482 | cause = ctx.exception[0] | |
483 | self.assertIn('ValueError: moustiquaire', cause.message) | |
484 | ||
485 | def test_can_seek_beginning_wrong_return_value(self): | |
486 | class MySink(bt2._UserSinkComponent): | |
487 | def __init__(self, params, obj): | |
488 | self._add_input_port('in') | |
489 | ||
490 | def _user_graph_is_configured(self): | |
491 | self._msg_iter = self._create_input_port_message_iterator( | |
492 | self._input_ports['in'] | |
493 | ) | |
494 | ||
495 | def _user_consume(self): | |
496 | # This is expected to raise. | |
14cfc8ce | 497 | self._msg_iter.can_seek_beginning() |
f2fb1b32 SM |
498 | |
499 | def _user_can_seek_beginning(self): | |
500 | return 'Amqui' | |
501 | ||
502 | graph = _setup_seek_test( | |
503 | MySink, user_can_seek_beginning=_user_can_seek_beginning | |
504 | ) | |
505 | ||
506 | with self.assertRaises(bt2._Error) as ctx: | |
507 | graph.run_once() | |
508 | ||
509 | cause = ctx.exception[0] | |
510 | self.assertIn("TypeError: 'str' is not a 'bool' object", cause.message) | |
511 | ||
f00b8d40 | 512 | def test_seek_beginning(self): |
6c373cc9 PP |
513 | class MySink(bt2._UserSinkComponent): |
514 | def __init__(self, params, obj): | |
515 | self._add_input_port('in') | |
f00b8d40 | 516 | |
6c373cc9 PP |
517 | def _user_graph_is_configured(self): |
518 | self._msg_iter = self._create_input_port_message_iterator( | |
519 | self._input_ports['in'] | |
520 | ) | |
521 | ||
522 | def _user_consume(self): | |
523 | nonlocal do_seek_beginning | |
524 | nonlocal msg | |
525 | ||
526 | if do_seek_beginning: | |
527 | self._msg_iter.seek_beginning() | |
528 | return | |
529 | ||
530 | msg = next(self._msg_iter) | |
531 | ||
0a6d7302 SM |
532 | def _user_seek_beginning(self): |
533 | self._at = 0 | |
534 | ||
6c373cc9 | 535 | msg = None |
0a6d7302 SM |
536 | graph = _setup_seek_test(MySink, user_seek_beginning=_user_seek_beginning) |
537 | ||
538 | # Consume message. | |
539 | do_seek_beginning = False | |
6c373cc9 | 540 | graph.run_once() |
f0a42b33 | 541 | self.assertIs(type(msg), bt2._StreamBeginningMessageConst) |
0a6d7302 SM |
542 | |
543 | # Consume message. | |
6c373cc9 | 544 | graph.run_once() |
f0a42b33 | 545 | self.assertIs(type(msg), bt2._PacketBeginningMessageConst) |
0a6d7302 SM |
546 | |
547 | # Seek beginning. | |
6c373cc9 PP |
548 | do_seek_beginning = True |
549 | graph.run_once() | |
0a6d7302 SM |
550 | |
551 | # Consume message. | |
6c373cc9 PP |
552 | do_seek_beginning = False |
553 | graph.run_once() | |
f0a42b33 | 554 | self.assertIs(type(msg), bt2._StreamBeginningMessageConst) |
f00b8d40 | 555 | |
6c373cc9 PP |
556 | def test_seek_beginning_user_error(self): |
557 | class MySink(bt2._UserSinkComponent): | |
558 | def __init__(self, params, obj): | |
559 | self._add_input_port('in') | |
f00b8d40 | 560 | |
6c373cc9 PP |
561 | def _user_graph_is_configured(self): |
562 | self._msg_iter = self._create_input_port_message_iterator( | |
563 | self._input_ports['in'] | |
564 | ) | |
f00b8d40 | 565 | |
6c373cc9 PP |
566 | def _user_consume(self): |
567 | self._msg_iter.seek_beginning() | |
f00b8d40 | 568 | |
0a6d7302 | 569 | def _user_seek_beginning(self): |
cfbd7cf3 | 570 | raise ValueError('ouch') |
f00b8d40 | 571 | |
0a6d7302 | 572 | graph = _setup_seek_test(MySink, user_seek_beginning=_user_seek_beginning) |
f00b8d40 | 573 | |
694c792b | 574 | with self.assertRaises(bt2._Error): |
6c373cc9 | 575 | graph.run_once() |
f00b8d40 SM |
576 | |
577 | ||
f00b8d40 SM |
578 | if __name__ == '__main__': |
579 | unittest.main() |