Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: GPL-2.0-only |
d2d857a8 MJ |
2 | # |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
d2d857a8 | 5 | |
fbbe9302 | 6 | import unittest |
f0a42b33 FD |
7 | from utils import ( |
8 | run_in_component_init, | |
9 | get_default_trace_class, | |
10 | get_const_stream_beginning_message, | |
11 | ) | |
12 | from bt2 import stream_class as bt2_stream_class | |
13 | from bt2 import trace_class as bt2_trace_class | |
1114a7d5 | 14 | from bt2 import utils as bt2_utils |
fbbe9302 SM |
15 | |
16 | ||
17 | class TraceClassTestCase(unittest.TestCase): | |
5783664e PP |
18 | def assertRaisesInComponentInit(self, expected_exc_type, user_code): |
19 | def f(comp_self): | |
20 | try: | |
21 | user_code(comp_self) | |
22 | except Exception as exc: | |
23 | return type(exc) | |
24 | ||
25 | exc_type = run_in_component_init(f) | |
26 | self.assertIsNotNone(exc_type) | |
27 | self.assertEqual(exc_type, expected_exc_type) | |
28 | ||
fbbe9302 SM |
29 | def test_create_default(self): |
30 | def f(comp_self): | |
31 | return comp_self._create_trace_class() | |
32 | ||
33 | tc = run_in_component_init(f) | |
34 | ||
35 | self.assertEqual(len(tc), 0) | |
f0a42b33 | 36 | self.assertIs(type(tc), bt2_trace_class._TraceClass) |
fbbe9302 | 37 | self.assertTrue(tc.assigns_automatic_stream_class_id) |
5783664e PP |
38 | self.assertEqual(len(tc.user_attributes), 0) |
39 | ||
40 | def test_create_user_attributes(self): | |
41 | def f(comp_self): | |
42 | return comp_self._create_trace_class(user_attributes={'salut': 23}) | |
43 | ||
44 | tc = run_in_component_init(f) | |
45 | self.assertEqual(tc.user_attributes, {'salut': 23}) | |
46 | ||
47 | def test_create_invalid_user_attributes(self): | |
48 | def f(comp_self): | |
49 | return comp_self._create_trace_class(user_attributes=object()) | |
50 | ||
51 | self.assertRaisesInComponentInit(TypeError, f) | |
52 | ||
53 | def test_create_invalid_user_attributes_value_type(self): | |
54 | def f(comp_self): | |
55 | return comp_self._create_trace_class(user_attributes=23) | |
56 | ||
57 | self.assertRaisesInComponentInit(TypeError, f) | |
fbbe9302 | 58 | |
46fc35d6 SM |
59 | def test_create_invalid_automatic_stream_class_id_type(self): |
60 | def f(comp_self): | |
61 | return comp_self._create_trace_class( | |
62 | assigns_automatic_stream_class_id='perchaude' | |
63 | ) | |
64 | ||
65 | self.assertRaisesInComponentInit(TypeError, f) | |
66 | ||
fbbe9302 SM |
67 | def test_automatic_stream_class_id(self): |
68 | def f(comp_self): | |
69 | return comp_self._create_trace_class(assigns_automatic_stream_class_id=True) | |
70 | ||
71 | tc = run_in_component_init(f) | |
72 | self.assertTrue(tc.assigns_automatic_stream_class_id) | |
73 | ||
74 | # This should not throw. | |
75 | sc1 = tc.create_stream_class() | |
76 | sc2 = tc.create_stream_class() | |
77 | ||
f0a42b33 FD |
78 | self.assertIs(type(sc1), bt2_stream_class._StreamClass) |
79 | self.assertIs(type(sc2), bt2_stream_class._StreamClass) | |
fbbe9302 SM |
80 | self.assertNotEqual(sc1.id, sc2.id) |
81 | ||
82 | def test_automatic_stream_class_id_raises(self): | |
83 | def f(comp_self): | |
84 | return comp_self._create_trace_class(assigns_automatic_stream_class_id=True) | |
85 | ||
86 | tc = run_in_component_init(f) | |
87 | self.assertTrue(tc.assigns_automatic_stream_class_id) | |
88 | ||
4430bc80 | 89 | with self.assertRaises(ValueError): |
082db648 | 90 | tc.create_stream_class(23) |
fbbe9302 SM |
91 | |
92 | def test_no_assigns_automatic_stream_class_id(self): | |
93 | def f(comp_self): | |
cfbd7cf3 FD |
94 | return comp_self._create_trace_class( |
95 | assigns_automatic_stream_class_id=False | |
96 | ) | |
fbbe9302 SM |
97 | |
98 | tc = run_in_component_init(f) | |
99 | self.assertFalse(tc.assigns_automatic_stream_class_id) | |
100 | ||
101 | sc = tc.create_stream_class(id=28) | |
102 | self.assertEqual(sc.id, 28) | |
103 | ||
104 | def test_no_assigns_automatic_stream_class_id_raises(self): | |
105 | def f(comp_self): | |
cfbd7cf3 FD |
106 | return comp_self._create_trace_class( |
107 | assigns_automatic_stream_class_id=False | |
108 | ) | |
fbbe9302 SM |
109 | |
110 | tc = run_in_component_init(f) | |
111 | self.assertFalse(tc.assigns_automatic_stream_class_id) | |
112 | ||
113 | # In this mode, it is required to pass an explicit id. | |
4430bc80 | 114 | with self.assertRaises(ValueError): |
fbbe9302 SM |
115 | tc.create_stream_class() |
116 | ||
fbbe9302 SM |
117 | @staticmethod |
118 | def _create_trace_class_with_some_stream_classes(): | |
119 | def f(comp_self): | |
cfbd7cf3 FD |
120 | return comp_self._create_trace_class( |
121 | assigns_automatic_stream_class_id=False | |
122 | ) | |
fbbe9302 SM |
123 | |
124 | tc = run_in_component_init(f) | |
125 | sc1 = tc.create_stream_class(id=12) | |
126 | sc2 = tc.create_stream_class(id=54) | |
127 | sc3 = tc.create_stream_class(id=2018) | |
128 | return tc, sc1, sc2, sc3 | |
129 | ||
130 | def test_getitem(self): | |
131 | tc, _, _, sc3 = self._create_trace_class_with_some_stream_classes() | |
f0a42b33 | 132 | self.assertIs(type(tc[2018]), bt2_stream_class._StreamClass) |
fbbe9302 SM |
133 | self.assertEqual(tc[2018].addr, sc3.addr) |
134 | ||
f0a42b33 FD |
135 | def test_const_getitem(self): |
136 | const_tc = get_const_stream_beginning_message().stream.trace.cls | |
137 | self.assertIs(type(const_tc[0]), bt2_stream_class._StreamClassConst) | |
138 | ||
fbbe9302 SM |
139 | def test_getitem_wrong_key_type(self): |
140 | tc, _, _, _ = self._create_trace_class_with_some_stream_classes() | |
141 | with self.assertRaises(TypeError): | |
142 | tc['hello'] | |
143 | ||
144 | def test_getitem_wrong_key(self): | |
145 | tc, _, _, _ = self._create_trace_class_with_some_stream_classes() | |
146 | with self.assertRaises(KeyError): | |
147 | tc[4] | |
148 | ||
149 | def test_len(self): | |
150 | tc = get_default_trace_class() | |
151 | self.assertEqual(len(tc), 0) | |
152 | tc.create_stream_class() | |
153 | self.assertEqual(len(tc), 1) | |
154 | ||
155 | def test_iter(self): | |
156 | tc, sc1, sc2, sc3 = self._create_trace_class_with_some_stream_classes() | |
157 | ||
158 | for sc_id, stream_class in tc.items(): | |
fbbe9302 | 159 | if sc_id == 12: |
f0a42b33 | 160 | self.assertIs(type(stream_class), bt2_stream_class._StreamClass) |
fbbe9302 SM |
161 | self.assertEqual(stream_class.addr, sc1.addr) |
162 | elif sc_id == 54: | |
163 | self.assertEqual(stream_class.addr, sc2.addr) | |
164 | elif sc_id == 2018: | |
165 | self.assertEqual(stream_class.addr, sc3.addr) | |
166 | ||
f0a42b33 FD |
167 | def test_const_iter(self): |
168 | const_tc = get_const_stream_beginning_message().stream.trace.cls | |
169 | const_sc = list(const_tc.values())[0] | |
170 | self.assertIs(type(const_sc), bt2_stream_class._StreamClassConst) | |
171 | ||
fbbe9302 SM |
172 | def test_destruction_listener(self): |
173 | def on_trace_class_destruction(trace_class): | |
1114a7d5 SM |
174 | nonlocal num_destruct_calls |
175 | num_destruct_calls += 1 | |
fbbe9302 | 176 | |
1114a7d5 | 177 | num_destruct_calls = 0 |
fbbe9302 SM |
178 | |
179 | trace_class = get_default_trace_class() | |
fbbe9302 | 180 | |
1114a7d5 SM |
181 | handle1 = trace_class.add_destruction_listener(on_trace_class_destruction) |
182 | self.assertIs(type(handle1), bt2_utils._ListenerHandle) | |
183 | ||
184 | handle2 = trace_class.add_destruction_listener(on_trace_class_destruction) | |
185 | ||
186 | trace_class.remove_destruction_listener(handle2) | |
187 | ||
1114a7d5 | 188 | self.assertEqual(num_destruct_calls, 0) |
fbbe9302 SM |
189 | |
190 | del trace_class | |
191 | ||
1114a7d5 SM |
192 | self.assertEqual(num_destruct_calls, 1) |
193 | ||
194 | def test_remove_destruction_listener_wrong_type(self): | |
195 | trace_class = get_default_trace_class() | |
196 | ||
197 | with self.assertRaisesRegex( | |
198 | TypeError, r"'int' is not a '<class 'bt2.utils._ListenerHandle'>' object" | |
199 | ): | |
200 | trace_class.remove_destruction_listener(123) | |
201 | ||
202 | def test_remove_destruction_listener_wrong_object(self): | |
203 | def on_trace_class_destruction(trace_class): | |
204 | pass | |
205 | ||
206 | trace_class_1 = get_default_trace_class() | |
207 | trace_class_2 = get_default_trace_class() | |
208 | ||
209 | handle1 = trace_class_1.add_destruction_listener(on_trace_class_destruction) | |
210 | ||
211 | with self.assertRaisesRegex( | |
212 | ValueError, | |
ab6a6968 | 213 | r'This trace class destruction listener does not match the trace class object\.', |
1114a7d5 SM |
214 | ): |
215 | trace_class_2.remove_destruction_listener(handle1) | |
216 | ||
217 | def test_remove_destruction_listener_twice(self): | |
218 | def on_trace_class_destruction(trace_class): | |
219 | pass | |
220 | ||
221 | trace_class = get_default_trace_class() | |
222 | handle = trace_class.add_destruction_listener(on_trace_class_destruction) | |
223 | ||
224 | trace_class.remove_destruction_listener(handle) | |
225 | ||
226 | with self.assertRaisesRegex( | |
227 | ValueError, r'This trace class destruction listener was already removed\.' | |
228 | ): | |
229 | trace_class.remove_destruction_listener(handle) | |
d14ddbba | 230 | |
64961f8b SM |
231 | def test_raise_in_destruction_listener(self): |
232 | def on_trace_class_destruction(trace_class): | |
233 | raise ValueError('it hurts') | |
234 | ||
235 | trace_class = get_default_trace_class() | |
236 | trace_class.add_destruction_listener(on_trace_class_destruction) | |
237 | ||
238 | del trace_class | |
239 | ||
d14ddbba SM |
240 | |
241 | if __name__ == '__main__': | |
242 | unittest.main() |