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