Commit | Line | Data |
---|---|---|
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 | 19 | import unittest |
838a5a52 | 20 | from utils import run_in_component_init |
9cf643d1 PP |
21 | |
22 | ||
23 | class PacketTestCase(unittest.TestCase): | |
838a5a52 SM |
24 | @staticmethod |
25 | def _create_packet(with_pc): | |
26 | def create_tc_cc(comp_self): | |
27 | cc = comp_self._create_clock_class(frequency=1000, name='my_cc') | |
28 | tc = comp_self._create_trace_class() | |
29 | return cc, tc | |
9cf643d1 | 30 | |
838a5a52 | 31 | clock_class, tc = run_in_component_init(create_tc_cc) |
9cf643d1 PP |
32 | |
33 | # stream event context | |
838a5a52 | 34 | sec = tc.create_structure_field_class() |
45c51519 | 35 | sec += [ |
838a5a52 SM |
36 | ('cpu_id', tc.create_signed_integer_field_class(8)), |
37 | ('stuff', tc.create_real_field_class()), | |
45c51519 | 38 | ] |
9cf643d1 PP |
39 | |
40 | # packet context | |
838a5a52 | 41 | pc = None |
9cf643d1 | 42 | if with_pc: |
838a5a52 | 43 | pc = tc.create_structure_field_class() |
45c51519 | 44 | pc += [ |
838a5a52 SM |
45 | ('something', tc.create_signed_integer_field_class(8)), |
46 | ('something_else', tc.create_real_field_class()), | |
47 | ('events_discarded', tc.create_unsigned_integer_field_class(64)), | |
48 | ('packet_seq_num', tc.create_unsigned_integer_field_class(64)), | |
45c51519 | 49 | ] |
9cf643d1 PP |
50 | |
51 | # stream class | |
cfbd7cf3 FD |
52 | sc = tc.create_stream_class( |
53 | default_clock_class=clock_class, | |
54 | event_common_context_field_class=sec, | |
55 | packet_context_field_class=pc, | |
56 | supports_packets=True, | |
57 | ) | |
9cf643d1 PP |
58 | |
59 | # event context | |
838a5a52 | 60 | ec = tc.create_structure_field_class() |
45c51519 | 61 | ec += [ |
838a5a52 SM |
62 | ('ant', tc.create_signed_integer_field_class(16)), |
63 | ('msg', tc.create_string_field_class()), | |
45c51519 | 64 | ] |
9cf643d1 PP |
65 | |
66 | # event payload | |
838a5a52 | 67 | ep = tc.create_structure_field_class() |
45c51519 | 68 | ep += [ |
838a5a52 SM |
69 | ('giraffe', tc.create_signed_integer_field_class(32)), |
70 | ('gnu', tc.create_signed_integer_field_class(8)), | |
71 | ('mosquito', tc.create_signed_integer_field_class(8)), | |
45c51519 | 72 | ] |
9cf643d1 PP |
73 | |
74 | # event class | |
838a5a52 SM |
75 | event_class = sc.create_event_class(name='ec', payload_field_class=ep) |
76 | event_class.common_context_field_class = ec | |
9cf643d1 | 77 | |
838a5a52 SM |
78 | # trace |
79 | trace = tc() | |
9cf643d1 PP |
80 | |
81 | # stream | |
838a5a52 | 82 | stream = trace.create_stream(sc) |
9cf643d1 PP |
83 | |
84 | # packet | |
838a5a52 | 85 | return stream.create_packet(), stream, pc |
9cf643d1 PP |
86 | |
87 | def test_attr_stream(self): | |
838a5a52 SM |
88 | packet, stream, _ = self._create_packet(with_pc=True) |
89 | self.assertEqual(packet.stream.addr, stream.addr) | |
9cf643d1 | 90 | |
838a5a52 SM |
91 | def test_context_field(self): |
92 | packet, stream, pc_fc = self._create_packet(with_pc=True) | |
93 | self.assertEqual(packet.context_field.field_class.addr, pc_fc.addr) | |
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) |