bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / tests / bindings / python / bt2 / test_graph.py
index bda08de2ec874aecb0c016c7e171fd14bf73e3e9..2ad2ac1f4395b0c8569910d12839558c34eac97a 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
 
@@ -87,10 +95,50 @@ 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
 
+    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()
+        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
+
+        self._graph.add_component(MySink, 'salut')
+        self.assertIsNone(comp_obj)
+        del comp_obj
+
+    def test_add_component_obj_non_python_comp_cls(self):
+        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):
+            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 +175,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 +202,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):
@@ -168,9 +216,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):
@@ -178,11 +224,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 +269,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 +316,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
 
@@ -282,16 +328,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
 
@@ -302,11 +348,58 @@ 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):
+        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')
+        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')
+        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,22 +417,22 @@ 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 _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
@@ -353,9 +446,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()
@@ -382,22 +473,22 @@ 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 _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
@@ -412,9 +503,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._Error):
             self._graph.run()
@@ -425,12 +514,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 +583,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 +604,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 +617,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 +638,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.028893 seconds and 4 git commands to generate.