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