bt2: make `bt2._OverflowError` inherit `bt2._Error`
[babeltrace.git] / tests / bindings / python / bt2 / test_graph.py
index f6f5dcaae9eab4a5bc70d06e8f1fcb873d19a1f2..453a62ed7a115f9f71ad8d72e93b89537473dde3 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):
@@ -266,21 +306,23 @@ class GraphTestCase(unittest.TestCase):
                 msg = next(comp_self._msg_iter)
 
                 if comp_self._at == 0:
-                    self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+                    self.assertIsInstance(msg, bt2._StreamBeginningMessage)
                 elif comp_self._at == 1:
-                    self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
+                    self.assertIsInstance(msg, bt2._PacketBeginningMessage)
                 elif comp_self._at >= 2 and comp_self._at <= 6:
-                    self.assertIsInstance(msg, bt2.message._EventMessage)
+                    self.assertIsInstance(msg, bt2._EventMessage)
                     self.assertEqual(msg.event.cls.name, 'salut')
                 elif comp_self._at == 7:
-                    self.assertIsInstance(msg, bt2.message._PacketEndMessage)
+                    self.assertIsInstance(msg, bt2._PacketEndMessage)
                 elif comp_self._at == 8:
-                    self.assertIsInstance(msg, bt2.message._StreamEndMessage)
+                    self.assertIsInstance(msg, bt2._StreamEndMessage)
 
                 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')
@@ -317,11 +359,11 @@ class GraphTestCase(unittest.TestCase):
             def _consume(comp_self):
                 msg = next(comp_self._msg_iter)
                 if comp_self._at == 0:
-                    self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+                    self.assertIsInstance(msg, bt2._StreamBeginningMessage)
                 elif comp_self._at == 1:
-                    self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
+                    self.assertIsInstance(msg, bt2._PacketBeginningMessage)
                 elif comp_self._at == 2:
-                    self.assertIsInstance(msg, bt2.message._EventMessage)
+                    self.assertIsInstance(msg, bt2._EventMessage)
                     raise bt2.TryAgain
                 else:
                     pass
@@ -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')
@@ -373,11 +417,11 @@ class GraphTestCase(unittest.TestCase):
             def _consume(comp_self):
                 msg = next(comp_self._msg_iter)
                 if comp_self._at == 0:
-                    self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+                    self.assertIsInstance(msg, bt2._StreamBeginningMessage)
                 elif comp_self._at == 1:
-                    self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
+                    self.assertIsInstance(msg, bt2._PacketBeginningMessage)
                 elif comp_self._at == 2:
-                    self.assertIsInstance(msg, bt2.message._EventMessage)
+                    self.assertIsInstance(msg, bt2._EventMessage)
                 elif comp_self._at == 3:
                     nonlocal raised_in_sink
                     raised_in_sink = True
@@ -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')
This page took 0.026813 seconds and 4 git commands to generate.