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