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