lib: add bt_{graph,query_executor}_add_interrupter()
[babeltrace.git] / tests / bindings / python / bt2 / test_graph.py
index 1c183c9bf1861be1103c1e56b41fabd3840d7b0b..7a403b5cd3e68dd8b949cd88e72c78659c8cabd7 100644 (file)
@@ -196,14 +196,52 @@ class GraphTestCase(unittest.TestCase):
                 sink.input_ports['in'], src.output_ports['out']
             )
 
-    def test_cancel(self):
-        self.assertFalse(self._graph.is_canceled)
-        self._graph.cancel()
-        self.assertTrue(self._graph.is_canceled)
-
-    # Test that Graph.run() raises bt2.Canceled if the graph gets canceled
-    # during execution.
-    def test_cancel_while_running(self):
+    def test_add_interrupter(self):
+        class MyIter(bt2._UserMessageIterator):
+            def __next__(self):
+                raise TypeError
+
+        class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
+            def __init__(self, params):
+                self._add_output_port('out')
+
+        class MySink(bt2._UserSinkComponent):
+            def __init__(self, params):
+                self._add_input_port('in')
+
+            def _consume(self):
+                next(self._msg_iter)
+
+            def _graph_is_configured(self):
+                self._msg_iter = self._create_input_port_message_iterator(
+                    self._input_ports['in']
+                )
+
+        # add two interrupters, set one of them
+        interrupter1 = bt2.Interrupter()
+        interrupter2 = bt2.Interrupter()
+        self._graph.add_interrupter(interrupter1)
+        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._graph.add_interrupter(interrupter2)
+
+        with self.assertRaises(bt2._Error):
+            self._graph.run()
+
+        interrupter2.set()
+
+        with self.assertRaises(bt2.TryAgain):
+            self._graph.run()
+
+        interrupter2.reset()
+
+        with self.assertRaises(bt2._Error):
+            self._graph.run()
+
+    # Test that Graph.run() raises bt2.Interrupted if the graph gets
+    # interrupted during execution.
+    def test_interrupt_while_running(self):
         class MyIter(_MyIter):
             def __next__(self):
                 return self._create_stream_beginning_message(self._stream)
@@ -217,21 +255,23 @@ class GraphTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _consume(self):
-                # Pretend that somebody asynchronously cancelled the graph.
+                # Pretend that somebody asynchronously interrupted the graph.
                 nonlocal graph
-                graph.cancel()
-
+                graph.interrupt()
                 return next(self._msg_iter)
 
             def _graph_is_configured(self):
-                self._msg_iter = self._input_ports['in'].create_message_iterator()
+                self._msg_iter = self._create_input_port_message_iterator(
+                    self._input_ports['in']
+                )
 
-        graph = bt2.Graph()
-        up = graph.add_component(MySource, 'down')
-        down = graph.add_component(MySink, 'up')
-        graph.connect_ports(up.output_ports['out'], down.input_ports['in'])
-        with self.assertRaises(bt2.Canceled):
-            graph.run()
+        graph = self._graph
+        up = self._graph.add_component(MySource, 'down')
+        down = self._graph.add_component(MySink, 'up')
+        self._graph.connect_ports(up.output_ports['out'], down.input_ports['in'])
+
+        with self.assertRaises(bt2.TryAgain):
+            self._graph.run()
 
     def test_run(self):
         class MyIter(_MyIter):
@@ -280,7 +320,9 @@ class GraphTestCase(unittest.TestCase):
                 comp_self._at += 1
 
             def _graph_is_configured(self):
-                self._msg_iter = self._input_port.create_message_iterator()
+                self._msg_iter = self._create_input_port_message_iterator(
+                    self._input_port
+                )
 
         src = self._graph.add_component(MySource, 'src')
         sink = self._graph.add_component(MySink, 'sink')
@@ -329,7 +371,9 @@ class GraphTestCase(unittest.TestCase):
                 comp_self._at += 1
 
             def _graph_is_configured(self):
-                self._msg_iter = self._input_port.create_message_iterator()
+                self._msg_iter = self._create_input_port_message_iterator(
+                    self._input_port
+                )
 
         src = self._graph.add_component(MySource, 'src')
         sink = self._graph.add_component(MySink, 'sink')
@@ -386,7 +430,9 @@ class GraphTestCase(unittest.TestCase):
                 comp_self._at += 1
 
             def _graph_is_configured(self):
-                self._msg_iter = self._input_port.create_message_iterator()
+                self._msg_iter = self._create_input_port_message_iterator(
+                    self._input_port
+                )
 
         src = self._graph.add_component(MySource, 'src')
         sink = self._graph.add_component(MySink, 'sink')
@@ -394,7 +440,7 @@ class GraphTestCase(unittest.TestCase):
             src.output_ports['out'], sink.input_ports['in']
         )
 
-        with self.assertRaises(bt2.Error):
+        with self.assertRaises(bt2._Error):
             self._graph.run()
 
     def test_listeners(self):
@@ -510,7 +556,7 @@ class GraphTestCase(unittest.TestCase):
 
         graph = bt2.Graph()
 
-        with self.assertRaises(bt2.Error):
+        with self.assertRaises(bt2._Error):
             graph.add_component(MySink, 'comp')
 
     def test_raise_in_port_added_listener(self):
@@ -530,7 +576,7 @@ class GraphTestCase(unittest.TestCase):
         graph = bt2.Graph()
         graph.add_port_added_listener(port_added_listener)
 
-        with self.assertRaises(bt2.Error):
+        with self.assertRaises(bt2._Error):
             graph.add_component(MySink, 'comp')
 
     def test_raise_in_ports_connected_listener(self):
@@ -562,5 +608,5 @@ class GraphTestCase(unittest.TestCase):
         up = graph.add_component(MySource, 'down')
         down = graph.add_component(MySink, 'up')
 
-        with self.assertRaises(bt2.Error):
+        with self.assertRaises(bt2._Error):
             graph.connect_ports(up.output_ports['out'], down.input_ports['in'])
This page took 0.025744 seconds and 4 git commands to generate.