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 AllMessagesTestCase(unittest
.TestCase
):
27 class MyIter(bt2
._UserMessageIterator
):
28 def __init__(self
, self_port_output
):
30 self
._with
_stream
_msgs
_clock
_snapshots
= self_port_output
.user_data
.get(
31 'with_stream_msgs_clock_snapshots', False
35 if test_obj
._clock
_class
:
37 if self
._with
_stream
_msgs
_clock
_snapshots
:
38 msg
= self
._create
_stream
_beginning
_message
(
39 test_obj
._stream
, default_clock_snapshot
=self
._at
42 msg
= self
._create
_stream
_beginning
_message
(
46 msg
= self
._create
_packet
_beginning
_message
(
47 test_obj
._packet
, self
._at
50 msg
= self
._create
_event
_message
(
51 test_obj
._event
_class
, test_obj
._packet
, self
._at
54 msg
= self
._create
_message
_iterator
_inactivity
_message
(
55 test_obj
._clock
_class
, self
._at
58 msg
= self
._create
_discarded
_events
_message
(
59 test_obj
._stream
, 890, self
._at
, self
._at
62 msg
= self
._create
_packet
_end
_message
(
63 test_obj
._packet
, self
._at
66 msg
= self
._create
_discarded
_packets
_message
(
67 test_obj
._stream
, 678, self
._at
, self
._at
70 if self
._with
_stream
_msgs
_clock
_snapshots
:
71 msg
= self
._create
_stream
_end
_message
(
72 test_obj
._stream
, default_clock_snapshot
=self
._at
75 msg
= self
._create
_stream
_end
_message
(test_obj
._stream
)
80 msg
= self
._create
_stream
_beginning
_message
(test_obj
._stream
)
82 msg
= self
._create
_packet
_beginning
_message
(test_obj
._packet
)
84 msg
= self
._create
_event
_message
(
85 test_obj
._event
_class
, test_obj
._packet
88 msg
= self
._create
_discarded
_events
_message
(
92 msg
= self
._create
_packet
_end
_message
(test_obj
._packet
)
94 msg
= self
._create
_discarded
_packets
_message
(
98 msg
= self
._create
_stream
_end
_message
(test_obj
._stream
)
105 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
106 def __init__(self
, params
, obj
):
107 self
._add
_output
_port
('out', params
)
109 with_cc
= bool(params
['with_cc'])
110 tc
= self
._create
_trace
_class
()
112 cc
= self
._create
_clock
_class
()
116 sc
= tc
.create_stream_class(
117 default_clock_class
=cc
,
118 supports_packets
=True,
119 packets_have_beginning_default_clock_snapshot
=with_cc
,
120 packets_have_end_default_clock_snapshot
=with_cc
,
121 supports_discarded_events
=True,
122 discarded_events_have_default_clock_snapshots
=with_cc
,
123 supports_discarded_packets
=True,
124 discarded_packets_have_default_clock_snapshots
=with_cc
,
127 # Create payload field class
128 my_int_fc
= tc
.create_signed_integer_field_class(32)
129 payload_fc
= tc
.create_structure_field_class()
130 payload_fc
+= [('my_int', my_int_fc
)]
132 ec
= sc
.create_event_class(name
='salut', payload_field_class
=payload_fc
)
135 stream
= trace
.create_stream(sc
)
136 packet
= stream
.create_packet()
138 test_obj
._trace
= trace
139 test_obj
._stream
= stream
140 test_obj
._packet
= packet
141 test_obj
._event
_class
= ec
142 test_obj
._clock
_class
= cc
145 self
._graph
= bt2
.Graph()
149 def test_all_msg_with_cc(self
):
150 params
= {'with_cc': True}
151 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
152 self
._msg
_iter
= TestOutputPortMessageIterator(
153 self
._graph
, self
._src
_comp
.output_ports
['out']
156 for i
, msg
in enumerate(self
._msg
_iter
):
158 self
.assertIsInstance(msg
, bt2
._StreamBeginningMessage
)
159 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
160 self
.assertIsInstance(
161 msg
.default_clock_snapshot
, bt2
._UnknownClockSnapshot
164 self
.assertIsInstance(msg
, bt2
._PacketBeginningMessage
)
165 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
166 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
168 self
.assertIsInstance(msg
, bt2
._EventMessage
)
169 self
.assertEqual(msg
.event
.cls
.addr
, self
._event
_class
.addr
)
170 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
172 self
.assertIsInstance(msg
, bt2
._MessageIteratorInactivityMessage
)
173 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
175 self
.assertIsInstance(msg
, bt2
._DiscardedEventsMessage
)
176 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
177 self
.assertEqual(msg
.count
, 890)
179 msg
.stream
.cls
.default_clock_class
.addr
, self
._clock
_class
.addr
181 self
.assertEqual(msg
.beginning_default_clock_snapshot
.value
, i
)
182 self
.assertEqual(msg
.end_default_clock_snapshot
.value
, i
)
184 self
.assertIsInstance(msg
, bt2
._PacketEndMessage
)
185 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
186 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
188 self
.assertIsInstance(msg
, bt2
._DiscardedPacketsMessage
)
189 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
190 self
.assertEqual(msg
.count
, 678)
192 msg
.stream
.cls
.default_clock_class
.addr
, self
._clock
_class
.addr
194 self
.assertEqual(msg
.beginning_default_clock_snapshot
.value
, i
)
195 self
.assertEqual(msg
.end_default_clock_snapshot
.value
, i
)
197 self
.assertIsInstance(msg
, bt2
._StreamEndMessage
)
198 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
199 self
.assertIsInstance(
200 msg
.default_clock_snapshot
, bt2
._UnknownClockSnapshot
205 def test_all_msg_without_cc(self
):
206 params
= {'with_cc': False}
207 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
208 self
._msg
_iter
= TestOutputPortMessageIterator(
209 self
._graph
, self
._src
_comp
.output_ports
['out']
212 for i
, msg
in enumerate(self
._msg
_iter
):
214 self
.assertIsInstance(msg
, bt2
._StreamBeginningMessage
)
215 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
216 with self
.assertRaisesRegex(
217 ValueError, 'stream class has no default clock class'
219 msg
.default_clock_snapshot
221 self
.assertIsInstance(msg
, bt2
._PacketBeginningMessage
)
222 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
224 self
.assertIsInstance(msg
, bt2
._EventMessage
)
225 self
.assertEqual(msg
.event
.cls
.addr
, self
._event
_class
.addr
)
226 with self
.assertRaisesRegex(
227 ValueError, 'stream class has no default clock class'
229 msg
.default_clock_snapshot
231 self
.assertIsInstance(msg
, bt2
._DiscardedEventsMessage
)
232 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
233 self
.assertEqual(msg
.count
, 890)
234 self
.assertIsNone(msg
.stream
.cls
.default_clock_class
)
235 with self
.assertRaisesRegex(
237 'such a message has no clock snapshots for this stream class',
239 msg
.beginning_default_clock_snapshot
240 with self
.assertRaisesRegex(
242 'such a message has no clock snapshots for this stream class',
244 msg
.end_default_clock_snapshot
246 self
.assertIsInstance(msg
, bt2
._PacketEndMessage
)
247 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
249 self
.assertIsInstance(msg
, bt2
._DiscardedPacketsMessage
)
250 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
251 self
.assertEqual(msg
.count
, 678)
252 self
.assertIsNone(msg
.stream
.cls
.default_clock_class
)
253 with self
.assertRaisesRegex(
255 'such a message has no clock snapshots for this stream class',
257 msg
.beginning_default_clock_snapshot
258 with self
.assertRaisesRegex(
260 'such a message has no clock snapshots for this stream class',
262 msg
.end_default_clock_snapshot
264 self
.assertIsInstance(msg
, bt2
._StreamEndMessage
)
265 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
266 with self
.assertRaisesRegex(
267 ValueError, 'stream class has no default clock class'
269 msg
.default_clock_snapshot
273 def test_msg_stream_with_clock_snapshots(self
):
274 params
= {'with_cc': True, 'with_stream_msgs_clock_snapshots': True}
276 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
277 self
._msg
_iter
= TestOutputPortMessageIterator(
278 self
._graph
, self
._src
_comp
.output_ports
['out']
280 msgs
= list(self
._msg
_iter
)
282 msg_stream_beg
= msgs
[0]
283 self
.assertIsInstance(msg_stream_beg
, bt2
._StreamBeginningMessage
)
284 self
.assertEqual(msg_stream_beg
.default_clock_snapshot
.value
, 0)
286 msg_stream_end
= msgs
[7]
287 self
.assertIsInstance(msg_stream_end
, bt2
._StreamEndMessage
)
288 self
.assertEqual(msg_stream_end
.default_clock_snapshot
.value
, 7)