| 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 | |
| 19 | import collections |
| 20 | import unittest |
| 21 | import bt2 |
| 22 | |
| 23 | |
| 24 | class AllMessagesTestCase(unittest.TestCase): |
| 25 | def setUp(self): |
| 26 | |
| 27 | class MyIter(bt2._UserMessageIterator): |
| 28 | def __init__(self, self_port_output): |
| 29 | self._at = 0 |
| 30 | |
| 31 | def __next__(self): |
| 32 | if test_obj._clock_class: |
| 33 | if self._at == 0: |
| 34 | msg = self._create_stream_beginning_message(test_obj._stream) |
| 35 | elif self._at == 1: |
| 36 | msg = self._create_stream_activity_beginning_message(test_obj._stream, default_clock_snapshot=self._at) |
| 37 | elif self._at == 2: |
| 38 | msg = self._create_packet_beginning_message(test_obj._packet, self._at) |
| 39 | elif self._at == 3: |
| 40 | msg = self._create_event_message(test_obj._event_class, test_obj._packet, self._at) |
| 41 | elif self._at == 4: |
| 42 | msg = self._create_message_iterator_inactivity_message(test_obj._clock_class, self._at) |
| 43 | elif self._at == 5: |
| 44 | msg = self._create_discarded_events_message(test_obj._stream, 890, self._at, self._at) |
| 45 | elif self._at == 6: |
| 46 | msg = self._create_packet_end_message(test_obj._packet, self._at) |
| 47 | elif self._at == 7: |
| 48 | msg = self._create_discarded_packets_message(test_obj._stream, 678, self._at, self._at) |
| 49 | elif self._at == 8: |
| 50 | msg = self._create_stream_activity_end_message(test_obj._stream, default_clock_snapshot=self._at) |
| 51 | elif self._at == 9: |
| 52 | msg = self._create_stream_end_message(test_obj._stream) |
| 53 | elif self._at >= 10: |
| 54 | raise bt2.Stop |
| 55 | else: |
| 56 | if self._at == 0: |
| 57 | msg = self._create_stream_beginning_message(test_obj._stream) |
| 58 | elif self._at == 1: |
| 59 | msg = self._create_stream_activity_beginning_message(test_obj._stream) |
| 60 | elif self._at == 2: |
| 61 | msg = self._create_packet_beginning_message(test_obj._packet) |
| 62 | elif self._at == 3: |
| 63 | msg = self._create_event_message(test_obj._event_class, test_obj._packet) |
| 64 | elif self._at == 4: |
| 65 | msg = self._create_discarded_events_message(test_obj._stream, 890) |
| 66 | elif self._at == 5: |
| 67 | msg = self._create_packet_end_message(test_obj._packet) |
| 68 | elif self._at == 6: |
| 69 | msg = self._create_discarded_packets_message(test_obj._stream, 678) |
| 70 | elif self._at == 7: |
| 71 | msg = self._create_stream_activity_end_message(test_obj._stream) |
| 72 | elif self._at == 8: |
| 73 | msg = self._create_stream_end_message(test_obj._stream) |
| 74 | elif self._at >= 9: |
| 75 | raise bt2.Stop |
| 76 | |
| 77 | self._at += 1 |
| 78 | return msg |
| 79 | |
| 80 | class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter): |
| 81 | def __init__(self, params): |
| 82 | self._add_output_port('out') |
| 83 | |
| 84 | with_cc = bool(params['with_cc']) |
| 85 | tc = self._create_trace_class() |
| 86 | if with_cc: |
| 87 | cc = self._create_clock_class() |
| 88 | else: |
| 89 | cc = None |
| 90 | |
| 91 | sc = tc.create_stream_class(default_clock_class=cc, |
| 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) |
| 98 | |
| 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), |
| 104 | ]) |
| 105 | |
| 106 | ec = sc.create_event_class(name='salut', payload_field_class=payload_fc) |
| 107 | |
| 108 | trace = tc() |
| 109 | stream = trace.create_stream(sc) |
| 110 | packet = stream.create_packet() |
| 111 | |
| 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 |
| 117 | |
| 118 | test_obj = self |
| 119 | self._graph = bt2.Graph() |
| 120 | self._src = MySrc |
| 121 | self._iter = MyIter |
| 122 | |
| 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']) |
| 127 | |
| 128 | for i, msg in enumerate(self._msg_iter): |
| 129 | if i == 0: |
| 130 | self.assertIsInstance(msg, bt2.message._StreamBeginningMessage) |
| 131 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 132 | elif i == 1: |
| 133 | self.assertIsInstance(msg, bt2.message._StreamActivityBeginningMessage) |
| 134 | self.assertEqual(msg.default_clock_snapshot.value, i) |
| 135 | elif i == 2: |
| 136 | self.assertIsInstance(msg, bt2.message._PacketBeginningMessage) |
| 137 | self.assertEqual(msg.packet.addr, self._packet.addr) |
| 138 | self.assertEqual(msg.default_clock_snapshot.value, i) |
| 139 | elif i == 3: |
| 140 | self.assertIsInstance(msg, bt2.message._EventMessage) |
| 141 | self.assertEqual(msg.event.cls.addr, self._event_class.addr) |
| 142 | self.assertEqual(msg.default_clock_snapshot.value, i) |
| 143 | elif i == 4: |
| 144 | self.assertIsInstance(msg, bt2.message._MessageIteratorInactivityMessage) |
| 145 | self.assertEqual(msg.default_clock_snapshot.value, i) |
| 146 | elif i == 5: |
| 147 | self.assertIsInstance(msg, bt2.message._DiscardedEventsMessage) |
| 148 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 149 | self.assertEqual(msg.count, 890) |
| 150 | self.assertEqual(msg.stream.cls.default_clock_class.addr, self._clock_class.addr) |
| 151 | self.assertEqual(msg.beginning_default_clock_snapshot.value, i) |
| 152 | self.assertEqual(msg.end_default_clock_snapshot.value, i) |
| 153 | elif i == 6: |
| 154 | self.assertIsInstance(msg, bt2.message._PacketEndMessage) |
| 155 | self.assertEqual(msg.packet.addr, self._packet.addr) |
| 156 | self.assertEqual(msg.default_clock_snapshot.value, i) |
| 157 | elif i == 7: |
| 158 | self.assertIsInstance(msg, bt2.message._DiscardedPacketsMessage) |
| 159 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 160 | self.assertEqual(msg.count, 678) |
| 161 | self.assertEqual(msg.stream.cls.default_clock_class.addr, self._clock_class.addr) |
| 162 | self.assertEqual(msg.beginning_default_clock_snapshot.value, i) |
| 163 | self.assertEqual(msg.end_default_clock_snapshot.value, i) |
| 164 | elif i == 8: |
| 165 | self.assertIsInstance(msg, bt2.message._StreamActivityEndMessage) |
| 166 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 167 | self.assertEqual(msg.default_clock_snapshot.value, i) |
| 168 | elif i == 9: |
| 169 | self.assertIsInstance(msg, bt2.message._StreamEndMessage) |
| 170 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 171 | else: |
| 172 | raise Exception |
| 173 | |
| 174 | def test_all_msg_without_cc(self): |
| 175 | params = {'with_cc': False} |
| 176 | self._src_comp = self._graph.add_component(self._src, 'my_source', params) |
| 177 | self._msg_iter = self._graph.create_output_port_message_iterator(self._src_comp.output_ports['out']) |
| 178 | |
| 179 | for i, msg in enumerate(self._msg_iter): |
| 180 | if i == 0: |
| 181 | self.assertIsInstance(msg, bt2.message._StreamBeginningMessage) |
| 182 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 183 | elif i == 1: |
| 184 | self.assertIsInstance(msg, bt2.message._StreamActivityBeginningMessage) |
| 185 | self.assertIsInstance(msg.default_clock_snapshot, |
| 186 | bt2._UnknownClockSnapshot) |
| 187 | elif i == 2: |
| 188 | self.assertIsInstance(msg, bt2.message._PacketBeginningMessage) |
| 189 | self.assertEqual(msg.packet.addr, self._packet.addr) |
| 190 | elif i == 3: |
| 191 | self.assertIsInstance(msg, bt2.message._EventMessage) |
| 192 | self.assertEqual(msg.event.cls.addr, self._event_class.addr) |
| 193 | with self.assertRaises(bt2.NonexistentClockSnapshot): |
| 194 | msg.default_clock_snapshot |
| 195 | elif i == 4: |
| 196 | self.assertIsInstance(msg, bt2.message._DiscardedEventsMessage) |
| 197 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 198 | self.assertEqual(msg.count, 890) |
| 199 | self.assertIsNone(msg.stream.cls.default_clock_class) |
| 200 | with self.assertRaises(bt2.NonexistentClockSnapshot): |
| 201 | msg.beginning_default_clock_snapshot |
| 202 | with self.assertRaises(bt2.NonexistentClockSnapshot): |
| 203 | msg.end_default_clock_snapshot |
| 204 | elif i == 5: |
| 205 | self.assertIsInstance(msg, bt2.message._PacketEndMessage) |
| 206 | self.assertEqual(msg.packet.addr, self._packet.addr) |
| 207 | elif i == 6: |
| 208 | self.assertIsInstance(msg, bt2.message._DiscardedPacketsMessage) |
| 209 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 210 | self.assertEqual(msg.count, 678) |
| 211 | self.assertIsNone(msg.stream.cls.default_clock_class) |
| 212 | with self.assertRaises(bt2.NonexistentClockSnapshot): |
| 213 | msg.beginning_default_clock_snapshot |
| 214 | with self.assertRaises(bt2.NonexistentClockSnapshot): |
| 215 | msg.end_default_clock_snapshot |
| 216 | elif i == 7: |
| 217 | self.assertIsInstance(msg, bt2.message._StreamActivityEndMessage) |
| 218 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 219 | self.assertIsInstance(msg.default_clock_snapshot, |
| 220 | bt2._UnknownClockSnapshot) |
| 221 | elif i == 8: |
| 222 | self.assertIsInstance(msg, bt2.message._StreamEndMessage) |
| 223 | self.assertEqual(msg.stream.addr, self._stream.addr) |
| 224 | else: |
| 225 | raise Exception |
| 226 | |
| 227 | |
| 228 | class StreamActivityMessagesTestCase(unittest.TestCase): |
| 229 | def _test_create_msg(self, with_cc, test_create_beginning_func, test_create_end_func): |
| 230 | class MyIter(bt2._UserMessageIterator): |
| 231 | def __init__(self, self_port_output): |
| 232 | self._at = 0 |
| 233 | |
| 234 | def __next__(self): |
| 235 | if self._at == 0: |
| 236 | msg = self._create_stream_beginning_message(self._component._stream) |
| 237 | elif self._at == 1: |
| 238 | msg = test_create_beginning_func(self, self._component._stream) |
| 239 | elif self._at == 2: |
| 240 | msg = test_create_end_func(self, self._component._stream) |
| 241 | elif self._at == 3: |
| 242 | msg = self._create_stream_end_message(self._component._stream) |
| 243 | elif self._at >= 4: |
| 244 | raise bt2.Stop |
| 245 | |
| 246 | self._at += 1 |
| 247 | return msg |
| 248 | |
| 249 | class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter): |
| 250 | def __init__(self, params): |
| 251 | self._add_output_port('out') |
| 252 | tc = self._create_trace_class() |
| 253 | |
| 254 | if with_cc: |
| 255 | cc = self._create_clock_class() |
| 256 | sc = tc.create_stream_class(default_clock_class=cc) |
| 257 | else: |
| 258 | sc = tc.create_stream_class() |
| 259 | |
| 260 | # Create payload field class |
| 261 | trace = tc() |
| 262 | self._stream = trace.create_stream(sc) |
| 263 | |
| 264 | graph = bt2.Graph() |
| 265 | src_comp = graph.add_component(MySrc, 'src') |
| 266 | msg_iter = graph.create_output_port_message_iterator(src_comp.output_ports['out']) |
| 267 | |
| 268 | for msg in msg_iter: |
| 269 | pass |
| 270 | |
| 271 | def test_create_beginning_with_cc_with_known_default_cs(self): |
| 272 | def create_beginning(msg_iter, stream): |
| 273 | msg = msg_iter._create_stream_activity_beginning_message(stream, 172) |
| 274 | self.assertEqual(msg.default_clock_snapshot.value, 172) |
| 275 | return msg |
| 276 | |
| 277 | def create_end(msg_iter, stream): |
| 278 | return msg_iter._create_stream_activity_end_message(stream, 199) |
| 279 | |
| 280 | self._test_create_msg(True, create_beginning, create_end) |
| 281 | |
| 282 | def test_create_end_with_cc_with_known_default_cs(self): |
| 283 | def create_beginning(msg_iter, stream): |
| 284 | return msg_iter._create_stream_activity_beginning_message(stream, 172) |
| 285 | |
| 286 | def create_end(msg_iter, stream): |
| 287 | msg = msg_iter._create_stream_activity_end_message(stream, 199) |
| 288 | self.assertEqual(msg.default_clock_snapshot.value, 199) |
| 289 | return msg |
| 290 | |
| 291 | self._test_create_msg(True, create_beginning, create_end) |
| 292 | |
| 293 | def test_create_beginning_with_cc_with_unknown_default_cs(self): |
| 294 | def create_beginning(msg_iter, stream): |
| 295 | msg = msg_iter._create_stream_activity_beginning_message(stream, |
| 296 | msg_iter._unknown_clock_snapshot) |
| 297 | self.assertIsInstance(msg.default_clock_snapshot, |
| 298 | bt2._UnknownClockSnapshot) |
| 299 | return msg |
| 300 | |
| 301 | def create_end(msg_iter, stream): |
| 302 | return msg_iter._create_stream_activity_end_message(stream, 199) |
| 303 | |
| 304 | self._test_create_msg(True, create_beginning, create_end) |
| 305 | |
| 306 | def test_create_end_with_cc_with_unknown_default_cs(self): |
| 307 | def create_beginning(msg_iter, stream): |
| 308 | return msg_iter._create_stream_activity_beginning_message(stream, 172) |
| 309 | |
| 310 | def create_end(msg_iter, stream): |
| 311 | msg = msg_iter._create_stream_activity_end_message(stream, |
| 312 | msg_iter._unknown_clock_snapshot) |
| 313 | self.assertIsInstance(msg.default_clock_snapshot, |
| 314 | bt2._UnknownClockSnapshot) |
| 315 | return msg |
| 316 | |
| 317 | self._test_create_msg(True, create_beginning, create_end) |
| 318 | |
| 319 | def test_create_beginning_with_cc_with_infinite_default_cs(self): |
| 320 | def create_beginning(msg_iter, stream): |
| 321 | msg = msg_iter._create_stream_activity_beginning_message(stream, |
| 322 | msg_iter._infinite_clock_snapshot) |
| 323 | self.assertIsInstance(msg.default_clock_snapshot, |
| 324 | bt2._InfiniteClockSnapshot) |
| 325 | return msg |
| 326 | |
| 327 | def create_end(msg_iter, stream): |
| 328 | return msg_iter._create_stream_activity_end_message(stream, 199) |
| 329 | |
| 330 | self._test_create_msg(True, create_beginning, create_end) |
| 331 | |
| 332 | def test_create_end_with_cc_with_infinite_default_cs(self): |
| 333 | def create_beginning(msg_iter, stream): |
| 334 | return msg_iter._create_stream_activity_beginning_message(stream, 172) |
| 335 | |
| 336 | def create_end(msg_iter, stream): |
| 337 | msg = msg_iter._create_stream_activity_end_message(stream, |
| 338 | msg_iter._infinite_clock_snapshot) |
| 339 | self.assertIsInstance(msg.default_clock_snapshot, |
| 340 | bt2._InfiniteClockSnapshot) |
| 341 | return msg |
| 342 | |
| 343 | self._test_create_msg(True, create_beginning, create_end) |
| 344 | |
| 345 | def test_create_beginning_without_cc_with_known_default_cs(self): |
| 346 | def create_beginning(msg_iter, stream): |
| 347 | with self.assertRaises(ValueError): |
| 348 | msg_iter._create_stream_activity_beginning_message(stream, 172) |
| 349 | |
| 350 | return msg_iter._create_stream_activity_beginning_message(stream) |
| 351 | |
| 352 | def create_end(msg_iter, stream): |
| 353 | return msg_iter._create_stream_activity_end_message(stream) |
| 354 | |
| 355 | self._test_create_msg(False, create_beginning, create_end) |
| 356 | |
| 357 | def test_create_end_without_cc_with_known_default_cs(self): |
| 358 | def create_beginning(msg_iter, stream): |
| 359 | return msg_iter._create_stream_activity_beginning_message(stream) |
| 360 | |
| 361 | def create_end(msg_iter, stream): |
| 362 | with self.assertRaises(ValueError): |
| 363 | msg_iter._create_stream_activity_end_message(stream, 199) |
| 364 | |
| 365 | return msg_iter._create_stream_activity_end_message(stream) |
| 366 | |
| 367 | self._test_create_msg(False, create_beginning, create_end) |
| 368 | |
| 369 | def test_create_beginning_without_cc_with_unknown_default_cs(self): |
| 370 | def create_beginning(msg_iter, stream): |
| 371 | msg = msg_iter._create_stream_activity_beginning_message(stream, |
| 372 | msg_iter._unknown_clock_snapshot) |
| 373 | self.assertIsInstance(msg.default_clock_snapshot, |
| 374 | bt2._UnknownClockSnapshot) |
| 375 | return msg |
| 376 | |
| 377 | def create_end(msg_iter, stream): |
| 378 | return msg_iter._create_stream_activity_end_message(stream) |
| 379 | |
| 380 | self._test_create_msg(False, create_beginning, create_end) |
| 381 | |
| 382 | def test_create_end_without_cc_with_unknown_default_cs(self): |
| 383 | def create_beginning(msg_iter, stream): |
| 384 | return msg_iter._create_stream_activity_beginning_message(stream) |
| 385 | |
| 386 | def create_end(msg_iter, stream): |
| 387 | msg = msg_iter._create_stream_activity_end_message(stream, |
| 388 | msg_iter._unknown_clock_snapshot) |
| 389 | self.assertIsInstance(msg.default_clock_snapshot, |
| 390 | bt2._UnknownClockSnapshot) |
| 391 | return msg |
| 392 | |
| 393 | self._test_create_msg(False, create_beginning, create_end) |
| 394 | |
| 395 | def test_create_beginning_without_cc_with_infinite_default_cs(self): |
| 396 | def create_beginning(msg_iter, stream): |
| 397 | msg = msg_iter._create_stream_activity_beginning_message(stream, |
| 398 | msg_iter._infinite_clock_snapshot) |
| 399 | self.assertIsInstance(msg.default_clock_snapshot, |
| 400 | bt2._InfiniteClockSnapshot) |
| 401 | return msg |
| 402 | |
| 403 | def create_end(msg_iter, stream): |
| 404 | return msg_iter._create_stream_activity_end_message(stream) |
| 405 | |
| 406 | self._test_create_msg(False, create_beginning, create_end) |
| 407 | |
| 408 | def test_create_end_without_cc_with_infinite_default_cs(self): |
| 409 | def create_beginning(msg_iter, stream): |
| 410 | return msg_iter._create_stream_activity_beginning_message(stream) |
| 411 | |
| 412 | def create_end(msg_iter, stream): |
| 413 | msg = msg_iter._create_stream_activity_end_message(stream, |
| 414 | msg_iter._infinite_clock_snapshot) |
| 415 | self.assertIsInstance(msg.default_clock_snapshot, |
| 416 | bt2._InfiniteClockSnapshot) |
| 417 | return msg |
| 418 | |
| 419 | self._test_create_msg(False, create_beginning, create_end) |
| 420 | |
| 421 | def test_create_beginning_default_cs_wrong_type(self): |
| 422 | def create_beginning(msg_iter, stream): |
| 423 | with self.assertRaises(TypeError): |
| 424 | msg_iter._create_stream_activity_beginning_message(stream, 'infinite') |
| 425 | |
| 426 | return msg_iter._create_stream_activity_beginning_message(stream) |
| 427 | |
| 428 | def create_end(msg_iter, stream): |
| 429 | return msg_iter._create_stream_activity_end_message(stream) |
| 430 | |
| 431 | self._test_create_msg(False, create_beginning, create_end) |
| 432 | |
| 433 | def test_create_end_without_default_cs_wrong_type(self): |
| 434 | def create_beginning(msg_iter, stream): |
| 435 | return msg_iter._create_stream_activity_beginning_message(stream) |
| 436 | |
| 437 | def create_end(msg_iter, stream): |
| 438 | with self.assertRaises(TypeError): |
| 439 | msg_iter._create_stream_activity_end_message(stream, 'unknown') |
| 440 | |
| 441 | return msg_iter._create_stream_activity_end_message(stream) |
| 442 | |
| 443 | self._test_create_msg(False, create_beginning, create_end) |