bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / tests / bindings / python / bt2 / test_event.py
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 bt2
21 import utils
22 from utils import TestOutputPortMessageIterator
23
24 from bt2 import field as bt2_field
25 from bt2 import stream as bt2_stream
26 from bt2 import event_class as bt2_event_class
27 from bt2 import clock_snapshot as bt2_clock_snapshot
28
29
30 class EventTestCase(unittest.TestCase):
31 def _create_test_const_event_message(
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 ):
41 class MyIter(bt2._UserMessageIterator):
42 def __init__(self, self_output_port):
43 self._at = 0
44 self._msgs = [self._create_stream_beginning_message(test_obj.stream)]
45
46 if with_packet:
47 assert test_obj.packet
48 self._msgs.append(
49 self._create_packet_beginning_message(test_obj.packet)
50 )
51
52 default_clock_snapshot = 789 if with_clockclass else None
53
54 if with_packet:
55 assert test_obj.packet
56 ev_parent = test_obj.packet
57 else:
58 assert test_obj.stream
59 ev_parent = test_obj.stream
60
61 msg = self._create_event_message(
62 test_obj.event_class, ev_parent, default_clock_snapshot
63 )
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):
77 raise bt2.Stop
78
79 msg = self._msgs[self._at]
80 self._at += 1
81 return msg
82
83 class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
84 def __init__(self, params, obj):
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()
96 cc += [
97 ('cpu_id', tc.create_signed_integer_field_class(8)),
98 ('stuff', tc.create_real_field_class()),
99 ]
100
101 # packet context (stream-class-defined)
102 pc = None
103
104 if with_packet:
105 pc = tc.create_structure_field_class()
106 pc += [
107 ('something', tc.create_unsigned_integer_field_class(8)),
108 ('something_else', tc.create_real_field_class()),
109 ]
110
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 )
117
118 # specific context (event-class-defined)
119 sc = None
120 if with_sc:
121 sc = tc.create_structure_field_class()
122 sc += [
123 ('ant', tc.create_signed_integer_field_class(16)),
124 ('msg', tc.create_string_field_class()),
125 ]
126
127 # event payload
128 ep = None
129 if with_ep:
130 ep = tc.create_structure_field_class()
131 ep += [
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)),
135 ]
136
137 event_class = stream_class.create_event_class(
138 name='garou',
139 specific_context_field_class=sc,
140 payload_field_class=ep,
141 )
142
143 trace = tc()
144 stream = trace.create_stream(stream_class)
145
146 if with_packet:
147 packet = stream.create_packet()
148
149 if packet_fields_config is not None:
150 assert packet
151 packet_fields_config(packet)
152
153 if with_packet:
154 test_obj.packet = packet
155
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')
162 self._msg_iter = TestOutputPortMessageIterator(
163 self._graph, self._src_comp.output_ports['out']
164 )
165
166 for msg in self._msg_iter:
167 if type(msg) is bt2._EventMessageConst:
168 self._event_msg = msg
169 return msg
170
171 def test_const_attr_event_class(self):
172 msg = self._create_test_const_event_message()
173 self.assertEqual(msg.event.cls.addr, self.event_class.addr)
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)
179
180 def test_const_attr_name(self):
181 msg = self._create_test_const_event_message()
182 self.assertEqual(msg.event.name, self.event_class.name)
183
184 def test_const_attr_id(self):
185 msg = self._create_test_const_event_message()
186 self.assertEqual(msg.event.id, self.event_class.id)
187
188 def test_const_get_common_context_field(self):
189 def event_fields_config(event):
190 event.common_context_field['cpu_id'] = 1
191 event.common_context_field['stuff'] = 13.194
192
193 msg = self._create_test_const_event_message(
194 event_fields_config=event_fields_config, with_cc=True
195 )
196
197 self.assertEqual(msg.event.common_context_field['cpu_id'], 1)
198 self.assertEqual(msg.event.common_context_field['stuff'], 13.194)
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)
206
207 def test_const_no_common_context_field(self):
208 msg = self._create_test_const_event_message(with_cc=False)
209 self.assertIsNone(msg.event.common_context_field)
210
211 def test_const_get_specific_context_field(self):
212 def event_fields_config(event):
213 event.specific_context_field['ant'] = -1
214 event.specific_context_field['msg'] = 'hellooo'
215
216 msg = self._create_test_const_event_message(
217 event_fields_config=event_fields_config, with_sc=True
218 )
219
220 self.assertEqual(msg.event.specific_context_field['ant'], -1)
221 self.assertEqual(msg.event.specific_context_field['msg'], 'hellooo')
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)
229
230 def test_const_no_specific_context_field(self):
231 msg = self._create_test_const_event_message(with_sc=False)
232 self.assertIsNone(msg.event.specific_context_field)
233
234 def test_const_get_event_payload_field(self):
235 def event_fields_config(event):
236 event.payload_field['giraffe'] = 1
237 event.payload_field['gnu'] = 23
238 event.payload_field['mosquito'] = 42
239
240 msg = self._create_test_const_event_message(
241 event_fields_config=event_fields_config, with_ep=True
242 )
243
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)
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)
252
253 def test_const_no_payload_field(self):
254 msg = self._create_test_const_event_message(with_ep=False)
255 self.assertIsNone(msg.event.payload_field)
256
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
264 def test_clock_value(self):
265 msg = utils.get_event_message()
266 self.assertEqual(msg.default_clock_snapshot.value, 789)
267 self.assertIs(
268 type(msg.default_clock_snapshot), bt2_clock_snapshot._ClockSnapshotConst
269 )
270
271 def test_const_no_clock_value(self):
272 msg = self._create_test_const_event_message(with_clockclass=False)
273 with self.assertRaisesRegex(
274 ValueError, 'stream class has no default clock class'
275 ):
276 msg.default_clock_snapshot
277
278 def test_const_stream(self):
279 msg = self._create_test_const_event_message()
280 self.assertEqual(msg.event.stream.addr, self.stream.addr)
281 self.assertIs(type(msg.event.stream), bt2_stream._StreamConst)
282
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):
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
301 msg = self._create_test_const_event_message(
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 )
309 ev = msg.event
310
311 # Test event fields
312 self.assertEqual(ev['giraffe'], 1)
313 self.assertIs(type(ev['giraffe']), bt2_field._SignedIntegerFieldConst)
314 self.assertEqual(ev['gnu'], 23)
315 self.assertEqual(ev['mosquito'], 42)
316 self.assertEqual(ev['ant'], -1)
317 self.assertIs(type(ev['ant']), bt2_field._SignedIntegerFieldConst)
318 self.assertEqual(ev['msg'], 'hellooo')
319 self.assertEqual(ev['cpu_id'], 1)
320 self.assertIs(type(ev['cpu_id']), bt2_field._SignedIntegerFieldConst)
321 self.assertEqual(ev['stuff'], 13.194)
322
323 # Test packet fields
324 self.assertEqual(ev['something'], 154)
325 self.assertIs(type(ev['something']), bt2_field._UnsignedIntegerFieldConst)
326 self.assertEqual(ev['something_else'], 17.2)
327
328 with self.assertRaises(KeyError):
329 ev['yes']
330
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
343
344 if __name__ == "__main__":
345 unittest.main()
This page took 0.035783 seconds and 4 git commands to generate.