2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
22 from utils
import TestOutputPortMessageIterator
25 class UserMessageIteratorTestCase(unittest
.TestCase
):
27 def _create_graph(src_comp_cls
, flt_comp_cls
=None):
28 class MySink(bt2
._UserSinkComponent
):
29 def __init__(self
, params
, obj
):
30 self
._add
_input
_port
('in')
32 def _user_consume(self
):
35 def _user_graph_is_configured(self
):
36 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
37 self
._input
_ports
['in']
41 src_comp
= graph
.add_component(src_comp_cls
, 'src')
43 if flt_comp_cls
is not None:
44 flt_comp
= graph
.add_component(flt_comp_cls
, 'flt')
46 sink_comp
= graph
.add_component(MySink
, 'sink')
48 if flt_comp_cls
is not None:
49 assert flt_comp
is not None
51 src_comp
.output_ports
['out'], flt_comp
.input_ports
['in']
53 out_port
= flt_comp
.output_ports
['out']
55 out_port
= src_comp
.output_ports
['out']
57 graph
.connect_ports(out_port
, sink_comp
.input_ports
['in'])
61 the_output_port_from_source
= None
62 the_output_port_from_iter
= None
64 class MyIter(bt2
._UserMessageIterator
):
65 def __init__(self
, self_port_output
):
67 nonlocal the_output_port_from_iter
69 the_output_port_from_iter
= self_port_output
71 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
72 def __init__(self
, params
, obj
):
73 nonlocal the_output_port_from_source
74 the_output_port_from_source
= self
._add
_output
_port
('out', 'user data')
77 graph
= self
._create
_graph
(MySource
)
79 self
.assertTrue(initialized
)
81 the_output_port_from_source
.addr
, the_output_port_from_iter
.addr
83 self
.assertEqual(the_output_port_from_iter
.user_data
, 'user data')
85 def test_create_from_message_iterator(self
):
86 class MySourceIter(bt2
._UserMessageIterator
):
87 def __init__(self
, self_port_output
):
88 nonlocal src_iter_initialized
89 src_iter_initialized
= True
91 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MySourceIter
):
92 def __init__(self
, params
, obj
):
93 self
._add
_output
_port
('out')
95 class MyFilterIter(bt2
._UserMessageIterator
):
96 def __init__(self
, self_port_output
):
97 nonlocal flt_iter_initialized
98 flt_iter_initialized
= True
99 self
._up
_iter
= self
._create
_input
_port
_message
_iterator
(
100 self
._component
._input
_ports
['in']
104 return next(self
._up
_iter
)
106 class MyFilter(bt2
._UserFilterComponent
, message_iterator_class
=MyFilterIter
):
107 def __init__(self
, params
, obj
):
108 self
._add
_input
_port
('in')
109 self
._add
_output
_port
('out')
111 src_iter_initialized
= False
112 flt_iter_initialized
= False
113 graph
= self
._create
_graph
(MySource
, MyFilter
)
115 self
.assertTrue(src_iter_initialized
)
116 self
.assertTrue(flt_iter_initialized
)
118 def test_finalize(self
):
119 class MyIter(bt2
._UserMessageIterator
):
120 def _user_finalize(self
):
124 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
125 def __init__(self
, params
, obj
):
126 self
._add
_output
_port
('out')
129 graph
= self
._create
_graph
(MySource
)
132 self
.assertTrue(finalized
)
134 def test_component(self
):
135 class MyIter(bt2
._UserMessageIterator
):
136 def __init__(self
, self_port_output
):
138 salut
= self
._component
._salut
140 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
141 def __init__(self
, params
, obj
):
142 self
._add
_output
_port
('out')
146 graph
= self
._create
_graph
(MySource
)
148 self
.assertEqual(salut
, 23)
151 class MyIter(bt2
._UserMessageIterator
):
152 def __init__(self
, self_port_output
):
156 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
157 def __init__(self
, params
, obj
):
158 self
._add
_output
_port
('out')
161 graph
= self
._create
_graph
(MySource
)
163 self
.assertIsNotNone(addr
)
164 self
.assertNotEqual(addr
, 0)
166 # Test that messages returned by _UserMessageIterator.__next__ remain valid
167 # and can be re-used.
168 def test_reuse_message(self
):
169 class MyIter(bt2
._UserMessageIterator
):
170 def __init__(self
, port
):
171 tc
, sc
, ec
= port
.user_data
173 stream
= trace
.create_stream(sc
)
174 packet
= stream
.create_packet()
176 # This message will be returned twice by __next__.
177 event_message
= self
._create
_event
_message
(ec
, packet
)
180 self
._create
_stream
_beginning
_message
(stream
),
181 self
._create
_packet
_beginning
_message
(packet
),
187 return self
._msgs
.pop(0)
189 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
190 def __init__(self
, params
, obj
):
191 tc
= self
._create
_trace
_class
()
192 sc
= tc
.create_stream_class(supports_packets
=True)
193 ec
= sc
.create_event_class()
194 self
._add
_output
_port
('out', (tc
, sc
, ec
))
197 src
= graph
.add_component(MySource
, 'src')
198 it
= TestOutputPortMessageIterator(graph
, src
.output_ports
['out'])
200 # Skip beginning messages.
202 self
.assertIsInstance(msg
, bt2
._StreamBeginningMessage
)
204 self
.assertIsInstance(msg
, bt2
._PacketBeginningMessage
)
209 self
.assertIsInstance(msg_ev1
, bt2
._EventMessage
)
210 self
.assertIsInstance(msg_ev2
, bt2
._EventMessage
)
211 self
.assertEqual(msg_ev1
.addr
, msg_ev2
.addr
)
214 def _setup_seek_beginning_test(sink_cls
):
215 # Use a source, a filter and an output port iterator. This allows us
216 # to test calling `seek_beginning` on both a _OutputPortMessageIterator
217 # and a _UserComponentInputPortMessageIterator, on top of checking that
218 # _UserMessageIterator._seek_beginning is properly called.
220 class MySourceIter(bt2
._UserMessageIterator
):
221 def __init__(self
, port
):
222 tc
, sc
, ec
= port
.user_data
224 stream
= trace
.create_stream(sc
)
225 packet
= stream
.create_packet()
228 self
._create
_stream
_beginning
_message
(stream
),
229 self
._create
_packet
_beginning
_message
(packet
),
230 self
._create
_event
_message
(ec
, packet
),
231 self
._create
_event
_message
(ec
, packet
),
232 self
._create
_packet
_end
_message
(packet
),
233 self
._create
_stream
_end
_message
(stream
),
237 def _user_seek_beginning(self
):
241 if self
._at
< len(self
._msgs
):
242 msg
= self
._msgs
[self
._at
]
248 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MySourceIter
):
249 def __init__(self
, params
, obj
):
250 tc
= self
._create
_trace
_class
()
251 sc
= tc
.create_stream_class(supports_packets
=True)
252 ec
= sc
.create_event_class()
254 self
._add
_output
_port
('out', (tc
, sc
, ec
))
256 class MyFilterIter(bt2
._UserMessageIterator
):
257 def __init__(self
, port
):
258 input_port
= port
.user_data
259 self
._upstream
_iter
= self
._create
_input
_port
_message
_iterator
(
264 return next(self
._upstream
_iter
)
266 def _user_seek_beginning(self
):
267 self
._upstream
_iter
.seek_beginning()
270 def _user_can_seek_beginning(self
):
271 return self
._upstream
_iter
.can_seek_beginning
273 class MyFilter(bt2
._UserFilterComponent
, message_iterator_class
=MyFilterIter
):
274 def __init__(self
, params
, obj
):
275 input_port
= self
._add
_input
_port
('in')
276 self
._add
_output
_port
('out', input_port
)
279 src
= graph
.add_component(MySource
, 'src')
280 flt
= graph
.add_component(MyFilter
, 'flt')
281 sink
= graph
.add_component(sink_cls
, 'sink')
282 graph
.connect_ports(src
.output_ports
['out'], flt
.input_ports
['in'])
283 graph
.connect_ports(flt
.output_ports
['out'], sink
.input_ports
['in'])
284 return MySourceIter
, graph
286 def test_can_seek_beginning(self
):
287 class MySink(bt2
._UserSinkComponent
):
288 def __init__(self
, params
, obj
):
289 self
._add
_input
_port
('in')
291 def _user_graph_is_configured(self
):
292 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
293 self
._input
_ports
['in']
296 def _user_consume(self
):
297 nonlocal can_seek_beginning
298 can_seek_beginning
= self
._msg
_iter
.can_seek_beginning
300 MySourceIter
, graph
= self
._setup
_seek
_beginning
_test
(MySink
)
302 def _user_can_seek_beginning(self
):
303 nonlocal input_port_iter_can_seek_beginning
304 return input_port_iter_can_seek_beginning
306 MySourceIter
._user
_can
_seek
_beginning
= property(_user_can_seek_beginning
)
308 input_port_iter_can_seek_beginning
= True
309 can_seek_beginning
= None
311 self
.assertTrue(can_seek_beginning
)
313 input_port_iter_can_seek_beginning
= False
314 can_seek_beginning
= None
316 self
.assertFalse(can_seek_beginning
)
318 # Once can_seek_beginning returns an error, verify that it raises when
319 # _can_seek_beginning has/returns the wrong type.
321 # Remove the _can_seek_beginning method, we now rely on the presence of
322 # a _seek_beginning method to know whether the iterator can seek to
324 del MySourceIter
._user
_can
_seek
_beginning
325 can_seek_beginning
= None
327 self
.assertTrue(can_seek_beginning
)
329 del MySourceIter
._user
_seek
_beginning
330 can_seek_beginning
= None
332 self
.assertFalse(can_seek_beginning
)
334 def test_seek_beginning(self
):
335 class MySink(bt2
._UserSinkComponent
):
336 def __init__(self
, params
, obj
):
337 self
._add
_input
_port
('in')
339 def _user_graph_is_configured(self
):
340 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
341 self
._input
_ports
['in']
344 def _user_consume(self
):
345 nonlocal do_seek_beginning
348 if do_seek_beginning
:
349 self
._msg
_iter
.seek_beginning()
352 msg
= next(self
._msg
_iter
)
354 do_seek_beginning
= False
356 MySourceIter
, graph
= self
._setup
_seek
_beginning
_test
(MySink
)
358 self
.assertIsInstance(msg
, bt2
._StreamBeginningMessage
)
360 self
.assertIsInstance(msg
, bt2
._PacketBeginningMessage
)
361 do_seek_beginning
= True
363 do_seek_beginning
= False
365 self
.assertIsInstance(msg
, bt2
._StreamBeginningMessage
)
367 def test_seek_beginning_user_error(self
):
368 class MySink(bt2
._UserSinkComponent
):
369 def __init__(self
, params
, obj
):
370 self
._add
_input
_port
('in')
372 def _user_graph_is_configured(self
):
373 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
374 self
._input
_ports
['in']
377 def _user_consume(self
):
378 self
._msg
_iter
.seek_beginning()
380 MySourceIter
, graph
= self
._setup
_seek
_beginning
_test
(MySink
)
382 def _user_seek_beginning_error(self
):
383 raise ValueError('ouch')
385 MySourceIter
._user
_seek
_beginning
= _user_seek_beginning_error
387 with self
.assertRaises(bt2
._Error
):
390 # Try consuming many times from an iterator that always returns TryAgain.
391 # This verifies that we are not missing an incref of Py_None, making the
392 # refcount of Py_None reach 0.
393 def test_try_again_many_times(self
):
394 class MyIter(bt2
._UserMessageIterator
):
398 class MySource(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
399 def __init__(self
, params
, obj
):
400 self
._add
_output
_port
('out')
403 src
= graph
.add_component(MySource
, 'src')
404 it
= TestOutputPortMessageIterator(graph
, src
.output_ports
['out'])
406 # Three times the initial ref count of `None` iterations should
407 # be enough to catch the bug even if there are small differences
408 # between configurations.
409 none_ref_count
= sys
.getrefcount(None) * 3
411 for i
in range(none_ref_count
):
412 with self
.assertRaises(bt2
.TryAgain
):
416 if __name__
== '__main__':