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_default_interrupter(self
):
69 interrupter
= self
._graph
.default_interrupter
70 self
.assertIs(type(interrupter
), bt2
.Interrupter
)
72 def test_add_component_user_cls(self
):
73 class MySink(bt2
._UserSinkComponent
):
74 def _user_consume(self
):
77 comp
= self
._graph
.add_component(MySink
, 'salut')
78 self
.assertEqual(comp
.name
, 'salut')
80 def test_add_component_gen_cls(self
):
81 class MySink(bt2
._UserSinkComponent
):
82 def _user_consume(self
):
85 comp
= self
._graph
.add_component(MySink
, 'salut')
87 comp2
= self
._graph
.add_component(comp
.cls
, 'salut2')
88 self
.assertEqual(comp2
.name
, 'salut2')
90 def test_add_component_params(self
):
93 class MySink(bt2
._UserSinkComponent
):
94 def __init__(self
, config
, params
, obj
):
98 def _user_consume(self
):
101 params
= {'hello': 23, 'path': '/path/to/stuff'}
102 self
._graph
.add_component(MySink
, 'salut', params
)
103 self
.assertEqual(params
, comp_params
)
106 def test_add_component_obj_python_comp_cls(self
):
109 class MySink(bt2
._UserSinkComponent
):
110 def __init__(self
, config
, params
, obj
):
114 def _user_consume(self
):
118 self
._graph
.add_component(MySink
, 'salut', obj
=obj
)
119 self
.assertIs(comp_obj
, obj
)
122 def test_add_component_obj_none_python_comp_cls(self
):
125 class MySink(bt2
._UserSinkComponent
):
126 def __init__(self
, config
, params
, obj
):
130 def _user_consume(self
):
133 self
._graph
.add_component(MySink
, 'salut')
134 self
.assertIsNone(comp_obj
)
137 def test_add_component_obj_non_python_comp_cls(self
):
138 plugin
= bt2
.find_plugin('text', find_in_user_dir
=False, find_in_sys_dir
=False)
139 assert plugin
is not None
140 cc
= plugin
.source_component_classes
['dmesg']
141 assert cc
is not None
143 with self
.assertRaises(ValueError):
144 self
._graph
.add_component(cc
, 'salut', obj
=57)
146 def test_add_component_invalid_cls_type(self
):
147 with self
.assertRaises(TypeError):
148 self
._graph
.add_component(int, 'salut')
150 def test_add_component_invalid_logging_level_type(self
):
151 class MySink(bt2
._UserSinkComponent
):
152 def _user_consume(self
):
155 with self
.assertRaises(TypeError):
156 self
._graph
.add_component(MySink
, 'salut', logging_level
='yo')
158 def test_add_component_invalid_logging_level_value(self
):
159 class MySink(bt2
._UserSinkComponent
):
160 def _user_consume(self
):
163 with self
.assertRaises(ValueError):
164 self
._graph
.add_component(MySink
, 'salut', logging_level
=12345)
166 def test_add_component_invalid_params_type(self
):
167 class MySink(bt2
._UserSinkComponent
):
168 def _user_consume(self
):
171 with self
.assertRaises(TypeError):
172 self
._graph
.add_component(MySink
, 'salut', params
=12)
174 def test_add_component_params_dict(self
):
177 class MySink(bt2
._UserSinkComponent
):
178 def __init__(self
, config
, params
, obj
):
182 def _user_consume(self
):
185 params
= {'plage': 12312}
186 self
._graph
.add_component(MySink
, 'salut', params
=params
)
188 # Check equality and not identity because `add_component()` method
189 # converts the Python `dict` to a `bt2.MapValue`.
190 self
.assertEqual(params
, params_obj
)
192 def test_add_component_params_mapvalue(self
):
195 class MySink(bt2
._UserSinkComponent
):
196 def __init__(self
, config
, params
, obj
):
200 def _user_consume(self
):
203 params
= bt2
.MapValue({'beachclub': '121'})
204 self
._graph
.add_component(MySink
, 'salut', params
=params
)
206 self
.assertEqual(params
, params_obj
)
208 def test_add_component_logging_level(self
):
209 class MySink(bt2
._UserSinkComponent
):
210 def _user_consume(self
):
213 comp
= self
._graph
.add_component(
214 MySink
, 'salut', logging_level
=bt2
.LoggingLevel
.DEBUG
216 self
.assertEqual(comp
.logging_level
, bt2
.LoggingLevel
.DEBUG
)
218 def test_connect_ports(self
):
220 bt2
._UserSourceComponent
, message_iterator_class
=bt2
._UserMessageIterator
222 def __init__(self
, config
, params
, obj
):
223 self
._add
_output
_port
('out')
225 class MySink(bt2
._UserSinkComponent
):
226 def __init__(self
, config
, params
, obj
):
227 self
._add
_input
_port
('in')
229 def _user_consume(self
):
232 src
= self
._graph
.add_component(MySource
, 'src')
233 sink
= self
._graph
.add_component(MySink
, 'sink')
235 conn
= self
._graph
.connect_ports(
236 src
.output_ports
['out'], sink
.input_ports
['in']
238 self
.assertTrue(src
.output_ports
['out'].is_connected
)
239 self
.assertTrue(sink
.input_ports
['in'].is_connected
)
240 self
.assertEqual(src
.output_ports
['out'].connection
.addr
, conn
.addr
)
241 self
.assertEqual(sink
.input_ports
['in'].connection
.addr
, conn
.addr
)
243 def test_connect_ports_invalid_direction(self
):
245 bt2
._UserSourceComponent
, message_iterator_class
=bt2
._UserMessageIterator
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
_message
_iterator
(self
._input
_ports
['in'])
282 # add two interrupters, set one of them
283 interrupter1
= bt2
.Interrupter()
284 interrupter2
= bt2
.Interrupter()
285 self
._graph
.add_interrupter(interrupter1
)
286 src
= self
._graph
.add_component(MySource
, 'src')
287 sink
= self
._graph
.add_component(MySink
, 'sink')
288 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
289 self
._graph
.add_interrupter(interrupter2
)
291 with self
.assertRaises(bt2
._Error
):
296 with self
.assertRaises(bt2
.TryAgain
):
301 with self
.assertRaises(bt2
._Error
):
304 # Test that Graph.run() raises bt2.Interrupted if the graph gets
305 # interrupted during execution.
306 def test_interrupt_while_running(self
):
307 class MyIter(_MyIter
):
309 return self
._create
_stream
_beginning
_message
(self
._stream
)
311 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
312 def __init__(self
, config
, params
, obj
):
313 self
._add
_output
_port
('out')
315 class MySink(bt2
._UserSinkComponent
):
316 def __init__(self
, config
, params
, obj
):
317 self
._add
_input
_port
('in')
319 def _user_consume(self
):
320 # Pretend that somebody asynchronously interrupted the graph.
322 graph
.default_interrupter
.set()
323 return next(self
._msg
_iter
)
325 def _user_graph_is_configured(self
):
326 self
._msg
_iter
= self
._create
_message
_iterator
(self
._input
_ports
['in'])
329 up
= self
._graph
.add_component(MySource
, 'down')
330 down
= self
._graph
.add_component(MySink
, 'up')
331 self
._graph
.connect_ports(up
.output_ports
['out'], down
.input_ports
['in'])
333 with self
.assertRaises(bt2
.TryAgain
):
337 class MyIter(_MyIter
):
343 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
345 msg
= self
._create
_packet
_beginning
_message
(self
._packet
)
347 msg
= self
._create
_packet
_end
_message
(self
._packet
)
349 msg
= self
._create
_stream
_end
_message
(self
._stream
)
351 msg
= self
._create
_event
_message
(self
._ec
, self
._packet
)
356 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
357 def __init__(self
, config
, params
, obj
):
358 self
._add
_output
_port
('out')
360 class MySink(bt2
._UserSinkComponent
):
361 def __init__(self
, config
, params
, obj
):
362 self
._input
_port
= self
._add
_input
_port
('in')
365 def _user_consume(comp_self
):
366 msg
= next(comp_self
._msg
_iter
)
368 if comp_self
._at
== 0:
369 self
.assertIs(type(msg
), bt2
._StreamBeginningMessageConst
)
370 elif comp_self
._at
== 1:
371 self
.assertIs(type(msg
), bt2
._PacketBeginningMessageConst
)
372 elif comp_self
._at
>= 2 and comp_self
._at
<= 6:
373 self
.assertIs(type(msg
), bt2
._EventMessageConst
)
374 self
.assertEqual(msg
.event
.cls
.name
, 'salut')
375 elif comp_self
._at
== 7:
376 self
.assertIs(type(msg
), bt2
._PacketEndMessageConst
)
377 elif comp_self
._at
== 8:
378 self
.assertIs(type(msg
), bt2
._StreamEndMessageConst
)
382 def _user_graph_is_configured(self
):
383 self
._msg
_iter
= self
._create
_message
_iterator
(self
._input
_port
)
385 src
= self
._graph
.add_component(MySource
, 'src')
386 sink
= self
._graph
.add_component(MySink
, 'sink')
387 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
390 def test_run_once(self
):
391 class MyIter(_MyIter
):
394 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
395 def __init__(self
, config
, params
, obj
):
396 self
._add
_output
_port
('out')
398 class MySink(bt2
._UserSinkComponent
):
399 def __init__(self
, config
, params
, obj
):
400 self
._input
_port
= self
._add
_input
_port
('in')
402 def _user_consume(comp_self
):
408 src
= self
._graph
.add_component(MySource
, 'src')
409 sink
= self
._graph
.add_component(MySink
, 'sink')
410 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
412 with self
.assertRaises(bt2
.TryAgain
):
413 self
._graph
.run_once()
415 self
.assertEqual(run_count
, 1)
417 def test_run_once_stops(self
):
418 class MyIter(_MyIter
):
421 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
422 def __init__(self
, config
, params
, obj
):
423 self
._add
_output
_port
('out')
425 class MySink(bt2
._UserSinkComponent
):
426 def __init__(self
, config
, params
, obj
):
427 self
._input
_port
= self
._add
_input
_port
('in')
429 def _user_consume(comp_self
):
432 src
= self
._graph
.add_component(MySource
, 'src')
433 sink
= self
._graph
.add_component(MySink
, 'sink')
434 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
436 with self
.assertRaises(bt2
.Stop
):
437 self
._graph
.run_once()
439 def test_run_again(self
):
440 class MyIter(_MyIter
):
446 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
448 msg
= self
._create
_packet
_beginning
_message
(self
._packet
)
450 msg
= self
._create
_event
_message
(self
._ec
, self
._packet
)
455 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
456 def __init__(self
, config
, params
, obj
):
457 self
._add
_output
_port
('out')
459 class MySink(bt2
._UserSinkComponent
):
460 def __init__(self
, config
, params
, obj
):
461 self
._input
_port
= self
._add
_input
_port
('in')
464 def _user_consume(comp_self
):
465 msg
= next(comp_self
._msg
_iter
)
466 if comp_self
._at
== 0:
467 self
.assertIs(type(msg
), bt2
._StreamBeginningMessageConst
)
468 elif comp_self
._at
== 1:
469 self
.assertIs(type(msg
), bt2
._PacketBeginningMessageConst
)
470 elif comp_self
._at
== 2:
471 self
.assertIs(type(msg
), bt2
._EventMessageConst
)
478 def _user_graph_is_configured(self
):
479 self
._msg
_iter
= self
._create
_message
_iterator
(self
._input
_port
)
481 src
= self
._graph
.add_component(MySource
, 'src')
482 sink
= self
._graph
.add_component(MySink
, 'sink')
483 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
485 with self
.assertRaises(bt2
.TryAgain
):
488 def test_run_error(self
):
489 raised_in_sink
= False
491 class MyIter(_MyIter
):
493 # If this gets called after the sink raised an exception, it is
495 nonlocal raised_in_sink
496 assert raised_in_sink
is False
499 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
501 msg
= self
._create
_packet
_beginning
_message
(self
._packet
)
502 elif self
._at
== 2 or self
._at
== 3:
503 msg
= self
._create
_event
_message
(self
._ec
, self
._packet
)
509 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
510 def __init__(self
, config
, params
, obj
):
511 self
._add
_output
_port
('out')
513 class MySink(bt2
._UserSinkComponent
):
514 def __init__(self
, config
, params
, obj
):
515 self
._input
_port
= self
._add
_input
_port
('in')
518 def _user_consume(comp_self
):
519 msg
= next(comp_self
._msg
_iter
)
520 if comp_self
._at
== 0:
521 self
.assertIs(type(msg
), bt2
._StreamBeginningMessageConst
)
522 elif comp_self
._at
== 1:
523 self
.assertIs(type(msg
), bt2
._PacketBeginningMessageConst
)
524 elif comp_self
._at
== 2:
525 self
.assertIs(type(msg
), bt2
._EventMessageConst
)
526 elif comp_self
._at
== 3:
527 nonlocal raised_in_sink
528 raised_in_sink
= True
529 raise RuntimeError('error!')
533 def _user_graph_is_configured(self
):
534 self
._msg
_iter
= self
._create
_message
_iterator
(self
._input
_port
)
536 src
= self
._graph
.add_component(MySource
, 'src')
537 sink
= self
._graph
.add_component(MySink
, 'sink')
538 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
540 with self
.assertRaises(bt2
._Error
):
543 def test_listeners(self
):
545 bt2
._UserSourceComponent
, message_iterator_class
=bt2
._UserMessageIterator
547 def __init__(self
, config
, params
, obj
):
548 self
._add
_output
_port
('out')
549 self
._add
_output
_port
('zero')
551 class MySink(bt2
._UserSinkComponent
):
552 def __init__(self
, config
, params
, obj
):
553 self
._add
_input
_port
('in')
555 def _user_consume(self
):
558 def _user_port_connected(self
, port
, other_port
):
559 self
._add
_input
_port
('taste')
561 def port_added_listener(component
, port
):
563 calls
.append((port_added_listener
, component
, port
))
566 self
._graph
.add_port_added_listener(port_added_listener
)
567 src
= self
._graph
.add_component(MySource
, 'src')
568 sink
= self
._graph
.add_component(MySink
, 'sink')
569 self
._graph
.connect_ports(src
.output_ports
['out'], sink
.input_ports
['in'])
571 self
.assertEqual(len(calls
), 4)
573 self
.assertIs(calls
[0][0], port_added_listener
)
574 self
.assertEqual(calls
[0][1].name
, 'src')
575 self
.assertEqual(calls
[0][2].name
, 'out')
577 self
.assertIs(calls
[1][0], port_added_listener
)
578 self
.assertEqual(calls
[1][1].name
, 'src')
579 self
.assertEqual(calls
[1][2].name
, 'zero')
581 self
.assertIs(calls
[2][0], port_added_listener
)
582 self
.assertEqual(calls
[2][1].name
, 'sink')
583 self
.assertEqual(calls
[2][2].name
, 'in')
585 self
.assertIs(calls
[3][0], port_added_listener
)
586 self
.assertEqual(calls
[3][1].name
, 'sink')
587 self
.assertEqual(calls
[3][2].name
, 'taste')
589 def test_invalid_listeners(self
):
591 bt2
._UserSourceComponent
, message_iterator_class
=bt2
._UserMessageIterator
593 def __init__(self
, config
, params
, obj
):
594 self
._add
_output
_port
('out')
595 self
._add
_output
_port
('zero')
597 class MySink(bt2
._UserSinkComponent
):
598 def __init__(self
, config
, params
, obj
):
599 self
._add
_input
_port
('in')
601 def _user_consume(self
):
604 def _user_port_connected(self
, port
, other_port
):
605 self
._add
_input
_port
('taste')
607 with self
.assertRaises(TypeError):
608 self
._graph
.add_port_added_listener(1234)
610 def test_raise_in_component_init(self
):
611 class MySink(bt2
._UserSinkComponent
):
612 def __init__(self
, config
, params
, obj
):
613 raise ValueError('oops!')
615 def _user_consume(self
):
620 with self
.assertRaises(bt2
._Error
):
621 graph
.add_component(MySink
, 'comp')
623 def test_raise_in_port_added_listener(self
):
624 class MySink(bt2
._UserSinkComponent
):
625 def __init__(self
, config
, params
, obj
):
626 self
._add
_input
_port
('in')
628 def _user_consume(self
):
631 def port_added_listener(component
, port
):
632 raise ValueError('oh noes!')
635 graph
.add_port_added_listener(port_added_listener
)
637 with self
.assertRaises(bt2
._Error
):
638 graph
.add_component(MySink
, 'comp')
641 if __name__
== '__main__':