Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: GPL-2.0-only |
d2d857a8 MJ |
2 | # |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
d2d857a8 | 5 | |
9cf643d1 | 6 | import unittest |
f0a42b33 | 7 | import utils |
838a5a52 | 8 | from utils import run_in_component_init |
f0a42b33 FD |
9 | from bt2 import stream as bt2_stream |
10 | from bt2 import field as bt2_field | |
9cf643d1 PP |
11 | |
12 | ||
13 | class PacketTestCase(unittest.TestCase): | |
838a5a52 SM |
14 | @staticmethod |
15 | def _create_packet(with_pc): | |
16 | def create_tc_cc(comp_self): | |
17 | cc = comp_self._create_clock_class(frequency=1000, name='my_cc') | |
18 | tc = comp_self._create_trace_class() | |
19 | return cc, tc | |
9cf643d1 | 20 | |
838a5a52 | 21 | clock_class, tc = run_in_component_init(create_tc_cc) |
9cf643d1 PP |
22 | |
23 | # stream event context | |
838a5a52 | 24 | sec = tc.create_structure_field_class() |
45c51519 | 25 | sec += [ |
838a5a52 | 26 | ('cpu_id', tc.create_signed_integer_field_class(8)), |
fe4df857 | 27 | ('stuff', tc.create_double_precision_real_field_class()), |
45c51519 | 28 | ] |
9cf643d1 PP |
29 | |
30 | # packet context | |
838a5a52 | 31 | pc = None |
9cf643d1 | 32 | if with_pc: |
838a5a52 | 33 | pc = tc.create_structure_field_class() |
45c51519 | 34 | pc += [ |
838a5a52 | 35 | ('something', tc.create_signed_integer_field_class(8)), |
fe4df857 | 36 | ('something_else', tc.create_double_precision_real_field_class()), |
838a5a52 SM |
37 | ('events_discarded', tc.create_unsigned_integer_field_class(64)), |
38 | ('packet_seq_num', tc.create_unsigned_integer_field_class(64)), | |
45c51519 | 39 | ] |
9cf643d1 PP |
40 | |
41 | # stream class | |
cfbd7cf3 FD |
42 | sc = tc.create_stream_class( |
43 | default_clock_class=clock_class, | |
44 | event_common_context_field_class=sec, | |
45 | packet_context_field_class=pc, | |
46 | supports_packets=True, | |
47 | ) | |
9cf643d1 PP |
48 | |
49 | # event context | |
838a5a52 | 50 | ec = tc.create_structure_field_class() |
45c51519 | 51 | ec += [ |
838a5a52 SM |
52 | ('ant', tc.create_signed_integer_field_class(16)), |
53 | ('msg', tc.create_string_field_class()), | |
45c51519 | 54 | ] |
9cf643d1 PP |
55 | |
56 | # event payload | |
838a5a52 | 57 | ep = tc.create_structure_field_class() |
45c51519 | 58 | ep += [ |
838a5a52 SM |
59 | ('giraffe', tc.create_signed_integer_field_class(32)), |
60 | ('gnu', tc.create_signed_integer_field_class(8)), | |
61 | ('mosquito', tc.create_signed_integer_field_class(8)), | |
45c51519 | 62 | ] |
9cf643d1 PP |
63 | |
64 | # event class | |
838a5a52 SM |
65 | event_class = sc.create_event_class(name='ec', payload_field_class=ep) |
66 | event_class.common_context_field_class = ec | |
9cf643d1 | 67 | |
838a5a52 SM |
68 | # trace |
69 | trace = tc() | |
9cf643d1 PP |
70 | |
71 | # stream | |
838a5a52 | 72 | stream = trace.create_stream(sc) |
9cf643d1 PP |
73 | |
74 | # packet | |
838a5a52 | 75 | return stream.create_packet(), stream, pc |
9cf643d1 PP |
76 | |
77 | def test_attr_stream(self): | |
838a5a52 SM |
78 | packet, stream, _ = self._create_packet(with_pc=True) |
79 | self.assertEqual(packet.stream.addr, stream.addr) | |
f0a42b33 FD |
80 | self.assertIs(type(packet.stream), bt2_stream._Stream) |
81 | ||
82 | def test_const_attr_stream(self): | |
83 | packet = utils.get_const_packet_beginning_message().packet | |
84 | self.assertIs(type(packet.stream), bt2_stream._StreamConst) | |
9cf643d1 | 85 | |
838a5a52 SM |
86 | def test_context_field(self): |
87 | packet, stream, pc_fc = self._create_packet(with_pc=True) | |
d8e2073c | 88 | self.assertEqual(packet.context_field.cls.addr, pc_fc.addr) |
f0a42b33 FD |
89 | self.assertIs(type(packet.context_field), bt2_field._StructureField) |
90 | ||
91 | def test_const_context_field(self): | |
92 | packet = utils.get_const_packet_beginning_message().packet | |
93 | self.assertIs(type(packet.context_field), bt2_field._StructureFieldConst) | |
9cf643d1 PP |
94 | |
95 | def test_no_context_field(self): | |
838a5a52 | 96 | packet, _, _ = self._create_packet(with_pc=False) |
9cf643d1 | 97 | self.assertIsNone(packet.context_field) |
d14ddbba SM |
98 | |
99 | ||
100 | if __name__ == '__main__': | |
101 | unittest.main() |