bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / tests / bindings / python / bt2 / test_event.py
CommitLineData
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 19import unittest
9cf643d1 20import bt2
f0a42b33 21import utils
6c373cc9 22from utils import TestOutputPortMessageIterator
9cf643d1 23
f0a42b33
FD
24from bt2 import field as bt2_field
25from bt2 import stream as bt2_stream
26from bt2 import event_class as bt2_event_class
27from bt2 import clock_snapshot as bt2_clock_snapshot
28
9cf643d1
PP
29
30class EventTestCase(unittest.TestCase):
f0a42b33 31 def _create_test_const_event_message(
cfbd7cf3
FD
32 self,
33 packet_fields_config=None,
34 event_fields_config=None,
35 with_clockclass=False,
36 with_cc=False,
37 with_sc=False,
38 with_ep=False,
39 with_packet=False,
40 ):
2ae9f48c 41 class MyIter(bt2._UserMessageIterator):
c5f330cd 42 def __init__(self, self_output_port):
2ae9f48c 43 self._at = 0
cfbd7cf3 44 self._msgs = [self._create_stream_beginning_message(test_obj.stream)]
2ae9f48c 45
26fc5aed 46 if with_packet:
2ae9f48c 47 assert test_obj.packet
cfbd7cf3
FD
48 self._msgs.append(
49 self._create_packet_beginning_message(test_obj.packet)
50 )
26fc5aed
PP
51
52 default_clock_snapshot = 789 if with_clockclass else None
53
54 if with_packet:
2ae9f48c 55 assert test_obj.packet
26fc5aed
PP
56 ev_parent = test_obj.packet
57 else:
58 assert test_obj.stream
59 ev_parent = test_obj.stream
60
cfbd7cf3
FD
61 msg = self._create_event_message(
62 test_obj.event_class, ev_parent, default_clock_snapshot
63 )
26fc5aed
PP
64
65 if event_fields_config is not None:
66 event_fields_config(msg.event)
67
68 self._msgs.append(msg)
69
70 if with_packet:
71 self._msgs.append(self._create_packet_end_message(test_obj.packet))
72
73 self._msgs.append(self._create_stream_end_message(test_obj.stream))
74
75 def __next__(self):
76 if self._at == len(self._msgs):
2ae9f48c
SM
77 raise bt2.Stop
78
26fc5aed 79 msg = self._msgs[self._at]
2ae9f48c
SM
80 self._at += 1
81 return msg
82
83 class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
66964f3f 84 def __init__(self, params, obj):
2ae9f48c
SM
85 self._add_output_port('out')
86 tc = self._create_trace_class()
87
88 clock_class = None
89 if with_clockclass:
90 clock_class = self._create_clock_class(frequency=1000)
91
92 # event common context (stream-class-defined)
93 cc = None
94 if with_cc:
95 cc = tc.create_structure_field_class()
45c51519 96 cc += [
2ae9f48c
SM
97 ('cpu_id', tc.create_signed_integer_field_class(8)),
98 ('stuff', tc.create_real_field_class()),
45c51519 99 ]
2ae9f48c
SM
100
101 # packet context (stream-class-defined)
26fc5aed
PP
102 pc = None
103
104 if with_packet:
105 pc = tc.create_structure_field_class()
45c51519 106 pc += [
26fc5aed
PP
107 ('something', tc.create_unsigned_integer_field_class(8)),
108 ('something_else', tc.create_real_field_class()),
45c51519 109 ]
2ae9f48c 110
cfbd7cf3
FD
111 stream_class = tc.create_stream_class(
112 default_clock_class=clock_class,
113 event_common_context_field_class=cc,
114 packet_context_field_class=pc,
115 supports_packets=with_packet,
116 )
2ae9f48c
SM
117
118 # specific context (event-class-defined)
119 sc = None
120 if with_sc:
121 sc = tc.create_structure_field_class()
45c51519 122 sc += [
2ae9f48c
SM
123 ('ant', tc.create_signed_integer_field_class(16)),
124 ('msg', tc.create_string_field_class()),
45c51519 125 ]
2ae9f48c
SM
126
127 # event payload
128 ep = None
129 if with_ep:
130 ep = tc.create_structure_field_class()
45c51519 131 ep += [
2ae9f48c
SM
132 ('giraffe', tc.create_signed_integer_field_class(32)),
133 ('gnu', tc.create_signed_integer_field_class(8)),
134 ('mosquito', tc.create_signed_integer_field_class(8)),
45c51519 135 ]
2ae9f48c 136
cfbd7cf3
FD
137 event_class = stream_class.create_event_class(
138 name='garou',
139 specific_context_field_class=sc,
140 payload_field_class=ep,
141 )
2ae9f48c
SM
142
143 trace = tc()
144 stream = trace.create_stream(stream_class)
26fc5aed
PP
145
146 if with_packet:
147 packet = stream.create_packet()
2ae9f48c
SM
148
149 if packet_fields_config is not None:
26fc5aed 150 assert packet
2ae9f48c
SM
151 packet_fields_config(packet)
152
26fc5aed
PP
153 if with_packet:
154 test_obj.packet = packet
155
2ae9f48c
SM
156 test_obj.stream = stream
157 test_obj.event_class = event_class
158
159 test_obj = self
160 self._graph = bt2.Graph()
161 self._src_comp = self._graph.add_component(MySrc, 'my_source')
6c373cc9
PP
162 self._msg_iter = TestOutputPortMessageIterator(
163 self._graph, self._src_comp.output_ports['out']
cfbd7cf3 164 )
2ae9f48c 165
26fc5aed 166 for msg in self._msg_iter:
f0a42b33
FD
167 if type(msg) is bt2._EventMessageConst:
168 self._event_msg = msg
2ae9f48c 169 return msg
9cf643d1 170
f0a42b33
FD
171 def test_const_attr_event_class(self):
172 msg = self._create_test_const_event_message()
e8ac1aae 173 self.assertEqual(msg.event.cls.addr, self.event_class.addr)
f0a42b33
FD
174 self.assertIs(type(msg.event.cls), bt2_event_class._EventClassConst)
175
176 def test_attr_event_class(self):
177 msg = utils.get_event_message()
178 self.assertIs(type(msg.event.cls), bt2_event_class._EventClass)
9cf643d1 179
f0a42b33
FD
180 def test_const_attr_name(self):
181 msg = self._create_test_const_event_message()
2ae9f48c 182 self.assertEqual(msg.event.name, self.event_class.name)
9cf643d1 183
f0a42b33
FD
184 def test_const_attr_id(self):
185 msg = self._create_test_const_event_message()
2ae9f48c
SM
186 self.assertEqual(msg.event.id, self.event_class.id)
187
f0a42b33 188 def test_const_get_common_context_field(self):
2ae9f48c
SM
189 def event_fields_config(event):
190 event.common_context_field['cpu_id'] = 1
191 event.common_context_field['stuff'] = 13.194
192
f0a42b33 193 msg = self._create_test_const_event_message(
cfbd7cf3
FD
194 event_fields_config=event_fields_config, with_cc=True
195 )
2ae9f48c
SM
196
197 self.assertEqual(msg.event.common_context_field['cpu_id'], 1)
198 self.assertEqual(msg.event.common_context_field['stuff'], 13.194)
f0a42b33
FD
199 self.assertIs(
200 type(msg.event.common_context_field), bt2_field._StructureFieldConst
201 )
202
203 def test_attr_common_context_field(self):
204 msg = utils.get_event_message()
205 self.assertIs(type(msg.event.common_context_field), bt2_field._StructureField)
2ae9f48c 206
f0a42b33
FD
207 def test_const_no_common_context_field(self):
208 msg = self._create_test_const_event_message(with_cc=False)
2ae9f48c
SM
209 self.assertIsNone(msg.event.common_context_field)
210
f0a42b33 211 def test_const_get_specific_context_field(self):
2ae9f48c
SM
212 def event_fields_config(event):
213 event.specific_context_field['ant'] = -1
214 event.specific_context_field['msg'] = 'hellooo'
215
f0a42b33 216 msg = self._create_test_const_event_message(
cfbd7cf3
FD
217 event_fields_config=event_fields_config, with_sc=True
218 )
2ae9f48c
SM
219
220 self.assertEqual(msg.event.specific_context_field['ant'], -1)
221 self.assertEqual(msg.event.specific_context_field['msg'], 'hellooo')
f0a42b33
FD
222 self.assertIs(
223 type(msg.event.specific_context_field), bt2_field._StructureFieldConst
224 )
225
226 def test_attr_specific_context_field(self):
227 msg = utils.get_event_message()
228 self.assertIs(type(msg.event.specific_context_field), bt2_field._StructureField)
2ae9f48c 229
f0a42b33
FD
230 def test_const_no_specific_context_field(self):
231 msg = self._create_test_const_event_message(with_sc=False)
2ae9f48c 232 self.assertIsNone(msg.event.specific_context_field)
9cf643d1 233
f0a42b33 234 def test_const_get_event_payload_field(self):
2ae9f48c
SM
235 def event_fields_config(event):
236 event.payload_field['giraffe'] = 1
237 event.payload_field['gnu'] = 23
238 event.payload_field['mosquito'] = 42
9cf643d1 239
f0a42b33 240 msg = self._create_test_const_event_message(
cfbd7cf3
FD
241 event_fields_config=event_fields_config, with_ep=True
242 )
9cf643d1 243
2ae9f48c
SM
244 self.assertEqual(msg.event.payload_field['giraffe'], 1)
245 self.assertEqual(msg.event.payload_field['gnu'], 23)
246 self.assertEqual(msg.event.payload_field['mosquito'], 42)
f0a42b33
FD
247 self.assertIs(type(msg.event.payload_field), bt2_field._StructureFieldConst)
248
249 def test_attr_payload_field(self):
250 msg = utils.get_event_message()
251 self.assertIs(type(msg.event.payload_field), bt2_field._StructureField)
9cf643d1 252
f0a42b33
FD
253 def test_const_no_payload_field(self):
254 msg = self._create_test_const_event_message(with_ep=False)
2ae9f48c 255 self.assertIsNone(msg.event.payload_field)
9cf643d1 256
f0a42b33
FD
257 def test_const_clock_value(self):
258 msg = self._create_test_const_event_message(with_clockclass=True)
259 self.assertEqual(msg.default_clock_snapshot.value, 789)
260 self.assertIs(
261 type(msg.default_clock_snapshot), bt2_clock_snapshot._ClockSnapshotConst
262 )
263
2ae9f48c 264 def test_clock_value(self):
f0a42b33 265 msg = utils.get_event_message()
2ae9f48c 266 self.assertEqual(msg.default_clock_snapshot.value, 789)
f0a42b33
FD
267 self.assertIs(
268 type(msg.default_clock_snapshot), bt2_clock_snapshot._ClockSnapshotConst
269 )
2ae9f48c 270
f0a42b33
FD
271 def test_const_no_clock_value(self):
272 msg = self._create_test_const_event_message(with_clockclass=False)
1153eccb
SM
273 with self.assertRaisesRegex(
274 ValueError, 'stream class has no default clock class'
275 ):
9ec609ec 276 msg.default_clock_snapshot
2ae9f48c 277
f0a42b33
FD
278 def test_const_stream(self):
279 msg = self._create_test_const_event_message()
2ae9f48c 280 self.assertEqual(msg.event.stream.addr, self.stream.addr)
f0a42b33 281 self.assertIs(type(msg.event.stream), bt2_stream._StreamConst)
2ae9f48c 282
f0a42b33
FD
283 def test_stream(self):
284 msg = utils.get_event_message()
285 self.assertIs(type(msg.event.stream), bt2_stream._Stream)
286
287 def test_const_getitem(self):
2ae9f48c
SM
288 def event_fields_config(event):
289 event.payload_field['giraffe'] = 1
290 event.payload_field['gnu'] = 23
291 event.payload_field['mosquito'] = 42
292 event.specific_context_field['ant'] = -1
293 event.specific_context_field['msg'] = 'hellooo'
294 event.common_context_field['cpu_id'] = 1
295 event.common_context_field['stuff'] = 13.194
296
297 def packet_fields_config(packet):
298 packet.context_field['something'] = 154
299 packet.context_field['something_else'] = 17.2
300
f0a42b33 301 msg = self._create_test_const_event_message(
cfbd7cf3
FD
302 packet_fields_config=packet_fields_config,
303 event_fields_config=event_fields_config,
304 with_cc=True,
305 with_sc=True,
306 with_ep=True,
307 with_packet=True,
308 )
2ae9f48c
SM
309 ev = msg.event
310
311 # Test event fields
9cf643d1 312 self.assertEqual(ev['giraffe'], 1)
f0a42b33 313 self.assertIs(type(ev['giraffe']), bt2_field._SignedIntegerFieldConst)
2ae9f48c
SM
314 self.assertEqual(ev['gnu'], 23)
315 self.assertEqual(ev['mosquito'], 42)
9cf643d1 316 self.assertEqual(ev['ant'], -1)
f0a42b33 317 self.assertIs(type(ev['ant']), bt2_field._SignedIntegerFieldConst)
2ae9f48c 318 self.assertEqual(ev['msg'], 'hellooo')
9cf643d1 319 self.assertEqual(ev['cpu_id'], 1)
f0a42b33 320 self.assertIs(type(ev['cpu_id']), bt2_field._SignedIntegerFieldConst)
2ae9f48c
SM
321 self.assertEqual(ev['stuff'], 13.194)
322
323 # Test packet fields
9cf643d1 324 self.assertEqual(ev['something'], 154)
f0a42b33 325 self.assertIs(type(ev['something']), bt2_field._UnsignedIntegerFieldConst)
2ae9f48c 326 self.assertEqual(ev['something_else'], 17.2)
9cf643d1
PP
327
328 with self.assertRaises(KeyError):
329 ev['yes']
330
f0a42b33
FD
331 def test_getitem(self):
332 msg = utils.get_event_message()
333 ev = msg.event
334 self.assertEqual(ev['giraffe'], 1)
335 self.assertIs(type(ev['giraffe']), bt2_field._SignedIntegerField)
336 self.assertEqual(ev['ant'], -1)
337 self.assertIs(type(ev['ant']), bt2_field._SignedIntegerField)
338 self.assertEqual(ev['cpu_id'], 1)
339 self.assertIs(type(ev['cpu_id']), bt2_field._SignedIntegerField)
340 self.assertEqual(ev['something'], 154)
341 self.assertIs(type(ev['something']), bt2_field._UnsignedIntegerField)
342
2ae9f48c
SM
343
344if __name__ == "__main__":
345 unittest.main()
This page took 0.062185 seconds and 4 git commands to generate.