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.
24 class AllMessagesTestCase(unittest
.TestCase
):
26 class MyIter(bt2
._UserMessageIterator
):
27 def __init__(self
, self_port_output
):
29 self
._with
_stream
_msgs
_clock
_snapshots
= self_port_output
.user_data
.get(
30 'with_stream_msgs_clock_snapshots', False
34 if test_obj
._clock
_class
:
36 if self
._with
_stream
_msgs
_clock
_snapshots
:
37 msg
= self
._create
_stream
_beginning
_message
(
38 test_obj
._stream
, default_clock_snapshot
=self
._at
41 msg
= self
._create
_stream
_beginning
_message
(
45 msg
= self
._create
_packet
_beginning
_message
(
46 test_obj
._packet
, self
._at
49 msg
= self
._create
_event
_message
(
50 test_obj
._event
_class
, test_obj
._packet
, self
._at
53 msg
= self
._create
_message
_iterator
_inactivity
_message
(
54 test_obj
._clock
_class
, self
._at
57 msg
= self
._create
_discarded
_events
_message
(
58 test_obj
._stream
, 890, self
._at
, self
._at
61 msg
= self
._create
_packet
_end
_message
(
62 test_obj
._packet
, self
._at
65 msg
= self
._create
_discarded
_packets
_message
(
66 test_obj
._stream
, 678, self
._at
, self
._at
69 if self
._with
_stream
_msgs
_clock
_snapshots
:
70 msg
= self
._create
_stream
_end
_message
(
71 test_obj
._stream
, default_clock_snapshot
=self
._at
74 msg
= self
._create
_stream
_end
_message
(test_obj
._stream
)
79 msg
= self
._create
_stream
_beginning
_message
(test_obj
._stream
)
81 msg
= self
._create
_packet
_beginning
_message
(test_obj
._packet
)
83 msg
= self
._create
_event
_message
(
84 test_obj
._event
_class
, test_obj
._packet
87 msg
= self
._create
_discarded
_events
_message
(
91 msg
= self
._create
_packet
_end
_message
(test_obj
._packet
)
93 msg
= self
._create
_discarded
_packets
_message
(
97 msg
= self
._create
_stream
_end
_message
(test_obj
._stream
)
104 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
105 def __init__(self
, params
):
106 self
._add
_output
_port
('out', params
)
108 with_cc
= bool(params
['with_cc'])
109 tc
= self
._create
_trace
_class
()
111 cc
= self
._create
_clock
_class
()
115 sc
= tc
.create_stream_class(
116 default_clock_class
=cc
,
117 supports_packets
=True,
118 packets_have_beginning_default_clock_snapshot
=with_cc
,
119 packets_have_end_default_clock_snapshot
=with_cc
,
120 supports_discarded_events
=True,
121 discarded_events_have_default_clock_snapshots
=with_cc
,
122 supports_discarded_packets
=True,
123 discarded_packets_have_default_clock_snapshots
=with_cc
,
126 # Create payload field class
127 my_int_fc
= tc
.create_signed_integer_field_class(32)
128 payload_fc
= tc
.create_structure_field_class()
129 payload_fc
+= [('my_int', my_int_fc
)]
131 ec
= sc
.create_event_class(name
='salut', payload_field_class
=payload_fc
)
134 stream
= trace
.create_stream(sc
)
135 packet
= stream
.create_packet()
137 test_obj
._trace
= trace
138 test_obj
._stream
= stream
139 test_obj
._packet
= packet
140 test_obj
._event
_class
= ec
141 test_obj
._clock
_class
= cc
144 self
._graph
= bt2
.Graph()
148 def test_all_msg_with_cc(self
):
149 params
= {'with_cc': True}
150 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
151 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(
152 self
._src
_comp
.output_ports
['out']
155 for i
, msg
in enumerate(self
._msg
_iter
):
157 self
.assertIsInstance(msg
, bt2
._StreamBeginningMessage
)
158 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
159 self
.assertIsInstance(
160 msg
.default_clock_snapshot
, bt2
._UnknownClockSnapshot
163 self
.assertIsInstance(msg
, bt2
._PacketBeginningMessage
)
164 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
165 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
167 self
.assertIsInstance(msg
, bt2
._EventMessage
)
168 self
.assertEqual(msg
.event
.cls
.addr
, self
._event
_class
.addr
)
169 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
171 self
.assertIsInstance(msg
, bt2
._MessageIteratorInactivityMessage
)
172 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
174 self
.assertIsInstance(msg
, bt2
._DiscardedEventsMessage
)
175 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
176 self
.assertEqual(msg
.count
, 890)
178 msg
.stream
.cls
.default_clock_class
.addr
, self
._clock
_class
.addr
180 self
.assertEqual(msg
.beginning_default_clock_snapshot
.value
, i
)
181 self
.assertEqual(msg
.end_default_clock_snapshot
.value
, i
)
183 self
.assertIsInstance(msg
, bt2
._PacketEndMessage
)
184 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
185 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
187 self
.assertIsInstance(msg
, bt2
._DiscardedPacketsMessage
)
188 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
189 self
.assertEqual(msg
.count
, 678)
191 msg
.stream
.cls
.default_clock_class
.addr
, self
._clock
_class
.addr
193 self
.assertEqual(msg
.beginning_default_clock_snapshot
.value
, i
)
194 self
.assertEqual(msg
.end_default_clock_snapshot
.value
, i
)
196 self
.assertIsInstance(msg
, bt2
._StreamEndMessage
)
197 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
198 self
.assertIsInstance(
199 msg
.default_clock_snapshot
, bt2
._UnknownClockSnapshot
204 def test_all_msg_without_cc(self
):
205 params
= {'with_cc': False}
206 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
207 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(
208 self
._src
_comp
.output_ports
['out']
211 for i
, msg
in enumerate(self
._msg
_iter
):
213 self
.assertIsInstance(msg
, bt2
._StreamBeginningMessage
)
214 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
215 with self
.assertRaisesRegex(
216 ValueError, 'stream class has no default clock class'
218 msg
.default_clock_snapshot
220 self
.assertIsInstance(msg
, bt2
._PacketBeginningMessage
)
221 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
223 self
.assertIsInstance(msg
, bt2
._EventMessage
)
224 self
.assertEqual(msg
.event
.cls
.addr
, self
._event
_class
.addr
)
225 with self
.assertRaisesRegex(
226 ValueError, 'stream class has no default clock class'
228 msg
.default_clock_snapshot
230 self
.assertIsInstance(msg
, bt2
._DiscardedEventsMessage
)
231 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
232 self
.assertEqual(msg
.count
, 890)
233 self
.assertIsNone(msg
.stream
.cls
.default_clock_class
)
234 with self
.assertRaisesRegex(
236 'such a message has no clock snapshots for this stream class',
238 msg
.beginning_default_clock_snapshot
239 with self
.assertRaisesRegex(
241 'such a message has no clock snapshots for this stream class',
243 msg
.end_default_clock_snapshot
245 self
.assertIsInstance(msg
, bt2
._PacketEndMessage
)
246 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
248 self
.assertIsInstance(msg
, bt2
._DiscardedPacketsMessage
)
249 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
250 self
.assertEqual(msg
.count
, 678)
251 self
.assertIsNone(msg
.stream
.cls
.default_clock_class
)
252 with self
.assertRaisesRegex(
254 'such a message has no clock snapshots for this stream class',
256 msg
.beginning_default_clock_snapshot
257 with self
.assertRaisesRegex(
259 'such a message has no clock snapshots for this stream class',
261 msg
.end_default_clock_snapshot
263 self
.assertIsInstance(msg
, bt2
._StreamEndMessage
)
264 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
265 with self
.assertRaisesRegex(
266 ValueError, 'stream class has no default clock class'
268 msg
.default_clock_snapshot
272 def test_msg_stream_with_clock_snapshots(self
):
273 params
= {'with_cc': True, 'with_stream_msgs_clock_snapshots': True}
275 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
276 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(
277 self
._src
_comp
.output_ports
['out']
279 msgs
= list(self
._msg
_iter
)
281 msg_stream_beg
= msgs
[0]
282 self
.assertIsInstance(msg_stream_beg
, bt2
._StreamBeginningMessage
)
283 self
.assertEqual(msg_stream_beg
.default_clock_snapshot
.value
, 0)
285 msg_stream_end
= msgs
[7]
286 self
.assertIsInstance(msg_stream_end
, bt2
._StreamEndMessage
)
287 self
.assertEqual(msg_stream_end
.default_clock_snapshot
.value
, 7)