bt2: check that port is connected when creating message iterator
authorSimon Marchi <simon.marchi@efficios.com>
Sun, 12 Apr 2020 18:52:20 +0000 (14:52 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 14 Apr 2020 19:59:39 +0000 (15:59 -0400)
Creating a message iterator for a non-connected input port hits a
precondition assertion.  Add some checks in the Python bindings to raise
a more friendly ValueError in that case.

Change-Id: I323e49c7408f5df8e1702eb993933ffc251b6c63
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/3396
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/bindings/python/bt2/bt2/component.py
src/bindings/python/bt2/bt2/message_iterator.py
tests/bindings/python/bt2/test_message_iterator.py

index 4e079f7743d934f1b6413efe35c0255028cf0b86..4a7e6bf1541c513a04edd0a9fd2feebf3cbde11d 100644 (file)
@@ -970,6 +970,9 @@ class _UserSinkComponent(_UserComponent, _SinkComponentConst):
     def _create_message_iterator(self, input_port):
         utils._check_type(input_port, bt2_port._UserComponentInputPort)
 
+        if not input_port.is_connected:
+            raise ValueError('input port is not connected')
+
         (
             status,
             msg_iter_ptr,
index ed5b8e58614cebf7d01f3313f50c267d84936a3e..56f67a5ad1b268c6425c3f7b7add6ee785b5014e 100644 (file)
@@ -218,6 +218,9 @@ class _UserMessageIterator(_MessageIterator):
     def _create_message_iterator(self, input_port):
         utils._check_type(input_port, bt2_port._UserComponentInputPort)
 
+        if not input_port.is_connected:
+            raise ValueError('input port is not connected')
+
         (
             status,
             msg_iter_ptr,
index 54fd232907dadc4c7a51f122844b1d31bb54bbe7..a40283b34f5ffcaa75b5e2d6245e03f3748385e7 100644 (file)
@@ -100,6 +100,67 @@ class UserMessageIteratorTestCase(unittest.TestCase):
         self.assertTrue(src_iter_initialized)
         self.assertTrue(flt_iter_initialized)
 
+    # Test that creating a message iterator from a sink component on a
+    # non-connected inport port raises.
+    def test_create_from_sink_component_unconnected_port_raises(self):
+        class MySink(bt2._UserSinkComponent):
+            def __init__(comp_self, config, params, obj):
+                comp_self._input_port = comp_self._add_input_port('in')
+
+            def _user_graph_is_configured(comp_self):
+                with self.assertRaisesRegex(ValueError, 'input port is not connected'):
+                    comp_self._create_message_iterator(comp_self._input_port)
+
+                nonlocal seen
+                seen = True
+
+            def _user_consume(self):
+                raise bt2.Stop
+
+        seen = False
+        graph = bt2.Graph()
+        graph.add_component(MySink, 'snk')
+        graph.run()
+        self.assertTrue(seen)
+
+    # Test that creating a message iterator from a message iteartor on a
+    # non-connected inport port raises.
+    def test_create_from_message_iterator_unconnected_port_raises(self):
+        class MyFilterIter(bt2._UserMessageIterator):
+            def __init__(iter_self, config, port):
+                input_port = iter_self._component._input_ports['in']
+
+                with self.assertRaisesRegex(ValueError, 'input port is not connected'):
+                    iter_self._create_message_iterator(input_port)
+
+                nonlocal seen
+                seen = True
+
+        class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter):
+            def __init__(comp_self, config, params, obj):
+                comp_self._add_input_port('in')
+                comp_self._add_output_port('out')
+
+        class MySink(bt2._UserSinkComponent):
+            def __init__(comp_self, config, params, obj):
+                comp_self._input_port = comp_self._add_input_port('in')
+
+            def _user_graph_is_configured(comp_self):
+                comp_self._input_iter = comp_self._create_message_iterator(
+                    comp_self._input_port
+                )
+
+            def _user_consume(self):
+                raise bt2.Stop
+
+        seen = False
+        graph = bt2.Graph()
+        flt = graph.add_component(MyFilter, 'flt')
+        snk = graph.add_component(MySink, 'snk')
+        graph.connect_ports(flt.output_ports['out'], snk.input_ports['in'])
+        graph.run()
+        self.assertTrue(seen)
+
     def test_create_user_error(self):
         # This tests both error handling by
         # _UserSinkComponent._create_message_iterator
This page took 0.027613 seconds and 4 git commands to generate.