From 8cc0e6eacdc050f30629b99a5c9d2dc4ba4513c3 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sun, 4 Aug 2019 01:11:03 -0400 Subject: [PATCH] bt2: wrap bt_graph_run_once() (Graph.run_once()) 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 Change-Id: I8c5908b7b1a265486534a3ec01c9eb813c2cde7c Reviewed-on: https://review.lttng.org/c/babeltrace/+/1819 Tested-by: jenkins --- src/bindings/python/bt2/bt2/graph.py | 4 ++ tests/bindings/python/bt2/test_graph.py | 53 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/bindings/python/bt2/bt2/graph.py b/src/bindings/python/bt2/bt2/graph.py index b8cdb1f4..6c75d52b 100644 --- a/src/bindings/python/bt2/bt2/graph.py +++ b/src/bindings/python/bt2/bt2/graph.py @@ -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) diff --git a/tests/bindings/python/bt2/test_graph.py b/tests/bindings/python/bt2/test_graph.py index b7c0f0cb..49c08512 100644 --- a/tests/bindings/python/bt2/test_graph.py +++ b/tests/bindings/python/bt2/test_graph.py @@ -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): -- 2.34.1