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