2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
23 class _MyIter(bt2
._UserMessageIterator
):
24 def __init__(self
, config
, self_output_port
):
28 def _build_meta(self
):
29 self
._tc
= self
._component
._create
_trace
_class
()
31 self
._sc
= self
._tc
.create_stream_class(supports_packets
=True)
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()
35 payload_ft
+= [('my_int', self
._my
_int
_ft
)]
36 self
._ec
.payload_field_type
= payload_ft
37 self
._stream
= self
._t
.create_stream(self
._sc
)
38 self
._packet
= self
._stream
.create_packet()
40 def _create_event(self
, value
):
42 ev
.payload_field
['my_int'] = value
43 ev
.packet
= self
._packet
47 class GraphTestCase(unittest
.TestCase
):
49 self
._graph
= bt2
.Graph()
54 def test_create_default(self
):
57 def test_create_known_mip_version(self
):
60 def test_create_invalid_mip_version_type(self
):
61 with self
.assertRaises(TypeError):
64 def test_create_unknown_mip_version(self
):
65 with self
.assertRaisesRegex(ValueError, 'unknown MIP version'):
68 def test_add_component_user_cls(self
):
69 class MySink(bt2
._UserSinkComponent
):
70 def _user_consume(self
):
73 comp
= self
._graph
.add_component(MySink
, 'salut')
74 self
.assertEqual(comp
.name
, 'salut')
76 def test_add_component_gen_cls(self
):
77 class MySink(bt2
._UserSinkComponent
):
78 def _user_consume(self
):
81 comp
= self
._graph
.add_component(MySink
, 'salut')
83 comp2
= self
._graph
.add_component(comp
.cls
, 'salut2')
84 self
.assertEqual(comp2
.name
, 'salut2')
86 def test_add_component_params(self
):
89 class MySink(bt2
._UserSinkComponent
):
90 def __init__(self
, config
, params
, obj
):
94 def _user_consume(self
):
97 params
= {'hello': 23, 'path': '/path/to/stuff'}
98 self
._graph
.add_component(MySink
, 'salut', params
)
99 self
.assertEqual(params
, comp_params
)
102 def test_add_component_obj_python_comp_cls(self
):
105 class MySink(bt2
._UserSinkComponent
):
106 def __init__(self
, config
, params
, obj
):
110 def _user_consume(self
):
114 self
._graph
.add_component(MySink
, 'salut', obj
=obj
)
115 self
.assertIs(comp_obj
, obj
)
118 def test_add_component_obj_none_python_comp_cls(self
):
121 class MySink(bt2
._UserSinkComponent
):
122 def __init__(self
, config
, params
, obj
):
126 def _user_consume(self
):
129 self
._graph
.add_component(MySink
, 'salut')
130 self
.assertIsNone(comp_obj
)
133 def test_add_component_obj_non_python_comp_cls(self
):
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
139 with self
.assertRaises(ValueError):
140 self
._graph
.add_component(cc
, 'salut', obj
=57)
142 def test_add_component_invalid_cls_type(self
):
143 with self
.assertRaises(TypeError):
144 self
._graph
.add_component(int, 'salut')
146 def test_add_component_invalid_logging_level_type(self
):
147 class MySink(bt2
._UserSinkComponent
):
148 def _user_consume(self
):
151 with self
.assertRaises(TypeError):
152 self
._graph
.add_component(MySink
, 'salut', logging_level
='yo')
154 def test_add_component_invalid_logging_level_value(self
):
155 class MySink(bt2
._UserSinkComponent
):
156 def _user_consume(self
):
159 with self
.assertRaises(ValueError):
160 self
._graph
.add_component(MySink
, 'salut', logging_level
=12345)
162 def test_add_component_invalid_params_type(self
):
163 class MySink(bt2
._UserSinkComponent
):
164 def _user_consume(self
):
167 with self
.assertRaises(TypeError):
168 self
._graph
.add_component(MySink
, 'salut', params
=12)
170 def test_add_component_params_dict(self
):
173 class MySink(bt2
._UserSinkComponent
):
174 def __init__(self
, config
, params
, obj
):
178 def _user_consume(self
):
181 params
= {'plage': 12312}
182 self
._graph
.add_component(MySink
, 'salut', params
=params
)
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
)
188 def test_add_component_params_mapvalue(self
):
191 class MySink(bt2
._UserSinkComponent
):
192 def __init__(self
, config
, params
, obj
):
196 def _user_consume(self
):
199 params
= bt2
.MapValue({'beachclub': '121'})
200 self
._graph
.add_component(MySink
, 'salut', params
=params
)
202 self
.assertEqual(params
, params_obj
)
204 def test_add_component_logging_level(self
):
205 class MySink(bt2
._UserSinkComponent
):
206 def _user_consume(self
):
209 comp
= self
._graph
.add_component(
210 MySink
, 'salut', logging_level
=bt2
.LoggingLevel
.DEBUG
212 self
.assertEqual(comp
.logging_level
, bt2
.LoggingLevel
.DEBUG
)
214 def test_connect_ports(self
):
215 class MyIter(bt2
._UserMessageIterator
):
219 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
220 def __init__(self
, config
, params
, obj
):
221 self
._add
_output
_port
('out')
223 class MySink(bt2
._UserSinkComponent
):
224 def __init__(self
, config
, params
, obj
):
225 self
._add
_input
_port
('in')
227 def _user_consume(self
):
230 src
= self
._graph
.add_component(MySource
, 'src')
231 sink
= self
._graph
.add_component(MySink
, 'sink')
233 conn
= self
._graph
.connect_ports(
234 src
.output_ports
['out'], sink
.input_ports
['in']
236 self
.assertTrue(src
.output_ports
['out'].is_connected
)
237 self
.assertTrue(sink
.input_ports
['in'].is_connected
)
238 self
.assertEqual(src
.output_ports
['out'].connection
.addr
, conn
.addr
)
239 self
.assertEqual(sink
.input_ports
['in'].connection
.addr
, conn
.addr
)
241 def test_connect_ports_invalid_direction(self
):
242 class MyIter(bt2
._UserMessageIterator
):
246 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
247 def __init__(self
, config
, params
, obj
):
248 self
._add
_output
_port
('out')
250 class MySink(bt2
._UserSinkComponent
):
251 def __init__(self
, config
, params
, obj
):
252 self
._add
_input
_port
('in')
254 def _user_consume(self
):
257 src
= self
._graph
.add_component(MySource
, 'src')
258 sink
= self
._graph
.add_component(MySink
, 'sink')
260 with self
.assertRaises(TypeError):
261 self
._graph
.connect_ports(sink
.input_ports
['in'], src
.output_ports
['out'])
263 def test_add_interrupter(self
):
264 class MyIter(bt2
._UserMessageIterator
):
268 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
269 def __init__(self
, config
, params
, obj
):
270 self
._add
_output
_port
('out')
272 class MySink(bt2
._UserSinkComponent
):
273 def __init__(self
, config
, params
, obj
):
274 self
._add
_input
_port
('in')
276 def _user_consume(self
):
279 def _user_graph_is_configured(self
):
280 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
281 self
._input
_ports
['in']
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
)
293 with self
.assertRaises(bt2
._Error
):
298 with self
.assertRaises(bt2
.TryAgain
):
303 with self
.assertRaises(bt2
._Error
):
306 # Test that Graph.run() raises bt2.Interrupted if the graph gets
307 # interrupted during execution.
308 def test_interrupt_while_running(self
):
309 class MyIter(_MyIter
):
311 return self
._create
_stream
_beginning
_message
(self
._stream
)
313 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
314 def __init__(self
, config
, params
, obj
):
315 self
._add
_output
_port
('out')
317 class MySink(bt2
._UserSinkComponent
):
318 def __init__(self
, config
, params
, obj
):
319 self
._add
_input
_port
('in')
321 def _user_consume(self
):
322 # Pretend that somebody asynchronously interrupted the graph.
325 return next(self
._msg
_iter
)
327 def _user_graph_is_configured(self
):
328 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
329 self
._input
_ports
['in']
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'])
337 with self
.assertRaises(bt2
.TryAgain
):
341 class MyIter(_MyIter
):
347 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
349 msg
= self
._create
_packet
_beginning
_message
(self
._packet
)
351 msg
= self
._create
_packet
_end
_message
(self
._packet
)
353 msg
= self
._create
_stream
_end
_message
(self
._stream
)
355 msg
= self
._create
_event
_message
(self
._ec
, self
._packet
)
360 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
361 def __init__(self
, config
, params
, obj
):
362 self
._add
_output
_port
('out')
364 class MySink(bt2
._UserSinkComponent
):
365 def __init__(self
, config
, params
, obj
):
366 self
._input
_port
= self
._add
_input
_port
('in')
369 def _user_consume(comp_self
):
370 msg
= next(comp_self
._msg
_iter
)
372 if comp_self
._at
== 0:
373 self
.assertIs(type(msg
), bt2
._StreamBeginningMessageConst
)
374 elif comp_self
._at
== 1:
375 self
.assertIs(type(msg
), bt2
._PacketBeginningMessageConst
)
376 elif comp_self
._at
>= 2 and comp_self
._at
<= 6:
377 self
.assertIs(type(msg
), bt2
._EventMessageConst
)
378 self
.assertEqual(msg
.event
.cls
.name
, 'salut')
379 elif comp_self
._at
== 7:
380 self
.assertIs(type(msg
), bt2
._PacketEndMessageConst
)
381 elif comp_self
._at
== 8:
382 self
.assertIs(type(msg
), bt2
._StreamEndMessageConst
)
386 def _user_graph_is_configured(self
):
387 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
391 src
= self
._graph
.add_component(MySource
, 'src')
392 sink
= self
._graph
.add_component(MySink
, 'sink')
393 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
396 def test_run_once(self
):
397 class MyIter(_MyIter
):
400 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
401 def __init__(self
, config
, params
, obj
):
402 self
._add
_output
_port
('out')
404 class MySink(bt2
._UserSinkComponent
):
405 def __init__(self
, config
, params
, obj
):
406 self
._input
_port
= self
._add
_input
_port
('in')
408 def _user_consume(comp_self
):
414 src
= self
._graph
.add_component(MySource
, 'src')
415 sink
= self
._graph
.add_component(MySink
, 'sink')
416 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
418 with self
.assertRaises(bt2
.TryAgain
):
419 self
._graph
.run_once()
421 self
.assertEqual(run_count
, 1)
423 def test_run_once_stops(self
):
424 class MyIter(_MyIter
):
427 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
428 def __init__(self
, config
, params
, obj
):
429 self
._add
_output
_port
('out')
431 class MySink(bt2
._UserSinkComponent
):
432 def __init__(self
, config
, params
, obj
):
433 self
._input
_port
= self
._add
_input
_port
('in')
435 def _user_consume(comp_self
):
438 src
= self
._graph
.add_component(MySource
, 'src')
439 sink
= self
._graph
.add_component(MySink
, 'sink')
440 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
442 with self
.assertRaises(bt2
.Stop
):
443 self
._graph
.run_once()
445 def test_run_again(self
):
446 class MyIter(_MyIter
):
452 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
454 msg
= self
._create
_packet
_beginning
_message
(self
._packet
)
456 msg
= self
._create
_event
_message
(self
._ec
, self
._packet
)
461 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
462 def __init__(self
, config
, params
, obj
):
463 self
._add
_output
_port
('out')
465 class MySink(bt2
._UserSinkComponent
):
466 def __init__(self
, config
, params
, obj
):
467 self
._input
_port
= self
._add
_input
_port
('in')
470 def _user_consume(comp_self
):
471 msg
= next(comp_self
._msg
_iter
)
472 if comp_self
._at
== 0:
473 self
.assertIs(type(msg
), bt2
._StreamBeginningMessageConst
)
474 elif comp_self
._at
== 1:
475 self
.assertIs(type(msg
), bt2
._PacketBeginningMessageConst
)
476 elif comp_self
._at
== 2:
477 self
.assertIs(type(msg
), bt2
._EventMessageConst
)
484 def _user_graph_is_configured(self
):
485 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
489 src
= self
._graph
.add_component(MySource
, 'src')
490 sink
= self
._graph
.add_component(MySink
, 'sink')
491 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
493 with self
.assertRaises(bt2
.TryAgain
):
496 def test_run_error(self
):
497 raised_in_sink
= False
499 class MyIter(_MyIter
):
501 # If this gets called after the sink raised an exception, it is
503 nonlocal raised_in_sink
504 assert raised_in_sink
is False
507 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
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
)
517 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
518 def __init__(self
, config
, params
, obj
):
519 self
._add
_output
_port
('out')
521 class MySink(bt2
._UserSinkComponent
):
522 def __init__(self
, config
, params
, obj
):
523 self
._input
_port
= self
._add
_input
_port
('in')
526 def _user_consume(comp_self
):
527 msg
= next(comp_self
._msg
_iter
)
528 if comp_self
._at
== 0:
529 self
.assertIs(type(msg
), bt2
._StreamBeginningMessageConst
)
530 elif comp_self
._at
== 1:
531 self
.assertIs(type(msg
), bt2
._PacketBeginningMessageConst
)
532 elif comp_self
._at
== 2:
533 self
.assertIs(type(msg
), bt2
._EventMessageConst
)
534 elif comp_self
._at
== 3:
535 nonlocal raised_in_sink
536 raised_in_sink
= True
537 raise RuntimeError('error!')
541 def _user_graph_is_configured(self
):
542 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
546 src
= self
._graph
.add_component(MySource
, 'src')
547 sink
= self
._graph
.add_component(MySink
, 'sink')
548 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
550 with self
.assertRaises(bt2
._Error
):
553 def test_listeners(self
):
554 class MyIter(bt2
._UserMessageIterator
):
558 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
559 def __init__(self
, config
, params
, obj
):
560 self
._add
_output
_port
('out')
561 self
._add
_output
_port
('zero')
563 class MySink(bt2
._UserSinkComponent
):
564 def __init__(self
, config
, params
, obj
):
565 self
._add
_input
_port
('in')
567 def _user_consume(self
):
570 def _user_port_connected(self
, port
, other_port
):
571 self
._add
_input
_port
('taste')
573 def port_added_listener(component
, port
):
575 calls
.append((port_added_listener
, component
, port
))
577 def ports_connected_listener(
578 upstream_component
, upstream_port
, downstream_component
, downstream_port
583 ports_connected_listener
,
586 downstream_component
,
592 self
._graph
.add_port_added_listener(port_added_listener
)
593 self
._graph
.add_ports_connected_listener(ports_connected_listener
)
594 src
= self
._graph
.add_component(MySource
, 'src')
595 sink
= self
._graph
.add_component(MySink
, 'sink')
596 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
598 self
.assertEqual(len(calls
), 5)
600 self
.assertIs(calls
[0][0], port_added_listener
)
601 self
.assertEqual(calls
[0][1].name
, 'src')
602 self
.assertEqual(calls
[0][2].name
, 'out')
604 self
.assertIs(calls
[1][0], port_added_listener
)
605 self
.assertEqual(calls
[1][1].name
, 'src')
606 self
.assertEqual(calls
[1][2].name
, 'zero')
608 self
.assertIs(calls
[2][0], port_added_listener
)
609 self
.assertEqual(calls
[2][1].name
, 'sink')
610 self
.assertEqual(calls
[2][2].name
, 'in')
612 self
.assertIs(calls
[3][0], port_added_listener
)
613 self
.assertEqual(calls
[3][1].name
, 'sink')
614 self
.assertEqual(calls
[3][2].name
, 'taste')
616 self
.assertIs(calls
[4][0], ports_connected_listener
)
617 self
.assertEqual(calls
[4][1].name
, 'src')
618 self
.assertEqual(calls
[4][2].name
, 'out')
619 self
.assertEqual(calls
[4][3].name
, 'sink')
620 self
.assertEqual(calls
[4][4].name
, 'in')
622 def test_invalid_listeners(self
):
623 class MyIter(bt2
._UserMessageIterator
):
627 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
628 def __init__(self
, config
, params
, obj
):
629 self
._add
_output
_port
('out')
630 self
._add
_output
_port
('zero')
632 class MySink(bt2
._UserSinkComponent
):
633 def __init__(self
, config
, params
, obj
):
634 self
._add
_input
_port
('in')
636 def _user_consume(self
):
639 def _user_port_connected(self
, port
, other_port
):
640 self
._add
_input
_port
('taste')
642 with self
.assertRaises(TypeError):
643 self
._graph
.add_port_added_listener(1234)
644 with self
.assertRaises(TypeError):
645 self
._graph
.add_ports_connected_listener(1234)
647 def test_raise_in_component_init(self
):
648 class MySink(bt2
._UserSinkComponent
):
649 def __init__(self
, config
, params
, obj
):
650 raise ValueError('oops!')
652 def _user_consume(self
):
657 with self
.assertRaises(bt2
._Error
):
658 graph
.add_component(MySink
, 'comp')
660 def test_raise_in_port_added_listener(self
):
661 class MySink(bt2
._UserSinkComponent
):
662 def __init__(self
, config
, params
, obj
):
663 self
._add
_input
_port
('in')
665 def _user_consume(self
):
668 def port_added_listener(component
, port
):
669 raise ValueError('oh noes!')
672 graph
.add_port_added_listener(port_added_listener
)
674 with self
.assertRaises(bt2
._Error
):
675 graph
.add_component(MySink
, 'comp')
677 def test_raise_in_ports_connected_listener(self
):
678 class MyIter(bt2
._UserMessageIterator
):
682 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
683 def __init__(self
, config
, params
, obj
):
684 self
._add
_output
_port
('out')
686 class MySink(bt2
._UserSinkComponent
):
687 def __init__(self
, config
, params
, obj
):
688 self
._add
_input
_port
('in')
690 def _user_consume(self
):
693 def ports_connected_listener(
694 upstream_component
, upstream_port
, downstream_component
, downstream_port
696 raise ValueError('oh noes!')
699 graph
.add_ports_connected_listener(ports_connected_listener
)
700 up
= graph
.add_component(MySource
, 'down')
701 down
= graph
.add_component(MySink
, 'up')
703 with self
.assertRaises(bt2
._Error
):
704 graph
.connect_ports(up
.output_ports
['out'], down
.input_ports
['in'])
707 if __name__
== '__main__':