bt2: clean available `bt2` package names
[babeltrace.git] / tests / bindings / python / bt2 / test_message.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
811644b8
PP
19import collections
20import unittest
811644b8
PP
21import bt2
22
23
9ec609ec 24class AllMessagesTestCase(unittest.TestCase):
811644b8 25 def setUp(self):
5602ef81 26 class MyIter(bt2._UserMessageIterator):
c5f330cd 27 def __init__(self, self_port_output):
9ec609ec 28 self._at = 0
cfbd7cf3
FD
29 self._with_stream_msgs_clock_snapshots = self_port_output.user_data.get(
30 'with_stream_msgs_clock_snapshots', False
31 )
811644b8
PP
32
33 def __next__(self):
9ec609ec
SM
34 if test_obj._clock_class:
35 if self._at == 0:
188edac1 36 if self._with_stream_msgs_clock_snapshots:
cfbd7cf3
FD
37 msg = self._create_stream_beginning_message(
38 test_obj._stream, default_clock_snapshot=self._at
39 )
188edac1 40 else:
cfbd7cf3
FD
41 msg = self._create_stream_beginning_message(
42 test_obj._stream
43 )
9ec609ec 44 elif self._at == 1:
cfbd7cf3
FD
45 msg = self._create_packet_beginning_message(
46 test_obj._packet, self._at
47 )
188edac1 48 elif self._at == 2:
cfbd7cf3
FD
49 msg = self._create_event_message(
50 test_obj._event_class, test_obj._packet, self._at
51 )
188edac1 52 elif self._at == 3:
cfbd7cf3
FD
53 msg = self._create_message_iterator_inactivity_message(
54 test_obj._clock_class, self._at
55 )
188edac1 56 elif self._at == 4:
cfbd7cf3
FD
57 msg = self._create_discarded_events_message(
58 test_obj._stream, 890, self._at, self._at
59 )
188edac1 60 elif self._at == 5:
cfbd7cf3
FD
61 msg = self._create_packet_end_message(
62 test_obj._packet, self._at
63 )
188edac1 64 elif self._at == 6:
cfbd7cf3
FD
65 msg = self._create_discarded_packets_message(
66 test_obj._stream, 678, self._at, self._at
67 )
188edac1
SM
68 elif self._at == 7:
69 if self._with_stream_msgs_clock_snapshots:
cfbd7cf3
FD
70 msg = self._create_stream_end_message(
71 test_obj._stream, default_clock_snapshot=self._at
72 )
188edac1
SM
73 else:
74 msg = self._create_stream_end_message(test_obj._stream)
75 elif self._at >= 8:
9ec609ec 76 raise bt2.Stop
811644b8 77 else:
9ec609ec
SM
78 if self._at == 0:
79 msg = self._create_stream_beginning_message(test_obj._stream)
80 elif self._at == 1:
9ec609ec 81 msg = self._create_packet_beginning_message(test_obj._packet)
188edac1 82 elif self._at == 2:
cfbd7cf3
FD
83 msg = self._create_event_message(
84 test_obj._event_class, test_obj._packet
85 )
188edac1 86 elif self._at == 3:
cfbd7cf3
FD
87 msg = self._create_discarded_events_message(
88 test_obj._stream, 890
89 )
188edac1 90 elif self._at == 4:
9ec609ec 91 msg = self._create_packet_end_message(test_obj._packet)
188edac1 92 elif self._at == 5:
cfbd7cf3
FD
93 msg = self._create_discarded_packets_message(
94 test_obj._stream, 678
95 )
188edac1 96 elif self._at == 6:
9ec609ec 97 msg = self._create_stream_end_message(test_obj._stream)
188edac1 98 elif self._at >= 7:
9ec609ec 99 raise bt2.Stop
811644b8
PP
100
101 self._at += 1
5602ef81 102 return msg
811644b8 103
9ec609ec 104 class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
811644b8 105 def __init__(self, params):
188edac1 106 self._add_output_port('out', params)
811644b8 107
2e90378a 108 with_cc = bool(params['with_cc'])
9ec609ec
SM
109 tc = self._create_trace_class()
110 if with_cc:
111 cc = self._create_clock_class()
811644b8 112 else:
9ec609ec 113 cc = None
9ec609ec 114
cfbd7cf3
FD
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,
124 )
9ec609ec
SM
125
126 # Create payload field class
127 my_int_fc = tc.create_signed_integer_field_class(32)
128 payload_fc = tc.create_structure_field_class()
cfbd7cf3 129 payload_fc += [('my_int', my_int_fc)]
9ec609ec
SM
130
131 ec = sc.create_event_class(name='salut', payload_field_class=payload_fc)
132
133 trace = tc()
134 stream = trace.create_stream(sc)
135 packet = stream.create_packet()
136
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
142
143 test_obj = self
144 self._graph = bt2.Graph()
145 self._src = MySrc
146 self._iter = MyIter
147
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)
cfbd7cf3
FD
151 self._msg_iter = self._graph.create_output_port_message_iterator(
152 self._src_comp.output_ports['out']
153 )
9ec609ec
SM
154
155 for i, msg in enumerate(self._msg_iter):
156 if i == 0:
3fb99a22 157 self.assertIsInstance(msg, bt2._StreamBeginningMessage)
9ec609ec 158 self.assertEqual(msg.stream.addr, self._stream.addr)
cfbd7cf3 159 self.assertIsInstance(
3fb99a22 160 msg.default_clock_snapshot, bt2._UnknownClockSnapshot
cfbd7cf3 161 )
9ec609ec 162 elif i == 1:
3fb99a22 163 self.assertIsInstance(msg, bt2._PacketBeginningMessage)
9ec609ec
SM
164 self.assertEqual(msg.packet.addr, self._packet.addr)
165 self.assertEqual(msg.default_clock_snapshot.value, i)
188edac1 166 elif i == 2:
3fb99a22 167 self.assertIsInstance(msg, bt2._EventMessage)
e8ac1aae 168 self.assertEqual(msg.event.cls.addr, self._event_class.addr)
9ec609ec 169 self.assertEqual(msg.default_clock_snapshot.value, i)
188edac1 170 elif i == 3:
3fb99a22 171 self.assertIsInstance(msg, bt2._MessageIteratorInactivityMessage)
9ec609ec 172 self.assertEqual(msg.default_clock_snapshot.value, i)
188edac1 173 elif i == 4:
3fb99a22 174 self.assertIsInstance(msg, bt2._DiscardedEventsMessage)
9ec609ec
SM
175 self.assertEqual(msg.stream.addr, self._stream.addr)
176 self.assertEqual(msg.count, 890)
cfbd7cf3
FD
177 self.assertEqual(
178 msg.stream.cls.default_clock_class.addr, self._clock_class.addr
179 )
9ec609ec
SM
180 self.assertEqual(msg.beginning_default_clock_snapshot.value, i)
181 self.assertEqual(msg.end_default_clock_snapshot.value, i)
188edac1 182 elif i == 5:
3fb99a22 183 self.assertIsInstance(msg, bt2._PacketEndMessage)
9ec609ec
SM
184 self.assertEqual(msg.packet.addr, self._packet.addr)
185 self.assertEqual(msg.default_clock_snapshot.value, i)
188edac1 186 elif i == 6:
3fb99a22 187 self.assertIsInstance(msg, bt2._DiscardedPacketsMessage)
9ec609ec
SM
188 self.assertEqual(msg.stream.addr, self._stream.addr)
189 self.assertEqual(msg.count, 678)
cfbd7cf3
FD
190 self.assertEqual(
191 msg.stream.cls.default_clock_class.addr, self._clock_class.addr
192 )
9ec609ec
SM
193 self.assertEqual(msg.beginning_default_clock_snapshot.value, i)
194 self.assertEqual(msg.end_default_clock_snapshot.value, i)
188edac1 195 elif i == 7:
3fb99a22 196 self.assertIsInstance(msg, bt2._StreamEndMessage)
9ec609ec 197 self.assertEqual(msg.stream.addr, self._stream.addr)
cfbd7cf3 198 self.assertIsInstance(
3fb99a22 199 msg.default_clock_snapshot, bt2._UnknownClockSnapshot
cfbd7cf3 200 )
9ec609ec
SM
201 else:
202 raise Exception
203
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)
cfbd7cf3
FD
207 self._msg_iter = self._graph.create_output_port_message_iterator(
208 self._src_comp.output_ports['out']
209 )
9ec609ec
SM
210
211 for i, msg in enumerate(self._msg_iter):
212 if i == 0:
3fb99a22 213 self.assertIsInstance(msg, bt2._StreamBeginningMessage)
9ec609ec 214 self.assertEqual(msg.stream.addr, self._stream.addr)
1153eccb
SM
215 with self.assertRaisesRegex(
216 ValueError, 'stream class has no default clock class'
217 ):
188edac1 218 msg.default_clock_snapshot
9ec609ec 219 elif i == 1:
3fb99a22 220 self.assertIsInstance(msg, bt2._PacketBeginningMessage)
9ec609ec 221 self.assertEqual(msg.packet.addr, self._packet.addr)
188edac1 222 elif i == 2:
3fb99a22 223 self.assertIsInstance(msg, bt2._EventMessage)
e8ac1aae 224 self.assertEqual(msg.event.cls.addr, self._event_class.addr)
1153eccb
SM
225 with self.assertRaisesRegex(
226 ValueError, 'stream class has no default clock class'
227 ):
9ec609ec 228 msg.default_clock_snapshot
188edac1 229 elif i == 3:
3fb99a22 230 self.assertIsInstance(msg, bt2._DiscardedEventsMessage)
9ec609ec
SM
231 self.assertEqual(msg.stream.addr, self._stream.addr)
232 self.assertEqual(msg.count, 890)
e8ac1aae 233 self.assertIsNone(msg.stream.cls.default_clock_class)
1153eccb
SM
234 with self.assertRaisesRegex(
235 ValueError,
236 'such a message has no clock snapshots for this stream class',
237 ):
9ec609ec 238 msg.beginning_default_clock_snapshot
1153eccb
SM
239 with self.assertRaisesRegex(
240 ValueError,
241 'such a message has no clock snapshots for this stream class',
242 ):
9ec609ec 243 msg.end_default_clock_snapshot
188edac1 244 elif i == 4:
3fb99a22 245 self.assertIsInstance(msg, bt2._PacketEndMessage)
9ec609ec 246 self.assertEqual(msg.packet.addr, self._packet.addr)
188edac1 247 elif i == 5:
3fb99a22 248 self.assertIsInstance(msg, bt2._DiscardedPacketsMessage)
9ec609ec
SM
249 self.assertEqual(msg.stream.addr, self._stream.addr)
250 self.assertEqual(msg.count, 678)
e8ac1aae 251 self.assertIsNone(msg.stream.cls.default_clock_class)
1153eccb
SM
252 with self.assertRaisesRegex(
253 ValueError,
254 'such a message has no clock snapshots for this stream class',
255 ):
9ec609ec 256 msg.beginning_default_clock_snapshot
1153eccb
SM
257 with self.assertRaisesRegex(
258 ValueError,
259 'such a message has no clock snapshots for this stream class',
260 ):
9ec609ec 261 msg.end_default_clock_snapshot
188edac1 262 elif i == 6:
3fb99a22 263 self.assertIsInstance(msg, bt2._StreamEndMessage)
9ec609ec 264 self.assertEqual(msg.stream.addr, self._stream.addr)
1153eccb
SM
265 with self.assertRaisesRegex(
266 ValueError, 'stream class has no default clock class'
267 ):
188edac1 268 msg.default_clock_snapshot
9ec609ec
SM
269 else:
270 raise Exception
811644b8 271
188edac1 272 def test_msg_stream_with_clock_snapshots(self):
cfbd7cf3 273 params = {'with_cc': True, 'with_stream_msgs_clock_snapshots': True}
c6af194f 274
188edac1 275 self._src_comp = self._graph.add_component(self._src, 'my_source', params)
cfbd7cf3
FD
276 self._msg_iter = self._graph.create_output_port_message_iterator(
277 self._src_comp.output_ports['out']
278 )
188edac1 279 msgs = list(self._msg_iter)
c6af194f 280
188edac1 281 msg_stream_beg = msgs[0]
3fb99a22 282 self.assertIsInstance(msg_stream_beg, bt2._StreamBeginningMessage)
188edac1 283 self.assertEqual(msg_stream_beg.default_clock_snapshot.value, 0)
c6af194f 284
188edac1 285 msg_stream_end = msgs[7]
3fb99a22 286 self.assertIsInstance(msg_stream_end, bt2._StreamEndMessage)
188edac1 287 self.assertEqual(msg_stream_end.default_clock_snapshot.value, 7)
This page took 0.052255 seconds and 4 git commands to generate.