Move to kernel style SPDX license identifiers
[babeltrace.git] / tests / bindings / python / bt2 / test_message_iterator.py
index d03cdb302757b4b0b698bc94b5ce9755921b6daf..54fd232907dadc4c7a51f122844b1d31bb54bbe7 100644 (file)
@@ -1,20 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-only
 #
 # Copyright (C) 2019 EfficiOS Inc.
 #
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; only version 2
-# of the License.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-#
 
 import unittest
 import bt2
@@ -35,9 +22,7 @@ class SimpleSink(bt2._UserSinkComponent):
         next(self._msg_iter)
 
     def _user_graph_is_configured(self):
-        self._msg_iter = self._create_input_port_message_iterator(
-            self._input_ports['in']
-        )
+        self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
 
 def _create_graph(src_comp_cls, sink_comp_cls, flt_comp_cls=None):
@@ -96,7 +81,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
             def __init__(self, config, self_port_output):
                 nonlocal flt_iter_initialized
                 flt_iter_initialized = True
-                self._up_iter = self._create_input_port_message_iterator(
+                self._up_iter = self._create_message_iterator(
                     self._component._input_ports['in']
                 )
 
@@ -117,8 +102,8 @@ class UserMessageIteratorTestCase(unittest.TestCase):
 
     def test_create_user_error(self):
         # This tests both error handling by
-        # _UserSinkComponent._create_input_port_message_iterator
-        # and _UserMessageIterator._create_input_port_message_iterator, as they
+        # _UserSinkComponent._create_message_iterator
+        # and _UserMessageIterator._create_message_iterator, as they
         # are both used in the graph.
         class MySourceIter(bt2._UserMessageIterator):
             def __init__(self, config, self_port_output):
@@ -132,9 +117,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
             def __init__(self, config, self_port_output):
                 # This is expected to raise because of the error in
                 # MySourceIter.__init__.
-                self._create_input_port_message_iterator(
-                    self._component._input_ports['in']
-                )
+                self._create_message_iterator(self._component._input_ports['in'])
 
         class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter):
             def __init__(self, config, params, obj):
@@ -200,9 +183,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 nonlocal can_seek_forward
@@ -348,9 +329,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
         class MyFilterIter(bt2._UserMessageIterator):
             def __init__(self, port):
                 input_port = port.user_data
-                self._upstream_iter = self._create_input_port_message_iterator(
-                    input_port
-                )
+                self._upstream_iter = self._create_message_iterator(input_port)
 
             def __next__(self):
                 return next(self._upstream_iter)
@@ -379,6 +358,61 @@ class UserMessageIteratorTestCase(unittest.TestCase):
             with self.assertRaises(bt2.TryAgain):
                 next(it)
 
