| 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | # |
| 3 | # Copyright (C) 2019 EfficiOS Inc. |
| 4 | # |
| 5 | |
| 6 | import unittest |
| 7 | import uuid |
| 8 | import bt2 |
| 9 | import utils |
| 10 | from utils import run_in_component_init, TestOutputPortMessageIterator |
| 11 | from bt2 import value as bt2_value |
| 12 | from bt2 import clock_class as bt2_clock_class |
| 13 | |
| 14 | |
| 15 | class ClockClassOffsetTestCase(unittest.TestCase): |
| 16 | def test_create_default(self): |
| 17 | cco = bt2.ClockClassOffset() |
| 18 | self.assertEqual(cco.seconds, 0) |
| 19 | self.assertEqual(cco.cycles, 0) |
| 20 | |
| 21 | def test_create(self): |
| 22 | cco = bt2.ClockClassOffset(23, 4871232) |
| 23 | self.assertEqual(cco.seconds, 23) |
| 24 | self.assertEqual(cco.cycles, 4871232) |
| 25 | |
| 26 | def test_create_kwargs(self): |
| 27 | cco = bt2.ClockClassOffset(seconds=23, cycles=4871232) |
| 28 | self.assertEqual(cco.seconds, 23) |
| 29 | self.assertEqual(cco.cycles, 4871232) |
| 30 | |
| 31 | def test_create_invalid_seconds(self): |
| 32 | with self.assertRaises(TypeError): |
| 33 | bt2.ClockClassOffset("hello", 4871232) |
| 34 | |
| 35 | def test_create_invalid_cycles(self): |
| 36 | with self.assertRaises(TypeError): |
| 37 | bt2.ClockClassOffset(23, "hello") |
| 38 | |
| 39 | def test_eq(self): |
| 40 | cco1 = bt2.ClockClassOffset(23, 42) |
| 41 | cco2 = bt2.ClockClassOffset(23, 42) |
| 42 | self.assertEqual(cco1, cco2) |
| 43 | |
| 44 | def test_ne_seconds(self): |
| 45 | cco1 = bt2.ClockClassOffset(23, 42) |
| 46 | cco2 = bt2.ClockClassOffset(24, 42) |
| 47 | self.assertNotEqual(cco1, cco2) |
| 48 | |
| 49 | def test_ne_cycles(self): |
| 50 | cco1 = bt2.ClockClassOffset(23, 42) |
| 51 | cco2 = bt2.ClockClassOffset(23, 43) |
| 52 | self.assertNotEqual(cco1, cco2) |
| 53 | |
| 54 | def test_eq_invalid(self): |
| 55 | self.assertFalse(bt2.ClockClassOffset() == 23) |
| 56 | |
| 57 | |
| 58 | class ClockClassTestCase(unittest.TestCase): |
| 59 | def assertRaisesInComponentInit(self, expected_exc_type, user_code): |
| 60 | def f(comp_self): |
| 61 | try: |
| 62 | user_code(comp_self) |
| 63 | except Exception as exc: |
| 64 | return type(exc) |
| 65 | |
| 66 | exc_type = run_in_component_init(f) |
| 67 | self.assertIsNotNone(exc_type) |
| 68 | self.assertEqual(exc_type, expected_exc_type) |
| 69 | |
| 70 | def test_create_default(self): |
| 71 | cc = run_in_component_init(lambda comp_self: comp_self._create_clock_class()) |
| 72 | |
| 73 | self.assertIsNone(cc.name) |
| 74 | self.assertEqual(cc.frequency, 1000000000) |
| 75 | self.assertIsNone(cc.description) |
| 76 | self.assertEqual(cc.precision, 0) |
| 77 | self.assertEqual(cc.offset, bt2.ClockClassOffset()) |
| 78 | self.assertTrue(cc.origin_is_unix_epoch) |
| 79 | self.assertIsNone(cc.uuid) |
| 80 | self.assertEqual(len(cc.user_attributes), 0) |
| 81 | |
| 82 | def test_create_name(self): |
| 83 | def f(comp_self): |
| 84 | return comp_self._create_clock_class(name="the_clock") |
| 85 | |
| 86 | cc = run_in_component_init(f) |
| 87 | self.assertEqual(cc.name, "the_clock") |
| 88 | |
| 89 | def test_create_invalid_name(self): |
| 90 | def f(comp_self): |
| 91 | comp_self._create_clock_class(name=23) |
| 92 | |
| 93 | self.assertRaisesInComponentInit(TypeError, f) |
| 94 | |
| 95 | def test_create_description(self): |
| 96 | def f(comp_self): |
| 97 | return comp_self._create_clock_class(description="hi people") |
| 98 | |
| 99 | cc = run_in_component_init(f) |
| 100 | self.assertEqual(cc.description, "hi people") |
| 101 | |
| 102 | def test_create_invalid_description(self): |
| 103 | def f(comp_self): |
| 104 | return comp_self._create_clock_class(description=23) |
| 105 | |
| 106 | self.assertRaisesInComponentInit(TypeError, f) |
| 107 | |
| 108 | def test_create_frequency(self): |
| 109 | def f(comp_self): |
| 110 | return comp_self._create_clock_class(frequency=987654321) |
| 111 | |
| 112 | cc = run_in_component_init(f) |
| 113 | self.assertEqual(cc.frequency, 987654321) |
| 114 | |
| 115 | def test_create_invalid_frequency(self): |
| 116 | def f(comp_self): |
| 117 | return comp_self._create_clock_class(frequency="lel") |
| 118 | |
| 119 | self.assertRaisesInComponentInit(TypeError, f) |
| 120 | |
| 121 | def test_create_precision(self): |
| 122 | def f(comp_self): |
| 123 | return comp_self._create_clock_class(precision=12) |
| 124 | |
| 125 | cc = run_in_component_init(f) |
| 126 | self.assertEqual(cc.precision, 12) |
| 127 | |
| 128 | def test_create_invalid_precision(self): |
| 129 | def f(comp_self): |
| 130 | return comp_self._create_clock_class(precision="lel") |
| 131 | |
| 132 | self.assertRaisesInComponentInit(TypeError, f) |
| 133 | |
| 134 | def test_create_offset(self): |
| 135 | def f(comp_self): |
| 136 | return comp_self._create_clock_class(offset=bt2.ClockClassOffset(12, 56)) |
| 137 | |
| 138 | cc = run_in_component_init(f) |
| 139 | self.assertEqual(cc.offset, bt2.ClockClassOffset(12, 56)) |
| 140 | |
| 141 | def test_create_invalid_offset(self): |
| 142 | def f(comp_self): |
| 143 | return comp_self._create_clock_class(offset=object()) |
| 144 | |
| 145 | self.assertRaisesInComponentInit(TypeError, f) |
| 146 | |
| 147 | def test_create_origin_is_unix_epoch(self): |
| 148 | def f(comp_self): |
| 149 | return comp_self._create_clock_class(origin_is_unix_epoch=False) |
| 150 | |
| 151 | cc = run_in_component_init(f) |
| 152 | self.assertEqual(cc.origin_is_unix_epoch, False) |
| 153 | |
| 154 | def test_create_invalid_origin_is_unix_epoch(self): |
| 155 | def f(comp_self): |
| 156 | return comp_self._create_clock_class(origin_is_unix_epoch=23) |
| 157 | |
| 158 | self.assertRaisesInComponentInit(TypeError, f) |
| 159 | |
| 160 | def test_cycles_to_ns_from_origin(self): |
| 161 | def f(comp_self): |
| 162 | return comp_self._create_clock_class( |
| 163 | frequency=10**8, origin_is_unix_epoch=True |
| 164 | ) |
| 165 | |
| 166 | cc = run_in_component_init(f) |
| 167 | self.assertEqual(cc.cycles_to_ns_from_origin(112), 1120) |
| 168 | |
| 169 | def test_cycles_to_ns_from_origin_overflow(self): |
| 170 | def f(comp_self): |
| 171 | return comp_self._create_clock_class(frequency=1000) |
| 172 | |
| 173 | cc = run_in_component_init(f) |
| 174 | with self.assertRaises(bt2._OverflowError): |
| 175 | cc.cycles_to_ns_from_origin(2**63) |
| 176 | |
| 177 | def test_create_uuid(self): |
| 178 | def f(comp_self): |
| 179 | return comp_self._create_clock_class( |
| 180 | uuid=uuid.UUID("b43372c32ef0be28444dfc1c5cdafd33") |
| 181 | ) |
| 182 | |
| 183 | cc = run_in_component_init(f) |
| 184 | self.assertEqual(cc.uuid, uuid.UUID("b43372c32ef0be28444dfc1c5cdafd33")) |
| 185 | |
| 186 | def test_create_invalid_uuid(self): |
| 187 | def f(comp_self): |
| 188 | return comp_self._create_clock_class(uuid=23) |
| 189 | |
| 190 | self.assertRaisesInComponentInit(TypeError, f) |
| 191 | |
| 192 | def test_create_user_attributes(self): |
| 193 | def f(comp_self): |
| 194 | return comp_self._create_clock_class(user_attributes={"salut": 23}) |
| 195 | |
| 196 | cc = run_in_component_init(f) |
| 197 | self.assertEqual(cc.user_attributes, {"salut": 23}) |
| 198 | self.assertIs(type(cc.user_attributes), bt2_value.MapValue) |
| 199 | |
| 200 | def test_create_invalid_user_attributes(self): |
| 201 | def f(comp_self): |
| 202 | return comp_self._create_clock_class(user_attributes=object()) |
| 203 | |
| 204 | self.assertRaisesInComponentInit(TypeError, f) |
| 205 | |
| 206 | def test_create_invalid_user_attributes_value_type(self): |
| 207 | def f(comp_self): |
| 208 | return comp_self._create_clock_class(user_attributes=23) |
| 209 | |
| 210 | self.assertRaisesInComponentInit(TypeError, f) |
| 211 | |
| 212 | def test_const_user_attributes(self): |
| 213 | cc = utils.get_const_event_message().default_clock_snapshot.clock_class |
| 214 | self.assertIs(type(cc.user_attributes), bt2_value._MapValueConst) |
| 215 | |
| 216 | |
| 217 | class ClockSnapshotTestCase(unittest.TestCase): |
| 218 | def setUp(self): |
| 219 | def f(comp_self): |
| 220 | cc = comp_self._create_clock_class( |
| 221 | 1000, "my_cc", offset=bt2.ClockClassOffset(45, 354) |
| 222 | ) |
| 223 | tc = comp_self._create_trace_class() |
| 224 | |
| 225 | return (cc, tc) |
| 226 | |
| 227 | _cc, _tc = run_in_component_init(f) |
| 228 | _trace = _tc() |
| 229 | _sc = _tc.create_stream_class(default_clock_class=_cc) |
| 230 | _ec = _sc.create_event_class(name="salut") |
| 231 | _stream = _trace.create_stream(_sc) |
| 232 | self._stream = _stream |
| 233 | self._ec = _ec |
| 234 | self._cc = _cc |
| 235 | |
| 236 | class MyIter(bt2._UserMessageIterator): |
| 237 | def __init__(self, config, self_port_output): |
| 238 | self._at = 0 |
| 239 | |
| 240 | def __next__(self): |
| 241 | if self._at == 0: |
| 242 | notif = self._create_stream_beginning_message(_stream) |
| 243 | elif self._at == 1: |
| 244 | notif = self._create_event_message(_ec, _stream, 123) |
| 245 | elif self._at == 2: |
| 246 | notif = self._create_event_message(_ec, _stream, 2**63) |
| 247 | elif self._at == 3: |
| 248 | notif = self._create_stream_end_message(_stream) |
| 249 | else: |
| 250 | raise bt2.Stop |
| 251 | |
| 252 | self._at += 1 |
| 253 | return notif |
| 254 | |
| 255 | class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter): |
| 256 | def __init__(self, config, params, obj): |
| 257 | self._add_output_port("out") |
| 258 | |
| 259 | self._graph = bt2.Graph() |
| 260 | self._src_comp = self._graph.add_component(MySrc, "my_source") |
| 261 | self._msg_iter = TestOutputPortMessageIterator( |
| 262 | self._graph, self._src_comp.output_ports["out"] |
| 263 | ) |
| 264 | |
| 265 | for i, msg in enumerate(self._msg_iter): |
| 266 | if i == 1: |
| 267 | self._msg = msg |
| 268 | elif i == 2: |
| 269 | self._msg_clock_overflow = msg |
| 270 | break |
| 271 | |
| 272 | def tearDown(self): |
| 273 | del self._cc |
| 274 | del self._msg |
| 275 | |
| 276 | def test_create_default(self): |
| 277 | self.assertEqual( |
| 278 | self._msg.default_clock_snapshot.clock_class.addr, self._cc.addr |
| 279 | ) |
| 280 | self.assertEqual(self._msg.default_clock_snapshot.value, 123) |
| 281 | |
| 282 | def test_clock_class(self): |
| 283 | cc = self._msg.default_clock_snapshot.clock_class |
| 284 | self.assertEqual(cc.addr, self._cc.addr) |
| 285 | self.assertIs(type(cc), bt2_clock_class._ClockClassConst) |
| 286 | |
| 287 | def test_ns_from_origin(self): |
| 288 | s_from_origin = 45 + ((354 + 123) / 1000) |
| 289 | ns_from_origin = int(s_from_origin * 1e9) |
| 290 | self.assertEqual( |
| 291 | self._msg.default_clock_snapshot.ns_from_origin, ns_from_origin |
| 292 | ) |
| 293 | |
| 294 | def test_ns_from_origin_overflow(self): |
| 295 | with self.assertRaises(bt2._OverflowError): |
| 296 | self._msg_clock_overflow.default_clock_snapshot.ns_from_origin |
| 297 | |
| 298 | def test_eq_int(self): |
| 299 | self.assertEqual(self._msg.default_clock_snapshot, 123) |
| 300 | |
| 301 | def test_eq_invalid(self): |
| 302 | self.assertFalse(self._msg.default_clock_snapshot == 23) |
| 303 | |
| 304 | def test_comparison(self): |
| 305 | self.assertTrue(self._msg.default_clock_snapshot > 100) |
| 306 | self.assertFalse(self._msg.default_clock_snapshot > 200) |
| 307 | |
| 308 | self.assertTrue(self._msg.default_clock_snapshot >= 123) |
| 309 | self.assertFalse(self._msg.default_clock_snapshot >= 200) |
| 310 | |
| 311 | self.assertTrue(self._msg.default_clock_snapshot < 200) |
| 312 | self.assertFalse(self._msg.default_clock_snapshot < 100) |
| 313 | |
| 314 | self.assertTrue(self._msg.default_clock_snapshot <= 123) |
| 315 | self.assertFalse(self._msg.default_clock_snapshot <= 100) |
| 316 | |
| 317 | |
| 318 | if __name__ == "__main__": |
| 319 | unittest.main() |