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