X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_graph.py;h=7c18e8a79256a98afde305af9300614a906db708;hb=5c61fb9d5be835a0efc87f7764117c4d05e0ae5a;hp=0c9d9279851ed2ae0f335767fee928f5cee66448;hpb=57081273d1191fc79edc101af619fab96b72460d;p=babeltrace.git diff --git a/tests/bindings/python/bt2/test_graph.py b/tests/bindings/python/bt2/test_graph.py index 0c9d9279..7c18e8a7 100644 --- a/tests/bindings/python/bt2/test_graph.py +++ b/tests/bindings/python/bt2/test_graph.py @@ -21,7 +21,7 @@ import bt2 class _MyIter(bt2._UserMessageIterator): - def __init__(self, self_output_port): + def __init__(self, config, self_output_port): self._build_meta() self._at = 0 @@ -51,8 +51,23 @@ class GraphTestCase(unittest.TestCase): def tearDown(self): del self._graph - def test_create_empty(self): - graph = bt2.Graph() + def test_create_default(self): + bt2.Graph() + + def test_create_known_mip_version(self): + bt2.Graph(0) + + def test_create_invalid_mip_version_type(self): + with self.assertRaises(TypeError): + bt2.Graph('') + + def test_create_unknown_mip_version(self): + with self.assertRaisesRegex(ValueError, 'unknown MIP version'): + bt2.Graph(1) + + def test_default_interrupter(self): + interrupter = self._graph.default_interrupter + self.assertIs(type(interrupter), bt2.Interrupter) def test_add_component_user_cls(self): class MySink(bt2._UserSinkComponent): @@ -76,7 +91,7 @@ class GraphTestCase(unittest.TestCase): comp_params = None class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): nonlocal comp_params comp_params = params @@ -84,7 +99,7 @@ class GraphTestCase(unittest.TestCase): pass params = {'hello': 23, 'path': '/path/to/stuff'} - comp = self._graph.add_component(MySink, 'salut', params) + self._graph.add_component(MySink, 'salut', params) self.assertEqual(params, comp_params) del comp_params @@ -92,7 +107,7 @@ class GraphTestCase(unittest.TestCase): comp_obj = None class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): nonlocal comp_obj comp_obj = obj @@ -100,7 +115,7 @@ class GraphTestCase(unittest.TestCase): pass obj = object() - comp = self._graph.add_component(MySink, 'salut', obj=obj) + self._graph.add_component(MySink, 'salut', obj=obj) self.assertIs(comp_obj, obj) del comp_obj @@ -108,27 +123,25 @@ class GraphTestCase(unittest.TestCase): comp_obj = None class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): nonlocal comp_obj comp_obj = obj def _user_consume(self): pass - comp = self._graph.add_component(MySink, 'salut') + self._graph.add_component(MySink, 'salut') self.assertIsNone(comp_obj) del comp_obj def test_add_component_obj_non_python_comp_cls(self): - comp_obj = None - plugin = bt2.find_plugin('text', find_in_user_dir=False, find_in_sys_dir=False) assert plugin is not None cc = plugin.source_component_classes['dmesg'] assert cc is not None with self.assertRaises(ValueError): - comp = self._graph.add_component(cc, 'salut', obj=57) + self._graph.add_component(cc, 'salut', obj=57) def test_add_component_invalid_cls_type(self): with self.assertRaises(TypeError): @@ -150,6 +163,48 @@ class GraphTestCase(unittest.TestCase): with self.assertRaises(ValueError): self._graph.add_component(MySink, 'salut', logging_level=12345) + def test_add_component_invalid_params_type(self): + class MySink(bt2._UserSinkComponent): + def _user_consume(self): + pass + + with self.assertRaises(TypeError): + self._graph.add_component(MySink, 'salut', params=12) + + def test_add_component_params_dict(self): + params_obj = None + + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + nonlocal params_obj + params_obj = params + + def _user_consume(self): + pass + + params = {'plage': 12312} + self._graph.add_component(MySink, 'salut', params=params) + + # Check equality and not identity because `add_component()` method + # converts the Python `dict` to a `bt2.MapValue`. + self.assertEqual(params, params_obj) + + def test_add_component_params_mapvalue(self): + params_obj = None + + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + nonlocal params_obj + params_obj = params + + def _user_consume(self): + pass + + params = bt2.MapValue({'beachclub': '121'}) + self._graph.add_component(MySink, 'salut', params=params) + + self.assertEqual(params, params_obj) + def test_add_component_logging_level(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): @@ -161,16 +216,14 @@ class GraphTestCase(unittest.TestCase): self.assertEqual(comp.logging_level, bt2.LoggingLevel.DEBUG) def test_connect_ports(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + class MySource( + bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator + ): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_consume(self): @@ -188,16 +241,14 @@ class GraphTestCase(unittest.TestCase): self.assertEqual(sink.input_ports['in'].connection.addr, conn.addr) def test_connect_ports_invalid_direction(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + class MySource( + bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator + ): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_consume(self): @@ -207,9 +258,7 @@ class GraphTestCase(unittest.TestCase): sink = self._graph.add_component(MySink, 'sink') with self.assertRaises(TypeError): - conn = self._graph.connect_ports( - sink.input_ports['in'], src.output_ports['out'] - ) + self._graph.connect_ports(sink.input_ports['in'], src.output_ports['out']) def test_add_interrupter(self): class MyIter(bt2._UserMessageIterator): @@ -217,11 +266,11 @@ class GraphTestCase(unittest.TestCase): raise TypeError class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_consume(self): @@ -262,17 +311,17 @@ class GraphTestCase(unittest.TestCase): return self._create_stream_beginning_message(self._stream) class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_consume(self): # Pretend that somebody asynchronously interrupted the graph. nonlocal graph - graph.interrupt() + graph.default_interrupter.set() return next(self._msg_iter) def _user_graph_is_configured(self): @@ -309,11 +358,11 @@ class GraphTestCase(unittest.TestCase): return msg class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._input_port = self._add_input_port('in') self._at = 0 @@ -321,16 +370,16 @@ class GraphTestCase(unittest.TestCase): msg = next(comp_self._msg_iter) if comp_self._at == 0: - self.assertIsInstance(msg, bt2._StreamBeginningMessage) + self.assertIs(type(msg), bt2._StreamBeginningMessageConst) elif comp_self._at == 1: - self.assertIsInstance(msg, bt2._PacketBeginningMessage) + self.assertIs(type(msg), bt2._PacketBeginningMessageConst) elif comp_self._at >= 2 and comp_self._at <= 6: - self.assertIsInstance(msg, bt2._EventMessage) + self.assertIs(type(msg), bt2._EventMessageConst) self.assertEqual(msg.event.cls.name, 'salut') elif comp_self._at == 7: - self.assertIsInstance(msg, bt2._PacketEndMessage) + self.assertIs(type(msg), bt2._PacketEndMessageConst) elif comp_self._at == 8: - self.assertIsInstance(msg, bt2._StreamEndMessage) + self.assertIs(type(msg), bt2._StreamEndMessageConst) comp_self._at += 1 @@ -341,9 +390,7 @@ class GraphTestCase(unittest.TestCase): src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - conn = self._graph.connect_ports( - src.output_ports['out'], sink.input_ports['in'] - ) + self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) self._graph.run() def test_run_once(self): @@ -351,11 +398,11 @@ class GraphTestCase(unittest.TestCase): pass class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._input_port = self._add_input_port('in') def _user_consume(comp_self): @@ -366,9 +413,7 @@ class GraphTestCase(unittest.TestCase): run_count = 0 src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - conn = self._graph.connect_ports( - src.output_ports['out'], sink.input_ports['in'] - ) + self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) with self.assertRaises(bt2.TryAgain): self._graph.run_once() @@ -380,11 +425,11 @@ class GraphTestCase(unittest.TestCase): pass class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._input_port = self._add_input_port('in') def _user_consume(comp_self): @@ -392,9 +437,7 @@ class GraphTestCase(unittest.TestCase): src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - conn = self._graph.connect_ports( - src.output_ports['out'], sink.input_ports['in'] - ) + self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) with self.assertRaises(bt2.Stop): self._graph.run_once() @@ -416,22 +459,22 @@ class GraphTestCase(unittest.TestCase): return msg class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._input_port = self._add_input_port('in') self._at = 0 def _user_consume(comp_self): msg = next(comp_self._msg_iter) if comp_self._at == 0: - self.assertIsInstance(msg, bt2._StreamBeginningMessage) + self.assertIs(type(msg), bt2._StreamBeginningMessageConst) elif comp_self._at == 1: - self.assertIsInstance(msg, bt2._PacketBeginningMessage) + self.assertIs(type(msg), bt2._PacketBeginningMessageConst) elif comp_self._at == 2: - self.assertIsInstance(msg, bt2._EventMessage) + self.assertIs(type(msg), bt2._EventMessageConst) raise bt2.TryAgain else: pass @@ -445,9 +488,7 @@ class GraphTestCase(unittest.TestCase): src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - conn = self._graph.connect_ports( - src.output_ports['out'], sink.input_ports['in'] - ) + self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) with self.assertRaises(bt2.TryAgain): self._graph.run() @@ -474,22 +515,22 @@ class GraphTestCase(unittest.TestCase): return msg class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._input_port = self._add_input_port('in') self._at = 0 def _user_consume(comp_self): msg = next(comp_self._msg_iter) if comp_self._at == 0: - self.assertIsInstance(msg, bt2._StreamBeginningMessage) + self.assertIs(type(msg), bt2._StreamBeginningMessageConst) elif comp_self._at == 1: - self.assertIsInstance(msg, bt2._PacketBeginningMessage) + self.assertIs(type(msg), bt2._PacketBeginningMessageConst) elif comp_self._at == 2: - self.assertIsInstance(msg, bt2._EventMessage) + self.assertIs(type(msg), bt2._EventMessageConst) elif comp_self._at == 3: nonlocal raised_in_sink raised_in_sink = True @@ -504,25 +545,21 @@ class GraphTestCase(unittest.TestCase): src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - conn = self._graph.connect_ports( - src.output_ports['out'], sink.input_ports['in'] - ) + self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) with self.assertRaises(bt2._Error): self._graph.run() def test_listeners(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + class MySource( + bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator + ): + def __init__(self, config, params, obj): self._add_output_port('out') self._add_output_port('zero') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_consume(self): @@ -535,28 +572,13 @@ class GraphTestCase(unittest.TestCase): nonlocal calls calls.append((port_added_listener, component, port)) - def ports_connected_listener( - upstream_component, upstream_port, downstream_component, downstream_port - ): - nonlocal calls - calls.append( - ( - ports_connected_listener, - upstream_component, - upstream_port, - downstream_component, - downstream_port, - ) - ) - calls = [] self._graph.add_port_added_listener(port_added_listener) - self._graph.add_ports_connected_listener(ports_connected_listener) src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) - self.assertEqual(len(calls), 5) + self.assertEqual(len(calls), 4) self.assertIs(calls[0][0], port_added_listener) self.assertEqual(calls[0][1].name, 'src') @@ -574,24 +596,16 @@ class GraphTestCase(unittest.TestCase): self.assertEqual(calls[3][1].name, 'sink') self.assertEqual(calls[3][2].name, 'taste') - self.assertIs(calls[4][0], ports_connected_listener) - self.assertEqual(calls[4][1].name, 'src') - self.assertEqual(calls[4][2].name, 'out') - self.assertEqual(calls[4][3].name, 'sink') - self.assertEqual(calls[4][4].name, 'in') - def test_invalid_listeners(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + class MySource( + bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator + ): + def __init__(self, config, params, obj): self._add_output_port('out') self._add_output_port('zero') class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_consume(self): @@ -602,12 +616,10 @@ class GraphTestCase(unittest.TestCase): with self.assertRaises(TypeError): self._graph.add_port_added_listener(1234) - with self.assertRaises(TypeError): - self._graph.add_ports_connected_listener(1234) def test_raise_in_component_init(self): class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): raise ValueError('oops!') def _user_consume(self): @@ -620,7 +632,7 @@ class GraphTestCase(unittest.TestCase): def test_raise_in_port_added_listener(self): class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_consume(self): @@ -635,31 +647,6 @@ class GraphTestCase(unittest.TestCase): with self.assertRaises(bt2._Error): graph.add_component(MySink, 'comp') - def test_raise_in_ports_connected_listener(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): - self._add_input_port('in') - - def _user_consume(self): - raise bt2.Stop - - def ports_connected_listener( - upstream_component, upstream_port, downstream_component, downstream_port - ): - raise ValueError('oh noes!') - - graph = bt2.Graph() - graph.add_ports_connected_listener(ports_connected_listener) - up = graph.add_component(MySource, 'down') - down = graph.add_component(MySink, 'up') - with self.assertRaises(bt2._Error): - graph.connect_ports(up.output_ports['out'], down.input_ports['in']) +if __name__ == '__main__': + unittest.main()