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