bt2: pass custom Python object to Python component's __init__()
[babeltrace.git] / tests / bindings / python / bt2 / test_graph.py
index 453a62ed7a115f9f71ad8d72e93b89537473dde3..b7c0f0cb22f19cf6f82ce7a7d424b1f48adf2127 100644 (file)
@@ -59,10 +59,7 @@ class GraphTestCase(unittest.TestCase):
 
     def test_add_component_user_cls(self):
         class MySink(bt2._UserSinkComponent):
-            def _consume(self):
-                pass
-
-            def _graph_is_configured(self):
+            def _user_consume(self):
                 pass
 
         comp = self._graph.add_component(MySink, 'salut')
@@ -70,10 +67,7 @@ class GraphTestCase(unittest.TestCase):
 
     def test_add_component_gen_cls(self):
         class MySink(bt2._UserSinkComponent):
-            def _consume(self):
-                pass
-
-            def _graph_is_configured(self):
+            def _user_consume(self):
                 pass
 
         comp = self._graph.add_component(MySink, 'salut')
@@ -85,14 +79,11 @@ class GraphTestCase(unittest.TestCase):
         comp_params = None
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 nonlocal comp_params
                 comp_params = params
 
-            def _consume(self):
-                pass
-
-            def _graph_is_configured(self):
+            def _user_consume(self):
                 pass
 
         params = {'hello': 23, 'path': '/path/to/stuff'}
@@ -100,16 +91,55 @@ class GraphTestCase(unittest.TestCase):
         self.assertEqual(params, comp_params)
         del comp_params
 
+    def test_add_component_obj_python_comp_cls(self):
+        comp_obj = None
+
+        class MySink(bt2._UserSinkComponent):
+            def __init__(self, params, obj):
+                nonlocal comp_obj
+                comp_obj = obj
+
+            def _user_consume(self):
+                pass
+
+        obj = object()
+        comp = self._graph.add_component(MySink, 'salut', obj=obj)
+        self.assertIs(comp_obj, obj)
+        del comp_obj
+
+    def test_add_component_obj_none_python_comp_cls(self):
+        comp_obj = None
+
+        class MySink(bt2._UserSinkComponent):
+            def __init__(self, params, obj):
+                nonlocal comp_obj
+                comp_obj = obj
+
+            def _user_consume(self):
+                pass
+
+        comp = 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)
+
     def test_add_component_invalid_cls_type(self):
         with self.assertRaises(TypeError):
             self._graph.add_component(int, 'salut')
 
     def test_add_component_invalid_logging_level_type(self):
         class MySink(bt2._UserSinkComponent):
-            def _consume(self):
-                pass
-
-            def _graph_is_configured(self):
+            def _user_consume(self):
                 pass
 
         with self.assertRaises(TypeError):
@@ -117,10 +147,7 @@ class GraphTestCase(unittest.TestCase):
 
     def test_add_component_invalid_logging_level_value(self):
         class MySink(bt2._UserSinkComponent):
-            def _consume(self):
-                pass
-
-            def _graph_is_configured(self):
+            def _user_consume(self):
                 pass
 
         with self.assertRaises(ValueError):
@@ -128,10 +155,7 @@ class GraphTestCase(unittest.TestCase):
 
     def test_add_component_logging_level(self):
         class MySink(bt2._UserSinkComponent):
