bt2: make `TraceCollectionMessageIterator` not use an output port msg iter
[babeltrace.git] / tests / bindings / python / bt2 / test_message_iterator.py
index 72003da701710ce91e19e015149fc5a933825a68..43af1e1f4d8457807f70f5bb10c7615499c3a6f5 100644 (file)
@@ -25,21 +25,37 @@ import bt2
 
 class UserMessageIteratorTestCase(unittest.TestCase):
     @staticmethod
-    def _create_graph(src_comp_cls):
+    def _create_graph(src_comp_cls, flt_comp_cls=None):
         class MySink(bt2._UserSinkComponent):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_input_port('in')
 
-            def _consume(self):
+            def _user_consume(self):
                 next(self._msg_iter)
 
-            def _graph_is_configured(self):
-                self._msg_iter = self._input_ports['in'].create_message_iterator()
+            def _user_graph_is_configured(self):
+                self._msg_iter = self._create_input_port_message_iterator(
+                    self._input_ports['in']
+                )
 
         graph = bt2.Graph()
         src_comp = graph.add_component(src_comp_cls, 'src')
+
+        if flt_comp_cls is not None:
+            flt_comp = graph.add_component(flt_comp_cls, 'flt')
+
         sink_comp = graph.add_component(MySink, 'sink')
-        graph.connect_ports(src_comp.output_ports['out'], sink_comp.input_ports['in'])
+
+        if flt_comp_cls is not None:
+            assert flt_comp is not None
+            graph.connect_ports(
+                src_comp.output_ports['out'], flt_comp.input_ports['in']
+            )
+            out_port = flt_comp.output_ports['out']
+        else:
+            out_port = src_comp.output_ports['out']
+
+        graph.connect_ports(out_port, sink_comp.input_ports['in'])
         return graph
 
     def test_init(self):
@@ -54,7 +70,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
                 the_output_port_from_iter = self_port_output
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 nonlocal the_output_port_from_source
                 the_output_port_from_source = self._add_output_port('out', 'user data')
 
@@ -67,14 +83,47 @@ class UserMessageIteratorTestCase(unittest.TestCase):
         )
         self.assertEqual(the_output_port_from_iter.user_data, 'user data')
 
+    def test_create_from_message_iterator(self):
+        class MySourceIter(bt2._UserMessageIterator):
+            def __init__(self, self_port_output):
+                nonlocal src_iter_initialized
+                src_iter_initialized = True
+
+        class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter):
+            def __init__(self, params, obj):
+                self._add_output_port('out')
+
+        class MyFilterIter(bt2._UserMessageIterator):
+            def __init__(self, self_port_output):
+                nonlocal flt_iter_initialized
+                flt_iter_initialized = True
+                self._up_iter = self._create_input_port_message_iterator(
+                    self._component._input_ports['in']
+                )
+
+            def __next__(self):
+                return next(self._up_iter)
+
+        class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter):
+            def __init__(self, params, obj):
+                self._add_input_port('in')
+                self._add_output_port('out')
+
+        src_iter_initialized = False
+        flt_iter_initialized = False
+        graph = self._create_graph(MySource, MyFilter)
+        graph.run()
+        self.assertTrue(src_iter_initialized)
+        self.assertTrue(flt_iter_initialized)
+
     def test_finalize(self):
         class MyIter(bt2._UserMessageIterator):
-            def _finalize(self):
+            def _user_finalize(self):
                 nonlocal finalized
                 finalized = True
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         finalized = False
@@ -90,7 +139,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
                 salut = self._component._salut
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
                 self._salut = 23
 
@@ -106,7 +155,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
                 addr = self.addr
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         addr = None
@@ -139,7 +188,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
                 return self._msgs.pop(0)
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 tc = self._create_trace_class()
                 sc = tc.create_stream_class(supports_packets=True)
                 ec = sc.create_event_class()
@@ -151,15 +200,15 @@ class UserMessageIteratorTestCase(unittest.TestCase):
 
         # Skip beginning messages.
         msg = next(it)
-        self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+        self.assertIsInstance(msg, bt2._StreamBeginningMessage)
         msg = next(it)
-        self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
+        self.assertIsInstance(msg, bt2._PacketBeginningMessage)
 
         msg_ev1 = next(it)
         msg_ev2 = next(it)
 
-        self.assertIsInstance(msg_ev1, bt2.message._EventMessage)
-        self.assertIsInstance(msg_ev2, bt2.message._EventMessage)
+        self.assertIsInstance(msg_ev1, bt2._EventMessage)
+        self.assertIsInstance(msg_ev2, bt2._EventMessage)
         self.assertEqual(msg_ev1.addr, msg_ev2.addr)
 
     @staticmethod
@@ -186,7 +235,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
                 ]
                 self._at = 0
 
-            def _seek_beginning(self):
+            def _user_seek_beginning(self):
                 self._at = 0
 
             def __next__(self):
@@ -198,7 +247,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
                     raise StopIteration
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 tc = self._create_trace_class()
                 sc = tc.create_stream_class(supports_packets=True)
                 ec = sc.create_event_class()
