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