Use Black stable to format python code
[babeltrace.git] / tests / bindings / python / bt2 / test_graph.py
index 2ad2ac1f4395b0c8569910d12839558c34eac97a..dfaa69342fd680b40a8b885aec6f533b53eae740 100644 (file)
@@ -1,27 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-only
 #
 # Copyright (C) 2019 EfficiOS Inc.
 #
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; only version 2
-# of the License.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-#
 
 import unittest
 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
 
@@ -65,6 +52,10 @@ class GraphTestCase(unittest.TestCase):
         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):
             def _user_consume(self):
@@ -87,7 +78,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
 
@@ -103,7 +94,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
 
@@ -119,7 +110,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
 
@@ -159,6 +150,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):
@@ -170,16 +203,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):
@@ -197,16 +228,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):
@@ -224,20 +253,18 @@ 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):
                 next(self._msg_iter)
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
         # add two interrupters, set one of them
         interrupter1 = bt2.Interrupter()
@@ -269,23 +296,21 @@ 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):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
         graph = self._graph
         up = self._graph.add_component(MySource, 'down')
@@ -316,11 +341,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
 
@@ -342,9 +367,7 @@ class GraphTestCase(unittest.TestCase):
                 comp_self._at += 1
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_port
-                )
+                self._msg_iter = self._create_message_iterator(self._input_port)
 
         src = self._graph.add_component(MySource, 'src')
         sink = self._graph.add_component(MySink, 'sink')
@@ -356,11 +379,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):
@@ -383,11 +406,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):
@@ -417,11 +440,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
 
@@ -440,9 +463,7 @@ class GraphTestCase(unittest.TestCase):
                 comp_self._at += 1
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_port
-                )
+                self._msg_iter = self._create_message_iterator(self._input_port)
 
         src = self._graph.add_component(MySource, 'src')
         sink = self._graph.add_component(MySink, 'sink')
@@ -473,11 +494,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
 
@@ -497,9 +518,7 @@ class GraphTestCase(unittest.TestCase):
                 comp_self._at += 1
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_port
-                )
+                self._msg_iter = self._create_message_iterator(self._input_port)
 
         src = self._graph.add_component(MySource, 'src')
         sink = self._graph.add_component(MySink, 'sink')
@@ -509,17 +528,15 @@ class GraphTestCase(unittest.TestCase):
             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):
@@ -532,28 +549,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')
@@ -571,24 +573,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):
@@ -599,12 +593,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):
@@ -617,7 +609,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):
@@ -632,31 +624,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()
This page took 0.033258 seconds and 4 git commands to generate.