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
)
90 self
.assertEqual(len(cc
.user_attributes
), 0)
92 def test_create_name(self
):
94 return comp_self
._create
_clock
_class
(name
='the_clock')
96 cc
= run_in_component_init(f
)
97 self
.assertEqual(cc
.name
, 'the_clock')
99 def test_create_invalid_name(self
):
101 comp_self
._create
_clock
_class
(name
=23)
103 self
.assertRaisesInComponentInit(TypeError, f
)
105 def test_create_description(self
):
107 return comp_self
._create
_clock
_class
(description
='hi people')
109 cc
= run_in_component_init(f
)
110 self
.assertEqual(cc
.description
, 'hi people')
112 def test_create_invalid_description(self
):
114 return comp_self
._create
_clock
_class
(description
=23)
116 self
.assertRaisesInComponentInit(TypeError, f
)
118 def test_create_frequency(self
):
120 return comp_self
._create
_clock
_class
(frequency
=987654321)
122 cc
= run_in_component_init(f
)
123 self
.assertEqual(cc
.frequency
, 987654321)
125 def test_create_invalid_frequency(self
):
127 return comp_self
._create
_clock
_class
(frequency
='lel')
129 self
.assertRaisesInComponentInit(TypeError, f
)
131 def test_create_precision(self
):
133 return comp_self
._create
_clock
_class
(precision
=12)
135 cc
= run_in_component_init(f
)
136 self
.assertEqual(cc
.precision
, 12)
138 def test_create_invalid_precision(self
):
140 return comp_self
._create
_clock
_class
(precision
='lel')
142 self
.assertRaisesInComponentInit(TypeError, f
)
144 def test_create_offset(self
):
146 return comp_self
._create
_clock
_class
(offset
=bt2
.ClockClassOffset(12, 56))
148 cc
= run_in_component_init(f
)
149 self
.assertEqual(cc
.offset
, bt2
.ClockClassOffset(12, 56))
151 def test_create_invalid_offset(self
):
153 return comp_self
._create
_clock
_class
(offset
=object())
155 self
.assertRaisesInComponentInit(TypeError, f
)
157 def test_create_origin_is_unix_epoch(self
):
159 return comp_self
._create
_clock
_class
(origin_is_unix_epoch
=False)
161 cc
= run_in_component_init(f
)
162 self
.assertEqual(cc
.origin_is_unix_epoch
, False)
164 def test_create_invalid_origin_is_unix_epoch(self
):
166 return comp_self
._create
_clock
_class
(origin_is_unix_epoch
=23)
168 self
.assertRaisesInComponentInit(TypeError, f
)
170 def test_cycles_to_ns_from_origin(self
):
172 return comp_self
._create
_clock
_class
(
173 frequency
=10 ** 8, origin_is_unix_epoch
=True
176 cc
= run_in_component_init(f
)
177 self
.assertEqual(cc
.cycles_to_ns_from_origin(112), 1120)
179 def test_cycles_to_ns_from_origin_overflow(self
):
181 return comp_self
._create
_clock
_class
(frequency
=1000)
183 cc
= run_in_component_init(f
)
184 with self
.assertRaises(bt2
._OverflowError
):
185 cc
.cycles_to_ns_from_origin(2 ** 63)
187 def test_create_uuid(self
):
189 return comp_self
._create
_clock
_class
(
190 uuid
=uuid
.UUID('b43372c32ef0be28444dfc1c5cdafd33')
193 cc
= run_in_component_init(f
)
194 self
.assertEqual(cc
.uuid
, uuid
.UUID('b43372c32ef0be28444dfc1c5cdafd33'))
196 def test_create_invalid_uuid(self
):
198 return comp_self
._create
_clock
_class
(uuid
=23)
200 self
.assertRaisesInComponentInit(TypeError, f
)
202 def test_create_user_attributes(self
):
204 return comp_self
._create
_clock
_class
(user_attributes
={'salut': 23})
206 cc
= run_in_component_init(f
)
207 self
.assertEqual(cc
.user_attributes
, {'salut': 23})
209 def test_create_invalid_user_attributes(self
):
211 return comp_self
._create
_clock
_class
(user_attributes
=object())
213 self
.assertRaisesInComponentInit(TypeError, f
)
215 def test_create_invalid_user_attributes_value_type(self
):
217 return comp_self
._create
_clock
_class
(user_attributes
=23)
219 self
.assertRaisesInComponentInit(TypeError, f
)
222 class ClockSnapshotTestCase(unittest
.TestCase
):
225 cc
= comp_self
._create
_clock
_class
(
226 1000, 'my_cc', offset
=bt2
.ClockClassOffset(45, 354)
228 tc
= comp_self
._create
_trace
_class
()
232 _cc
, _tc
= run_in_component_init(f
)
234 _sc
= _tc
.create_stream_class(default_clock_class
=_cc
)
235 _ec
= _sc
.create_event_class(name
='salut')
236 _stream
= _trace
.create_stream(_sc
)
237 self
._stream
= _stream
241 class MyIter(bt2
._UserMessageIterator
):
242 def __init__(self
, self_port_output
):
247 notif
= self
._create
_stream
_beginning
_message
(_stream
)
249 notif
= self
._create
_event
_message
(_ec
, _stream
, 123)
251 notif
= self
._create
_event
_message
(_ec
, _stream
, 2 ** 63)
253 notif
= self
._create
_stream
_end
_message
(_stream
)
260 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
261 def __init__(self
, params
, obj
):
262 self
._add
_output
_port
('out')
264 self
._graph
= bt2
.Graph()
265 self
._src
_comp
= self
._graph
.add_component(MySrc
, 'my_source')
266 self
._msg
_iter
= TestOutputPortMessageIterator(
267 self
._graph
, self
._src
_comp
.output_ports
['out']
270 for i
, msg
in enumerate(self
._msg
_iter
):
274 self
._msg
_clock
_overflow
= msg
281 def test_create_default(self
):
283 self
._msg
.default_clock_snapshot
.clock_class
.addr
, self
._cc
.addr
285 self
.assertEqual(self
._msg
.default_clock_snapshot
.value
, 123)
287 def test_clock_class(self
):
289 self
._msg
.default_clock_snapshot
.clock_class
.addr
, self
._cc
.addr
292 def test_ns_from_origin(self
):
293 s_from_origin
= 45 + ((354 + 123) / 1000)
294 ns_from_origin
= int(s_from_origin
* 1e9
)
296 self
._msg
.default_clock_snapshot
.ns_from_origin
, ns_from_origin
299 def test_ns_from_origin_overflow(self
):
300 with self
.assertRaises(bt2
._OverflowError
):
301 self
._msg
_clock
_overflow
.default_clock_snapshot
.ns_from_origin
303 def test_eq_int(self
):
304 self
.assertEqual(self
._msg
.default_clock_snapshot
, 123)
306 def test_eq_invalid(self
):
307 self
.assertFalse(self
._msg
.default_clock_snapshot
== 23)
309 def test_comparison(self
):
310 self
.assertTrue(self
._msg
.default_clock_snapshot
> 100)
311 self
.assertFalse(self
._msg
.default_clock_snapshot
> 200)
313 self
.assertTrue(self
._msg
.default_clock_snapshot
>= 123)
314 self
.assertFalse(self
._msg
.default_clock_snapshot
>= 200)
316 self
.assertTrue(self
._msg
.default_clock_snapshot
< 200)
317 self
.assertFalse(self
._msg
.default_clock_snapshot
< 100)
319 self
.assertTrue(self
._msg
.default_clock_snapshot
<= 123)
320 self
.assertFalse(self
._msg
.default_clock_snapshot
<= 100)