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() | |
34 | self._sc = self._tc.create_stream_class() | |
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() | |
38 | payload_ft += collections.OrderedDict([ | |
39 | ('my_int', self._my_int_ft), | |
40 | ]) | |
41 | self._ec.payload_field_type = payload_ft | |
42 | self._stream = self._t.create_stream(self._sc) | |
43 | self._packet = self._stream.create_packet() | |
44 | ||
45 | def _create_event(self, value): | |
46 | ev = self._ec() | |
47 | ev.payload_field['my_int'] = value | |
48 | ev.packet = self._packet | |
49 | return ev | |
50 | ||
51 | ||
811644b8 PP |
52 | class GraphTestCase(unittest.TestCase): |
53 | def setUp(self): | |
54 | self._graph = bt2.Graph() | |
55 | ||
56 | def tearDown(self): | |
57 | del self._graph | |
58 | ||
59 | def test_create_empty(self): | |
60 | graph = bt2.Graph() | |
61 | ||
62 | def test_add_component_user_cls(self): | |
63 | class MySink(bt2._UserSinkComponent): | |
64 | def _consume(self): | |
65 | pass | |
66 | ||
67 | comp = self._graph.add_component(MySink, 'salut') | |
68 | self.assertEqual(comp.name, 'salut') | |
69 | ||
70 | def test_add_component_gen_cls(self): | |
71 | class MySink(bt2._UserSinkComponent): | |
72 | def _consume(self): | |
73 | pass | |
74 | ||
75 | comp = self._graph.add_component(MySink, 'salut') | |
5f25509b | 76 | assert comp |
e8ac1aae | 77 | comp2 = self._graph.add_component(comp.cls, 'salut2') |
811644b8 PP |
78 | self.assertEqual(comp2.name, 'salut2') |
79 | ||
80 | def test_add_component_params(self): | |
81 | comp_params = None | |
82 | ||
83 | class MySink(bt2._UserSinkComponent): | |
84 | def __init__(self, params): | |
85 | nonlocal comp_params | |
86 | comp_params = params | |
87 | ||
88 | def _consume(self): | |
89 | pass | |
90 | ||
91 | params = {'hello': 23, 'path': '/path/to/stuff'} | |
92 | comp = self._graph.add_component(MySink, 'salut', params) | |
93 | self.assertEqual(params, comp_params) | |
94 | del comp_params | |
95 | ||
96 | def test_add_component_invalid_cls_type(self): | |
97 | with self.assertRaises(TypeError): | |
98 | self._graph.add_component(int, 'salut') | |
99 | ||
8ef46e79 PP |
100 | def test_add_component_invalid_logging_level_type(self): |
101 | class MySink(bt2._UserSinkComponent): | |
102 | def _consume(self): | |
103 | pass | |
104 | ||
105 | with self.assertRaises(TypeError): | |
106 | self._graph.add_component(MySink, 'salut', logging_level='yo') | |
107 | ||
108 | def test_add_component_invalid_logging_level_value(self): | |
109 | class MySink(bt2._UserSinkComponent): | |
110 | def _consume(self): | |
111 | pass | |
112 | ||
113 | with self.assertRaises(ValueError): | |
114 | self._graph.add_component(MySink, 'salut', logging_level=12345) | |
115 | ||
116 | def test_add_component_logging_level(self): | |
117 | class MySink(bt2._UserSinkComponent): | |
118 | def _consume(self): | |
119 | pass | |
120 | ||
121 | comp = self._graph.add_component(MySink, 'salut', | |
122 | logging_level=bt2.LoggingLevel.DEBUG) | |
123 | self.assertEqual(comp.logging_level, bt2.LoggingLevel.DEBUG) | |
124 | ||
811644b8 | 125 | def test_connect_ports(self): |
5602ef81 | 126 | class MyIter(bt2._UserMessageIterator): |
811644b8 PP |
127 | def __next__(self): |
128 | raise bt2.Stop | |
129 | ||
130 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 131 | message_iterator_class=MyIter): |
811644b8 PP |
132 | def __init__(self, params): |
133 | self._add_output_port('out') | |
134 | ||
135 | class MySink(bt2._UserSinkComponent): | |
136 | def __init__(self, params): | |
137 | self._add_input_port('in') | |
138 | ||
139 | def _consume(self): | |
140 | raise bt2.Stop | |
141 | ||
142 | src = self._graph.add_component(MySource, 'src') | |
143 | sink = self._graph.add_component(MySink, 'sink') | |
5f25509b | 144 | |
811644b8 PP |
145 | conn = self._graph.connect_ports(src.output_ports['out'], |
146 | sink.input_ports['in']) | |
147 | self.assertTrue(src.output_ports['out'].is_connected) | |
148 | self.assertTrue(sink.input_ports['in'].is_connected) | |
89b5033a FD |
149 | self.assertEqual(src.output_ports['out'].connection.addr, conn.addr) |
150 | self.assertEqual(sink.input_ports['in'].connection.addr, conn.addr) | |
811644b8 PP |
151 | |
152 | def test_connect_ports_invalid_direction(self): | |
5602ef81 | 153 | class MyIter(bt2._UserMessageIterator): |
811644b8 PP |
154 | def __next__(self): |
155 | raise bt2.Stop | |
156 | ||
157 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 158 | message_iterator_class=MyIter): |
811644b8 PP |
159 | def __init__(self, params): |
160 | self._add_output_port('out') | |
161 | ||
162 | class MySink(bt2._UserSinkComponent): | |
163 | def __init__(self, params): | |
164 | self._add_input_port('in') | |
165 | ||
166 | def _consume(self): | |
167 | raise bt2.Stop | |
168 | ||
169 | src = self._graph.add_component(MySource, 'src') | |
170 | sink = self._graph.add_component(MySink, 'sink') | |
171 | ||
172 | with self.assertRaises(TypeError): | |
173 | conn = self._graph.connect_ports(sink.input_ports['in'], | |
174 | src.output_ports['out']) | |
175 | ||
5f25509b SM |
176 | def test_cancel(self): |
177 | self.assertFalse(self._graph.is_canceled) | |
811644b8 | 178 | self._graph.cancel() |
5f25509b | 179 | self.assertTrue(self._graph.is_canceled) |
811644b8 | 180 | |
d24d5663 | 181 | # Test that Graph.run() raises bt2.Canceled if the graph gets canceled |
5f25509b SM |
182 | # during execution. |
183 | def test_cancel_while_running(self): | |
184 | class MyIter(_MyIter): | |
1d915789 | 185 | def __next__(self): |
5f25509b | 186 | return self._create_stream_beginning_message(self._stream) |
1d915789 PP |
187 | |
188 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 189 | message_iterator_class=MyIter): |
1d915789 PP |
190 | def __init__(self, params): |
191 | self._add_output_port('out') | |
192 | ||
193 | class MySink(bt2._UserSinkComponent): | |
194 | def __init__(self, params): | |
195 | self._add_input_port('in') | |
196 | ||
197 | def _consume(self): | |
5f25509b SM |
198 | # Pretend that somebody asynchronously cancelled the graph. |
199 | nonlocal graph | |
200 | graph.cancel() | |
1d915789 | 201 | |
5f25509b | 202 | return next(self._msg_iter) |
1d915789 | 203 | |
5f25509b SM |
204 | def _graph_is_configured(self): |
205 | self._msg_iter = self._input_ports['in'].create_message_iterator() | |
1d915789 | 206 | |
5f25509b SM |
207 | graph = bt2.Graph() |
208 | up = graph.add_component(MySource, 'down') | |
209 | down = graph.add_component(MySink, 'up') | |
210 | graph.connect_ports(up.output_ports['out'], down.input_ports['in']) | |
d24d5663 | 211 | with self.assertRaises(bt2.Canceled): |
5f25509b | 212 | graph.run() |
811644b8 PP |
213 | |
214 | def test_run(self): | |
5f25509b | 215 | class MyIter(_MyIter): |
811644b8 | 216 | def __next__(self): |
5f25509b SM |
217 | if self._at == 9: |
218 | raise StopIteration | |
219 | ||
220 | if self._at == 0: | |
221 | msg = self._create_stream_beginning_message(self._stream) | |
222 | elif self._at == 1: | |
223 | msg = self._create_packet_beginning_message(self._packet) | |
224 | elif self._at == 7: | |
225 | msg = self._create_packet_end_message(self._packet) | |
226 | elif self._at == 8: | |
227 | msg = self._create_stream_end_message(self._stream) | |
228 | else: | |
229 | msg = self._create_event_message(self._ec, self._packet) | |
811644b8 | 230 | |
811644b8 | 231 | self._at += 1 |
5602ef81 | 232 | return msg |
811644b8 PP |
233 | |
234 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 235 | message_iterator_class=MyIter): |
811644b8 PP |
236 | def __init__(self, params): |
237 | self._add_output_port('out') | |
238 | ||
239 | class MySink(bt2._UserSinkComponent): | |
240 | def __init__(self, params): | |
5f25509b | 241 | self._input_port = self._add_input_port('in') |
811644b8 PP |
242 | self._at = 0 |
243 | ||
244 | def _consume(comp_self): | |
5602ef81 | 245 | msg = next(comp_self._msg_iter) |
811644b8 PP |
246 | |
247 | if comp_self._at == 0: | |
5f25509b | 248 | self.assertIsInstance(msg, bt2.message._StreamBeginningMessage) |
811644b8 | 249 | elif comp_self._at == 1: |
5f25509b | 250 | self.assertIsInstance(msg, bt2.message._PacketBeginningMessage) |
811644b8 | 251 | elif comp_self._at >= 2 and comp_self._at <= 6: |
5f25509b | 252 | self.assertIsInstance(msg, bt2.message._EventMessage) |
e8ac1aae | 253 | self.assertEqual(msg.event.cls.name, 'salut') |
811644b8 | 254 | elif comp_self._at == 7: |
5f25509b | 255 | self.assertIsInstance(msg, bt2.message._PacketEndMessage) |
811644b8 | 256 | elif comp_self._at == 8: |
5f25509b | 257 | self.assertIsInstance(msg, bt2.message._StreamEndMessage) |
811644b8 PP |
258 | |
259 | comp_self._at += 1 | |
260 | ||
5f25509b SM |
261 | def _graph_is_configured(self): |
262 | self._msg_iter = self._input_port.create_message_iterator() | |
811644b8 PP |
263 | |
264 | src = self._graph.add_component(MySource, 'src') | |
265 | sink = self._graph.add_component(MySink, 'sink') | |
266 | conn = self._graph.connect_ports(src.output_ports['out'], | |
267 | sink.input_ports['in']) | |
268 | self._graph.run() | |
269 | ||
270 | def test_run_again(self): | |
5f25509b | 271 | class MyIter(_MyIter): |
811644b8 | 272 | def __next__(self): |
5f25509b | 273 | if self._at == 3: |
811644b8 PP |
274 | raise bt2.TryAgain |
275 | ||
5f25509b SM |
276 | if self._at == 0: |
277 | msg = self._create_stream_beginning_message(self._stream) | |
278 | elif self._at == 1: | |
279 | msg = self._create_packet_beginning_message(self._packet) | |
280 | elif self._at == 2: | |
281 | msg = self._create_event_message(self._ec, self._packet) | |
282 | ||
811644b8 | 283 | self._at += 1 |
5602ef81 | 284 | return msg |
811644b8 PP |
285 | |
286 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 287 | message_iterator_class=MyIter): |
811644b8 PP |
288 | def __init__(self, params): |
289 | self._add_output_port('out') | |
290 | ||
291 | class MySink(bt2._UserSinkComponent): | |
292 | def __init__(self, params): | |
5f25509b | 293 | self._input_port = self._add_input_port('in') |
811644b8 PP |
294 | self._at = 0 |
295 | ||
296 | def _consume(comp_self): | |
5f25509b | 297 | msg = next(comp_self._msg_iter) |
811644b8 | 298 | if comp_self._at == 0: |
5f25509b | 299 | self.assertIsInstance(msg, bt2.message._StreamBeginningMessage) |
811644b8 | 300 | elif comp_self._at == 1: |
5f25509b SM |
301 | self.assertIsInstance(msg, bt2.message._PacketBeginningMessage) |
302 | elif comp_self._at == 2: | |
303 | self.assertIsInstance(msg, bt2.message._EventMessage) | |
811644b8 | 304 | raise bt2.TryAgain |
5f25509b SM |
305 | else: |
306 | pass | |
811644b8 PP |
307 | |
308 | comp_self._at += 1 | |
309 | ||
5f25509b SM |
310 | def _graph_is_configured(self): |
311 | self._msg_iter = self._input_port.create_message_iterator() | |
811644b8 PP |
312 | |
313 | src = self._graph.add_component(MySource, 'src') | |
314 | sink = self._graph.add_component(MySink, 'sink') | |
315 | conn = self._graph.connect_ports(src.output_ports['out'], | |
316 | sink.input_ports['in']) | |
317 | ||
318 | with self.assertRaises(bt2.TryAgain): | |
319 | self._graph.run() | |
320 | ||
811644b8 | 321 | def test_run_error(self): |
5f25509b | 322 | raised_in_sink = False |
811644b8 | 323 | |
5f25509b | 324 | class MyIter(_MyIter): |
811644b8 | 325 | def __next__(self): |
5f25509b SM |
326 | # If this gets called after the sink raised an exception, it is |
327 | # an error. | |
328 | nonlocal raised_in_sink | |
329 | assert raised_in_sink is False | |
330 | ||
331 | if self._at == 0: | |
332 | msg = self._create_stream_beginning_message(self._stream) | |
333 | elif self._at == 1: | |
334 | msg = self._create_packet_beginning_message(self._packet) | |
335 | elif self._at == 2 or self._at == 3: | |
336 | msg = self._create_event_message(self._ec, self._packet) | |
337 | else: | |
811644b8 | 338 | raise bt2.TryAgain |
811644b8 | 339 | self._at += 1 |
5602ef81 | 340 | return msg |
811644b8 PP |
341 | |
342 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 343 | message_iterator_class=MyIter): |
811644b8 PP |
344 | def __init__(self, params): |
345 | self._add_output_port('out') | |
346 | ||
347 | class MySink(bt2._UserSinkComponent): | |
348 | def __init__(self, params): | |
5f25509b | 349 | self._input_port = self._add_input_port('in') |
811644b8 PP |
350 | self._at = 0 |
351 | ||
352 | def _consume(comp_self): | |
5f25509b | 353 | msg = next(comp_self._msg_iter) |
811644b8 | 354 | if comp_self._at == 0: |
5f25509b | 355 | self.assertIsInstance(msg, bt2.message._StreamBeginningMessage) |
811644b8 | 356 | elif comp_self._at == 1: |
5f25509b SM |
357 | self.assertIsInstance(msg, bt2.message._PacketBeginningMessage) |
358 | elif comp_self._at == 2: | |
359 | self.assertIsInstance(msg, bt2.message._EventMessage) | |
360 | elif comp_self._at == 3: | |
361 | nonlocal raised_in_sink | |
362 | raised_in_sink = True | |
811644b8 PP |
363 | raise RuntimeError('error!') |
364 | ||
365 | comp_self._at += 1 | |
366 | ||
5f25509b SM |
367 | def _graph_is_configured(self): |
368 | self._msg_iter = self._input_port.create_message_iterator() | |
811644b8 PP |
369 | |
370 | src = self._graph.add_component(MySource, 'src') | |
371 | sink = self._graph.add_component(MySink, 'sink') | |
372 | conn = self._graph.connect_ports(src.output_ports['out'], | |
373 | sink.input_ports['in']) | |
374 | ||
375 | with self.assertRaises(bt2.Error): | |
376 | self._graph.run() | |
377 | ||
5f25509b | 378 | def test_listeners(self): |
5602ef81 | 379 | class MyIter(bt2._UserMessageIterator): |
5f25509b SM |
380 | def __next__(self): |
381 | raise bt2.Stop | |
1d915789 PP |
382 | |
383 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 384 | message_iterator_class=MyIter): |
1d915789 PP |
385 | def __init__(self, params): |
386 | self._add_output_port('out') | |
5f25509b | 387 | self._add_output_port('zero') |
1d915789 PP |
388 | |
389 | class MySink(bt2._UserSinkComponent): | |
390 | def __init__(self, params): | |
391 | self._add_input_port('in') | |
1d915789 | 392 | |
5f25509b SM |
393 | def _consume(self): |
394 | raise bt2.Stop | |
1d915789 | 395 | |
5f25509b SM |
396 | def _port_connected(self, port, other_port): |
397 | self._add_input_port('taste') | |
1d915789 | 398 | |
5f25509b SM |
399 | def port_added_listener(component, port): |
400 | nonlocal calls | |
401 | calls.append((port_added_listener, component, port)) | |
1d915789 | 402 | |
5f25509b SM |
403 | def ports_connected_listener(upstream_component, upstream_port, |
404 | downstream_component, downstream_port): | |
405 | nonlocal calls | |
406 | calls.append((ports_connected_listener, | |
407 | upstream_component, upstream_port, | |
408 | downstream_component, downstream_port)) | |
409 | ||
410 | calls = [] | |
411 | self._graph.add_port_added_listener(port_added_listener) | |
412 | self._graph.add_ports_connected_listener(ports_connected_listener) | |
1d915789 PP |
413 | src = self._graph.add_component(MySource, 'src') |
414 | sink = self._graph.add_component(MySink, 'sink') | |
5f25509b SM |
415 | self._graph.connect_ports(src.output_ports['out'], |
416 | sink.input_ports['in']) | |
1d915789 | 417 | |
5f25509b SM |
418 | self.assertEqual(len(calls), 5) |
419 | ||
420 | self.assertIs(calls[0][0], port_added_listener) | |
421 | self.assertEqual(calls[0][1].name, 'src') | |
422 | self.assertEqual(calls[0][2].name, 'out') | |
423 | ||
424 | self.assertIs(calls[1][0], port_added_listener) | |
425 | self.assertEqual(calls[1][1].name, 'src') | |
426 | self.assertEqual(calls[1][2].name, 'zero') | |
427 | ||
428 | self.assertIs(calls[2][0], port_added_listener) | |
429 | self.assertEqual(calls[2][1].name, 'sink') | |
430 | self.assertEqual(calls[2][2].name, 'in') | |
431 | ||
432 | self.assertIs(calls[3][0], port_added_listener) | |
433 | self.assertEqual(calls[3][1].name, 'sink') | |
434 | self.assertEqual(calls[3][2].name, 'taste') | |
435 | ||
436 | self.assertIs(calls[4][0], ports_connected_listener) | |
437 | self.assertEqual(calls[4][1].name, 'src') | |
438 | self.assertEqual(calls[4][2].name, 'out') | |
439 | self.assertEqual(calls[4][3].name, 'sink') | |
440 | self.assertEqual(calls[4][4].name, 'in') | |
441 | ||
442 | def test_invalid_listeners(self): | |
5602ef81 | 443 | class MyIter(bt2._UserMessageIterator): |
811644b8 PP |
444 | def __next__(self): |
445 | raise bt2.Stop | |
446 | ||
447 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 448 | message_iterator_class=MyIter): |
811644b8 PP |
449 | def __init__(self, params): |
450 | self._add_output_port('out') | |
451 | self._add_output_port('zero') | |
452 | ||
811644b8 PP |
453 | class MySink(bt2._UserSinkComponent): |
454 | def __init__(self, params): | |
455 | self._add_input_port('in') | |
456 | ||
457 | def _consume(self): | |
458 | raise bt2.Stop | |
459 | ||
460 | def _port_connected(self, port, other_port): | |
461 | self._add_input_port('taste') | |
462 | ||
5f25509b SM |
463 | with self.assertRaises(TypeError): |
464 | self._graph.add_port_added_listener(1234) | |
465 | with self.assertRaises(TypeError): | |
466 | self._graph.add_ports_connected_listener(1234) | |
811644b8 | 467 | |
5f25509b SM |
468 | def test_raise_in_component_init(self): |
469 | class MySink(bt2._UserSinkComponent): | |
470 | def __init__(self, params): | |
471 | raise ValueError('oops!') | |
811644b8 | 472 | |
5f25509b SM |
473 | def _consume(self): |
474 | raise bt2.Stop | |
475 | ||
476 | graph = bt2.Graph() | |
477 | ||
478 | with self.assertRaises(bt2.Error): | |
479 | graph.add_component(MySink, 'comp') | |
480 | ||
481 | def test_raise_in_port_added_listener(self): | |
482 | class MySink(bt2._UserSinkComponent): | |
483 | def __init__(self, params): | |
484 | self._add_input_port('in') | |
485 | ||
486 | def _consume(self): | |
487 | raise bt2.Stop | |
488 | ||
489 | def port_added_listener(component, port): | |
490 | raise ValueError('oh noes!') | |
491 | ||
492 | graph = bt2.Graph() | |
493 | graph.add_port_added_listener(port_added_listener) | |
494 | ||
495 | with self.assertRaises(bt2.Error): | |
496 | graph.add_component(MySink, 'comp') | |
497 | ||
498 | def test_raise_in_ports_connected_listener(self): | |
499 | class MyIter(bt2._UserMessageIterator): | |
500 | def __next__(self): | |
501 | raise bt2.Stop | |
502 | ||
503 | class MySource(bt2._UserSourceComponent, | |
504 | message_iterator_class=MyIter): | |
505 | def __init__(self, params): | |
506 | self._add_output_port('out') | |
507 | ||
508 | class MySink(bt2._UserSinkComponent): | |
509 | def __init__(self, params): | |
510 | self._add_input_port('in') | |
511 | ||
512 | def _consume(self): | |
513 | raise bt2.Stop | |
811644b8 | 514 | |
c5f330cd SM |
515 | def ports_connected_listener(upstream_component, upstream_port, |
516 | downstream_component, downstream_port): | |
5f25509b | 517 | raise ValueError('oh noes!') |
811644b8 | 518 | |
5f25509b SM |
519 | graph = bt2.Graph() |
520 | graph.add_ports_connected_listener(ports_connected_listener) | |
521 | up = graph.add_component(MySource, 'down') | |
522 | down = graph.add_component(MySink, 'up') | |
811644b8 | 523 | |
5f25509b SM |
524 | with self.assertRaises(bt2.Error): |
525 | graph.connect_ports(up.output_ports['out'], down.input_ports['in']) |