2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
22 from utils
import run_in_component_init
, TestOutputPortMessageIterator
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)
31 def test_create(self
):
32 cco
= bt2
.ClockClassOffset(23, 4871232)
33 self
.assertEqual(cco
.seconds
, 23)
34 self
.assertEqual(cco
.cycles
, 4871232)
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)
41 def test_create_invalid_seconds(self
):
42 with self
.assertRaises(TypeError):
43 bt2
.ClockClassOffset('hello', 4871232)
45 def test_create_invalid_cycles(self
):
46 with self
.assertRaises(TypeError):
47 bt2
.ClockClassOffset(23, 'hello')
50 cco1
= bt2
.ClockClassOffset(23, 42)
51 cco2
= bt2
.ClockClassOffset(23, 42)
52 self
.assertEqual(cco1
, cco2
)
54 def test_ne_seconds(self
):
55 cco1
= bt2
.ClockClassOffset(23, 42)
56 cco2
= bt2
.ClockClassOffset(24, 42)
57 self
.assertNotEqual(cco1
, cco2
)
59 def test_ne_cycles(self
):
60 cco1
= bt2
.ClockClassOffset(23, 42)
61 cco2
= bt2
.ClockClassOffset(23, 43)
62 self
.assertNotEqual(cco1
, cco2
)
64 def test_eq_invalid(self
):
65 self
.assertFalse(bt2
.ClockClassOffset() == 23)
68 class ClockClassTestCase(unittest
.TestCase
):
69 def assertRaisesInComponentInit(self
, expected_exc_type
, user_code
):
73 except Exception as exc
:
76 exc_type
= run_in_component_init(f
)
77 self
.assertIsNotNone(exc_type
)
78 self
.assertEqual(exc_type
, expected_exc_type
)
80 def test_create_default(self
):
81 cc
= run_in_component_init(lambda comp_self
: comp_self
._create
_clock
_class
())
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
)
91 def test_create_name(self
):
93 return comp_self
._create
_clock
_class
(name
='the_clock')
95 cc
= run_in_component_init(f
)
96 self
.assertEqual(cc
.name
, 'the_clock')
98 def test_create_invalid_name(self
):
100 comp_self
._create
_clock
_class
(name
=23)
102 self
.assertRaisesInComponentInit(TypeError, f
)
104 def test_create_description(self
):
106 return comp_self
._create
_clock
_class
(description
='hi people')
108 cc
= run_in_component_init(f
)
109 self
.assertEqual(cc
.description
, 'hi people')
111 def test_create_invalid_description(self
):
113 return comp_self
._create
_clock
_class
(description
=23)
115 self
.assertRaisesInComponentInit(TypeError, f
)
117 def test_create_frequency(self
):
119 return comp_self
._create
_clock
_class
(frequency
=987654321)
121 cc
= run_in_component_init(f
)
122 self
.assertEqual(cc
.frequency
, 987654321)
124 def test_create_invalid_frequency(self
):
126 return comp_self
._create
_clock
_class
(frequency
='lel')
128 self
.assertRaisesInComponentInit(TypeError, f
)
130 def test_create_precision(self
):
132 return comp_self
._create
_clock
_class
(precision
=12)
134 cc
= run_in_component_init(f
)
135 self
.assertEqual(cc
.precision
, 12)
137 def test_create_invalid_precision(self
):
139 return comp_self
._create
_clock
_class
(precision
='lel')
141 self
.assertRaisesInComponentInit(TypeError, f
)
143 def test_create_offset(self
):
145 return comp_self
._create
_clock
_class
(offset
=bt2
.ClockClassOffset(12, 56))
147 cc
= run_in_component_init(f
)
148 self
.assertEqual(cc
.offset
, bt2
.ClockClassOffset(12, 56))
150 def test_create_invalid_offset(self
):
152 return comp_self
._create
_clock
_class
(offset
=object())
154 self
.assertRaisesInComponentInit(TypeError, f
)
156 def test_create_origin_is_unix_epoch(self
):
158 return comp_self
._create
_clock
_class
(origin_is_unix_epoch
=False)
160 cc
= run_in_component_init(f
)
161 self
.assertEqual(cc
.origin_is_unix_epoch
, False)
163 def test_create_invalid_origin_is_unix_epoch(self
):
165 return comp_self
._create
_clock
_class
(origin_is_unix_epoch
=23)
167 self
.assertRaisesInComponentInit(TypeError, f
)
169 def test_cycles_to_ns_from_origin(self
):
171 return comp_self
._create
_clock
_class
(
172 frequency
=10 ** 8, origin_is_unix_epoch
=True
175 cc
= run_in_component_init(f
)
176 self
.assertEqual(cc
.cycles_to_ns_from_origin(112), 1120)
178 def test_cycles_to_ns_from_origin_overflow(self
):
180 return comp_self
._create
_clock
_class
(frequency
=1000)
182 cc
= run_in_component_init(f
)
183 with self
.assertRaises(bt2
._OverflowError
):
184 cc
.cycles_to_ns_from_origin(2 ** 63)
186 def test_create_uuid(self
):
188 return comp_self
._create
_clock
_class
(
189 uuid
=uuid
.UUID('b43372c32ef0be28444dfc1c5cdafd33')
192 cc
= run_in_component_init(f
)
193 self
.assertEqual(cc
.uuid
, uuid
.UUID('b43372c32ef0be28444dfc1c5cdafd33'))
195 def test_create_invalid_uuid(self
):
197 return comp_self
._create
_clock
_class
(uuid
=23)
199 self
.assertRaisesInComponentInit(TypeError, f
)
202 class ClockSnapshotTestCase(unittest
.TestCase
):
205 cc
= comp_self
._create
_clock
_class
(
206 1000, 'my_cc', offset
=bt2
.ClockClassOffset(45, 354)
208 tc
= comp_self
._create
_trace
_class
()
212 _cc
, _tc
= run_in_component_init(f
)
214 _sc
= _tc
.create_stream_class(default_clock_class
=_cc
)
215 _ec
= _sc
.create_event_class(name
='salut')
216 _stream
= _trace
.create_stream(_sc
)
217 self
._stream
= _stream
221 class MyIter(bt2
._UserMessageIterator
):
222 def __init__(self
, self_port_output
):
227 notif
= self
._create
_stream
_beginning
_message
(_stream
)
229 notif
= self
._create
_event
_message
(_ec
, _stream
, 123)
231 notif
= self
._create
_event
_message
(_ec
, _stream
, 2 ** 63)
233 notif
= self
._create
_stream
_end
_message
(_stream
)
240 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
241 def __init__(self
, params
, obj
):
242 self
._add
_output
_port
('out')
244 self
._graph
= bt2
.Graph()
245 self
._src
_comp
= self
._graph
.add_component(MySrc
, 'my_source')
246 self
._msg
_iter
= TestOutputPortMessageIterator(
247 self
._graph
, self
._src
_comp
.output_ports
['out']
250 for i
, msg
in enumerate(self
._msg
_iter
):
254 self
._msg
_clock
_overflow
= msg
261 def test_create_default(self
):
263 self
._msg
.default_clock_snapshot
.clock_class
.addr
, self
._cc
.addr
265 self
.assertEqual(self
._msg
.default_clock_snapshot
.value
, 123)
267 def test_clock_class(self
):
269 self
._msg
.default_clock_snapshot
.clock_class
.addr
, self
._cc
.addr
272 def test_ns_from_origin(self
):
273 s_from_origin
= 45 + ((354 + 123) / 1000)
274 ns_from_origin
= int(s_from_origin
* 1e9
)
276 self
._msg
.default_clock_snapshot
.ns_from_origin
, ns_from_origin
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
283 def test_eq_int(self
):
284 self
.assertEqual(self
._msg
.default_clock_snapshot
, 123)
286 def test_eq_invalid(self
):
287 self
.assertFalse(self
._msg
.default_clock_snapshot
== 23)
289 def test_comparison(self
):
290 self
.assertTrue(self
._msg
.default_clock_snapshot
> 100)
291 self
.assertFalse(self
._msg
.default_clock_snapshot
> 200)
293 self
.assertTrue(self
._msg
.default_clock_snapshot
>= 123)
294 self
.assertFalse(self
._msg
.default_clock_snapshot
>= 200)
296 self
.assertTrue(self
._msg
.default_clock_snapshot
< 200)
297 self
.assertFalse(self
._msg
.default_clock_snapshot
< 100)
299 self
.assertTrue(self
._msg
.default_clock_snapshot
<= 123)
300 self
.assertFalse(self
._msg
.default_clock_snapshot
<= 100)