lib: bt_graph_create(): accept MIP version
[babeltrace.git] / tests / bindings / python / bt2 / test_graph.py
index bda08de2ec874aecb0c016c7e171fd14bf73e3e9..b719d9ca133e7b6956e7bbdb72a0bfc257f4ae01 100644 (file)
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #
 
-from bt2 import value
-import collections
 import unittest
-import copy
 import bt2
 
 
@@ -54,8 +51,19 @@ 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_add_component_user_cls(self):
         class MySink(bt2._UserSinkComponent):
@@ -79,7 +87,7 @@ 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
 
@@ -91,6 +99,48 @@ 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')
@@ -127,11 +177,11 @@ 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 _user_consume(self):
@@ -154,11 +204,11 @@ 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 _user_consume(self):
@@ -178,11 +228,11 @@ 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 _user_consume(self):
@@ -223,11 +273,11 @@ 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 _user_consume(self):
@@ -270,11 +320,11 @@ 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
 
@@ -307,6 +357,59 @@ class GraphTestCase(unittest.TestCase):
         )
         self._graph.run()
 
+    def test_run_once(self):
+        class MyIter(_MyIter):
+            pass
+
+        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._input_port = self._add_input_port('in')
+
+            def _user_consume(comp_self):
+                nonlocal run_count
+                run_count += 1
+                raise bt2.TryAgain
+
+        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']
+        )
+
+        with self.assertRaises(bt2.TryAgain):
+            self._graph.run_once()
+
+        self.assertEqual(run_count, 1)
+
+    def test_run_once_stops(self):
+        class MyIter(_MyIter):
+            pass
+
+        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._input_port = self._add_input_port('in')
+
+            def _user_consume(comp_self):
+                raise bt2.Stop
+
+        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']
+        )
+
+        with self.assertRaises(bt2.Stop):
+            self._graph.run_once()
+
     def test_run_again(self):
         class MyIter(_MyIter):
             def __next__(self):
@@ -324,11 +427,11 @@ 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
 
@@ -382,11 +485,11 @@ 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
 
@@ -425,12 +528,12 @@ 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 _user_consume(self):
@@ -494,12 +597,12 @@ 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 _user_consume(self):
@@ -515,7 +618,7 @@ 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 _user_consume(self):
@@ -528,7 +631,7 @@ 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 _user_consume(self):
@@ -549,11 +652,11 @@ 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 _user_consume(self):
This page took 0.043159 seconds and 4 git commands to generate.