+    def test_error_in_iterator_with_cycle_after_having_created_upstream_iterator(self):
+        # Test a failure that triggered an abort in libbabeltrace2, in this situation:
+        #
+        #   - The filter iterator creates an upstream iterator.
+        #   - The filter iterator creates a reference cycle, including itself.
+        #   - An exception is raised, causing the filter iterator's
+        #     initialization method to fail.
+        class MySourceIter(bt2._UserMessageIterator):
+            pass
+
+        class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter):
+            def __init__(self, config, params, obj):
+                self._add_output_port('out')
+
+        class MyFilterIter(bt2._UserMessageIterator):
+            def __init__(self, config, port):
+                # First, create an upstream iterator.
+                self._upstream_iter = self._create_message_iterator(
+                    self._component._input_ports['in']
+                )
+
+                # Then, voluntarily make a reference cycle that will keep this
+                # Python object alive, which will keep the upstream iterator
+                # Babeltrace object alive.
+                self._self = self
+
+                # Finally, raise an exception to make __init__ fail.
+                raise ValueError('woops')
+
+        class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter):
+            def __init__(self, config, params, obj):
+                self._in = self._add_input_port('in')
+                self._out = self._add_output_port('out')
+
+        class MySink(bt2._UserSinkComponent):
+            def __init__(self, config, params, obj):
+                self._input_port = self._add_input_port('in')
+
+            def _user_graph_is_configured(self):
+                self._upstream_iter = self._create_message_iterator(self._input_port)
+
+            def _user_consume(self):
+                # We should not reach this.
+                assert False
+
+        g = bt2.Graph()
+        src = g.add_component(MySource, 'src')
+        flt = g.add_component(MyFilter, 'flt')
+        snk = g.add_component(MySink, 'snk')
+        g.connect_ports(src.output_ports['out'], flt.input_ports['in'])
+        g.connect_ports(flt.output_ports['out'], snk.input_ports['in'])
+
+        with self.assertRaisesRegex(bt2._Error, 'ValueError: woops'):
+            g.run()
+
 
 def _setup_seek_test(
     sink_cls,
@@ -436,7 +470,7 @@ def _setup_seek_test(
 
     class MyFilterIter(bt2._UserMessageIterator):
         def __init__(self, config, port):
-            self._upstream_iter = self._create_input_port_message_iterator(
+            self._upstream_iter = self._create_message_iterator(
                 self._component._input_ports['in']
             )
             config.can_seek_forward = self._upstream_iter.can_seek_forward
@@ -478,9 +512,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 nonlocal can_seek_beginning
@@ -514,9 +546,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 nonlocal can_seek_beginning
@@ -538,9 +568,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 nonlocal can_seek_beginning
@@ -557,9 +585,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 # This is expected to raise.
@@ -586,9 +612,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 # This is expected to raise.
@@ -615,9 +639,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 nonlocal do_seek_beginning
@@ -659,9 +681,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 self._msg_iter.seek_beginning()
@@ -719,7 +739,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
                 )
 
     def test_can_seek_ns_from_origin_returns_false_can_seek_beginning_forward_seekable(
-        self
+        self,
     ):
         # Test the case where:
         #
@@ -738,7 +758,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
         )
 
     def test_can_seek_ns_from_origin_returns_false_can_seek_beginning_not_forward_seekable(
-        self
+        self,
     ):
         # Test the case where:
         #
@@ -757,7 +777,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
         )
 
     def test_can_seek_ns_from_origin_returns_false_cant_seek_beginning_forward_seekable(
-        self
+        self,
     ):
         # Test the case where:
         #
@@ -776,7 +796,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
         )
 
     def test_can_seek_ns_from_origin_returns_false_cant_seek_beginning_not_forward_seekable(
-        self
+        self,
     ):
         # Test the case where:
         #
@@ -814,7 +834,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
                 )
 
     def test_no_can_seek_ns_from_origin_no_seek_ns_from_origin_can_seek_beginning_forward_seekable(
-        self
+        self,
     ):
         # Test the case where:
         #
@@ -833,7 +853,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
         )
 
     def test_no_can_seek_ns_from_origin_no_seek_ns_from_origin_can_seek_beginning_not_forward_seekable(
-        self
+        self,
     ):
         # Test the case where:
         #
@@ -852,7 +872,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
         )
 
     def test_no_can_seek_ns_from_origin_no_seek_ns_from_origin_cant_seek_beginning_forward_seekable(
-        self
+        self,
     ):
         # Test the case where:
         #
@@ -871,7 +891,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
         )
 
     def test_no_can_seek_ns_from_origin_no_seek_ns_from_origin_cant_seek_beginning_not_forward_seekable(
-        self
+        self,
     ):
         # Test the case where:
         #
@@ -902,9 +922,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 nonlocal can_seek_ns_from_origin
@@ -961,9 +979,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 # This is expected to raise.
@@ -990,9 +1006,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 # This is expected to raise.
@@ -1019,9 +1033,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase):
                 self._add_input_port('in')
 
             def _user_graph_is_configured(self):
-                self._msg_iter = self._create_input_port_message_iterator(
-                    self._input_ports['in']
-                )
+                self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
             def _user_consume(self):
                 self._msg_iter.seek_ns_from_origin(17)
This page took 0.026704 seconds and 4 git commands to generate.