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