Commit | Line | Data |
---|---|---|
32d2d479 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 | ||
f6a5e476 | 19 | import unittest |
f6a5e476 PP |
20 | import bt2 |
21 | ||
22 | ||
871a292a | 23 | class _MyIter(bt2._UserMessageIterator): |
9415de1c | 24 | def __init__(self, config, self_output_port): |
871a292a 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() | |
37a93d41 | 31 | self._sc = self._tc.create_stream_class(supports_packets=True) |
871a292a 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() | |
61d96b89 | 35 | payload_ft += [('my_int', self._my_int_ft)] |
871a292a 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 | ||
f6a5e476 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 | ||
253a9ecc 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) | |
f6a5e476 | 67 | |
f09b3ae9 SM |
68 | def test_default_interrupter(self): |
69 | interrupter = self._graph.default_interrupter | |
70 | self.assertIs(type(interrupter), bt2.Interrupter) | |
71 | ||
f6a5e476 PP |
72 | def test_add_component_user_cls(self): |
73 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 74 | def _user_consume(self): |
8a08af82 | 75 | pass |
f6a5e476 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): | |
819d0ae7 | 82 | def _user_consume(self): |
8a08af82 | 83 | pass |
f6a5e476 PP |
84 | |
85 | comp = self._graph.add_component(MySink, 'salut') | |
871a292a | 86 | assert comp |
c88be1c8 | 87 | comp2 = self._graph.add_component(comp.cls, 'salut2') |
f6a5e476 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): | |
e3250e61 | 94 | def __init__(self, config, params, obj): |
f6a5e476 PP |
95 | nonlocal comp_params |
96 | comp_params = params | |
97 | ||
819d0ae7 | 98 | def _user_consume(self): |
8a08af82 | 99 | pass |
f6a5e476 PP |
100 | |
101 | params = {'hello': 23, 'path': '/path/to/stuff'} | |
36153ada | 102 | self._graph.add_component(MySink, 'salut', params) |
f6a5e476 PP |
103 | self.assertEqual(params, comp_params) |
104 | del comp_params | |
105 | ||
b20382e2 PP |
106 | def test_add_component_obj_python_comp_cls(self): |
107 | comp_obj = None | |
108 | ||
109 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 110 | def __init__(self, config, params, obj): |
b20382e2 PP |
111 | nonlocal comp_obj |
112 | comp_obj = obj | |
113 | ||
114 | def _user_consume(self): | |
115 | pass | |
116 | ||
117 | obj = object() | |
36153ada | 118 | self._graph.add_component(MySink, 'salut', obj=obj) |
b20382e2 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): | |
e3250e61 | 126 | def __init__(self, config, params, obj): |
b20382e2 PP |
127 | nonlocal comp_obj |
128 | comp_obj = obj | |
129 | ||
130 | def _user_consume(self): | |
131 | pass | |
132 | ||
36153ada | 133 | self._graph.add_component(MySink, 'salut') |
b20382e2 PP |
134 | self.assertIsNone(comp_obj) |
135 | del comp_obj | |
136 | ||
137 | def test_add_component_obj_non_python_comp_cls(self): | |
b20382e2 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): | |
36153ada | 144 | self._graph.add_component(cc, 'salut', obj=57) |
b20382e2 | 145 | |
f6a5e476 PP |
146 | def test_add_component_invalid_cls_type(self): |
147 | with self.assertRaises(TypeError): | |
148 | self._graph.add_component(int, 'salut') | |
149 | ||
78c432bb PP |
150 | def test_add_component_invalid_logging_level_type(self): |
151 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 152 | def _user_consume(self): |
8a08af82 | 153 | pass |
78c432bb 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): | |
819d0ae7 | 160 | def _user_consume(self): |
8a08af82 | 161 | pass |
78c432bb PP |
162 | |
163 | with self.assertRaises(ValueError): | |
164 | self._graph.add_component(MySink, 'salut', logging_level=12345) | |
165 | ||
3e03e5e3 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 | ||
78c432bb PP |
208 | def test_add_component_logging_level(self): |
209 | class MySink(bt2._UserSinkComponent): | |
819d0ae7 | 210 | def _user_consume(self): |
8a08af82 | 211 | pass |
78c432bb | 212 | |
61d96b89 FD |
213 | comp = self._graph.add_component( |
214 | MySink, 'salut', logging_level=bt2.LoggingLevel.DEBUG | |
215 | ) | |
78c432bb PP |
216 | self.assertEqual(comp.logging_level, bt2.LoggingLevel.DEBUG) |
217 | ||
f6a5e476 | 218 | def test_connect_ports(self): |
a797a36f SM |
219 | class MySource( |
220 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator | |
221 | ): | |
e3250e61 | 222 | def __init__(self, config, params, obj): |
f6a5e476 PP |
223 | self._add_output_port('out') |
224 | ||
225 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 226 | def __init__(self, config, params, obj): |
f6a5e476 PP |
227 | self._add_input_port('in') |
228 | ||
819d0ae7 | 229 | def _user_consume(self): |
f6a5e476 PP |
230 | raise bt2.Stop |
231 | ||
232 | src = self._graph.add_component(MySource, 'src') | |
233 | sink = self._graph.add_component(MySink, 'sink') | |
871a292a | 234 | |
61d96b89 FD |
235 | conn = self._graph.connect_ports( |
236 | src.output_ports['out'], sink.input_ports['in'] | |
237 | ) | |
f6a5e476 PP |
238 | self.assertTrue(src.output_ports['out'].is_connected) |
239 | self.assertTrue(sink.input_ports['in'].is_connected) | |
3e2cc5dc FD |
240 | self.assertEqual(src.output_ports['out'].connection.addr, conn.addr) |
241 | self.assertEqual(sink.input_ports['in'].connection.addr, conn.addr) | |
f6a5e476 PP |
242 | |
243 | def test_connect_ports_invalid_direction(self): | |
a797a36f SM |
244 | class MySource( |
245 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator | |
246 | ): | |
e3250e61 | 247 | def __init__(self, config, params, obj): |
f6a5e476 PP |
248 | self._add_output_port('out') |
249 | ||
250 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 251 | def __init__(self, config, params, obj): |
f6a5e476 PP |
252 | self._add_input_port('in') |
253 | ||
819d0ae7 | 254 | def _user_consume(self): |
f6a5e476 PP |
255 | raise bt2.Stop |
256 | ||
257 | src = self._graph.add_component(MySource, 'src') | |
258 | sink = self._graph.add_component(MySink, 'sink') | |
259 | ||
260 | with self.assertRaises(TypeError): | |
36153ada | 261 | self._graph.connect_ports(sink.input_ports['in'], src.output_ports['out']) |
f6a5e476 | 262 | |
d73bb381 PP |
263 | def test_add_interrupter(self): |
264 | class MyIter(bt2._UserMessageIterator): | |
265 | def __next__(self): | |
266 | raise TypeError | |
267 | ||
268 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): | |
e3250e61 | 269 | def __init__(self, config, params, obj): |
d73bb381 PP |
270 | self._add_output_port('out') |
271 | ||
272 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 273 | def __init__(self, config, params, obj): |
d73bb381 PP |
274 | self._add_input_port('in') |
275 | ||
819d0ae7 | 276 | def _user_consume(self): |
d73bb381 PP |
277 | next(self._msg_iter) |
278 | ||
819d0ae7 | 279 | def _user_graph_is_configured(self): |
fbd8a4e0 | 280 | self._msg_iter = self._create_message_iterator(self._input_ports['in']) |
d73bb381 PP |
281 | |
282 | # add two interrupters, set one of them | |
283 | interrupter1 = bt2.Interrupter() | |
284 | interrupter2 = bt2.Interrupter() | |
285 | self._graph.add_interrupter(interrupter1) | |
286 | src = self._graph.add_component(MySource, 'src') | |
287 | sink = self._graph.add_component(MySink, 'sink') | |
288 | self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) | |
289 | self._graph.add_interrupter(interrupter2) | |
290 | ||
291 | with self.assertRaises(bt2._Error): | |
292 | self._graph.run() | |
293 | ||
294 | interrupter2.set() | |
295 | ||
296 | with self.assertRaises(bt2.TryAgain): | |
297 | self._graph.run() | |
298 | ||
299 | interrupter2.reset() | |
300 | ||
301 | with self.assertRaises(bt2._Error): | |
302 | self._graph.run() | |
303 | ||
304 | # Test that Graph.run() raises bt2.Interrupted if the graph gets | |
305 | # interrupted during execution. | |
306 | def test_interrupt_while_running(self): | |
871a292a | 307 | class MyIter(_MyIter): |
9ef22b36 | 308 | def __next__(self): |
871a292a | 309 | return self._create_stream_beginning_message(self._stream) |
9ef22b36 | 310 | |
61d96b89 | 311 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
e3250e61 | 312 | def __init__(self, config, params, obj): |
9ef22b36 PP |
313 | self._add_output_port('out') |
314 | ||
315 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 316 | def __init__(self, config, params, obj): |
9ef22b36 PP |
317 | self._add_input_port('in') |
318 | ||
819d0ae7 | 319 | def _user_consume(self): |
d73bb381 | 320 | # Pretend that somebody asynchronously interrupted the graph. |
871a292a | 321 | nonlocal graph |
f09b3ae9 | 322 | graph.default_interrupter.set() |
871a292a | 323 | return next(self._msg_iter) |
9ef22b36 | 324 | |
819d0ae7 | 325 | def _user_graph_is_configured(self): |
fbd8a4e0 | 326 | self._msg_iter = self._create_message_iterator(self._input_ports['in']) |
9ef22b36 | 327 | |
d73bb381 PP |
328 | graph = self._graph |
329 | up = self._graph.add_component(MySource, 'down') | |
330 | down = self._graph.add_component(MySink, 'up') | |
331 | self._graph.connect_ports(up.output_ports['out'], down.input_ports['in']) | |
332 | ||
333 | with self.assertRaises(bt2.TryAgain): | |
334 | self._graph.run() | |
f6a5e476 PP |
335 | |
336 | def test_run(self): | |
871a292a | 337 | class MyIter(_MyIter): |
f6a5e476 | 338 | def __next__(self): |
871a292a SM |
339 | if self._at == 9: |
340 | raise StopIteration | |
341 | ||
342 | if self._at == 0: | |
343 | msg = self._create_stream_beginning_message(self._stream) | |
344 | elif self._at == 1: | |
345 | msg = self._create_packet_beginning_message(self._packet) | |
346 | elif self._at == 7: | |
347 | msg = self._create_packet_end_message(self._packet) | |
348 | elif self._at == 8: | |
349 | msg = self._create_stream_end_message(self._stream) | |
350 | else: | |
351 | msg = self._create_event_message(self._ec, self._packet) | |
f6a5e476 | 352 | |
f6a5e476 | 353 | self._at += 1 |
fa4c33e3 | 354 | return msg |
f6a5e476 | 355 | |
61d96b89 | 356 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
e3250e61 | 357 | def __init__(self, config, params, obj): |
f6a5e476 PP |
358 | self._add_output_port('out') |
359 | ||
360 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 361 | def __init__(self, config, params, obj): |
871a292a | 362 | self._input_port = self._add_input_port('in') |
f6a5e476 PP |
363 | self._at = 0 |
364 | ||
819d0ae7 | 365 | def _user_consume(comp_self): |
fa4c33e3 | 366 | msg = next(comp_self._msg_iter) |
f6a5e476 PP |
367 | |
368 | if comp_self._at == 0: | |
9cbe0c59 | 369 | self.assertIs(type(msg), bt2._StreamBeginningMessageConst) |
f6a5e476 | 370 | elif comp_self._at == 1: |
9cbe0c59 | 371 | self.assertIs(type(msg), bt2._PacketBeginningMessageConst) |
f6a5e476 | 372 | elif comp_self._at >= 2 and comp_self._at <= 6: |
9cbe0c59 | 373 | self.assertIs(type(msg), bt2._EventMessageConst) |
c88be1c8 | 374 | self.assertEqual(msg.event.cls.name, 'salut') |
f6a5e476 | 375 | elif comp_self._at == 7: |
9cbe0c59 | 376 | self.assertIs(type(msg), bt2._PacketEndMessageConst) |
f6a5e476 | 377 | elif comp_self._at == 8: |
9cbe0c59 | 378 | self.assertIs(type(msg), bt2._StreamEndMessageConst) |
f6a5e476 PP |
379 | |
380 | comp_self._at += 1 | |
381 | ||
819d0ae7 | 382 | def _user_graph_is_configured(self): |
fbd8a4e0 | 383 | self._msg_iter = self._create_message_iterator(self._input_port) |
f6a5e476 PP |
384 | |
385 | src = self._graph.add_component(MySource, 'src') | |
386 | sink = self._graph.add_component(MySink, 'sink') | |
36153ada | 387 | self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
f6a5e476 PP |
388 | self._graph.run() |
389 | ||
22b4de6a PP |
390 | def test_run_once(self): |
391 | class MyIter(_MyIter): | |
392 | pass | |
393 | ||
394 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): | |
e3250e61 | 395 | def __init__(self, config, params, obj): |
22b4de6a PP |
396 | self._add_output_port('out') |
397 | ||
398 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 399 | def __init__(self, config, params, obj): |
22b4de6a PP |
400 | self._input_port = self._add_input_port('in') |
401 | ||
402 | def _user_consume(comp_self): | |
403 | nonlocal run_count | |
404 | run_count += 1 | |
405 | raise bt2.TryAgain | |
406 | ||
407 | run_count = 0 | |
408 | src = self._graph.add_component(MySource, 'src') | |
409 | sink = self._graph.add_component(MySink, 'sink') | |
36153ada | 410 | self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
22b4de6a PP |
411 | |
412 | with self.assertRaises(bt2.TryAgain): | |
413 | self._graph.run_once() | |
414 | ||
415 | self.assertEqual(run_count, 1) | |
416 | ||
417 | def test_run_once_stops(self): | |
418 | class MyIter(_MyIter): | |
419 | pass | |
420 | ||
421 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): | |
e3250e61 | 422 | def __init__(self, config, params, obj): |
22b4de6a PP |
423 | self._add_output_port('out') |
424 | ||
425 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 426 | def __init__(self, config, params, obj): |
22b4de6a PP |
427 | self._input_port = self._add_input_port('in') |
428 | ||
429 | def _user_consume(comp_self): | |
430 | raise bt2.Stop | |
431 | ||
432 | src = self._graph.add_component(MySource, 'src') | |
433 | sink = self._graph.add_component(MySink, 'sink') | |
36153ada | 434 | self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
22b4de6a PP |
435 | |
436 | with self.assertRaises(bt2.Stop): | |
437 | self._graph.run_once() | |
438 | ||
f6a5e476 | 439 | def test_run_again(self): |
871a292a | 440 | class MyIter(_MyIter): |
f6a5e476 | 441 | def __next__(self): |
871a292a | 442 | if self._at == 3: |
f6a5e476 PP |
443 | raise bt2.TryAgain |
444 | ||
871a292a SM |
445 | if self._at == 0: |
446 | msg = self._create_stream_beginning_message(self._stream) | |
447 | elif self._at == 1: | |
448 | msg = self._create_packet_beginning_message(self._packet) | |
449 | elif self._at == 2: | |
450 | msg = self._create_event_message(self._ec, self._packet) | |
451 | ||
f6a5e476 | 452 | self._at += 1 |
fa4c33e3 | 453 | return msg |
f6a5e476 | 454 | |
61d96b89 | 455 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
e3250e61 | 456 | def __init__(self, config, params, obj): |
f6a5e476 PP |
457 | self._add_output_port('out') |
458 | ||
459 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 460 | def __init__(self, config, params, obj): |
871a292a | 461 | self._input_port = self._add_input_port('in') |
f6a5e476 PP |
462 | self._at = 0 |
463 | ||
819d0ae7 | 464 | def _user_consume(comp_self): |
871a292a | 465 | msg = next(comp_self._msg_iter) |
f6a5e476 | 466 | if comp_self._at == 0: |
9cbe0c59 | 467 | self.assertIs(type(msg), bt2._StreamBeginningMessageConst) |
f6a5e476 | 468 | elif comp_self._at == 1: |
9cbe0c59 | 469 | self.assertIs(type(msg), bt2._PacketBeginningMessageConst) |
871a292a | 470 | elif comp_self._at == 2: |
9cbe0c59 | 471 | self.assertIs(type(msg), bt2._EventMessageConst) |
f6a5e476 | 472 | raise bt2.TryAgain |
871a292a SM |
473 | else: |
474 | pass | |
f6a5e476 PP |
475 | |
476 | comp_self._at += 1 | |
477 | ||
819d0ae7 | 478 | def _user_graph_is_configured(self): |
fbd8a4e0 | 479 | self._msg_iter = self._create_message_iterator(self._input_port) |
f6a5e476 PP |
480 | |
481 | src = self._graph.add_component(MySource, 'src') | |
482 | sink = self._graph.add_component(MySink, 'sink') | |
36153ada | 483 | self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
f6a5e476 PP |
484 | |
485 | with self.assertRaises(bt2.TryAgain): | |
486 | self._graph.run() | |
487 | ||
f6a5e476 | 488 | def test_run_error(self): |
871a292a | 489 | raised_in_sink = False |
f6a5e476 | 490 | |
871a292a | 491 | class MyIter(_MyIter): |
f6a5e476 | 492 | def __next__(self): |
871a292a SM |
493 | # If this gets called after the sink raised an exception, it is |
494 | # an error. | |
495 | nonlocal raised_in_sink | |
496 | assert raised_in_sink is False | |
497 | ||
498 | if self._at == 0: | |
499 | msg = self._create_stream_beginning_message(self._stream) | |
500 | elif self._at == 1: | |
501 | msg = self._create_packet_beginning_message(self._packet) | |
502 | elif self._at == 2 or self._at == 3: | |
503 | msg = self._create_event_message(self._ec, self._packet) | |
504 | else: | |
f6a5e476 | 505 | raise bt2.TryAgain |
f6a5e476 | 506 | self._at += 1 |
fa4c33e3 | 507 | return msg |
f6a5e476 | 508 | |
61d96b89 | 509 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
e3250e61 | 510 | def __init__(self, config, params, obj): |
f6a5e476 PP |
511 | self._add_output_port('out') |
512 | ||
513 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 514 | def __init__(self, config, params, obj): |
871a292a | 515 | self._input_port = self._add_input_port('in') |
f6a5e476 PP |
516 | self._at = 0 |
517 | ||
819d0ae7 | 518 | def _user_consume(comp_self): |
871a292a | 519 | msg = next(comp_self._msg_iter) |
f6a5e476 | 520 | if comp_self._at == 0: |
9cbe0c59 | 521 | self.assertIs(type(msg), bt2._StreamBeginningMessageConst) |
f6a5e476 | 522 | elif comp_self._at == 1: |
9cbe0c59 | 523 | self.assertIs(type(msg), bt2._PacketBeginningMessageConst) |
871a292a | 524 | elif comp_self._at == 2: |
9cbe0c59 | 525 | self.assertIs(type(msg), bt2._EventMessageConst) |
871a292a SM |
526 | elif comp_self._at == 3: |
527 | nonlocal raised_in_sink | |
528 | raised_in_sink = True | |
f6a5e476 PP |
529 | raise RuntimeError('error!') |
530 | ||
531 | comp_self._at += 1 | |
532 | ||
819d0ae7 | 533 | def _user_graph_is_configured(self): |
fbd8a4e0 | 534 | self._msg_iter = self._create_message_iterator(self._input_port) |
f6a5e476 PP |
535 | |
536 | src = self._graph.add_component(MySource, 'src') | |
537 | sink = self._graph.add_component(MySink, 'sink') | |
36153ada | 538 | self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
f6a5e476 | 539 | |
614743a5 | 540 | with self.assertRaises(bt2._Error): |
f6a5e476 PP |
541 | self._graph.run() |
542 | ||
871a292a | 543 | def test_listeners(self): |
a797a36f SM |
544 | class MySource( |
545 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator | |
546 | ): | |
e3250e61 | 547 | def __init__(self, config, params, obj): |
9ef22b36 | 548 | self._add_output_port('out') |
871a292a | 549 | self._add_output_port('zero') |
9ef22b36 PP |
550 | |
551 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 552 | def __init__(self, config, params, obj): |
9ef22b36 | 553 | self._add_input_port('in') |
9ef22b36 | 554 | |
819d0ae7 | 555 | def _user_consume(self): |
871a292a | 556 | raise bt2.Stop |
9ef22b36 | 557 | |
819d0ae7 | 558 | def _user_port_connected(self, port, other_port): |
871a292a | 559 | self._add_input_port('taste') |
9ef22b36 | 560 | |
871a292a SM |
561 | def port_added_listener(component, port): |
562 | nonlocal calls | |
563 | calls.append((port_added_listener, component, port)) | |
9ef22b36 | 564 | |
871a292a SM |
565 | calls = [] |
566 | self._graph.add_port_added_listener(port_added_listener) | |
9ef22b36 PP |
567 | src = self._graph.add_component(MySource, 'src') |
568 | sink = self._graph.add_component(MySink, 'sink') | |
61d96b89 | 569 | self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
9ef22b36 | 570 | |
d9d764cc | 571 | self.assertEqual(len(calls), 4) |
871a292a SM |
572 | |
573 | self.assertIs(calls[0][0], port_added_listener) | |
574 | self.assertEqual(calls[0][1].name, 'src') | |
575 | self.assertEqual(calls[0][2].name, 'out') | |
576 | ||
577 | self.assertIs(calls[1][0], port_added_listener) | |
578 | self.assertEqual(calls[1][1].name, 'src') | |
579 | self.assertEqual(calls[1][2].name, 'zero') | |
580 | ||
581 | self.assertIs(calls[2][0], port_added_listener) | |
582 | self.assertEqual(calls[2][1].name, 'sink') | |
583 | self.assertEqual(calls[2][2].name, 'in') | |
584 | ||
585 | self.assertIs(calls[3][0], port_added_listener) | |
586 | self.assertEqual(calls[3][1].name, 'sink') | |
587 | self.assertEqual(calls[3][2].name, 'taste') | |
588 | ||
871a292a | 589 | def test_invalid_listeners(self): |
a797a36f SM |
590 | class MySource( |
591 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator | |
592 | ): | |
e3250e61 | 593 | def __init__(self, config, params, obj): |
f6a5e476 PP |
594 | self._add_output_port('out') |
595 | self._add_output_port('zero') | |
596 | ||
f6a5e476 | 597 | class MySink(bt2._UserSinkComponent): |
e3250e61 | 598 | def __init__(self, config, params, obj): |
f6a5e476 PP |
599 | self._add_input_port('in') |
600 | ||
819d0ae7 | 601 | def _user_consume(self): |
f6a5e476 PP |
602 | raise bt2.Stop |
603 | ||
819d0ae7 | 604 | def _user_port_connected(self, port, other_port): |
f6a5e476 PP |
605 | self._add_input_port('taste') |
606 | ||
871a292a SM |
607 | with self.assertRaises(TypeError): |
608 | self._graph.add_port_added_listener(1234) | |
f6a5e476 | 609 | |
871a292a SM |
610 | def test_raise_in_component_init(self): |
611 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 612 | def __init__(self, config, params, obj): |
871a292a | 613 | raise ValueError('oops!') |
f6a5e476 | 614 | |
819d0ae7 | 615 | def _user_consume(self): |
871a292a SM |
616 | raise bt2.Stop |
617 | ||
618 | graph = bt2.Graph() | |
619 | ||
614743a5 | 620 | with self.assertRaises(bt2._Error): |
871a292a SM |
621 | graph.add_component(MySink, 'comp') |
622 | ||
623 | def test_raise_in_port_added_listener(self): | |
624 | class MySink(bt2._UserSinkComponent): | |
e3250e61 | 625 | def __init__(self, config, params, obj): |
871a292a SM |
626 | self._add_input_port('in') |
627 | ||
819d0ae7 | 628 | def _user_consume(self): |
871a292a SM |
629 | raise bt2.Stop |
630 | ||
631 | def port_added_listener(component, port): | |
632 | raise ValueError('oh noes!') | |
633 | ||
634 | graph = bt2.Graph() | |
635 | graph.add_port_added_listener(port_added_listener) | |
636 | ||
614743a5 | 637 | with self.assertRaises(bt2._Error): |
871a292a SM |
638 | graph.add_component(MySink, 'comp') |
639 | ||
3db06b1d SM |
640 | |
641 | if __name__ == '__main__': | |
642 | unittest.main() |