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