@@ -208,20 +257,22 @@ class UserMessageIteratorTestCase(unittest.TestCase):
         class MyFilterIter(bt2._UserMessageIterator):
             def __init__(self, port):
                 input_port = port.user_data
-                self._upstream_iter = input_port.create_message_iterator()
+                self._upstream_iter = self._create_input_port_message_iterator(
+                    input_port
+                )
 
             def __next__(self):
                 return next(self._upstream_iter)
 
-            def _seek_beginning(self):
+            def _user_seek_beginning(self):
                 self._upstream_iter.seek_beginning()
 
             @property
-            def _can_seek_beginning(self):
+            def _user_can_seek_beginning(self):
                 return self._upstream_iter.can_seek_beginning
 
         class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 input_port = self._add_input_port('in')
                 self._add_output_port('out', input_port)
 
@@ -236,11 +287,11 @@ class UserMessageIteratorTestCase(unittest.TestCase):
     def test_can_seek_beginning(self):
         it, MySourceIter = self._setup_seek_beginning_test()
 
-        def _can_seek_beginning(self):
+        def _user_can_seek_beginning(self):
             nonlocal can_seek_beginning
             return can_seek_beginning
 
-        MySourceIter._can_seek_beginning = property(_can_seek_beginning)
+        MySourceIter._user_can_seek_beginning = property(_user_can_seek_beginning)
 
         can_seek_beginning = True
         self.assertTrue(it.can_seek_beginning)
@@ -254,24 +305,24 @@ class UserMessageIteratorTestCase(unittest.TestCase):
         # Remove the _can_seek_beginning method, we now rely on the presence of
         # a _seek_beginning method to know whether the iterator can seek to
         # beginning or not.
-        del MySourceIter._can_seek_beginning
+        del MySourceIter._user_can_seek_beginning
         self.assertTrue(it.can_seek_beginning)
 
-        del MySourceIter._seek_beginning
+        del MySourceIter._user_seek_beginning
         self.assertFalse(it.can_seek_beginning)
 
     def test_seek_beginning(self):
         it, MySourceIter = self._setup_seek_beginning_test()
 
         msg = next(it)
-        self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+        self.assertIsInstance(msg, bt2._StreamBeginningMessage)
         msg = next(it)
-        self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
+        self.assertIsInstance(msg, bt2._PacketBeginningMessage)
 
         it.seek_beginning()
 
         msg = next(it)
-        self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+        self.assertIsInstance(msg, bt2._StreamBeginningMessage)
 
         # Verify that we can seek beginning after having reached the end.
         #
@@ -287,17 +338,17 @@ class UserMessageIteratorTestCase(unittest.TestCase):
         #
         # it.seek_beginning()
         # msg = next(it)
-        # self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+        # self.assertIsInstance(msg, bt2._StreamBeginningMessage)
 
     def test_seek_beginning_user_error(self):
         it, MySourceIter = self._setup_seek_beginning_test()
 
-        def _seek_beginning_error(self):
+        def _user_seek_beginning_error(self):
             raise ValueError('ouch')
 
-        MySourceIter._seek_beginning = _seek_beginning_error
+        MySourceIter._user_seek_beginning = _user_seek_beginning_error
 
-        with self.assertRaises(bt2.Error):
+        with self.assertRaises(bt2._Error):
             it.seek_beginning()
 
     # Try consuming many times from an iterator that always returns TryAgain.
@@ -309,7 +360,7 @@ class UserMessageIteratorTestCase(unittest.TestCase):
                 raise bt2.TryAgain
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
         graph = bt2.Graph()
@@ -352,7 +403,7 @@ class OutputPortMessageIteratorTestCase(unittest.TestCase):
                 return msg
 
         class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
-            def __init__(self, params):
+            def __init__(self, params, obj):
                 self._add_output_port('out')
 
                 trace_class = self._create_trace_class()
@@ -382,15 +433,15 @@ class OutputPortMessageIteratorTestCase(unittest.TestCase):
 
         for at, msg in enumerate(msg_iter):
             if at == 0:
-                self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+                self.assertIsInstance(msg, bt2._StreamBeginningMessage)
             elif at == 1:
-                self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
+                self.assertIsInstance(msg, bt2._PacketBeginningMessage)
             elif at == 5:
-                self.assertIsInstance(msg, bt2.message._PacketEndMessage)
+                self.assertIsInstance(msg, bt2._PacketEndMessage)
             elif at == 6:
-                self.assertIsInstance(msg, bt2.message._StreamEndMessage)
+                self.assertIsInstance(msg, bt2._StreamEndMessage)
             else:
-                self.assertIsInstance(msg, bt2.message._EventMessage)
+                self.assertIsInstance(msg, bt2._EventMessage)
                 self.assertEqual(msg.event.cls.name, 'salut')
                 field = msg.event.payload_field['my_int']
                 self.assertEqual(field, at * 3)
This page took 0.028165 seconds and 4 git commands to generate.