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.
19 from collections
import OrderedDict
24 class EventTestCase(unittest
.TestCase
):
25 def _create_test_event_message(
27 packet_fields_config
=None,
28 event_fields_config
=None,
29 with_clockclass
=False,
35 class MyIter(bt2
._UserMessageIterator
):
36 def __init__(self
, self_output_port
):
38 self
._msgs
= [self
._create
_stream
_beginning
_message
(test_obj
.stream
)]
41 assert test_obj
.packet
43 self
._create
_packet
_beginning
_message
(test_obj
.packet
)
46 default_clock_snapshot
= 789 if with_clockclass
else None
49 assert test_obj
.packet
50 ev_parent
= test_obj
.packet
52 assert test_obj
.stream
53 ev_parent
= test_obj
.stream
55 msg
= self
._create
_event
_message
(
56 test_obj
.event_class
, ev_parent
, default_clock_snapshot
59 if event_fields_config
is not None:
60 event_fields_config(msg
.event
)
62 self
._msgs
.append(msg
)
65 self
._msgs
.append(self
._create
_packet
_end
_message
(test_obj
.packet
))
67 self
._msgs
.append(self
._create
_stream
_end
_message
(test_obj
.stream
))
70 if self
._at
== len(self
._msgs
):
73 msg
= self
._msgs
[self
._at
]
77 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
78 def __init__(self
, params
):
79 self
._add
_output
_port
('out')
80 tc
= self
._create
_trace
_class
()
84 clock_class
= self
._create
_clock
_class
(frequency
=1000)
86 # event common context (stream-class-defined)
89 cc
= tc
.create_structure_field_class()
91 ('cpu_id', tc
.create_signed_integer_field_class(8)),
92 ('stuff', tc
.create_real_field_class()),
95 # packet context (stream-class-defined)
99 pc
= tc
.create_structure_field_class()
101 ('something', tc
.create_unsigned_integer_field_class(8)),
102 ('something_else', tc
.create_real_field_class()),
105 stream_class
= tc
.create_stream_class(
106 default_clock_class
=clock_class
,
107 event_common_context_field_class
=cc
,
108 packet_context_field_class
=pc
,
109 supports_packets
=with_packet
,
112 # specific context (event-class-defined)
115 sc
= tc
.create_structure_field_class()
117 ('ant', tc
.create_signed_integer_field_class(16)),
118 ('msg', tc
.create_string_field_class()),
124 ep
= tc
.create_structure_field_class()
126 ('giraffe', tc
.create_signed_integer_field_class(32)),
127 ('gnu', tc
.create_signed_integer_field_class(8)),
128 ('mosquito', tc
.create_signed_integer_field_class(8)),
131 event_class
= stream_class
.create_event_class(
133 specific_context_field_class
=sc
,
134 payload_field_class
=ep
,
138 stream
= trace
.create_stream(stream_class
)
141 packet
= stream
.create_packet()
143 if packet_fields_config
is not None:
145 packet_fields_config(packet
)
148 test_obj
.packet
= packet
150 test_obj
.stream
= stream
151 test_obj
.event_class
= event_class
154 self
._graph
= bt2
.Graph()
155 self
._src
_comp
= self
._graph
.add_component(MySrc
, 'my_source')
156 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(
157 self
._src
_comp
.output_ports
['out']
160 for msg
in self
._msg
_iter
:
161 if type(msg
) is bt2
._EventMessage
:
164 def test_attr_event_class(self
):
165 msg
= self
._create
_test
_event
_message
()
166 self
.assertEqual(msg
.event
.cls
.addr
, self
.event_class
.addr
)
168 def test_attr_name(self
):
169 msg
= self
._create
_test
_event
_message
()
170 self
.assertEqual(msg
.event
.name
, self
.event_class
.name
)
172 def test_attr_id(self
):
173 msg
= self
._create
_test
_event
_message
()
174 self
.assertEqual(msg
.event
.id, self
.event_class
.id)
176 def test_get_common_context_field(self
):
177 def event_fields_config(event
):
178 event
.common_context_field
['cpu_id'] = 1
179 event
.common_context_field
['stuff'] = 13.194
181 msg
= self
._create
_test
_event
_message
(
182 event_fields_config
=event_fields_config
, with_cc
=True
185 self
.assertEqual(msg
.event
.common_context_field
['cpu_id'], 1)
186 self
.assertEqual(msg
.event
.common_context_field
['stuff'], 13.194)
188 def test_no_common_context_field(self
):
189 msg
= self
._create
_test
_event
_message
(with_cc
=False)
190 self
.assertIsNone(msg
.event
.common_context_field
)
192 def test_get_specific_context_field(self
):
193 def event_fields_config(event
):
194 event
.specific_context_field
['ant'] = -1
195 event
.specific_context_field
['msg'] = 'hellooo'
197 msg
= self
._create
_test
_event
_message
(
198 event_fields_config
=event_fields_config
, with_sc
=True
201 self
.assertEqual(msg
.event
.specific_context_field
['ant'], -1)
202 self
.assertEqual(msg
.event
.specific_context_field
['msg'], 'hellooo')
204 def test_no_specific_context_field(self
):
205 msg
= self
._create
_test
_event
_message
(with_sc
=False)
206 self
.assertIsNone(msg
.event
.specific_context_field
)
208 def test_get_event_payload_field(self
):
209 def event_fields_config(event
):
210 event
.payload_field
['giraffe'] = 1
211 event
.payload_field
['gnu'] = 23
212 event
.payload_field
['mosquito'] = 42
214 msg
= self
._create
_test
_event
_message
(
215 event_fields_config
=event_fields_config
, with_ep
=True
218 self
.assertEqual(msg
.event
.payload_field
['giraffe'], 1)
219 self
.assertEqual(msg
.event
.payload_field
['gnu'], 23)
220 self
.assertEqual(msg
.event
.payload_field
['mosquito'], 42)
222 def test_no_payload_field(self
):
223 msg
= self
._create
_test
_event
_message
(with_ep
=False)
224 self
.assertIsNone(msg
.event
.payload_field
)
226 def test_clock_value(self
):
227 msg
= self
._create
_test
_event
_message
(with_clockclass
=True)
228 self
.assertEqual(msg
.default_clock_snapshot
.value
, 789)
230 def test_no_clock_value(self
):
231 msg
= self
._create
_test
_event
_message
(with_clockclass
=False)
232 with self
.assertRaisesRegex(
233 ValueError, 'stream class has no default clock class'
235 msg
.default_clock_snapshot
237 def test_stream(self
):
238 msg
= self
._create
_test
_event
_message
()
239 self
.assertEqual(msg
.event
.stream
.addr
, self
.stream
.addr
)
241 def test_getitem(self
):
242 def event_fields_config(event
):
243 event
.payload_field
['giraffe'] = 1
244 event
.payload_field
['gnu'] = 23
245 event
.payload_field
['mosquito'] = 42
246 event
.specific_context_field
['ant'] = -1
247 event
.specific_context_field
['msg'] = 'hellooo'
248 event
.common_context_field
['cpu_id'] = 1
249 event
.common_context_field
['stuff'] = 13.194
251 def packet_fields_config(packet
):
252 packet
.context_field
['something'] = 154
253 packet
.context_field
['something_else'] = 17.2
255 msg
= self
._create
_test
_event
_message
(
256 packet_fields_config
=packet_fields_config
,
257 event_fields_config
=event_fields_config
,
266 self
.assertEqual(ev
['giraffe'], 1)
267 self
.assertEqual(ev
['gnu'], 23)
268 self
.assertEqual(ev
['mosquito'], 42)
269 self
.assertEqual(ev
['ant'], -1)
270 self
.assertEqual(ev
['msg'], 'hellooo')
271 self
.assertEqual(ev
['cpu_id'], 1)
272 self
.assertEqual(ev
['stuff'], 13.194)
275 self
.assertEqual(ev
['something'], 154)
276 self
.assertEqual(ev
['something_else'], 17.2)
278 with self
.assertRaises(KeyError):
282 if __name__
== "__main__":