-            def _consume(self):
-                pass
-
-            def _graph_is_configured(self):
+            def _user_consume(self):
                 pass
 
         comp = self._graph.add_component(
@@ -145,19 +169,16 @@ class GraphTestCase(unittest.TestCase):
                 raise bt2.Stop
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 raise bt2.Stop
 
-            def _graph_is_configured(self):
-                pass
-
         src = self._graph.add_component(MySource, 'src')
         sink = self._graph.add_component(MySink, 'sink')
 
@@ -175,19 +196,16 @@ class GraphTestCase(unittest.TestCase):
                 raise bt2.Stop
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 raise bt2.Stop
 
-            def _graph_is_configured(self):
-                pass
-
         src = self._graph.add_component(MySource, 'src')
         sink = self._graph.add_component(MySink, 'sink')
 
@@ -202,17 +220,17 @@ class GraphTestCase(unittest.TestCase):
                 raise TypeError
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 next(self._msg_iter)
 
-            def _graph_is_configured(self):
+            def _user_graph_is_configured(self):
                 self._msg_iter = self._create_input_port_message_iterator(
                     self._input_ports['in']
                 )
@@ -247,20 +265,20 @@ class GraphTestCase(unittest.TestCase):
                 return self._create_stream_beginning_message(self._stream)
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 # Pretend that somebody asynchronously interrupted the graph.
                 nonlocal graph
                 graph.interrupt()
                 return next(self._msg_iter)
 
-            def _graph_is_configured(self):
+            def _user_graph_is_configured(self):
                 self._msg_iter = self._create_input_port_message_iterator(
                     self._input_ports['in']
                 )
@@ -294,15 +312,15 @@ class GraphTestCase(unittest.TestCase):
                 return msg
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._input_port = self._add_input_port('in')
                 self._at = 0
 
-            def _consume(comp_self):
+            def _user_consume(comp_self):
                 msg = next(comp_self._msg_iter)
 
                 if comp_self._at == 0:
@@ -319,7 +337,7 @@ class GraphTestCase(unittest.TestCase):
 
                 comp_self._at += 1
 
-            def _graph_is_configured(self):
+            def _user_graph_is_configured(self):
                 self._msg_iter = self._create_input_port_message_iterator(
                     self._input_port
                 )
@@ -348,15 +366,15 @@ class GraphTestCase(unittest.TestCase):
                 return msg
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._input_port = self._add_input_port('in')
                 self._at = 0
 
-            def _consume(comp_self):
+            def _user_consume(comp_self):
                 msg = next(comp_self._msg_iter)
                 if comp_self._at == 0:
                     self.assertIsInstance(msg, bt2._StreamBeginningMessage)
@@ -370,7 +388,7 @@ class GraphTestCase(unittest.TestCase):
 
                 comp_self._at += 1
 
-            def _graph_is_configured(self):
+            def _user_graph_is_configured(self):
                 self._msg_iter = self._create_input_port_message_iterator(
                     self._input_port
                 )
@@ -406,15 +424,15 @@ class GraphTestCase(unittest.TestCase):
                 return msg
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._input_port = self._add_input_port('in')
                 self._at = 0
 
-            def _consume(comp_self):
+            def _user_consume(comp_self):
                 msg = next(comp_self._msg_iter)
                 if comp_self._at == 0:
                     self.assertIsInstance(msg, bt2._StreamBeginningMessage)
@@ -429,7 +447,7 @@ class GraphTestCase(unittest.TestCase):
 
                 comp_self._at += 1
 
-            def _graph_is_configured(self):
+            def _user_graph_is_configured(self):
                 self._msg_iter = self._create_input_port_message_iterator(
                     self._input_port
                 )
@@ -449,21 +467,18 @@ class GraphTestCase(unittest.TestCase):
                 raise bt2.Stop
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
                 self._add_output_port('zero')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 raise bt2.Stop
 
-            def _graph_is_configured(self):
-                pass
-
-            def _port_connected(self, port, other_port):
+            def _user_port_connected(self, port, other_port):
                 self._add_input_port('taste')
 
         def port_added_listener(component, port):
@@ -521,21 +536,18 @@ class GraphTestCase(unittest.TestCase):
                 raise bt2.Stop
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
                 self._add_output_port('zero')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 raise bt2.Stop
 
-            def _graph_is_configured(self):
-                pass
-
-            def _port_connected(self, port, other_port):
+            def _user_port_connected(self, port, other_port):
                 self._add_input_port('taste')
 
         with self.assertRaises(TypeError):
@@ -545,15 +557,12 @@ class GraphTestCase(unittest.TestCase):
 
     def test_raise_in_component_init(self):
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 raise ValueError('oops!')
 
-            def _consume(self):
+            def _user_consume(self):
                 raise bt2.Stop
 
-            def _graph_is_configured(self):
-                pass
-
         graph = bt2.Graph()
 
         with self.assertRaises(bt2._Error):
@@ -561,15 +570,12 @@ class GraphTestCase(unittest.TestCase):
 
     def test_raise_in_port_added_listener(self):
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 raise bt2.Stop
 
-            def _graph_is_configured(self):
-                pass
-
         def port_added_listener(component, port):
             raise ValueError('oh noes!')
 
@@ -585,19 +591,16 @@ class GraphTestCase(unittest.TestCase):
                 raise bt2.Stop
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 raise bt2.Stop
 
-            def _graph_is_configured(self):
-                pass
-
         def ports_connected_listener(
             upstream_component, upstream_port, downstream_component, downstream_port
         ):
This page took 0.029953 seconds and 4 git commands to generate.