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