From 415d43a1b12feae338e910af6d6a1865054c2ef4 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Sun, 12 Apr 2020 14:52:20 -0400 Subject: [PATCH] bt2: check that port is connected when creating message iterator 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/3396 Reviewed-by: Philippe Proulx --- src/bindings/python/bt2/bt2/component.py | 3 + .../python/bt2/bt2/message_iterator.py | 3 + .../python/bt2/test_message_iterator.py | 61 +++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/src/bindings/python/bt2/bt2/component.py b/src/bindings/python/bt2/bt2/component.py index 4e079f77..4a7e6bf1 100644 --- a/src/bindings/python/bt2/bt2/component.py +++ b/src/bindings/python/bt2/bt2/component.py @@ -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, diff --git a/src/bindings/python/bt2/bt2/message_iterator.py b/src/bindings/python/bt2/bt2/message_iterator.py index ed5b8e58..56f67a5a 100644 --- a/src/bindings/python/bt2/bt2/message_iterator.py +++ b/src/bindings/python/bt2/bt2/message_iterator.py @@ -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, diff --git a/tests/bindings/python/bt2/test_message_iterator.py b/tests/bindings/python/bt2/test_message_iterator.py index 54fd2329..a40283b3 100644 --- a/tests/bindings/python/bt2/test_message_iterator.py +++ b/tests/bindings/python/bt2/test_message_iterator.py @@ -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 -- 2.34.1