Move to kernel style SPDX license identifiers
[babeltrace.git] / tests / bindings / python / bt2 / test_stream_class.py
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_packets_without_cs(self):
348 sc = self._tc.create_stream_class(
349 default_clock_class=self._cc,
350 supports_discarded_packets=True,
351 supports_packets=True,
352 )
353 self.assertTrue(sc.supports_discarded_packets)
354 self.assertFalse(sc.discarded_packets_have_default_clock_snapshots)
355
356 def test_supports_discarded_packets_with_cs(self):
357 sc = self._tc.create_stream_class(
358 default_clock_class=self._cc,
359 supports_discarded_packets=True,
360 discarded_packets_have_default_clock_snapshots=True,
361 supports_packets=True,
362 )
363 self.assertTrue(sc.supports_discarded_packets)
364 self.assertTrue(sc.discarded_packets_have_default_clock_snapshots)
365
366 def test_supports_discarded_packets_raises_without_packet_support(self):
367 with self.assertRaisesRegex(
368 ValueError, "cannot support discarded packets, but not support packets"
369 ):
370 self._tc.create_stream_class(
371 default_clock_class=self._cc, supports_discarded_packets=True
372 )
373
374 self.assertEqual(len(self._tc), 0)
375
376 def test_supports_discarded_packets_raises_type_error(self):
377 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
378 self._tc.create_stream_class(
379 default_clock_class=self._cc,
380 supports_discarded_packets=23,
381 supports_packets=True,
382 )
383
384 self.assertEqual(len(self._tc), 0)
385
386 def test_discarded_packets_have_default_cs_raises_type_error(self):
387 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
388 self._tc.create_stream_class(
389 default_clock_class=self._cc,
390 discarded_packets_have_default_clock_snapshots=23,
391 supports_packets=True,
392 )
393
394 self.assertEqual(len(self._tc), 0)
395
396 def test_does_not_support_discarded_packets_raises_with_cs(self):
397 with self.assertRaisesRegex(
398 ValueError,
399 "cannot not support discarded packets, but have default clock snapshots for discarded packet messages",
400 ):
401 self._tc.create_stream_class(
402 default_clock_class=self._cc,
403 discarded_packets_have_default_clock_snapshots=True,
404 supports_packets=True,
405 )
406
407 self.assertEqual(len(self._tc), 0)
408
409 def test_trace_class(self):
410 sc = self._tc.create_stream_class()
411 self.assertEqual(sc.trace_class.addr, self._tc.addr)
412 self.assertIs(type(sc.trace_class), bt2_trace_class._TraceClass)
413
414 def _create_stream_class_with_event_classes(self):
415 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
416 ec1 = sc.create_event_class(id=23)
417 ec2 = sc.create_event_class(id=17)
418 return sc, ec1, ec2
419
420 def test_getitem(self):
421 sc, ec1, ec2 = self._create_stream_class_with_event_classes()
422
423 self.assertEqual(sc[23].addr, ec1.addr)
424 self.assertEqual(type(sc[23]), bt2_event_class._EventClass)
425 self.assertEqual(sc[17].addr, ec2.addr)
426 self.assertEqual(type(sc[17]), bt2_event_class._EventClass)
427
428 def test_getitem_wrong_key_type(self):
429 sc, _, _ = self._create_stream_class_with_event_classes()
430
431 with self.assertRaisesRegex(TypeError, "'str' is not an 'int' object"):
432 sc['event23']
433
434 def test_getitem_wrong_key(self):
435 sc, _, _ = self._create_stream_class_with_event_classes()
436
437 with self.assertRaisesRegex(KeyError, '19'):
438 sc[19]
439
440 def test_len(self):
441 sc, _, _ = self._create_stream_class_with_event_classes()
442
443 self.assertEqual(len(sc), 2)
444
445 def test_iter(self):
446 sc, _, _ = self._create_stream_class_with_event_classes()
447
448 ec_ids = sorted(sc)
449 self.assertEqual(ec_ids, [17, 23])
450
451
452 if __name__ == '__main__':
453 unittest.main()
This page took 0.044637 seconds and 4 git commands to generate.