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
):
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('with_stream_msgs_clock_snapshots', False)
33 if test_obj
._clock
_class
:
35 if self
._with
_stream
_msgs
_clock
_snapshots
:
36 msg
= self
._create
_stream
_beginning
_message
(test_obj
._stream
, default_clock_snapshot
=self
._at
)
38 msg
= self
._create
_stream
_beginning
_message
(test_obj
._stream
)
40 msg
= self
._create
_packet
_beginning
_message
(test_obj
._packet
, self
._at
)
42 msg
= self
._create
_event
_message
(test_obj
._event
_class
, test_obj
._packet
, self
._at
)
44 msg
= self
._create
_message
_iterator
_inactivity
_message
(test_obj
._clock
_class
, self
._at
)
46 msg
= self
._create
_discarded
_events
_message
(test_obj
._stream
, 890, self
._at
, self
._at
)
48 msg
= self
._create
_packet
_end
_message
(test_obj
._packet
, self
._at
)
50 msg
= self
._create
_discarded
_packets
_message
(test_obj
._stream
, 678, self
._at
, self
._at
)
52 if self
._with
_stream
_msgs
_clock
_snapshots
:
53 msg
= self
._create
_stream
_end
_message
(test_obj
._stream
, default_clock_snapshot
=self
._at
)
55 msg
= self
._create
_stream
_end
_message
(test_obj
._stream
)
60 msg
= self
._create
_stream
_beginning
_message
(test_obj
._stream
)
62 msg
= self
._create
_packet
_beginning
_message
(test_obj
._packet
)
64 msg
= self
._create
_event
_message
(test_obj
._event
_class
, test_obj
._packet
)
66 msg
= self
._create
_discarded
_events
_message
(test_obj
._stream
, 890)
68 msg
= self
._create
_packet
_end
_message
(test_obj
._packet
)
70 msg
= self
._create
_discarded
_packets
_message
(test_obj
._stream
, 678)
72 msg
= self
._create
_stream
_end
_message
(test_obj
._stream
)
79 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
80 def __init__(self
, params
):
81 self
._add
_output
_port
('out', params
)
83 with_cc
= bool(params
['with_cc'])
84 tc
= self
._create
_trace
_class
()
86 cc
= self
._create
_clock
_class
()
90 sc
= tc
.create_stream_class(default_clock_class
=cc
,
91 supports_packets
=True,
92 packets_have_beginning_default_clock_snapshot
=with_cc
,
93 packets_have_end_default_clock_snapshot
=with_cc
,
94 supports_discarded_events
=True,
95 discarded_events_have_default_clock_snapshots
=with_cc
,
96 supports_discarded_packets
=True,
97 discarded_packets_have_default_clock_snapshots
=with_cc
)
99 # Create payload field class
100 my_int_fc
= tc
.create_signed_integer_field_class(32)
101 payload_fc
= tc
.create_structure_field_class()
102 payload_fc
+= collections
.OrderedDict([
103 ('my_int', my_int_fc
),
106 ec
= sc
.create_event_class(name
='salut', payload_field_class
=payload_fc
)
109 stream
= trace
.create_stream(sc
)
110 packet
= stream
.create_packet()
112 test_obj
._trace
= trace
113 test_obj
._stream
= stream
114 test_obj
._packet
= packet
115 test_obj
._event
_class
= ec
116 test_obj
._clock
_class
= cc
119 self
._graph
= bt2
.Graph()
123 def test_all_msg_with_cc(self
):
124 params
= {'with_cc': True}
125 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
126 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(self
._src
_comp
.output_ports
['out'])
128 for i
, msg
in enumerate(self
._msg
_iter
):
130 self
.assertIsInstance(msg
, bt2
.message
._StreamBeginningMessage
)
131 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
132 self
.assertIsInstance(msg
.default_clock_snapshot
, bt2
.clock_snapshot
._UnknownClockSnapshot
)
134 self
.assertIsInstance(msg
, bt2
.message
._PacketBeginningMessage
)
135 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
136 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
138 self
.assertIsInstance(msg
, bt2
.message
._EventMessage
)
139 self
.assertEqual(msg
.event
.cls
.addr
, self
._event
_class
.addr
)
140 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
142 self
.assertIsInstance(msg
, bt2
.message
._MessageIteratorInactivityMessage
)
143 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
145 self
.assertIsInstance(msg
, bt2
.message
._DiscardedEventsMessage
)
146 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
147 self
.assertEqual(msg
.count
, 890)
148 self
.assertEqual(msg
.stream
.cls
.default_clock_class
.addr
, self
._clock
_class
.addr
)
149 self
.assertEqual(msg
.beginning_default_clock_snapshot
.value
, i
)
150 self
.assertEqual(msg
.end_default_clock_snapshot
.value
, i
)
152 self
.assertIsInstance(msg
, bt2
.message
._PacketEndMessage
)
153 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
154 self
.assertEqual(msg
.default_clock_snapshot
.value
, i
)
156 self
.assertIsInstance(msg
, bt2
.message
._DiscardedPacketsMessage
)
157 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
158 self
.assertEqual(msg
.count
, 678)
159 self
.assertEqual(msg
.stream
.cls
.default_clock_class
.addr
, self
._clock
_class
.addr
)
160 self
.assertEqual(msg
.beginning_default_clock_snapshot
.value
, i
)
161 self
.assertEqual(msg
.end_default_clock_snapshot
.value
, i
)
163 self
.assertIsInstance(msg
, bt2
.message
._StreamEndMessage
)
164 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
165 self
.assertIsInstance(msg
.default_clock_snapshot
, bt2
.clock_snapshot
._UnknownClockSnapshot
)
169 def test_all_msg_without_cc(self
):
170 params
= {'with_cc': False}
171 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
172 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(self
._src
_comp
.output_ports
['out'])
174 for i
, msg
in enumerate(self
._msg
_iter
):
176 self
.assertIsInstance(msg
, bt2
.message
._StreamBeginningMessage
)
177 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
178 with self
.assertRaises(bt2
.NonexistentClockSnapshot
):
179 msg
.default_clock_snapshot
181 self
.assertIsInstance(msg
, bt2
.message
._PacketBeginningMessage
)
182 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
184 self
.assertIsInstance(msg
, bt2
.message
._EventMessage
)
185 self
.assertEqual(msg
.event
.cls
.addr
, self
._event
_class
.addr
)
186 with self
.assertRaises(bt2
.NonexistentClockSnapshot
):
187 msg
.default_clock_snapshot
189 self
.assertIsInstance(msg
, bt2
.message
._DiscardedEventsMessage
)
190 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
191 self
.assertEqual(msg
.count
, 890)
192 self
.assertIsNone(msg
.stream
.cls
.default_clock_class
)
193 with self
.assertRaises(bt2
.NonexistentClockSnapshot
):
194 msg
.beginning_default_clock_snapshot
195 with self
.assertRaises(bt2
.NonexistentClockSnapshot
):
196 msg
.end_default_clock_snapshot
198 self
.assertIsInstance(msg
, bt2
.message
._PacketEndMessage
)
199 self
.assertEqual(msg
.packet
.addr
, self
._packet
.addr
)
201 self
.assertIsInstance(msg
, bt2
.message
._DiscardedPacketsMessage
)
202 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
203 self
.assertEqual(msg
.count
, 678)
204 self
.assertIsNone(msg
.stream
.cls
.default_clock_class
)
205 with self
.assertRaises(bt2
.NonexistentClockSnapshot
):
206 msg
.beginning_default_clock_snapshot
207 with self
.assertRaises(bt2
.NonexistentClockSnapshot
):
208 msg
.end_default_clock_snapshot
210 self
.assertIsInstance(msg
, bt2
.message
._StreamEndMessage
)
211 self
.assertEqual(msg
.stream
.addr
, self
._stream
.addr
)
212 with self
.assertRaises(bt2
.NonexistentClockSnapshot
):
213 msg
.default_clock_snapshot
217 def test_msg_stream_with_clock_snapshots(self
):
220 'with_stream_msgs_clock_snapshots': True,
223 self
._src
_comp
= self
._graph
.add_component(self
._src
, 'my_source', params
)
224 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(self
._src
_comp
.output_ports
['out'])
225 msgs
= list(self
._msg
_iter
)
227 msg_stream_beg
= msgs
[0]
228 self
.assertIsInstance(msg_stream_beg
, bt2
.message
._StreamBeginningMessage
)
229 self
.assertEqual(msg_stream_beg
.default_clock_snapshot
.value
, 0)
231 msg_stream_end
= msgs
[7]
232 self
.assertIsInstance(msg_stream_end
, bt2
.message
._StreamEndMessage
)
233 self
.assertEqual(msg_stream_end
.default_clock_snapshot
.value
, 7)