+ src = graph.add_component(MySource, "src")
+ it = TestOutputPortMessageIterator(graph, src.output_ports["out"])
+
+ # Three times the initial ref count of `None` iterations should
+ # be enough to catch the bug even if there are small differences
+ # between configurations.
+ none_ref_count = sys.getrefcount(None) * 3
+
+ for i in range(none_ref_count):
+ 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,
+ user_seek_beginning=None,
+ user_can_seek_beginning=None,
+ user_seek_ns_from_origin=None,
+ user_can_seek_ns_from_origin=None,
+ can_seek_forward=False,
+):
+ class MySourceIter(bt2._UserMessageIterator):
+ def __init__(self, config, port):
+ tc, sc, ec = port.user_data
+ trace = tc()
+ stream = trace.create_stream(sc)
+ packet = stream.create_packet()
+
+ self._msgs = [
+ self._create_stream_beginning_message(stream),
+ self._create_packet_beginning_message(packet),
+ self._create_event_message(ec, packet),
+ self._create_event_message(ec, packet),
+ self._create_packet_end_message(packet),
+ self._create_stream_end_message(stream),
+ ]
+ self._at = 0
+ config.can_seek_forward = can_seek_forward
+
+ def __next__(self):
+ if self._at < len(self._msgs):
+ msg = self._msgs[self._at]
+ self._at += 1
+ return msg
+ else:
+ raise StopIteration
+
+ if user_seek_beginning is not None:
+ MySourceIter._user_seek_beginning = user_seek_beginning
+
+ if user_can_seek_beginning is not None:
+ MySourceIter._user_can_seek_beginning = user_can_seek_beginning
+
+ if user_seek_ns_from_origin is not None:
+ MySourceIter._user_seek_ns_from_origin = user_seek_ns_from_origin
+
+ if user_can_seek_ns_from_origin is not None:
+ MySourceIter._user_can_seek_ns_from_origin = user_can_seek_ns_from_origin
+
+ class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter):
+ def __init__(self, config, params, obj):
+ tc = self._create_trace_class()
+ sc = tc.create_stream_class(supports_packets=True)
+ ec = sc.create_event_class()
+
+ self._add_output_port("out", (tc, sc, ec))
+
+ class MyFilterIter(bt2._UserMessageIterator):
+ def __init__(self, config, port):
+ self._upstream_iter = self._create_message_iterator(
+ self._component._input_ports["in"]
+ )
+ config.can_seek_forward = self._upstream_iter.can_seek_forward