Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: GPL-2.0-only |
d2d857a8 MJ |
2 | # |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
d2d857a8 | 5 | |
9cf643d1 PP |
6 | import unittest |
7 | import uuid | |
9cf643d1 | 8 | import bt2 |
f0a42b33 | 9 | import utils |
6c373cc9 | 10 | from utils import run_in_component_init, TestOutputPortMessageIterator |
eddea575 FD |
11 | from bt2 import value as bt2_value |
12 | from bt2 import clock_class as bt2_clock_class | |
9cf643d1 PP |
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): | |
be7bbff9 SM |
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) | |
811644b8 | 65 | |
be7bbff9 SM |
66 | exc_type = run_in_component_init(f) |
67 | self.assertIsNotNone(exc_type) | |
68 | self.assertEqual(exc_type, expected_exc_type) | |
9cf643d1 PP |
69 | |
70 | def test_create_default(self): | |
be7bbff9 | 71 | cc = run_in_component_init(lambda comp_self: comp_self._create_clock_class()) |
9cf643d1 | 72 | |
be7bbff9 SM |
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) | |
5783664e | 80 | self.assertEqual(len(cc.user_attributes), 0) |
9cf643d1 | 81 | |
be7bbff9 SM |
82 | def test_create_name(self): |
83 | def f(comp_self): | |
84 | return comp_self._create_clock_class(name='the_clock') | |
9cf643d1 | 85 | |
be7bbff9 SM |
86 | cc = run_in_component_init(f) |
87 | self.assertEqual(cc.name, 'the_clock') | |
9cf643d1 | 88 | |
be7bbff9 SM |
89 | def test_create_invalid_name(self): |
90 | def f(comp_self): | |
91 | comp_self._create_clock_class(name=23) | |
9cf643d1 | 92 | |
be7bbff9 | 93 | self.assertRaisesInComponentInit(TypeError, f) |
9cf643d1 | 94 | |
be7bbff9 SM |
95 | def test_create_description(self): |
96 | def f(comp_self): | |
97 | return comp_self._create_clock_class(description='hi people') | |
9cf643d1 | 98 | |
be7bbff9 SM |
99 | cc = run_in_component_init(f) |
100 | self.assertEqual(cc.description, 'hi people') | |
9cf643d1 | 101 | |
be7bbff9 SM |
102 | def test_create_invalid_description(self): |
103 | def f(comp_self): | |
104 | return comp_self._create_clock_class(description=23) | |
9cf643d1 | 105 | |
be7bbff9 | 106 | self.assertRaisesInComponentInit(TypeError, f) |
9cf643d1 | 107 | |
be7bbff9 SM |
108 | def test_create_frequency(self): |
109 | def f(comp_self): | |
110 | return comp_self._create_clock_class(frequency=987654321) | |
9cf643d1 | 111 | |
be7bbff9 SM |
112 | cc = run_in_component_init(f) |
113 | self.assertEqual(cc.frequency, 987654321) | |
9cf643d1 | 114 | |
be7bbff9 SM |
115 | def test_create_invalid_frequency(self): |
116 | def f(comp_self): | |
117 | return comp_self._create_clock_class(frequency='lel') | |
9cf643d1 | 118 | |
be7bbff9 | 119 | self.assertRaisesInComponentInit(TypeError, f) |
9cf643d1 | 120 | |
be7bbff9 SM |
121 | def test_create_precision(self): |
122 | def f(comp_self): | |
123 | return comp_self._create_clock_class(precision=12) | |
9cf643d1 | 124 | |
be7bbff9 SM |
125 | cc = run_in_component_init(f) |
126 | self.assertEqual(cc.precision, 12) | |
9cf643d1 | 127 | |
be7bbff9 SM |
128 | def test_create_invalid_precision(self): |
129 | def f(comp_self): | |
130 | return comp_self._create_clock_class(precision='lel') | |
9cf643d1 | 131 | |
be7bbff9 | 132 | self.assertRaisesInComponentInit(TypeError, f) |
9cf643d1 | 133 | |
be7bbff9 SM |
134 | def test_create_offset(self): |
135 | def f(comp_self): | |
136 | return comp_self._create_clock_class(offset=bt2.ClockClassOffset(12, 56)) | |
9cf643d1 | 137 | |
be7bbff9 SM |
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): | |
cfbd7cf3 FD |
162 | return comp_self._create_clock_class( |
163 | frequency=10 ** 8, origin_is_unix_epoch=True | |
164 | ) | |
be7bbff9 SM |
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) | |
cb06aa27 | 174 | with self.assertRaises(bt2._OverflowError): |
cfbd7cf3 | 175 | cc.cycles_to_ns_from_origin(2 ** 63) |
be7bbff9 SM |
176 | |
177 | def test_create_uuid(self): | |
178 | def f(comp_self): | |
cfbd7cf3 FD |
179 | return comp_self._create_clock_class( |
180 | uuid=uuid.UUID('b43372c32ef0be28444dfc1c5cdafd33') | |
181 | ) | |
be7bbff9 SM |
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) | |
9cf643d1 | 191 | |
5783664e PP |
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}) | |
eddea575 | 198 | self.assertIs(type(cc.user_attributes), bt2_value.MapValue) |
5783664e PP |
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 | ||
f0a42b33 FD |
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 | ||
9cf643d1 | 216 | |
4b552f8b | 217 | class ClockSnapshotTestCase(unittest.TestCase): |
9cf643d1 | 218 | def setUp(self): |
be7bbff9 | 219 | def f(comp_self): |
cfbd7cf3 FD |
220 | cc = comp_self._create_clock_class( |
221 | 1000, 'my_cc', offset=bt2.ClockClassOffset(45, 354) | |
222 | ) | |
be7bbff9 SM |
223 | tc = comp_self._create_trace_class() |
224 | ||
225 | return (cc, tc) | |
226 | ||
227 | _cc, _tc = run_in_component_init(f) | |
228 | _trace = _tc() | |
26fc5aed | 229 | _sc = _tc.create_stream_class(default_clock_class=_cc) |
be7bbff9 SM |
230 | _ec = _sc.create_event_class(name='salut') |
231 | _stream = _trace.create_stream(_sc) | |
be7bbff9 SM |
232 | self._stream = _stream |
233 | self._ec = _ec | |
234 | self._cc = _cc | |
235 | ||
236 | class MyIter(bt2._UserMessageIterator): | |
8d8b141d | 237 | def __init__(self, config, self_port_output): |
be7bbff9 SM |
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: | |
26fc5aed | 244 | notif = self._create_event_message(_ec, _stream, 123) |
be7bbff9 | 245 | elif self._at == 2: |
cfbd7cf3 | 246 | notif = self._create_event_message(_ec, _stream, 2 ** 63) |
be7bbff9 | 247 | elif self._at == 3: |
be7bbff9 SM |
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): | |
59225a3e | 256 | def __init__(self, config, params, obj): |
be7bbff9 SM |
257 | self._add_output_port('out') |
258 | ||
259 | self._graph = bt2.Graph() | |
260 | self._src_comp = self._graph.add_component(MySrc, 'my_source') | |
6c373cc9 PP |
261 | self._msg_iter = TestOutputPortMessageIterator( |
262 | self._graph, self._src_comp.output_ports['out'] | |
cfbd7cf3 | 263 | ) |
be7bbff9 SM |
264 | |
265 | for i, msg in enumerate(self._msg_iter): | |
26fc5aed | 266 | if i == 1: |
be7bbff9 | 267 | self._msg = msg |
26fc5aed | 268 | elif i == 2: |
be7bbff9 SM |
269 | self._msg_clock_overflow = msg |
270 | break | |
811644b8 PP |
271 | |
272 | def tearDown(self): | |
273 | del self._cc | |
be7bbff9 | 274 | del self._msg |
9cf643d1 PP |
275 | |
276 | def test_create_default(self): | |
be7bbff9 | 277 | self.assertEqual( |
cfbd7cf3 FD |
278 | self._msg.default_clock_snapshot.clock_class.addr, self._cc.addr |
279 | ) | |
be7bbff9 | 280 | self.assertEqual(self._msg.default_clock_snapshot.value, 123) |
9cf643d1 | 281 | |
be7bbff9 | 282 | def test_clock_class(self): |
eddea575 FD |
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) | |
9cf643d1 | 286 | |
be7bbff9 SM |
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( | |
cfbd7cf3 FD |
291 | self._msg.default_clock_snapshot.ns_from_origin, ns_from_origin |
292 | ) | |
9cf643d1 | 293 | |
be7bbff9 | 294 | def test_ns_from_origin_overflow(self): |
cb06aa27 | 295 | with self.assertRaises(bt2._OverflowError): |
be7bbff9 | 296 | self._msg_clock_overflow.default_clock_snapshot.ns_from_origin |
9cf643d1 | 297 | |
811644b8 | 298 | def test_eq_int(self): |
be7bbff9 | 299 | self.assertEqual(self._msg.default_clock_snapshot, 123) |
9cf643d1 PP |
300 | |
301 | def test_eq_invalid(self): | |
be7bbff9 SM |
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) | |
9cf643d1 | 307 | |
be7bbff9 SM |
308 | self.assertTrue(self._msg.default_clock_snapshot >= 123) |
309 | self.assertFalse(self._msg.default_clock_snapshot >= 200) | |
9cf643d1 | 310 | |
be7bbff9 SM |
311 | self.assertTrue(self._msg.default_clock_snapshot < 200) |
312 | self.assertFalse(self._msg.default_clock_snapshot < 100) | |
9cf643d1 | 313 | |
be7bbff9 SM |
314 | self.assertTrue(self._msg.default_clock_snapshot <= 123) |
315 | self.assertFalse(self._msg.default_clock_snapshot <= 100) | |
d14ddbba SM |
316 | |
317 | ||
318 | if __name__ == '__main__': | |
319 | unittest.main() |