bt2: wrap bt_graph_run_once() (Graph.run_once())
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sun, 4 Aug 2019 05:11:03 +0000 (01:11 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 5 Aug 2019 19:10:13 +0000 (15:10 -0400)
As opposed to Graph.run() which simply returns `None` when it's finished
running, Graph.run_once() needs a way to indicate that the whole graph
is finished. In that case, the method raises `bt2.Stop`. Otherwise,
Graph.run_once() returns `None`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I8c5908b7b1a265486534a3ec01c9eb813c2cde7c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1819
Tested-by: jenkins <jenkins@lttng.org>
src/bindings/python/bt2/bt2/graph.py
tests/bindings/python/bt2/test_graph.py

index b8cdb1f4f9653682baa0830629206b2c68904d24..6c75d52b843d1de01911b07632bc7d2e849971bb 100644 (file)
@@ -175,6 +175,10 @@ class Graph(object._SharedObject):
 
         return utils._ListenerHandle(listener_ids, self)
 
+    def run_once(self):
+        status = native_bt.graph_run_once(self._ptr)
+        utils._handle_func_status(status, 'graph object could not run once')
+
     def run(self):
         status = native_bt.graph_run(self._ptr)
 
index b7c0f0cb22f19cf6f82ce7a7d424b1f48adf2127..49c08512505c8210e2446ff338ee91cbb91edeb6 100644 (file)
@@ -349,6 +349,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):
This page took 0.029181 seconds and 4 git commands to generate.