bt2: fix some whitespace issues
[babeltrace.git] / tests / bindings / python / bt2 / test_graph.py
CommitLineData
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
ae0bfae8 19from bt2 import value
f6a5e476
PP
20import collections
21import unittest
22import copy
23import bt2
24
25
871a292a 26class _MyIter(bt2._UserMessageIterator):
a4dcfa96 27 def __init__(self, self_output_port):
871a292a
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()
37a93d41 34 self._sc = self._tc.create_stream_class(supports_packets=True)
871a292a
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()
61d96b89 38 payload_ft += [('my_int', self._my_int_ft)]
871a292a
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
f6a5e476
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
61d96b89 64
8a08af82
SM
65 def _graph_is_configured(self):
66 pass
f6a5e476
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
61d96b89 75
8a08af82
SM
76 def _graph_is_configured(self):
77 pass
f6a5e476
PP
78
79 comp = self._graph.add_component(MySink, 'salut')
871a292a 80 assert comp
c88be1c8 81 comp2 = self._graph.add_component(comp.cls, 'salut2')
f6a5e476
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
61d96b89 94
8a08af82
SM
95 def _graph_is_configured(self):
96 pass
f6a5e476
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
78c432bb
PP
107 def test_add_component_invalid_logging_level_type(self):
108 class MySink(bt2._UserSinkComponent):
109 def _consume(self):
110 pass
61d96b89 111
8a08af82
SM
112 def _graph_is_configured(self):
113 pass
78c432bb
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
61d96b89 122
8a08af82
SM
123 def _graph_is_configured(self):
124 pass
78c432bb
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
61d96b89 133
8a08af82
SM
134 def _graph_is_configured(self):
135 pass
78c432bb 136
61d96b89
FD
137 comp = self._graph.add_component(
138 MySink, 'salut', logging_level=bt2.LoggingLevel.DEBUG
139 )
78c432bb
PP
140 self.assertEqual(comp.logging_level, bt2.LoggingLevel.DEBUG)
141
f6a5e476 142 def test_connect_ports(self):
fa4c33e3 143 class MyIter(bt2._UserMessageIterator):
f6a5e476
PP
144 def __next__(self):
145 raise bt2.Stop
146
61d96b89 147 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
f6a5e476
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
8a08af82
SM
158 def _graph_is_configured(self):
159 pass
160
f6a5e476
PP
161 src = self._graph.add_component(MySource, 'src')
162 sink = self._graph.add_component(MySink, 'sink')
871a292a 163
61d96b89
FD
164 conn = self._graph.connect_ports(
165 src.output_ports['out'], sink.input_ports['in']
166 )
f6a5e476
PP
167 self.assertTrue(src.output_ports['out'].is_connected)
168 self.assertTrue(sink.input_ports['in'].is_connected)
3e2cc5dc
FD
169 self.assertEqual(src.output_ports['out'].connection.addr, conn.addr)
170 self.assertEqual(sink.input_ports['in'].connection.addr, conn.addr)
f6a5e476
PP
171
172 def test_connect_ports_invalid_direction(self):
fa4c33e3 173 class MyIter(bt2._UserMessageIterator):
f6a5e476
PP
174 def __next__(self):
175 raise bt2.Stop
176
61d96b89 177 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
f6a5e476
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
8a08af82
SM
188 def _graph_is_configured(self):
189 pass
190
f6a5e476
PP
191 src = self._graph.add_component(MySource, 'src')
192 sink = self._graph.add_component(MySink, 'sink')
193
194 with self.assertRaises(TypeError):
61d96b89
FD
195 conn = self._graph.connect_ports(
196 sink.input_ports['in'], src.output_ports['out']
197 )
f6a5e476 198
871a292a
SM
199 def test_cancel(self):
200 self.assertFalse(self._graph.is_canceled)
f6a5e476 201 self._graph.cancel()
871a292a 202 self.assertTrue(self._graph.is_canceled)
f6a5e476 203
fb25b9e3 204 # Test that Graph.run() raises bt2.Canceled if the graph gets canceled
871a292a
SM
205 # during execution.
206 def test_cancel_while_running(self):
207 class MyIter(_MyIter):
9ef22b36 208 def __next__(self):
871a292a 209 return self._create_stream_beginning_message(self._stream)
9ef22b36 210
61d96b89 211 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
9ef22b36
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):
871a292a
SM
220 # Pretend that somebody asynchronously cancelled the graph.
221 nonlocal graph
222 graph.cancel()
9ef22b36 223
871a292a 224 return next(self._msg_iter)
9ef22b36 225
871a292a
SM
226 def _graph_is_configured(self):
227 self._msg_iter = self._input_ports['in'].create_message_iterator()
9ef22b36 228
871a292a
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'])
fb25b9e3 233 with self.assertRaises(bt2.Canceled):
871a292a 234 graph.run()
f6a5e476
PP
235
236 def test_run(self):
871a292a 237 class MyIter(_MyIter):
f6a5e476 238 def __next__(self):
871a292a
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)
f6a5e476 252
f6a5e476 253 self._at += 1
fa4c33e3 254 return msg
f6a5e476 255
61d96b89 256 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
f6a5e476
PP
257 def __init__(self, params):
258 self._add_output_port('out')
259
260 class MySink(bt2._UserSinkComponent):
261 def __init__(self, params):
871a292a 262 self._input_port = self._add_input_port('in')
f6a5e476
PP
263 self._at = 0
264
265 def _consume(comp_self):
fa4c33e3 266 msg = next(comp_self._msg_iter)
f6a5e476
PP
267
268 if comp_self._at == 0:
871a292a 269 self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
f6a5e476 270 elif comp_self._at == 1:
871a292a 271 self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
f6a5e476 272 elif comp_self._at >= 2 and comp_self._at <= 6:
871a292a 273 self.assertIsInstance(msg, bt2.message._EventMessage)
c88be1c8 274 self.assertEqual(msg.event.cls.name, 'salut')
f6a5e476 275 elif comp_self._at == 7:
871a292a 276 self.assertIsInstance(msg, bt2.message._PacketEndMessage)
f6a5e476 277 elif comp_self._at == 8:
871a292a 278 self.assertIsInstance(msg, bt2.message._StreamEndMessage)
f6a5e476
PP
279
280 comp_self._at += 1
281
871a292a
SM
282 def _graph_is_configured(self):
283 self._msg_iter = self._input_port.create_message_iterator()
f6a5e476
PP
284
285 src = self._graph.add_component(MySource, 'src')
286 sink = self._graph.add_component(MySink, 'sink')
61d96b89
FD
287 conn = self._graph.connect_ports(
288 src.output_ports['out'], sink.input_ports['in']
289 )
f6a5e476
PP
290 self._graph.run()
291
292 def test_run_again(self):
871a292a 293 class MyIter(_MyIter):
f6a5e476 294 def __next__(self):
871a292a 295 if self._at == 3:
f6a5e476
PP
296 raise bt2.TryAgain
297
871a292a
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
f6a5e476 305 self._at += 1
fa4c33e3 306 return msg
f6a5e476 307
61d96b89 308 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
f6a5e476
PP
309 def __init__(self, params):
310 self._add_output_port('out')
311
312 class MySink(bt2._UserSinkComponent):
313 def __init__(self, params):
871a292a 314 self._input_port = self._add_input_port('in')
f6a5e476
PP
315 self._at = 0
316
317 def _consume(comp_self):
871a292a 318 msg = next(comp_self._msg_iter)
f6a5e476 319 if comp_self._at == 0:
871a292a 320 self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
f6a5e476 321 elif comp_self._at == 1:
871a292a
SM
322 self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
323 elif comp_self._at == 2:
324 self.assertIsInstance(msg, bt2.message._EventMessage)
f6a5e476 325 raise bt2.TryAgain
871a292a
SM
326 else:
327 pass
f6a5e476
PP
328
329 comp_self._at += 1
330
871a292a
SM
331 def _graph_is_configured(self):
332 self._msg_iter = self._input_port.create_message_iterator()
f6a5e476
PP
333
334 src = self._graph.add_component(MySource, 'src')
335 sink = self._graph.add_component(MySink, 'sink')
61d96b89
FD
336 conn = self._graph.connect_ports(
337 src.output_ports['out'], sink.input_ports['in']
338 )
f6a5e476
PP
339
340 with self.assertRaises(bt2.TryAgain):
341 self._graph.run()
342
f6a5e476 343 def test_run_error(self):
871a292a 344 raised_in_sink = False
f6a5e476 345
871a292a 346 class MyIter(_MyIter):
f6a5e476 347 def __next__(self):
871a292a
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:
f6a5e476 360 raise bt2.TryAgain
f6a5e476 361 self._at += 1
fa4c33e3 362 return msg
f6a5e476 363
61d96b89 364 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
f6a5e476
PP
365 def __init__(self, params):
366 self._add_output_port('out')
367
368 class MySink(bt2._UserSinkComponent):
369 def __init__(self, params):
871a292a 370 self._input_port = self._add_input_port('in')
f6a5e476
PP
371 self._at = 0
372
373 def _consume(comp_self):
871a292a 374 msg = next(comp_self._msg_iter)
f6a5e476 375 if comp_self._at == 0:
871a292a 376 self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
f6a5e476 377 elif comp_self._at == 1:
871a292a
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
f6a5e476
PP
384 raise RuntimeError('error!')
385
386 comp_self._at += 1
387
871a292a
SM
388 def _graph_is_configured(self):
389 self._msg_iter = self._input_port.create_message_iterator()
f6a5e476
PP
390
391 src = self._graph.add_component(MySource, 'src')
392 sink = self._graph.add_component(MySink, 'sink')
61d96b89
FD
393 conn = self._graph.connect_ports(
394 src.output_ports['out'], sink.input_ports['in']
395 )
f6a5e476 396
614743a5 397 with self.assertRaises(bt2._Error):
f6a5e476
PP
398 self._graph.run()
399
871a292a 400 def test_listeners(self):
fa4c33e3 401 class MyIter(bt2._UserMessageIterator):
871a292a
SM
402 def __next__(self):
403 raise bt2.Stop
9ef22b36 404
61d96b89 405 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
9ef22b36
PP
406 def __init__(self, params):
407 self._add_output_port('out')
871a292a 408 self._add_output_port('zero')
9ef22b36
PP
409
410 class MySink(bt2._UserSinkComponent):
411 def __init__(self, params):
412 self._add_input_port('in')
9ef22b36 413
871a292a
SM
414 def _consume(self):
415 raise bt2.Stop
9ef22b36 416
8a08af82
SM
417 def _graph_is_configured(self):
418 pass
419
871a292a
SM
420 def _port_connected(self, port, other_port):
421 self._add_input_port('taste')
9ef22b36 422
871a292a
SM
423 def port_added_listener(component, port):
424 nonlocal calls
425 calls.append((port_added_listener, component, port))
9ef22b36 426
61d96b89
FD
427 def ports_connected_listener(
428 upstream_component, upstream_port, downstream_component, downstream_port
429 ):
871a292a 430 nonlocal calls
61d96b89
FD
431 calls.append(
432 (
433 ports_connected_listener,
434 upstream_component,
435 upstream_port,
436 downstream_component,
437 downstream_port,
438 )
439 )
871a292a
SM
440
441 calls = []
442 self._graph.add_port_added_listener(port_added_listener)
443 self._graph.add_ports_connected_listener(ports_connected_listener)
9ef22b36
PP
444 src = self._graph.add_component(MySource, 'src')
445 sink = self._graph.add_component(MySink, 'sink')
61d96b89 446 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
9ef22b36 447
871a292a
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):
fa4c33e3 473 class MyIter(bt2._UserMessageIterator):
f6a5e476
PP
474 def __next__(self):
475 raise bt2.Stop
476
61d96b89 477 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
f6a5e476
PP
478 def __init__(self, params):
479 self._add_output_port('out')
480 self._add_output_port('zero')
481
f6a5e476
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
8a08af82
SM
489 def _graph_is_configured(self):
490 pass
491
f6a5e476
PP
492 def _port_connected(self, port, other_port):
493 self._add_input_port('taste')
494
871a292a
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)
f6a5e476 499
871a292a
SM
500 def test_raise_in_component_init(self):
501 class MySink(bt2._UserSinkComponent):
502 def __init__(self, params):
503 raise ValueError('oops!')
f6a5e476 504
871a292a
SM
505 def _consume(self):
506 raise bt2.Stop
507
8a08af82
SM
508 def _graph_is_configured(self):
509 pass
510
871a292a
SM
511 graph = bt2.Graph()
512
614743a5 513 with self.assertRaises(bt2._Error):
871a292a
SM
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
8a08af82
SM
524 def _graph_is_configured(self):
525 pass
526
871a292a
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
614743a5 533 with self.assertRaises(bt2._Error):
871a292a
SM
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
61d96b89 541 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
871a292a
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
f6a5e476 551
8a08af82
SM
552 def _graph_is_configured(self):
553 pass
554
61d96b89
FD
555 def ports_connected_listener(
556 upstream_component, upstream_port, downstream_component, downstream_port
557 ):
871a292a 558 raise ValueError('oh noes!')
f6a5e476 559
871a292a
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')
f6a5e476 564
614743a5 565 with self.assertRaises(bt2._Error):
871a292a 566 graph.connect_ports(up.output_ports['out'], down.input_ports['in'])
This page took 0.061134 seconds and 4 git commands to generate.