| 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | # |
| 3 | # Copyright (C) 2019 EfficiOS Inc. |
| 4 | # |
| 5 | |
| 6 | import unittest |
| 7 | from utils import run_in_component_init |
| 8 | from bt2 import stream_class as bt2_stream_class |
| 9 | from bt2 import trace_class as bt2_trace_class |
| 10 | from bt2 import clock_class as bt2_clock_class |
| 11 | from bt2 import event_class as bt2_event_class |
| 12 | from bt2 import field_class as bt2_field_class |
| 13 | |
| 14 | |
| 15 | class StreamClassTestCase(unittest.TestCase): |
| 16 | def setUp(self): |
| 17 | def f(comp_self): |
| 18 | tc = comp_self._create_trace_class(assigns_automatic_stream_class_id=True) |
| 19 | cc = comp_self._create_clock_class() |
| 20 | return tc, cc |
| 21 | |
| 22 | self._tc, self._cc = run_in_component_init(f) |
| 23 | self._trace = self._tc() |
| 24 | |
| 25 | def test_create_default(self): |
| 26 | sc = self._tc.create_stream_class() |
| 27 | |
| 28 | self.assertIs(type(sc), bt2_stream_class._StreamClass) |
| 29 | self.assertIsNone(sc.name) |
| 30 | self.assertIsNone(sc.packet_context_field_class) |
| 31 | self.assertIsNone(sc.event_common_context_field_class) |
| 32 | self.assertIsNone(sc.default_clock_class) |
| 33 | self.assertTrue(sc.assigns_automatic_event_class_id) |
| 34 | self.assertTrue(sc.assigns_automatic_stream_id) |
| 35 | self.assertFalse(sc.supports_packets) |
| 36 | self.assertFalse(sc.packets_have_beginning_default_clock_snapshot) |
| 37 | self.assertFalse(sc.packets_have_end_default_clock_snapshot) |
| 38 | self.assertFalse(sc.supports_discarded_events) |
| 39 | self.assertFalse(sc.discarded_events_have_default_clock_snapshots) |
| 40 | self.assertFalse(sc.supports_discarded_packets) |
| 41 | self.assertFalse(sc.discarded_packets_have_default_clock_snapshots) |
| 42 | self.assertEqual(len(sc.user_attributes), 0) |
| 43 | |
| 44 | def test_create_name(self): |
| 45 | sc = self._tc.create_stream_class(name="bozo") |
| 46 | self.assertEqual(sc.name, "bozo") |
| 47 | |
| 48 | def test_create_invalid_name(self): |
| 49 | with self.assertRaisesRegex(TypeError, "'int' is not a 'str' object"): |
| 50 | self._tc.create_stream_class(name=17) |
| 51 | |
| 52 | self.assertEqual(len(self._tc), 0) |
| 53 | |
| 54 | def test_create_packet_context_field_class(self): |
| 55 | fc = self._tc.create_structure_field_class() |
| 56 | sc = self._tc.create_stream_class( |
| 57 | packet_context_field_class=fc, supports_packets=True |
| 58 | ) |
| 59 | self.assertEqual(sc.packet_context_field_class, fc) |
| 60 | self.assertIs( |
| 61 | type(sc.packet_context_field_class), bt2_field_class._StructureFieldClass |
| 62 | ) |
| 63 | |
| 64 | def test_create_invalid_packet_context_field_class(self): |
| 65 | with self.assertRaisesRegex( |
| 66 | TypeError, |
| 67 | "'int' is not a '<class 'bt2.field_class._StructureFieldClass'>' object", |
| 68 | ): |
| 69 | self._tc.create_stream_class( |
| 70 | packet_context_field_class=22, supports_packets=True |
| 71 | ) |
| 72 | |
| 73 | self.assertEqual(len(self._tc), 0) |
| 74 | |
| 75 | def test_create_invalid_packet_context_field_class_no_packets(self): |
| 76 | fc = self._tc.create_structure_field_class() |
| 77 | |
| 78 | with self.assertRaisesRegex( |
| 79 | ValueError, |
| 80 | "cannot have a packet context field class without supporting packets", |
| 81 | ): |
| 82 | self._tc.create_stream_class(packet_context_field_class=fc) |
| 83 | |
| 84 | self.assertEqual(len(self._tc), 0) |
| 85 | |
| 86 | def test_create_event_common_context_field_class(self): |
| 87 | fc = self._tc.create_structure_field_class() |
| 88 | sc = self._tc.create_stream_class(event_common_context_field_class=fc) |
| 89 | self.assertEqual(sc.event_common_context_field_class, fc) |
| 90 | self.assertIs( |
| 91 | type(sc.event_common_context_field_class), |
| 92 | bt2_field_class._StructureFieldClass, |
| 93 | ) |
| 94 | |
| 95 | def test_create_invalid_event_common_context_field_class(self): |
| 96 | with self.assertRaisesRegex( |
| 97 | TypeError, |
| 98 | "'int' is not a '<class 'bt2.field_class._StructureFieldClass'>' object", |
| 99 | ): |
| 100 | self._tc.create_stream_class(event_common_context_field_class=22) |
| 101 | |
| 102 | self.assertEqual(len(self._tc), 0) |
| 103 | |
| 104 | def test_create_default_clock_class(self): |
| 105 | sc = self._tc.create_stream_class(default_clock_class=self._cc) |
| 106 | self.assertEqual(sc.default_clock_class.addr, self._cc.addr) |
| 107 | self.assertIs(type(sc.default_clock_class), bt2_clock_class._ClockClass) |
| 108 | |
| 109 | def test_create_invalid_default_clock_class(self): |
| 110 | with self.assertRaisesRegex( |
| 111 | TypeError, "'int' is not a '<class 'bt2.clock_class._ClockClass'>' object" |
| 112 | ): |
| 113 | self._tc.create_stream_class(default_clock_class=12) |
| 114 | |
| 115 | self.assertEqual(len(self._tc), 0) |
| 116 | |
| 117 | def test_create_user_attributes(self): |
| 118 | sc = self._tc.create_stream_class(user_attributes={"salut": 23}) |
| 119 | self.assertEqual(sc.user_attributes, {"salut": 23}) |
| 120 | |
| 121 | def test_create_invalid_user_attributes(self): |
| 122 | with self.assertRaisesRegex( |
| 123 | TypeError, "cannot create value object from 'object' object" |
| 124 | ): |
| 125 | self._tc.create_stream_class(user_attributes=object()) |
| 126 | |
| 127 | self.assertEqual(len(self._tc), 0) |
| 128 | |
| 129 | def test_create_invalid_user_attributes_value_type(self): |
| 130 | with self.assertRaisesRegex( |
| 131 | TypeError, |
| 132 | "'SignedIntegerValue' is not a '<class 'bt2.value.MapValue'>' object", |
| 133 | ): |
| 134 | self._tc.create_stream_class(user_attributes=23) |
| 135 | |
| 136 | self.assertEqual(len(self._tc), 0) |
| 137 | |
| 138 | def test_automatic_stream_ids(self): |
| 139 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=True) |
| 140 | self.assertTrue(sc.assigns_automatic_stream_id) |
| 141 | |
| 142 | stream = self._trace.create_stream(sc) |
| 143 | self.assertIsNotNone(stream.id) |
| 144 | |
| 145 | def test_automatic_stream_ids_raises(self): |
| 146 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=True) |
| 147 | self.assertTrue(sc.assigns_automatic_stream_id) |
| 148 | |
| 149 | with self.assertRaisesRegex( |
| 150 | ValueError, "id provided, but stream class assigns automatic stream ids" |
| 151 | ): |
| 152 | self._trace.create_stream(sc, id=123) |
| 153 | |
| 154 | self.assertEqual(len(self._trace), 0) |
| 155 | |
| 156 | def test_automatic_stream_ids_wrong_type(self): |
| 157 | with self.assertRaisesRegex(TypeError, "str' is not a 'bool' object"): |
| 158 | self._tc.create_stream_class(assigns_automatic_stream_id="True") |
| 159 | |
| 160 | self.assertEqual(len(self._tc), 0) |
| 161 | |
| 162 | def test_no_automatic_stream_ids(self): |
| 163 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=False) |
| 164 | self.assertFalse(sc.assigns_automatic_stream_id) |
| 165 | |
| 166 | stream = self._trace.create_stream(sc, id=333) |
| 167 | self.assertEqual(stream.id, 333) |
| 168 | |
| 169 | def test_no_automatic_stream_ids_raises(self): |
| 170 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=False) |
| 171 | self.assertFalse(sc.assigns_automatic_stream_id) |
| 172 | |
| 173 | with self.assertRaisesRegex( |
| 174 | ValueError, |
| 175 | "id not provided, but stream class does not assign automatic stream ids", |
| 176 | ): |
| 177 | self._trace.create_stream(sc) |
| 178 | |
| 179 | self.assertEqual(len(self._trace), 0) |
| 180 | |
| 181 | def test_automatic_event_class_ids(self): |
| 182 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True) |
| 183 | self.assertTrue(sc.assigns_automatic_event_class_id) |
| 184 | |
| 185 | ec = sc.create_event_class() |
| 186 | self.assertIsNotNone(ec.id) |
| 187 | |
| 188 | def test_automatic_event_class_ids_raises(self): |
| 189 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True) |
| 190 | self.assertTrue(sc.assigns_automatic_event_class_id) |
| 191 | |
| 192 | with self.assertRaisesRegex( |
| 193 | ValueError, |
| 194 | "id provided, but stream class assigns automatic event class ids", |
| 195 | ): |
| 196 | sc.create_event_class(id=123) |
| 197 | |
| 198 | self.assertEqual(len(sc), 0) |
| 199 | |
| 200 | def test_automatic_event_class_ids_wrong_type(self): |
| 201 | with self.assertRaisesRegex(TypeError, "'str' is not a 'bool' object"): |
| 202 | self._tc.create_stream_class(assigns_automatic_event_class_id="True") |
| 203 | |
| 204 | self.assertEqual(len(self._tc), 0) |
| 205 | |
| 206 | def test_no_automatic_event_class_ids(self): |
| 207 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) |
| 208 | self.assertFalse(sc.assigns_automatic_event_class_id) |
| 209 | |
| 210 | ec = sc.create_event_class(id=333) |
| 211 | self.assertEqual(ec.id, 333) |
| 212 | |
| 213 | def test_no_automatic_event_class_ids_raises(self): |
| 214 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) |
| 215 | self.assertFalse(sc.assigns_automatic_event_class_id) |
| 216 | |
| 217 | with self.assertRaisesRegex( |
| 218 | ValueError, |
| 219 | "id not provided, but stream class does not assign automatic event class ids", |
| 220 | ): |
| 221 | sc.create_event_class() |
| 222 | |
| 223 | self.assertEqual(len(sc), 0) |
| 224 | |
| 225 | def test_supports_packets_without_cs(self): |
| 226 | sc = self._tc.create_stream_class( |
| 227 | default_clock_class=self._cc, supports_packets=True |
| 228 | ) |
| 229 | self.assertTrue(sc.supports_packets) |
| 230 | self.assertFalse(sc.packets_have_beginning_default_clock_snapshot) |
| 231 | self.assertFalse(sc.packets_have_end_default_clock_snapshot) |
| 232 | |
| 233 | def test_supports_packets_with_begin_cs(self): |
| 234 | sc = self._tc.create_stream_class( |
| 235 | default_clock_class=self._cc, |
| 236 | supports_packets=True, |
| 237 | packets_have_beginning_default_clock_snapshot=True, |
| 238 | ) |
| 239 | self.assertTrue(sc.supports_packets) |
| 240 | self.assertTrue(sc.packets_have_beginning_default_clock_snapshot) |
| 241 | self.assertFalse(sc.packets_have_end_default_clock_snapshot) |
| 242 | |
| 243 | def test_supports_packets_with_end_cs(self): |
| 244 | sc = self._tc.create_stream_class( |
| 245 | default_clock_class=self._cc, |
| 246 | supports_packets=True, |
| 247 | packets_have_end_default_clock_snapshot=True, |
| 248 | ) |
| 249 | self.assertTrue(sc.supports_packets) |
| 250 | self.assertFalse(sc.packets_have_beginning_default_clock_snapshot) |
| 251 | self.assertTrue(sc.packets_have_end_default_clock_snapshot) |
| 252 | |
| 253 | def test_supports_packets_raises_type_error(self): |
| 254 | with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"): |
| 255 | self._tc.create_stream_class( |
| 256 | default_clock_class=self._cc, supports_packets=23 |
| 257 | ) |
| 258 | |
| 259 | self.assertEqual(len(self._tc), 0) |
| 260 | |
| 261 | def test_packets_have_begin_default_cs_raises_type_error(self): |
| 262 | with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"): |
| 263 | self._tc.create_stream_class( |
| 264 | default_clock_class=self._cc, |
| 265 | packets_have_beginning_default_clock_snapshot=23, |
| 266 | ) |
| 267 | |
| 268 | self.assertEqual(len(self._tc), 0) |
| 269 | |
| 270 | def test_packets_have_end_default_cs_raises_type_error(self): |
| 271 | with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"): |
| 272 | self._tc.create_stream_class( |
| 273 | default_clock_class=self._cc, packets_have_end_default_clock_snapshot=23 |
| 274 | ) |
| 275 | |
| 276 | self.assertEqual(len(self._tc), 0) |
| 277 | |
| 278 | def test_does_not_support_packets_raises_with_begin_cs(self): |
| 279 | with self.assertRaisesRegex( |
| 280 | ValueError, |
| 281 | "cannot not support packets, but have packet beginning default clock snapshot", |
| 282 | ): |
| 283 | self._tc.create_stream_class( |
| 284 | default_clock_class=self._cc, |
| 285 | packets_have_beginning_default_clock_snapshot=True, |
| 286 | ) |
| 287 | |
| 288 | self.assertEqual(len(self._tc), 0) |
| 289 | |
| 290 | def test_does_not_support_packets_raises_with_end_cs(self): |
| 291 | with self.assertRaisesRegex( |
| 292 | ValueError, |
| 293 | "cannot not support packets, but have packet end default clock snapshots", |
| 294 | ): |
| 295 | self._tc.create_stream_class( |
| 296 | default_clock_class=self._cc, |
| 297 | packets_have_end_default_clock_snapshot=True, |
| 298 | ) |
| 299 | |
| 300 | self.assertEqual(len(self._tc), 0) |
| 301 | |
| 302 | def test_supports_discarded_events_without_cs(self): |
| 303 | sc = self._tc.create_stream_class( |
| 304 | default_clock_class=self._cc, supports_discarded_events=True |
| 305 | ) |
| 306 | self.assertTrue(sc.supports_discarded_events) |
| 307 | self.assertFalse(sc.discarded_events_have_default_clock_snapshots) |
| 308 | |
| 309 | def test_supports_discarded_events_with_cs(self): |
| 310 | sc = self._tc.create_stream_class( |
| 311 | default_clock_class=self._cc, |
| 312 | supports_discarded_events=True, |
| 313 | discarded_events_have_default_clock_snapshots=True, |
| 314 | ) |
| 315 | self.assertTrue(sc.supports_discarded_events) |
| 316 | self.assertTrue(sc.discarded_events_have_default_clock_snapshots) |
| 317 | |
| 318 | def test_supports_discarded_events_raises_type_error(self): |
| 319 | with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"): |
| 320 | self._tc.create_stream_class( |
| 321 | default_clock_class=self._cc, supports_discarded_events=23 |
| 322 | ) |
| 323 | |
| 324 | self.assertEqual(len(self._tc), 0) |
| 325 | |
| 326 | def test_discarded_events_have_default_cs_raises_type_error(self): |
| 327 | with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"): |
| 328 | self._tc.create_stream_class( |
| 329 | default_clock_class=self._cc, |
| 330 | discarded_events_have_default_clock_snapshots=23, |
| 331 | ) |
| 332 | |
| 333 | self.assertEqual(len(self._tc), 0) |
| 334 | |
| 335 | def test_does_not_support_discarded_events_raises_with_cs(self): |
| 336 | with self.assertRaisesRegex( |
| 337 | ValueError, |
| 338 | "cannot not support discarded events, but have default clock snapshots for discarded event messages", |
| 339 | ): |
| 340 | self._tc.create_stream_class( |
| 341 | default_clock_class=self._cc, |
| 342 | discarded_events_have_default_clock_snapshots=True, |
| 343 | ) |
| 344 | |
| 345 | self.assertEqual(len(self._tc), 0) |
| 346 | |
| 347 | def test_supports_discarded_events_with_clock_snapshots_without_default_clock_class_raises( |
| 348 | self, |
| 349 | ): |
| 350 | with self.assertRaisesRegex( |
| 351 | ValueError, |
| 352 | "cannot have no default clock class, but have default clock snapshots for discarded event messages", |
| 353 | ): |
| 354 | self._tc.create_stream_class( |
| 355 | supports_discarded_events=True, |
| 356 | discarded_events_have_default_clock_snapshots=True, |
| 357 | ) |
| 358 | |
| 359 | self.assertEqual(len(self._tc), 0) |
| 360 | |
| 361 | def test_supports_discarded_packets_without_cs(self): |
| 362 | sc = self._tc.create_stream_class( |
| 363 | default_clock_class=self._cc, |
| 364 | supports_discarded_packets=True, |
| 365 | supports_packets=True, |
| 366 | ) |
| 367 | self.assertTrue(sc.supports_discarded_packets) |
| 368 | self.assertFalse(sc.discarded_packets_have_default_clock_snapshots) |
| 369 | |
| 370 | def test_supports_discarded_packets_with_cs(self): |
| 371 | sc = self._tc.create_stream_class( |
| 372 | default_clock_class=self._cc, |
| 373 | supports_discarded_packets=True, |
| 374 | discarded_packets_have_default_clock_snapshots=True, |
| 375 | supports_packets=True, |
| 376 | ) |
| 377 | self.assertTrue(sc.supports_discarded_packets) |
| 378 | self.assertTrue(sc.discarded_packets_have_default_clock_snapshots) |
| 379 | |
| 380 | def test_supports_discarded_packets_raises_without_packet_support(self): |
| 381 | with self.assertRaisesRegex( |
| 382 | ValueError, "cannot support discarded packets, but not support packets" |
| 383 | ): |
| 384 | self._tc.create_stream_class( |
| 385 | default_clock_class=self._cc, supports_discarded_packets=True |
| 386 | ) |
| 387 | |
| 388 | self.assertEqual(len(self._tc), 0) |
| 389 | |
| 390 | def test_supports_discarded_packets_raises_type_error(self): |
| 391 | with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"): |
| 392 | self._tc.create_stream_class( |
| 393 | default_clock_class=self._cc, |
| 394 | supports_discarded_packets=23, |
| 395 | supports_packets=True, |
| 396 | ) |
| 397 | |
| 398 | self.assertEqual(len(self._tc), 0) |
| 399 | |
| 400 | def test_discarded_packets_have_default_cs_raises_type_error(self): |
| 401 | with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"): |
| 402 | self._tc.create_stream_class( |
| 403 | default_clock_class=self._cc, |
| 404 | discarded_packets_have_default_clock_snapshots=23, |
| 405 | supports_packets=True, |
| 406 | ) |
| 407 | |
| 408 | self.assertEqual(len(self._tc), 0) |
| 409 | |
| 410 | def test_does_not_support_discarded_packets_raises_with_cs(self): |
| 411 | with self.assertRaisesRegex( |
| 412 | ValueError, |
| 413 | "cannot not support discarded packets, but have default clock snapshots for discarded packet messages", |
| 414 | ): |
| 415 | self._tc.create_stream_class( |
| 416 | default_clock_class=self._cc, |
| 417 | discarded_packets_have_default_clock_snapshots=True, |
| 418 | supports_packets=True, |
| 419 | ) |
| 420 | |
| 421 | self.assertEqual(len(self._tc), 0) |
| 422 | |
| 423 | def test_supports_discarded_packets_with_clock_snapshots_without_default_clock_class_raises( |
| 424 | self, |
| 425 | ): |
| 426 | with self.assertRaisesRegex( |
| 427 | ValueError, |
| 428 | "cannot have no default clock class, but have default clock snapshots for discarded packet messages", |
| 429 | ): |
| 430 | self._tc.create_stream_class( |
| 431 | supports_packets=True, |
| 432 | supports_discarded_packets=True, |
| 433 | discarded_packets_have_default_clock_snapshots=True, |
| 434 | ) |
| 435 | |
| 436 | self.assertEqual(len(self._tc), 0) |
| 437 | |
| 438 | def test_trace_class(self): |
| 439 | sc = self._tc.create_stream_class() |
| 440 | self.assertEqual(sc.trace_class.addr, self._tc.addr) |
| 441 | self.assertIs(type(sc.trace_class), bt2_trace_class._TraceClass) |
| 442 | |
| 443 | def _create_stream_class_with_event_classes(self): |
| 444 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) |
| 445 | ec1 = sc.create_event_class(id=23) |
| 446 | ec2 = sc.create_event_class(id=17) |
| 447 | return sc, ec1, ec2 |
| 448 | |
| 449 | def test_getitem(self): |
| 450 | sc, ec1, ec2 = self._create_stream_class_with_event_classes() |
| 451 | |
| 452 | self.assertEqual(sc[23].addr, ec1.addr) |
| 453 | self.assertEqual(type(sc[23]), bt2_event_class._EventClass) |
| 454 | self.assertEqual(sc[17].addr, ec2.addr) |
| 455 | self.assertEqual(type(sc[17]), bt2_event_class._EventClass) |
| 456 | |
| 457 | def test_getitem_wrong_key_type(self): |
| 458 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 459 | |
| 460 | with self.assertRaisesRegex(TypeError, "'str' is not an 'int' object"): |
| 461 | sc["event23"] |
| 462 | |
| 463 | def test_getitem_wrong_key(self): |
| 464 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 465 | |
| 466 | with self.assertRaisesRegex(KeyError, "19"): |
| 467 | sc[19] |
| 468 | |
| 469 | def test_len(self): |
| 470 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 471 | |
| 472 | self.assertEqual(len(sc), 2) |
| 473 | |
| 474 | def test_iter(self): |
| 475 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 476 | |
| 477 | ec_ids = sorted(sc) |
| 478 | self.assertEqual(ec_ids, [17, 23]) |
| 479 | |
| 480 | |
| 481 | if __name__ == "__main__": |
| 482 | unittest.main() |