lib: remove output port message iterator
[babeltrace.git] / tests / bindings / python / bt2 / test_message_iterator.py
CommitLineData
d2d857a8
MJ
1#
2# Copyright (C) 2019 EfficiOS Inc.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; only version 2
7# of the License.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17#
18
c4239792 19from bt2 import value
548bb510 20import collections
811644b8
PP
21import unittest
22import copy
23import bt2
6c373cc9 24from utils import TestOutputPortMessageIterator
811644b8
PP
25
26
5602ef81 27class UserMessageIteratorTestCase(unittest.TestCase):
811644b8 28 @staticmethod
ca02df0a 29 def _create_graph(src_comp_cls, flt_comp_cls=None):
811644b8 30 class MySink(bt2._UserSinkComponent):
66964f3f 31 def __init__(self, params, obj):
811644b8
PP
32 self._add_input_port('in')
33
6a91742b 34 def _user_consume(self):
5602ef81 35 next(self._msg_iter)
811644b8 36
6a91742b 37 def _user_graph_is_configured(self):
ca02df0a
PP
38 self._msg_iter = self._create_input_port_message_iterator(
39 self._input_ports['in']
40 )
811644b8
PP
41
42 graph = bt2.Graph()
43 src_comp = graph.add_component(src_comp_cls, 'src')
ca02df0a
PP
44
45 if flt_comp_cls is not None:
46 flt_comp = graph.add_component(flt_comp_cls, 'flt')
47
811644b8 48 sink_comp = graph.add_component(MySink, 'sink')
ca02df0a
PP
49
50 if flt_comp_cls is not None:
51 assert flt_comp is not None
52 graph.connect_ports(
53 src_comp.output_ports['out'], flt_comp.input_ports['in']
54 )
55 out_port = flt_comp.output_ports['out']
56 else:
57 out_port = src_comp.output_ports['out']
58
59 graph.connect_ports(out_port, sink_comp.input_ports['in'])
811644b8
PP
60 return graph
61
62 def test_init(self):
c5f330cd
SM
63 the_output_port_from_source = None
64 the_output_port_from_iter = None
65
5602ef81 66 class MyIter(bt2._UserMessageIterator):
c5f330cd 67 def __init__(self, self_port_output):
811644b8 68 nonlocal initialized
c5f330cd 69 nonlocal the_output_port_from_iter
811644b8 70 initialized = True
c5f330cd 71 the_output_port_from_iter = self_port_output
811644b8 72
cfbd7cf3 73 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
66964f3f 74 def __init__(self, params, obj):
c5f330cd 75 nonlocal the_output_port_from_source
2e00bc76 76 the_output_port_from_source = self._add_output_port('out', 'user data')
811644b8
PP
77
78 initialized = False
79 graph = self._create_graph(MySource)
c5f330cd 80 graph.run()
811644b8 81 self.assertTrue(initialized)
cfbd7cf3
FD
82 self.assertEqual(
83 the_output_port_from_source.addr, the_output_port_from_iter.addr
84 )
2e00bc76 85 self.assertEqual(the_output_port_from_iter.user_data, 'user data')
811644b8 86
ca02df0a
PP
87 def test_create_from_message_iterator(self):
88 class MySourceIter(bt2._UserMessageIterator):
89 def __init__(self, self_port_output):
90 nonlocal src_iter_initialized
91 src_iter_initialized = True
92
93 class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter):
66964f3f 94 def __init__(self, params, obj):
ca02df0a
PP
95 self._add_output_port('out')
96
97 class MyFilterIter(bt2._UserMessageIterator):
98 def __init__(self, self_port_output):
99 nonlocal flt_iter_initialized
100 flt_iter_initialized = True
101 self._up_iter = self._create_input_port_message_iterator(
102 self._component._input_ports['in']
103 )
104
105 def __next__(self):
106 return next(self._up_iter)
107
108 class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter):
66964f3f 109 def __init__(self, params, obj):
ca02df0a
PP
110 self._add_input_port('in')
111 self._add_output_port('out')
112
113 src_iter_initialized = False
114 flt_iter_initialized = False
115 graph = self._create_graph(MySource, MyFilter)
116 graph.run()
117 self.assertTrue(src_iter_initialized)
118 self.assertTrue(flt_iter_initialized)
119
811644b8 120 def test_finalize(self):
5602ef81 121 class MyIter(bt2._UserMessageIterator):
6a91742b 122 def _user_finalize(self):
811644b8
PP
123 nonlocal finalized
124 finalized = True
125
cfbd7cf3 126 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
66964f3f 127 def __init__(self, params, obj):
811644b8
PP
128 self._add_output_port('out')
129
130 finalized = False
131 graph = self._create_graph(MySource)
c5f330cd 132 graph.run()
811644b8
PP
133 del graph
134 self.assertTrue(finalized)
135
136 def test_component(self):
5602ef81 137 class MyIter(bt2._UserMessageIterator):
c5f330cd 138 def __init__(self, self_port_output):
811644b8
PP
139 nonlocal salut
140 salut = self._component._salut
141
cfbd7cf3 142 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
66964f3f 143 def __init__(self, params, obj):
811644b8
PP
144 self._add_output_port('out')
145 self._salut = 23
146
147 salut = None
148 graph = self._create_graph(MySource)
c5f330cd 149 graph.run()
811644b8
PP
150 self.assertEqual(salut, 23)
151
152 def test_addr(self):
5602ef81 153 class MyIter(bt2._UserMessageIterator):
c5f330cd 154 def __init__(self, self_port_output):
811644b8
PP
155 nonlocal addr
156 addr = self.addr
157
cfbd7cf3 158 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
66964f3f 159 def __init__(self, params, obj):
811644b8
PP
160 self._add_output_port('out')
161
162 addr = None
163 graph = self._create_graph(MySource)
c5f330cd 164 graph.run()
811644b8
PP
165 self.assertIsNotNone(addr)
166 self.assertNotEqual(addr, 0)
167
d79a8353
SM
168 # Test that messages returned by _UserMessageIterator.__next__ remain valid
169 # and can be re-used.
170 def test_reuse_message(self):
171 class MyIter(bt2._UserMessageIterator):
172 def __init__(self, port):
173 tc, sc, ec = port.user_data
174 trace = tc()
175 stream = trace.create_stream(sc)
176 packet = stream.create_packet()
177
178 # This message will be returned twice by __next__.
179 event_message = self._create_event_message(ec, packet)
180
181 self._msgs = [
182 self._create_stream_beginning_message(stream),
d79a8353
SM
183 self._create_packet_beginning_message(packet),
184 event_message,
185 event_message,
186 ]
187
188 def __next__(self):
189 return self._msgs.pop(0)
190
191 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
66964f3f 192 def __init__(self, params, obj):
d79a8353 193 tc = self._create_trace_class()
26fc5aed 194 sc = tc.create_stream_class(supports_packets=True)
d79a8353
SM
195 ec = sc.create_event_class()
196 self._add_output_port('out', (tc, sc, ec))
197
198 graph = bt2.Graph()
199 src = graph.add_component(MySource, 'src')
6c373cc9 200 it = TestOutputPortMessageIterator(graph, src.output_ports['out'])
d79a8353
SM
201
202 # Skip beginning messages.
188edac1 203 msg = next(it)
3fb99a22 204 self.assertIsInstance(msg, bt2._StreamBeginningMessage)
188edac1 205 msg = next(it)
3fb99a22 206 self.assertIsInstance(msg, bt2._PacketBeginningMessage)
d79a8353
SM
207
208 msg_ev1 = next(it)
209 msg_ev2 = next(it)
210
3fb99a22
PP
211 self.assertIsInstance(msg_ev1, bt2._EventMessage)
212 self.assertIsInstance(msg_ev2, bt2._EventMessage)
d79a8353
SM
213 self.assertEqual(msg_ev1.addr, msg_ev2.addr)
214
f00b8d40 215 @staticmethod
6c373cc9 216 def _setup_seek_beginning_test(sink_cls):
f00b8d40
SM
217 # Use a source, a filter and an output port iterator. This allows us
218 # to test calling `seek_beginning` on both a _OutputPortMessageIterator
219 # and a _UserComponentInputPortMessageIterator, on top of checking that
220 # _UserMessageIterator._seek_beginning is properly called.
221
222 class MySourceIter(bt2._UserMessageIterator):
223 def __init__(self, port):
224 tc, sc, ec = port.user_data
225 trace = tc()
226 stream = trace.create_stream(sc)
227 packet = stream.create_packet()
228
229 self._msgs = [
230 self._create_stream_beginning_message(stream),
f00b8d40
SM
231 self._create_packet_beginning_message(packet),
232 self._create_event_message(ec, packet),
233 self._create_event_message(ec, packet),
234 self._create_packet_end_message(packet),
f00b8d40
SM
235 self._create_stream_end_message(stream),
236 ]
237 self._at = 0
238
6a91742b 239 def _user_seek_beginning(self):
f00b8d40
SM
240 self._at = 0
241
242 def __next__(self):
243 if self._at < len(self._msgs):
244 msg = self._msgs[self._at]
245 self._at += 1
246 return msg
247 else:
248 raise StopIteration
249
cfbd7cf3 250 class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter):
66964f3f 251 def __init__(self, params, obj):
f00b8d40 252 tc = self._create_trace_class()
26fc5aed 253 sc = tc.create_stream_class(supports_packets=True)
f00b8d40
SM
254 ec = sc.create_event_class()
255
256 self._add_output_port('out', (tc, sc, ec))
257
258 class MyFilterIter(bt2._UserMessageIterator):
259 def __init__(self, port):
260 input_port = port.user_data
ca02df0a
PP
261 self._upstream_iter = self._create_input_port_message_iterator(
262 input_port
263 )
f00b8d40
SM
264
265 def __next__(self):
266 return next(self._upstream_iter)
267
6a91742b 268 def _user_seek_beginning(self):
f00b8d40
SM
269 self._upstream_iter.seek_beginning()
270
271 @property
6a91742b 272 def _user_can_seek_beginning(self):
f00b8d40
SM
273 return self._upstream_iter.can_seek_beginning
274
275 class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter):
66964f3f 276 def __init__(self, params, obj):
f00b8d40
SM
277 input_port = self._add_input_port('in')
278 self._add_output_port('out', input_port)
279
f00b8d40
SM
280 graph = bt2.Graph()
281 src = graph.add_component(MySource, 'src')
282 flt = graph.add_component(MyFilter, 'flt')
6c373cc9 283 sink = graph.add_component(sink_cls, 'sink')
f00b8d40 284 graph.connect_ports(src.output_ports['out'], flt.input_ports['in'])
6c373cc9
PP
285 graph.connect_ports(flt.output_ports['out'], sink.input_ports['in'])
286 return MySourceIter, graph
f00b8d40
SM
287
288 def test_can_seek_beginning(self):
6c373cc9
PP
289 class MySink(bt2._UserSinkComponent):
290 def __init__(self, params, obj):
291 self._add_input_port('in')
292
293 def _user_graph_is_configured(self):
294 self._msg_iter = self._create_input_port_message_iterator(
295 self._input_ports['in']
296 )
297
298 def _user_consume(self):
299 nonlocal can_seek_beginning
300 can_seek_beginning = self._msg_iter.can_seek_beginning
301
302 MySourceIter, graph = self._setup_seek_beginning_test(MySink)
f00b8d40 303
6a91742b 304 def _user_can_seek_beginning(self):
6c373cc9
PP
305 nonlocal input_port_iter_can_seek_beginning
306 return input_port_iter_can_seek_beginning
f00b8d40 307
6a91742b 308 MySourceIter._user_can_seek_beginning = property(_user_can_seek_beginning)
f00b8d40 309
6c373cc9
PP
310 input_port_iter_can_seek_beginning = True
311 can_seek_beginning = None
312 graph.run_once()
313 self.assertTrue(can_seek_beginning)
f00b8d40 314
6c373cc9
PP
315 input_port_iter_can_seek_beginning = False
316 can_seek_beginning = None
317 graph.run_once()
318 self.assertFalse(can_seek_beginning)
f00b8d40
SM
319
320 # Once can_seek_beginning returns an error, verify that it raises when
321 # _can_seek_beginning has/returns the wrong type.
322
323 # Remove the _can_seek_beginning method, we now rely on the presence of
324 # a _seek_beginning method to know whether the iterator can seek to
325 # beginning or not.
6a91742b 326 del MySourceIter._user_can_seek_beginning
6c373cc9
PP
327 can_seek_beginning = None
328 graph.run_once()
329 self.assertTrue(can_seek_beginning)
f00b8d40 330
6a91742b 331 del MySourceIter._user_seek_beginning
6c373cc9
PP
332 can_seek_beginning = None
333 graph.run_once()
334 self.assertFalse(can_seek_beginning)
f00b8d40
SM
335
336 def test_seek_beginning(self):
6c373cc9
PP
337 class MySink(bt2._UserSinkComponent):
338 def __init__(self, params, obj):
339 self._add_input_port('in')
f00b8d40 340
6c373cc9
PP
341 def _user_graph_is_configured(self):
342 self._msg_iter = self._create_input_port_message_iterator(
343 self._input_ports['in']
344 )
345
346 def _user_consume(self):
347 nonlocal do_seek_beginning
348 nonlocal msg
349
350 if do_seek_beginning:
351 self._msg_iter.seek_beginning()
352 return
353
354 msg = next(self._msg_iter)
355
356 do_seek_beginning = False
357 msg = None
358 MySourceIter, graph = self._setup_seek_beginning_test(MySink)
359 graph.run_once()
3fb99a22 360 self.assertIsInstance(msg, bt2._StreamBeginningMessage)
6c373cc9 361 graph.run_once()
3fb99a22 362 self.assertIsInstance(msg, bt2._PacketBeginningMessage)
6c373cc9
PP
363 do_seek_beginning = True
364 graph.run_once()
365 do_seek_beginning = False
366 graph.run_once()
367 self.assertIsInstance(msg, bt2._StreamBeginningMessage)
f00b8d40 368
6c373cc9
PP
369 def test_seek_beginning_user_error(self):
370 class MySink(bt2._UserSinkComponent):
371 def __init__(self, params, obj):
372 self._add_input_port('in')
f00b8d40 373
6c373cc9
PP
374 def _user_graph_is_configured(self):
375 self._msg_iter = self._create_input_port_message_iterator(
376 self._input_ports['in']
377 )
f00b8d40 378
6c373cc9
PP
379 def _user_consume(self):
380 self._msg_iter.seek_beginning()
f00b8d40 381
6c373cc9 382 MySourceIter, graph = self._setup_seek_beginning_test(MySink)
f00b8d40 383
6a91742b 384 def _user_seek_beginning_error(self):
cfbd7cf3 385 raise ValueError('ouch')
f00b8d40 386
6a91742b 387 MySourceIter._user_seek_beginning = _user_seek_beginning_error
f00b8d40 388
694c792b 389 with self.assertRaises(bt2._Error):
6c373cc9 390 graph.run_once()
f00b8d40 391
0361868a
SM
392 # Try consuming many times from an iterator that always returns TryAgain.
393 # This verifies that we are not missing an incref of Py_None, making the
394 # refcount of Py_None reach 0.
395 def test_try_again_many_times(self):
396 class MyIter(bt2._UserMessageIterator):
397 def __next__(self):
398 raise bt2.TryAgain
399
400 class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
66964f3f 401 def __init__(self, params, obj):
0361868a
SM
402 self._add_output_port('out')
403
404 graph = bt2.Graph()
405 src = graph.add_component(MySource, 'src')
6c373cc9 406 it = TestOutputPortMessageIterator(graph, src.output_ports['out'])
0361868a
SM
407
408 # The initial refcount of Py_None was in the 7000, so 100000 iterations
409 # should be enough to catch the bug even if there are small differences
410 # between configurations.
411 for i in range(100000):
412 with self.assertRaises(bt2.TryAgain):
413 next(it)
414
f00b8d40 415
f00b8d40
SM
416if __name__ == '__main__':
417 unittest.main()
This page took 0.057079 seconds and 4 git commands to generate.