lib: add bt_{graph,query_executor}_add_interrupter()
[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
9b4f9b42
PP
199 def test_add_interrupter(self):
200 class MyIter(bt2._UserMessageIterator):
201 def __next__(self):
202 raise TypeError
203
204 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
205 def __init__(self, params):
206 self._add_output_port('out')
207
208 class MySink(bt2._UserSinkComponent):
209 def __init__(self, params):
210 self._add_input_port('in')
211
212 def _consume(self):
213 next(self._msg_iter)
214
215 def _graph_is_configured(self):
216 self._msg_iter = self._create_input_port_message_iterator(
217 self._input_ports['in']
218 )
219
220 # add two interrupters, set one of them
221 interrupter1 = bt2.Interrupter()
222 interrupter2 = bt2.Interrupter()
223 self._graph.add_interrupter(interrupter1)
224 src = self._graph.add_component(MySource, 'src')
225 sink = self._graph.add_component(MySink, 'sink')
226 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
227 self._graph.add_interrupter(interrupter2)
228
229 with self.assertRaises(bt2._Error):
230 self._graph.run()
231
232 interrupter2.set()
233
234 with self.assertRaises(bt2.TryAgain):
235 self._graph.run()
236
237 interrupter2.reset()
238
239 with self.assertRaises(bt2._Error):
240 self._graph.run()
241
242 # Test that Graph.run() raises bt2.Interrupted if the graph gets
243 # interrupted during execution.
244 def test_interrupt_while_running(self):
5f25509b 245 class MyIter(_MyIter):
1d915789 246 def __next__(self):
5f25509b 247 return self._create_stream_beginning_message(self._stream)
1d915789 248
cfbd7cf3 249 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
1d915789
PP
250 def __init__(self, params):
251 self._add_output_port('out')
252
253 class MySink(bt2._UserSinkComponent):
254 def __init__(self, params):
255 self._add_input_port('in')
256
257 def _consume(self):
9b4f9b42 258 # Pretend that somebody asynchronously interrupted the graph.
5f25509b 259 nonlocal graph
9b4f9b42 260 graph.interrupt()
5f25509b 261 return next(self._msg_iter)
1d915789 262
5f25509b 263 def _graph_is_configured(self):
ca02df0a
PP
264 self._msg_iter = self._create_input_port_message_iterator(
265 self._input_ports['in']
266 )
1d915789 267
9b4f9b42
PP
268 graph = self._graph
269 up = self._graph.add_component(MySource, 'down')
270 down = self._graph.add_component(MySink, 'up')
271 self._graph.connect_ports(up.output_ports['out'], down.input_ports['in'])
272
273 with self.assertRaises(bt2.TryAgain):
274 self._graph.run()
811644b8
PP
275
276 def test_run(self):
5f25509b 277 class MyIter(_MyIter):
811644b8 278 def __next__(self):
5f25509b
SM
279 if self._at == 9:
280 raise StopIteration
281
282 if self._at == 0:
283 msg = self._create_stream_beginning_message(self._stream)
284 elif self._at == 1:
285 msg = self._create_packet_beginning_message(self._packet)
286 elif self._at == 7:
287 msg = self._create_packet_end_message(self._packet)
288 elif self._at == 8:
289 msg = self._create_stream_end_message(self._stream)
290 else:
291 msg = self._create_event_message(self._ec, self._packet)
811644b8 292
811644b8 293 self._at += 1
5602ef81 294 return msg
811644b8 295
cfbd7cf3 296 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
811644b8
PP
297 def __init__(self, params):
298 self._add_output_port('out')
299
300 class MySink(bt2._UserSinkComponent):
301 def __init__(self, params):
5f25509b 302 self._input_port = self._add_input_port('in')
811644b8
PP
303 self._at = 0
304
305 def _consume(comp_self):
5602ef81 306 msg = next(comp_self._msg_iter)
811644b8
PP
307
308 if comp_self._at == 0:
5f25509b 309 self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
811644b8 310 elif comp_self._at == 1:
5f25509b 311 self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
811644b8 312 elif comp_self._at >= 2 and comp_self._at <= 6:
5f25509b 313 self.assertIsInstance(msg, bt2.message._EventMessage)
e8ac1aae 314 self.assertEqual(msg.event.cls.name, 'salut')
811644b8 315 elif comp_self._at == 7:
5f25509b 316 self.assertIsInstance(msg, bt2.message._PacketEndMessage)
811644b8 317 elif comp_self._at == 8:
5f25509b 318 self.assertIsInstance(msg, bt2.message._StreamEndMessage)
811644b8
PP
319
320 comp_self._at += 1
321
5f25509b 322 def _graph_is_configured(self):
ca02df0a
PP
323 self._msg_iter = self._create_input_port_message_iterator(
324 self._input_port
325 )
811644b8
PP
326
327 src = self._graph.add_component(MySource, 'src')
328 sink = self._graph.add_component(MySink, 'sink')
cfbd7cf3
FD
329 conn = self._graph.connect_ports(
330 src.output_ports['out'], sink.input_ports['in']
331 )
811644b8
PP
332 self._graph.run()
333
334 def test_run_again(self):
5f25509b 335 class MyIter(_MyIter):
811644b8 336 def __next__(self):
5f25509b 337 if self._at == 3:
811644b8
PP
338 raise bt2.TryAgain
339
5f25509b
SM
340 if self._at == 0:
341 msg = self._create_stream_beginning_message(self._stream)
342 elif self._at == 1:
343 msg = self._create_packet_beginning_message(self._packet)
344 elif self._at == 2:
345 msg = self._create_event_message(self._ec, self._packet)
346
811644b8 347 self._at += 1
5602ef81 348 return msg
811644b8 349
cfbd7cf3 350 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
811644b8
PP
351 def __init__(self, params):
352 self._add_output_port('out')
353
354 class MySink(bt2._UserSinkComponent):
355 def __init__(self, params):
5f25509b 356 self._input_port = self._add_input_port('in')
811644b8
PP
357 self._at = 0
358
359 def _consume(comp_self):
5f25509b 360 msg = next(comp_self._msg_iter)
811644b8 361 if comp_self._at == 0:
5f25509b 362 self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
811644b8 363 elif comp_self._at == 1:
5f25509b
SM
364 self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
365 elif comp_self._at == 2:
366 self.assertIsInstance(msg, bt2.message._EventMessage)
811644b8 367 raise bt2.TryAgain
5f25509b
SM
368 else:
369 pass
811644b8
PP
370
371 comp_self._at += 1
372
5f25509b 373 def _graph_is_configured(self):
ca02df0a
PP
374 self._msg_iter = self._create_input_port_message_iterator(
375 self._input_port
376 )
811644b8
PP
377
378 src = self._graph.add_component(MySource, 'src')
379 sink = self._graph.add_component(MySink, 'sink')
cfbd7cf3
FD
380 conn = self._graph.connect_ports(
381 src.output_ports['out'], sink.input_ports['in']
382 )
811644b8
PP
383
384 with self.assertRaises(bt2.TryAgain):
385 self._graph.run()
386
811644b8 387 def test_run_error(self):
5f25509b 388 raised_in_sink = False
811644b8 389
5f25509b 390 class MyIter(_MyIter):
811644b8 391 def __next__(self):
5f25509b
SM
392 # If this gets called after the sink raised an exception, it is
393 # an error.
394 nonlocal raised_in_sink
395 assert raised_in_sink is False
396
397 if self._at == 0:
398 msg = self._create_stream_beginning_message(self._stream)
399 elif self._at == 1:
400 msg = self._create_packet_beginning_message(self._packet)
401 elif self._at == 2 or self._at == 3:
402 msg = self._create_event_message(self._ec, self._packet)
403 else:
811644b8 404 raise bt2.TryAgain
811644b8 405 self._at += 1
5602ef81 406 return msg
811644b8 407
cfbd7cf3 408 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
811644b8
PP
409 def __init__(self, params):
410 self._add_output_port('out')
411
412 class MySink(bt2._UserSinkComponent):
413 def __init__(self, params):
5f25509b 414 self._input_port = self._add_input_port('in')
811644b8
PP
415 self._at = 0
416
417 def _consume(comp_self):
5f25509b 418 msg = next(comp_self._msg_iter)
811644b8 419 if comp_self._at == 0:
5f25509b 420 self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
811644b8 421 elif comp_self._at == 1:
5f25509b
SM
422 self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
423 elif comp_self._at == 2:
424 self.assertIsInstance(msg, bt2.message._EventMessage)
425 elif comp_self._at == 3:
426 nonlocal raised_in_sink
427 raised_in_sink = True
811644b8
PP
428 raise RuntimeError('error!')
429
430 comp_self._at += 1
431
5f25509b 432 def _graph_is_configured(self):
ca02df0a
PP
433 self._msg_iter = self._create_input_port_message_iterator(
434 self._input_port
435 )
811644b8
PP
436
437 src = self._graph.add_component(MySource, 'src')
438 sink = self._graph.add_component(MySink, 'sink')
cfbd7cf3
FD
439 conn = self._graph.connect_ports(
440 src.output_ports['out'], sink.input_ports['in']
441 )
811644b8 442
694c792b 443 with self.assertRaises(bt2._Error):
811644b8
PP
444 self._graph.run()
445
5f25509b 446 def test_listeners(self):
5602ef81 447 class MyIter(bt2._UserMessageIterator):
5f25509b
SM
448 def __next__(self):
449 raise bt2.Stop
1d915789 450
cfbd7cf3 451 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
1d915789
PP
452 def __init__(self, params):
453 self._add_output_port('out')
5f25509b 454 self._add_output_port('zero')
1d915789
PP
455
456 class MySink(bt2._UserSinkComponent):
457 def __init__(self, params):
458 self._add_input_port('in')
1d915789 459
5f25509b
SM
460 def _consume(self):
461 raise bt2.Stop
1d915789 462
a01b452b
SM
463 def _graph_is_configured(self):
464 pass
465
5f25509b
SM
466 def _port_connected(self, port, other_port):
467 self._add_input_port('taste')
1d915789 468
5f25509b
SM
469 def port_added_listener(component, port):
470 nonlocal calls
471 calls.append((port_added_listener, component, port))
1d915789 472
cfbd7cf3
FD
473 def ports_connected_listener(
474 upstream_component, upstream_port, downstream_component, downstream_port
475 ):
5f25509b 476 nonlocal calls
cfbd7cf3
FD
477 calls.append(
478 (
479 ports_connected_listener,
480 upstream_component,
481 upstream_port,
482 downstream_component,
483 downstream_port,
484 )
485 )
5f25509b
SM
486
487 calls = []
488 self._graph.add_port_added_listener(port_added_listener)
489 self._graph.add_ports_connected_listener(ports_connected_listener)
1d915789
PP
490 src = self._graph.add_component(MySource, 'src')
491 sink = self._graph.add_component(MySink, 'sink')
cfbd7cf3 492 self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in'])
1d915789 493
5f25509b
SM
494 self.assertEqual(len(calls), 5)
495
496 self.assertIs(calls[0][0], port_added_listener)
497 self.assertEqual(calls[0][1].name, 'src')
498 self.assertEqual(calls[0][2].name, 'out')
499
500 self.assertIs(calls[1][0], port_added_listener)
501 self.assertEqual(calls[1][1].name, 'src')
502 self.assertEqual(calls[1][2].name, 'zero')
503
504 self.assertIs(calls[2][0], port_added_listener)
505 self.assertEqual(calls[2][1].name, 'sink')
506 self.assertEqual(calls[2][2].name, 'in')
507
508 self.assertIs(calls[3][0], port_added_listener)
509 self.assertEqual(calls[3][1].name, 'sink')
510 self.assertEqual(calls[3][2].name, 'taste')
511
512 self.assertIs(calls[4][0], ports_connected_listener)
513 self.assertEqual(calls[4][1].name, 'src')
514 self.assertEqual(calls[4][2].name, 'out')
515 self.assertEqual(calls[4][3].name, 'sink')
516 self.assertEqual(calls[4][4].name, 'in')
517
518 def test_invalid_listeners(self):
5602ef81 519 class MyIter(bt2._UserMessageIterator):
811644b8
PP
520 def __next__(self):
521 raise bt2.Stop
522
cfbd7cf3 523 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
811644b8
PP
524 def __init__(self, params):
525 self._add_output_port('out')
526 self._add_output_port('zero')
527
811644b8
PP
528 class MySink(bt2._UserSinkComponent):
529 def __init__(self, params):
530 self._add_input_port('in')
531
532 def _consume(self):
533 raise bt2.Stop
534
a01b452b
SM
535 def _graph_is_configured(self):
536 pass
537
811644b8
PP
538 def _port_connected(self, port, other_port):
539 self._add_input_port('taste')
540
5f25509b
SM
541 with self.assertRaises(TypeError):
542 self._graph.add_port_added_listener(1234)
543 with self.assertRaises(TypeError):
544 self._graph.add_ports_connected_listener(1234)
811644b8 545
5f25509b
SM
546 def test_raise_in_component_init(self):
547 class MySink(bt2._UserSinkComponent):
548 def __init__(self, params):
549 raise ValueError('oops!')
811644b8 550
5f25509b
SM
551 def _consume(self):
552 raise bt2.Stop
553
a01b452b
SM
554 def _graph_is_configured(self):
555 pass
556
5f25509b
SM
557 graph = bt2.Graph()
558
694c792b 559 with self.assertRaises(bt2._Error):
5f25509b
SM
560 graph.add_component(MySink, 'comp')
561
562 def test_raise_in_port_added_listener(self):
563 class MySink(bt2._UserSinkComponent):
564 def __init__(self, params):
565 self._add_input_port('in')
566
567 def _consume(self):
568 raise bt2.Stop
569
a01b452b
SM
570 def _graph_is_configured(self):
571 pass
572
5f25509b
SM
573 def port_added_listener(component, port):
574 raise ValueError('oh noes!')
575
576 graph = bt2.Graph()
577 graph.add_port_added_listener(port_added_listener)
578
694c792b 579 with self.assertRaises(bt2._Error):
5f25509b
SM
580 graph.add_component(MySink, 'comp')
581
582 def test_raise_in_ports_connected_listener(self):
583 class MyIter(bt2._UserMessageIterator):
584 def __next__(self):
585 raise bt2.Stop
586
cfbd7cf3 587 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
5f25509b
SM
588 def __init__(self, params):
589 self._add_output_port('out')
590
591 class MySink(bt2._UserSinkComponent):
592 def __init__(self, params):
593 self._add_input_port('in')
594
595 def _consume(self):
596 raise bt2.Stop
811644b8 597
a01b452b
SM
598 def _graph_is_configured(self):
599 pass
600
cfbd7cf3
FD
601 def ports_connected_listener(
602 upstream_component, upstream_port, downstream_component, downstream_port
603 ):
5f25509b 604 raise ValueError('oh noes!')
811644b8 605
5f25509b
SM
606 graph = bt2.Graph()
607 graph.add_ports_connected_listener(ports_connected_listener)
608 up = graph.add_component(MySource, 'down')
609 down = graph.add_component(MySink, 'up')
811644b8 610
694c792b 611 with self.assertRaises(bt2._Error):
5f25509b 612 graph.connect_ports(up.output_ports['out'], down.input_ports['in'])
This page took 0.063069 seconds and 4 git commands to generate.