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