tests: make test_sink_self_port_user_data actually test a sink
[babeltrace.git] / tests / bindings / python / bt2 / test_graph.py
CommitLineData
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 19import unittest
811644b8
PP
20import bt2
21
22
5f25509b 23class _MyIter(bt2._UserMessageIterator):
8d8b141d 24 def __init__(self, config, self_output_port):
5f25509b
SM
25 self._build_meta()
26 self._at = 0
27
28 def _build_meta(self):
29 self._tc = self._component._create_trace_class()
30 self._t = self._tc()
26fc5aed 31 self._sc = self._tc.create_stream_class(supports_packets=True)
5f25509b
SM
32 self._ec = self._sc.create_event_class(name='salut')
33 self._my_int_ft = self._tc.create_signed_integer_field_class(32)
34 payload_ft = self._tc.create_structure_field_class()
cfbd7cf3 35 payload_ft += [('my_int', self._my_int_ft)]
5f25509b
SM
36 self._ec.payload_field_type = payload_ft
37 self._stream = self._t.create_stream(self._sc)
38 self._packet = self._stream.create_packet()
39
40 def _create_event(self, value):
41 ev = self._ec()
42 ev.payload_field['my_int'] = value
43 ev.packet = self._packet
44 return ev
45
46
811644b8
PP
47class GraphTestCase(unittest.TestCase):
48 def setUp(self):
49 self._graph = bt2.Graph()
50
51 def tearDown(self):
52 del self._graph
53
056deb59
PP
54 def test_create_default(self):
55 bt2.Graph()
56
57 def test_create_known_mip_version(self):
58 bt2.Graph(0)
59
60 def test_create_invalid_mip_version_type(self):
61 with self.assertRaises(TypeError):
62 bt2.Graph('')
63
64 def test_create_unknown_mip_version(self):
65 with self.assertRaisesRegex(ValueError, 'unknown MIP version'):
66 bt2.Graph(1)
811644b8 67
c513d240
SM
68 def test_default_interrupter(self):
69 interrupter = self._graph.default_interrupter
70 self.assertIs(type(interrupter), bt2.Interrupter)
71
811644b8
PP
72 def test_add_component_user_cls(self):
73 class MySink(bt2._UserSinkComponent):
6a91742b 74 def _user_consume(self):
a01b452b 75 pass
811644b8
PP
76
77 comp = self._graph.add_component(MySink, 'salut')
78 self.assertEqual(comp.name, 'salut')
79
80 def test_add_component_gen_cls(self):
81 class MySink(bt2._UserSinkComponent):
6a91742b 82 def _user_consume(self):
a01b452b 83 pass
811644b8
PP
84
85 comp = self._graph.add_component(MySink, 'salut')
5f25509b 86 assert comp
e8ac1aae 87 comp2 = self._graph.add_component(comp.cls, 'salut2')
811644b8
PP
88 self.assertEqual(comp2.name, 'salut2')
89
90 def test_add_component_params(self):
91 comp_params = None
92
93 class MySink(bt2._UserSinkComponent):
59225a3e 94 def __init__(self, config, params, obj):
811644b8
PP
95 nonlocal comp_params
96 comp_params = params
97
6a91742b 98 def _user_consume(self):
a01b452b 99 pass
811644b8
PP
100
101 params = {'hello': 23, 'path': '/path/to/stuff'}
082db648 102 self._graph.add_component(MySink, 'salut', params)
811644b8
PP
103 self.assertEqual(params, comp_params)
104 del comp_params
105
66964f3f
PP
106 def test_add_component_obj_python_comp_cls(self):
107 comp_obj = None
108
109 class MySink(bt2._UserSinkComponent):
59225a3e 110 def __init__(self, config, params, obj):
66964f3f
PP
111 nonlocal comp_obj
112 comp_obj = obj
113
114 def _user_consume(self):
115 pass
116
117 obj = object()
082db648 118 self._graph.add_component(MySink, 'salut', obj=obj)
66964f3f
PP
119 self.assertIs(comp_obj, obj)
120 del comp_obj
121
122 def test_add_component_obj_none_python_comp_cls(self):
123 comp_obj = None
124
125 class MySink(bt2._UserSinkComponent):
59225a3e 126 def __init__(self, config, params, obj):
66964f3f
PP
127 nonlocal comp_obj
128 comp_obj = obj
129
130 def _user_consume(self):
131 pass
132
082db648 133 self._graph.add_component(MySink, 'salut')
66964f3f
PP
134 self.assertIsNone(comp_obj)
135 del comp_obj
136
137 def test_add_component_obj_non_python_comp_cls(self):
66964f3f
PP
138 plugin = bt2.find_plugin('text', find_in_user_dir=False, find_in_sys_dir=False)
139 assert plugin is not None
140 cc = plugin.source_component_classes['dmesg']
141 assert cc is not None
142
143 with self.assertRaises(ValueError):
082db648 144 self._graph.add_component(cc, 'salut', obj=57)
66964f3f 145
811644b8
PP
146 def test_add_component_invalid_cls_type(self):
147 with self.assertRaises(TypeError):
148 self._graph.add_component(int, 'salut')
149
8ef46e79
PP
150 def test_add_component_invalid_logging_level_type(self):
151 class MySink(bt2._UserSinkComponent):
6a91742b 152 def _user_consume(self):
a01b452b 153 pass
8ef46e79
PP
154
155 with self.assertRaises(TypeError):
156 self._graph.add_component(MySink, 'salut', logging_level='yo')
157
158 def test_add_component_invalid_logging_level_value(self):
159 class MySink(bt2._UserSinkComponent):
6a91742b 160 def _user_consume(self):
a01b452b 161 pass
8ef46e79
PP
162
163 with self.assertRaises(ValueError):
164 self._graph.add_component(MySink, 'salut', logging_level=12345)
165
401b7022
FD
166 def test_add_component_invalid_params_type(self):
167 class MySink(bt2._UserSinkComponent):
168 def _user_consume(self):
169 pass
170
171 with self.assertRaises(TypeError):
172 self._graph.add_component(MySink, 'salut', params=12)
173
174 def test_add_component_params_dict(self):
175 params_obj = None
176
177 class MySink(bt2._UserSinkComponent):
178 def __init__(self, config, params, obj):
179 nonlocal params_obj
180 params_obj = params
181
182 def _user_consume(self):
183 pass
184
185 params = {'plage': 12312}
186 self._graph.add_component(MySink, 'salut', params=params)
187
188 # Check equality and not identity because `add_component()` method
189 # converts the Python `dict` to a `bt2.MapValue`.
190 self.assertEqual(params, params_obj)
191
192 def test_add_component_params_mapvalue(self):
193 params_obj = None
194
195 class MySink(bt2._UserSinkComponent):
196 def __init__(self, config, params, obj):
197 nonlocal params_obj
198 params_obj = params
199
200 def _user_consume(self):
201 pass
202
203 params = bt2.MapValue({'beachclub': '121'})
204 self._graph.add_component(MySink, 'salut', params=params)
205
206 self.assertEqual(params, params_obj)
207
8ef46e79
PP
208 def test_add_component_logging_level(self):
209 class MySink(bt2._UserSinkComponent):
6a91742b 210 def _user_consume(self):
a01b452b 211 pass
8ef46e79 212
cfbd7cf3
FD
213 comp = self._graph.add_component(
214 MySink, 'salut', logging_level=bt2.LoggingLevel.DEBUG
215 )
8ef46e79
PP
216 self.assertEqual(comp.logging_level, bt2.LoggingLevel.DEBUG)
217
811644b8 218 def test_connect_ports(self):
5602ef81 219 class MyIter(bt2._UserMessageIterator):
811644b8
PP
220 def __next__(self):
221 raise bt2.Stop
222
cfbd7cf3 223 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 224 def __init__(self, config, params, obj):
811644b8
PP
225 self._add_output_port('out')
226
227 class MySink(bt2._UserSinkComponent):
59225a3e 228 def __init__(self, config, params, obj):
811644b8
PP
229 self._add_input_port('in')
230
6a91742b 231 def _user_consume(self):
811644b8
PP
232 raise bt2.Stop
233
234 src = self._graph.add_component(MySource, 'src')
235 sink = self._graph.add_component(MySink, 'sink')
5f25509b 236
cfbd7cf3
FD
237 conn = self._graph.connect_ports(
238 src.output_ports['out'], sink.input_ports['in']
239 )
811644b8
PP
240 self.assertTrue(src.output_ports['out'].is_connected)
241 self.assertTrue(sink.input_ports['in'].is_connected)
89b5033a
FD
242 self.assertEqual(src.output_ports['out'].connection.addr, conn.addr)
243 self.assertEqual(sink.input_ports['in'].connection.addr, conn.addr)
811644b8
PP
244
245 def test_connect_ports_invalid_direction(self):
5602ef81 246 class MyIter(bt2._UserMessageIterator):
811644b8
PP
247 def __next__(self):
248 raise bt2.Stop
249
cfbd7cf3 250 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 251 def __init__(self, config, params, obj):
811644b8
PP
252 self._add_output_port('out')
253
254 class MySink(bt2._UserSinkComponent):
59225a3e 255 def __init__(self, config, params, obj):
811644b8
PP
256 self._add_input_port('in')
257
6a91742b 258 def _user_consume(self):
811644b8
PP
259 raise bt2.Stop
260
261 src = self._graph.add_component(MySource, 'src')
262 sink = self._graph.add_component(MySink, 'sink')
263
264 with self.assertRaises(TypeError):
082db648 265 self._graph.connect_ports(sink.input_ports['in'], src.output_ports['out'])
811644b8 266
9b4f9b42
PP
267 def test_add_interrupter(self):
268 class MyIter(bt2._UserMessageIterator):
269 def __next__(self):
270 raise TypeError
271
272 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 273 def __init__(self, config, params, obj):
9b4f9b42
PP
274 self._add_output_port('out')
275
276 class MySink(bt2._UserSinkComponent):
59225a3e 277 def __init__(self, config, params, obj):
9b4f9b42
PP
278 self._add_input_port('in')
279
6a91742b 280 def _user_consume(self):
9b4f9b42
PP
281 next(self._msg_iter)
282
6a91742b 283 def _user_graph_is_configured(self):
9b4f9b42
PP
284 self._msg_iter = self._create_input_port_message_iterator(
285 self._input_ports['in']
286 )
287
288 # add two interrupters, set one of them
289 interrupter1 = bt2.Interrupter()
290 interrupter2 = bt2.Interrupter()
291 self._graph.add_interrupter(interrupter1)
292 src = self._graph.add_component(MySource, 'src')
293 sink = self._graph.add_component(MySink, 'sink')
294 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
295 self._graph.add_interrupter(interrupter2)
296
297 with self.assertRaises(bt2._Error):
298 self._graph.run()
299
300 interrupter2.set()
301
302 with self.assertRaises(bt2.TryAgain):
303 self._graph.run()
304
305 interrupter2.reset()
306
307 with self.assertRaises(bt2._Error):
308 self._graph.run()
309
310 # Test that Graph.run() raises bt2.Interrupted if the graph gets
311 # interrupted during execution.
312 def test_interrupt_while_running(self):
5f25509b 313 class MyIter(_MyIter):
1d915789 314 def __next__(self):
5f25509b 315 return self._create_stream_beginning_message(self._stream)
1d915789 316
cfbd7cf3 317 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 318 def __init__(self, config, params, obj):
1d915789
PP
319 self._add_output_port('out')
320
321 class MySink(bt2._UserSinkComponent):
59225a3e 322 def __init__(self, config, params, obj):
1d915789
PP
323 self._add_input_port('in')
324
6a91742b 325 def _user_consume(self):
9b4f9b42 326 # Pretend that somebody asynchronously interrupted the graph.
5f25509b 327 nonlocal graph
c513d240 328 graph.default_interrupter.set()
5f25509b 329 return next(self._msg_iter)
1d915789 330
6a91742b 331 def _user_graph_is_configured(self):
ca02df0a
PP
332 self._msg_iter = self._create_input_port_message_iterator(
333 self._input_ports['in']
334 )
1d915789 335
9b4f9b42
PP
336 graph = self._graph
337 up = self._graph.add_component(MySource, 'down')
338 down = self._graph.add_component(MySink, 'up')
339 self._graph.connect_ports(up.output_ports['out'], down.input_ports['in'])
340
341 with self.assertRaises(bt2.TryAgain):
342 self._graph.run()
811644b8
PP
343
344 def test_run(self):
5f25509b 345 class MyIter(_MyIter):
811644b8 346 def __next__(self):
5f25509b
SM
347 if self._at == 9:
348 raise StopIteration
349
350 if self._at == 0:
351 msg = self._create_stream_beginning_message(self._stream)
352 elif self._at == 1:
353 msg = self._create_packet_beginning_message(self._packet)
354 elif self._at == 7:
355 msg = self._create_packet_end_message(self._packet)
356 elif self._at == 8:
357 msg = self._create_stream_end_message(self._stream)
358 else:
359 msg = self._create_event_message(self._ec, self._packet)
811644b8 360
811644b8 361 self._at += 1
5602ef81 362 return msg
811644b8 363
cfbd7cf3 364 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 365 def __init__(self, config, params, obj):
811644b8
PP
366 self._add_output_port('out')
367
368 class MySink(bt2._UserSinkComponent):
59225a3e 369 def __init__(self, config, params, obj):
5f25509b 370 self._input_port = self._add_input_port('in')
811644b8
PP
371 self._at = 0
372
6a91742b 373 def _user_consume(comp_self):
5602ef81 374 msg = next(comp_self._msg_iter)
811644b8
PP
375
376 if comp_self._at == 0:
f0a42b33 377 self.assertIs(type(msg), bt2._StreamBeginningMessageConst)
811644b8 378 elif comp_self._at == 1:
f0a42b33 379 self.assertIs(type(msg), bt2._PacketBeginningMessageConst)
811644b8 380 elif comp_self._at >= 2 and comp_self._at <= 6:
f0a42b33 381 self.assertIs(type(msg), bt2._EventMessageConst)
e8ac1aae 382 self.assertEqual(msg.event.cls.name, 'salut')
811644b8 383 elif comp_self._at == 7:
f0a42b33 384 self.assertIs(type(msg), bt2._PacketEndMessageConst)
811644b8 385 elif comp_self._at == 8:
f0a42b33 386 self.assertIs(type(msg), bt2._StreamEndMessageConst)
811644b8
PP
387
388 comp_self._at += 1
389
6a91742b 390 def _user_graph_is_configured(self):
ca02df0a
PP
391 self._msg_iter = self._create_input_port_message_iterator(
392 self._input_port
393 )
811644b8
PP
394
395 src = self._graph.add_component(MySource, 'src')
396 sink = self._graph.add_component(MySink, 'sink')
082db648 397 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
811644b8
PP
398 self._graph.run()
399
8cc0e6ea
PP
400 def test_run_once(self):
401 class MyIter(_MyIter):
402 pass
403
404 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 405 def __init__(self, config, params, obj):
8cc0e6ea
PP
406 self._add_output_port('out')
407
408 class MySink(bt2._UserSinkComponent):
59225a3e 409 def __init__(self, config, params, obj):
8cc0e6ea
PP
410 self._input_port = self._add_input_port('in')
411
412 def _user_consume(comp_self):
413 nonlocal run_count
414 run_count += 1
415 raise bt2.TryAgain
416
417 run_count = 0
418 src = self._graph.add_component(MySource, 'src')
419 sink = self._graph.add_component(MySink, 'sink')
082db648 420 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
8cc0e6ea
PP
421
422 with self.assertRaises(bt2.TryAgain):
423 self._graph.run_once()
424
425 self.assertEqual(run_count, 1)
426
427 def test_run_once_stops(self):
428 class MyIter(_MyIter):
429 pass
430
431 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 432 def __init__(self, config, params, obj):
8cc0e6ea
PP
433 self._add_output_port('out')
434
435 class MySink(bt2._UserSinkComponent):
59225a3e 436 def __init__(self, config, params, obj):
8cc0e6ea
PP
437 self._input_port = self._add_input_port('in')
438
439 def _user_consume(comp_self):
440 raise bt2.Stop
441
442 src = self._graph.add_component(MySource, 'src')
443 sink = self._graph.add_component(MySink, 'sink')
082db648 444 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
8cc0e6ea
PP
445
446 with self.assertRaises(bt2.Stop):
447 self._graph.run_once()
448
811644b8 449 def test_run_again(self):
5f25509b 450 class MyIter(_MyIter):
811644b8 451 def __next__(self):
5f25509b 452 if self._at == 3:
811644b8
PP
453 raise bt2.TryAgain
454
5f25509b
SM
455 if self._at == 0:
456 msg = self._create_stream_beginning_message(self._stream)
457 elif self._at == 1:
458 msg = self._create_packet_beginning_message(self._packet)
459 elif self._at == 2:
460 msg = self._create_event_message(self._ec, self._packet)
461
811644b8 462 self._at += 1
5602ef81 463 return msg
811644b8 464
cfbd7cf3 465 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 466 def __init__(self, config, params, obj):
811644b8
PP
467 self._add_output_port('out')
468
469 class MySink(bt2._UserSinkComponent):
59225a3e 470 def __init__(self, config, params, obj):
5f25509b 471 self._input_port = self._add_input_port('in')
811644b8
PP
472 self._at = 0
473
6a91742b 474 def _user_consume(comp_self):
5f25509b 475 msg = next(comp_self._msg_iter)
811644b8 476 if comp_self._at == 0:
f0a42b33 477 self.assertIs(type(msg), bt2._StreamBeginningMessageConst)
811644b8 478 elif comp_self._at == 1:
f0a42b33 479 self.assertIs(type(msg), bt2._PacketBeginningMessageConst)
5f25509b 480 elif comp_self._at == 2:
f0a42b33 481 self.assertIs(type(msg), bt2._EventMessageConst)
811644b8 482 raise bt2.TryAgain
5f25509b
SM
483 else:
484 pass
811644b8
PP
485
486 comp_self._at += 1
487
6a91742b 488 def _user_graph_is_configured(self):
ca02df0a
PP
489 self._msg_iter = self._create_input_port_message_iterator(
490 self._input_port
491 )
811644b8
PP
492
493 src = self._graph.add_component(MySource, 'src')
494 sink = self._graph.add_component(MySink, 'sink')
082db648 495 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
811644b8
PP
496
497 with self.assertRaises(bt2.TryAgain):
498 self._graph.run()
499
811644b8 500 def test_run_error(self):
5f25509b 501 raised_in_sink = False
811644b8 502
5f25509b 503 class MyIter(_MyIter):
811644b8 504 def __next__(self):
5f25509b
SM
505 # If this gets called after the sink raised an exception, it is
506 # an error.
507 nonlocal raised_in_sink
508 assert raised_in_sink is False
509
510 if self._at == 0:
511 msg = self._create_stream_beginning_message(self._stream)
512 elif self._at == 1:
513 msg = self._create_packet_beginning_message(self._packet)
514 elif self._at == 2 or self._at == 3:
515 msg = self._create_event_message(self._ec, self._packet)
516 else:
811644b8 517 raise bt2.TryAgain
811644b8 518 self._at += 1
5602ef81 519 return msg
811644b8 520
cfbd7cf3 521 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 522 def __init__(self, config, params, obj):
811644b8
PP
523 self._add_output_port('out')
524
525 class MySink(bt2._UserSinkComponent):
59225a3e 526 def __init__(self, config, params, obj):
5f25509b 527 self._input_port = self._add_input_port('in')
811644b8
PP
528 self._at = 0
529
6a91742b 530 def _user_consume(comp_self):
5f25509b 531 msg = next(comp_self._msg_iter)
811644b8 532 if comp_self._at == 0:
f0a42b33 533 self.assertIs(type(msg), bt2._StreamBeginningMessageConst)
811644b8 534 elif comp_self._at == 1:
f0a42b33 535 self.assertIs(type(msg), bt2._PacketBeginningMessageConst)
5f25509b 536 elif comp_self._at == 2:
f0a42b33 537 self.assertIs(type(msg), bt2._EventMessageConst)
5f25509b
SM
538 elif comp_self._at == 3:
539 nonlocal raised_in_sink
540 raised_in_sink = True
811644b8
PP
541 raise RuntimeError('error!')
542
543 comp_self._at += 1
544
6a91742b 545 def _user_graph_is_configured(self):
ca02df0a
PP
546 self._msg_iter = self._create_input_port_message_iterator(
547 self._input_port
548 )
811644b8
PP
549
550 src = self._graph.add_component(MySource, 'src')
551 sink = self._graph.add_component(MySink, 'sink')
082db648 552 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
811644b8 553
694c792b 554 with self.assertRaises(bt2._Error):
811644b8
PP
555 self._graph.run()
556
5f25509b 557 def test_listeners(self):
5602ef81 558 class MyIter(bt2._UserMessageIterator):
5f25509b
SM
559 def __next__(self):
560 raise bt2.Stop
1d915789 561
cfbd7cf3 562 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 563 def __init__(self, config, params, obj):
1d915789 564 self._add_output_port('out')
5f25509b 565 self._add_output_port('zero')
1d915789
PP
566
567 class MySink(bt2._UserSinkComponent):
59225a3e 568 def __init__(self, config, params, obj):
1d915789 569 self._add_input_port('in')
1d915789 570
6a91742b 571 def _user_consume(self):
5f25509b 572 raise bt2.Stop
1d915789 573
6a91742b 574 def _user_port_connected(self, port, other_port):
5f25509b 575 self._add_input_port('taste')
1d915789 576
5f25509b
SM
577 def port_added_listener(component, port):
578 nonlocal calls
579 calls.append((port_added_listener, component, port))
1d915789 580
5f25509b
SM
581 calls = []
582 self._graph.add_port_added_listener(port_added_listener)
1d915789
PP
583 src = self._graph.add_component(MySource, 'src')
584 sink = self._graph.add_component(MySink, 'sink')
cfbd7cf3 585 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
1d915789 586
2945edfe 587 self.assertEqual(len(calls), 4)
5f25509b
SM
588
589 self.assertIs(calls[0][0], port_added_listener)
590 self.assertEqual(calls[0][1].name, 'src')
591 self.assertEqual(calls[0][2].name, 'out')
592
593 self.assertIs(calls[1][0], port_added_listener)
594 self.assertEqual(calls[1][1].name, 'src')
595 self.assertEqual(calls[1][2].name, 'zero')
596
597 self.assertIs(calls[2][0], port_added_listener)
598 self.assertEqual(calls[2][1].name, 'sink')
599 self.assertEqual(calls[2][2].name, 'in')
600
601 self.assertIs(calls[3][0], port_added_listener)
602 self.assertEqual(calls[3][1].name, 'sink')
603 self.assertEqual(calls[3][2].name, 'taste')
604
5f25509b 605 def test_invalid_listeners(self):
5602ef81 606 class MyIter(bt2._UserMessageIterator):
811644b8
PP
607 def __next__(self):
608 raise bt2.Stop
609
cfbd7cf3 610 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
59225a3e 611 def __init__(self, config, params, obj):
811644b8
PP
612 self._add_output_port('out')
613 self._add_output_port('zero')
614
811644b8 615 class MySink(bt2._UserSinkComponent):
59225a3e 616 def __init__(self, config, params, obj):
811644b8
PP
617 self._add_input_port('in')
618
6a91742b 619 def _user_consume(self):
811644b8
PP
620 raise bt2.Stop
621
6a91742b 622 def _user_port_connected(self, port, other_port):
811644b8
PP
623 self._add_input_port('taste')
624
5f25509b
SM
625 with self.assertRaises(TypeError):
626 self._graph.add_port_added_listener(1234)
811644b8 627
5f25509b
SM
628 def test_raise_in_component_init(self):
629 class MySink(bt2._UserSinkComponent):
59225a3e 630 def __init__(self, config, params, obj):
5f25509b 631 raise ValueError('oops!')
811644b8 632
6a91742b 633 def _user_consume(self):
5f25509b
SM
634 raise bt2.Stop
635
636 graph = bt2.Graph()
637
694c792b 638 with self.assertRaises(bt2._Error):
5f25509b
SM
639 graph.add_component(MySink, 'comp')
640
641 def test_raise_in_port_added_listener(self):
642 class MySink(bt2._UserSinkComponent):
59225a3e 643 def __init__(self, config, params, obj):
5f25509b
SM
644 self._add_input_port('in')
645
6a91742b 646 def _user_consume(self):
5f25509b
SM
647 raise bt2.Stop
648
649 def port_added_listener(component, port):
650 raise ValueError('oh noes!')
651
652 graph = bt2.Graph()
653 graph.add_port_added_listener(port_added_listener)
654
694c792b 655 with self.assertRaises(bt2._Error):
5f25509b
SM
656 graph.add_component(MySink, 'comp')
657
d14ddbba
SM
658
659if __name__ == '__main__':
660 unittest.main()
This page took 0.080177 seconds and 4 git commands to generate.