| 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 | |
| 19 | import unittest |
| 20 | import uuid |
| 21 | import copy |
| 22 | import bt2 |
| 23 | from utils import run_in_component_init |
| 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): |
| 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) |
| 76 | |
| 77 | exc_type = run_in_component_init(f) |
| 78 | self.assertIsNotNone(exc_type) |
| 79 | self.assertEqual(exc_type, expected_exc_type) |
| 80 | |
| 81 | def test_create_default(self): |
| 82 | cc = run_in_component_init(lambda comp_self: comp_self._create_clock_class()) |
| 83 | |
| 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) |
| 91 | |
| 92 | def test_create_name(self): |
| 93 | def f(comp_self): |
| 94 | return comp_self._create_clock_class(name='the_clock') |
| 95 | |
| 96 | cc = run_in_component_init(f) |
| 97 | self.assertEqual(cc.name, 'the_clock') |
| 98 | |
| 99 | def test_create_invalid_name(self): |
| 100 | def f(comp_self): |
| 101 | comp_self._create_clock_class(name=23) |
| 102 | |
| 103 | self.assertRaisesInComponentInit(TypeError, f) |
| 104 | |
| 105 | def test_create_description(self): |
| 106 | def f(comp_self): |
| 107 | return comp_self._create_clock_class(description='hi people') |
| 108 | |
| 109 | cc = run_in_component_init(f) |
| 110 | self.assertEqual(cc.description, 'hi people') |
| 111 | |
| 112 | def test_create_invalid_description(self): |
| 113 | def f(comp_self): |
| 114 | return comp_self._create_clock_class(description=23) |
| 115 | |
| 116 | self.assertRaisesInComponentInit(TypeError, f) |
| 117 | |
| 118 | def test_create_frequency(self): |
| 119 | def f(comp_self): |
| 120 | return comp_self._create_clock_class(frequency=987654321) |
| 121 | |
| 122 | cc = run_in_component_init(f) |
| 123 | self.assertEqual(cc.frequency, 987654321) |
| 124 | |
| 125 | def test_create_invalid_frequency(self): |
| 126 | def f(comp_self): |
| 127 | return comp_self._create_clock_class(frequency='lel') |
| 128 | |
| 129 | self.assertRaisesInComponentInit(TypeError, f) |
| 130 | |
| 131 | def test_create_precision(self): |
| 132 | def f(comp_self): |
| 133 | return comp_self._create_clock_class(precision=12) |
| 134 | |
| 135 | cc = run_in_component_init(f) |
| 136 | self.assertEqual(cc.precision, 12) |
| 137 | |
| 138 | def test_create_invalid_precision(self): |
| 139 | def f(comp_self): |
| 140 | return comp_self._create_clock_class(precision='lel') |
| 141 | |
| 142 | self.assertRaisesInComponentInit(TypeError, f) |
| 143 | |
| 144 | def test_create_offset(self): |
| 145 | def f(comp_self): |
| 146 | return comp_self._create_clock_class(offset=bt2.ClockClassOffset(12, 56)) |
| 147 | |
| 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(bt2.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) |
| 197 | |
| 198 | |
| 199 | class ClockSnapshotTestCase(unittest.TestCase): |
| 200 | def setUp(self): |
| 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, |
| 211 | packets_have_beginning_default_clock_snapshot=True, |
| 212 | packets_have_end_default_clock_snapshot=True) |
| 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): |
| 222 | def __init__(self, self_port_output): |
| 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 |
| 259 | |
| 260 | def tearDown(self): |
| 261 | del self._cc |
| 262 | del self._msg |
| 263 | |
| 264 | def test_create_default(self): |
| 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) |
| 268 | |
| 269 | def test_clock_class(self): |
| 270 | self.assertEqual( |
| 271 | self._msg.default_clock_snapshot.clock_class.addr, self._cc.addr) |
| 272 | |
| 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) |
| 278 | |
| 279 | def test_ns_from_origin_overflow(self): |
| 280 | with self.assertRaises(bt2.OverflowError): |
| 281 | self._msg_clock_overflow.default_clock_snapshot.ns_from_origin |
| 282 | |
| 283 | def test_eq_int(self): |
| 284 | self.assertEqual(self._msg.default_clock_snapshot, 123) |
| 285 | |
| 286 | def test_eq_invalid(self): |
| 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) |
| 292 | |
| 293 | self.assertTrue(self._msg.default_clock_snapshot >= 123) |
| 294 | self.assertFalse(self._msg.default_clock_snapshot >= 200) |
| 295 | |
| 296 | self.assertTrue(self._msg.default_clock_snapshot < 200) |
| 297 | self.assertFalse(self._msg.default_clock_snapshot < 100) |
| 298 | |
| 299 | self.assertTrue(self._msg.default_clock_snapshot <= 123) |
| 300 | self.assertFalse(self._msg.default_clock_snapshot <= 100) |