Fix: bt2: pass _TraceClassConst to destruction listeners
[babeltrace.git] / tests / bindings / python / bt2 / test_trace.py
CommitLineData
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
cd03c43c 19import uuid
9cf643d1 20import unittest
9cbe0c59 21import utils
f377a958 22from utils import get_default_trace_class
9cbe0c59
FD
23from bt2 import trace_class as bt2_trace_class
24from bt2 import value as bt2_value
25from bt2 import trace as bt2_trace
26from bt2 import stream as bt2_stream
ab778b5c 27from bt2 import utils as bt2_utils
9cf643d1
PP
28
29
30class TraceTestCase(unittest.TestCase):
31 def setUp(self):
f377a958 32 self._tc = get_default_trace_class()
9cf643d1
PP
33
34 def test_create_default(self):
f377a958 35 trace = self._tc()
cd03c43c
PP
36 self.assertIsNone(trace.name)
37 self.assertIsNone(trace.uuid)
3f1a6e26 38 self.assertEqual(len(trace.environment), 0)
b2df5780 39 self.assertEqual(len(trace.user_attributes), 0)
9cf643d1 40
f377a958 41 def test_create_invalid_name(self):
9cf643d1 42 with self.assertRaises(TypeError):
f377a958 43 self._tc(name=17)
9cf643d1 44
b2df5780
PP
45 def test_create_user_attributes(self):
46 trace = self._tc(user_attributes={'salut': 23})
47 self.assertEqual(trace.user_attributes, {'salut': 23})
9cbe0c59 48 self.assertIs(type(trace.user_attributes), bt2_value.MapValue)
b2df5780
PP
49
50 def test_create_invalid_user_attributes(self):
51 with self.assertRaises(TypeError):
52 self._tc(user_attributes=object())
53
54 def test_create_invalid_user_attributes_value_type(self):
55 with self.assertRaises(TypeError):
56 self._tc(user_attributes=23)
57
a7f0580d 58 def test_attr_trace_class(self):
cd03c43c 59 trace = self._tc()
a7f0580d 60 self.assertEqual(trace.cls.addr, self._tc.addr)
9cbe0c59
FD
61 self.assertIs(type(trace.cls), bt2_trace_class._TraceClass)
62
63 def test_const_attr_trace_class(self):
64 trace = utils.get_const_stream_beginning_message().stream.trace
65 self.assertIs(type(trace.cls), bt2_trace_class._TraceClassConst)
a7f0580d 66
cd03c43c
PP
67 def test_attr_name(self):
68 trace = self._tc(name='mein trace')
69 self.assertEqual(trace.name, 'mein trace')
70
71 def test_attr_uuid(self):
72 trace = self._tc(uuid=uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
73 self.assertEqual(trace.uuid, uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
74
75 def test_env_get(self):
3f1a6e26 76 trace = self._tc(environment={'hello': 'you', 'foo': -5})
9cbe0c59
FD
77 self.assertIs(type(trace.environment), bt2_trace._TraceEnvironment)
78 self.assertIs(type(trace.environment['foo']), bt2_value.SignedIntegerValue)
3f1a6e26
PP
79 self.assertEqual(trace.environment['hello'], 'you')
80 self.assertEqual(trace.environment['foo'], -5)
cd03c43c 81
9cbe0c59
FD
82 def test_env_iter(self):
83 trace = self._tc(environment={'hello': 'you', 'foo': -5})
84 values = set(trace.environment)
85 self.assertEqual(values, {'hello', 'foo'})
86
87 def test_const_env_get(self):
88 trace = utils.get_const_stream_beginning_message().stream.trace
89 self.assertIs(type(trace.environment), bt2_trace._TraceEnvironmentConst)
90 self.assertIs(
91 type(trace.environment['patate']), bt2_value._SignedIntegerValueConst
92 )
93
2e30cfd1 94 def test_const_env_iter(self):
9cbe0c59
FD
95 trace = utils.get_const_stream_beginning_message().stream.trace
96 values = set(trace.environment)
97 self.assertEqual(values, {'patate'})
98
99 def test_const_env_set(self):
100 trace = utils.get_const_stream_beginning_message().stream.trace
101 with self.assertRaises(TypeError):
102 trace.environment['patate'] = 33
103
cd03c43c 104 def test_env_get_non_existent(self):
3f1a6e26 105 trace = self._tc(environment={'hello': 'you', 'foo': -5})
cd03c43c
PP
106
107 with self.assertRaises(KeyError):
3f1a6e26 108 trace.environment['lel']
cd03c43c 109
f377a958
SM
110 def test_len(self):
111 trace = self._tc()
112 sc = self._tc.create_stream_class()
113 self.assertEqual(len(trace), 0)
9cf643d1 114
f377a958
SM
115 trace.create_stream(sc)
116 self.assertEqual(len(trace), 1)
9cf643d1 117
f377a958
SM
118 def _create_trace_with_some_streams(self):
119 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
120 trace = self._tc()
121 trace.create_stream(sc, id=12)
122 trace.create_stream(sc, id=15)
123 trace.create_stream(sc, id=17)
9cf643d1 124
f377a958 125 return trace
9cf643d1
PP
126
127 def test_iter(self):
f377a958
SM
128 trace = self._create_trace_with_some_streams()
129 stream_ids = set(trace)
130 self.assertEqual(stream_ids, {12, 15, 17})
9cf643d1 131
f377a958
SM
132 def test_getitem(self):
133 trace = self._create_trace_with_some_streams()
f377a958 134 self.assertEqual(trace[12].id, 12)
9cbe0c59
FD
135 self.assertIs(type(trace[12]), bt2_stream._Stream)
136
137 def test_const_getitem(self):
138 trace = utils.get_const_stream_beginning_message().stream.trace
139 self.assertIs(type(trace[0]), bt2_stream._StreamConst)
9cf643d1 140
f377a958
SM
141 def test_getitem_invalid_key(self):
142 trace = self._create_trace_with_some_streams()
9cf643d1 143 with self.assertRaises(KeyError):
f377a958 144 trace[18]
9cf643d1 145
f377a958
SM
146 def test_destruction_listener(self):
147 def on_trace_class_destruction(trace_class):
ab778b5c
SM
148 nonlocal num_trace_class_destroyed_calls
149 num_trace_class_destroyed_calls += 1
f6a5e476 150
f377a958 151 def on_trace_destruction(trace):
eead7a76
SM
152 nonlocal type_of_passed_trace
153 type_of_passed_trace = type(trace)
154
ab778b5c
SM
155 nonlocal num_trace_destroyed_calls
156 num_trace_destroyed_calls += 1
f6a5e476 157
ab778b5c
SM
158 num_trace_class_destroyed_calls = 0
159 num_trace_destroyed_calls = 0
eead7a76 160 type_of_passed_trace = None
f6a5e476 161
f377a958
SM
162 trace_class = get_default_trace_class()
163 stream_class = trace_class.create_stream_class()
164 trace = trace_class()
165 stream = trace.create_stream(stream_class)
f6a5e476 166
f377a958 167 trace_class.add_destruction_listener(on_trace_class_destruction)
ab778b5c
SM
168 td_handle1 = trace.add_destruction_listener(on_trace_destruction)
169 td_handle2 = trace.add_destruction_listener(on_trace_destruction)
f6a5e476 170
ab778b5c
SM
171 self.assertIs(type(td_handle1), bt2_utils._ListenerHandle)
172
173 trace.remove_destruction_listener(td_handle2)
174
ab778b5c
SM
175 self.assertEqual(num_trace_class_destroyed_calls, 0)
176 self.assertEqual(num_trace_destroyed_calls, 0)
9cf643d1 177
f377a958 178 del trace
9cf643d1 179
ab778b5c
SM
180 self.assertEqual(num_trace_class_destroyed_calls, 0)
181 self.assertEqual(num_trace_destroyed_calls, 0)
9cf643d1 182
f377a958 183 del stream
9cf643d1 184
ab778b5c
SM
185 self.assertEqual(num_trace_class_destroyed_calls, 0)
186 self.assertEqual(num_trace_destroyed_calls, 1)
eead7a76 187 self.assertIs(type_of_passed_trace, bt2_trace._TraceConst)
9cf643d1 188
f377a958 189 del trace_class
9cf643d1 190
ab778b5c
SM
191 self.assertEqual(num_trace_class_destroyed_calls, 0)
192 self.assertEqual(num_trace_destroyed_calls, 1)
9cf643d1 193
f377a958 194 del stream_class
9cf643d1 195
ab778b5c
SM
196 self.assertEqual(num_trace_class_destroyed_calls, 1)
197 self.assertEqual(num_trace_destroyed_calls, 1)
198
199 def test_remove_destruction_listener_wrong_type(self):
200 trace_class = get_default_trace_class()
201 trace = trace_class()
202
203 with self.assertRaisesRegex(
204 TypeError, r"'int' is not a '<class 'bt2.utils._ListenerHandle'>' object"
205 ):
206 trace.remove_destruction_listener(123)
207
208 def test_remove_destruction_listener_wrong_object(self):
209 def on_trace_destruction(trace):
210 pass
211
212 trace_class_1 = get_default_trace_class()
213 trace1 = trace_class_1()
214 trace_class_2 = get_default_trace_class()
215 trace2 = trace_class_2()
216
217 handle1 = trace1.add_destruction_listener(on_trace_destruction)
218
219 with self.assertRaisesRegex(
220 ValueError,
221 r'This trace destruction listener does not match the trace object\.',
222 ):
223 trace2.remove_destruction_listener(handle1)
224
225 def test_remove_destruction_listener_twice(self):
226 def on_trace_destruction(trace_class):
227 pass
228
229 trace_class = get_default_trace_class()
230 trace = trace_class()
231 handle = trace.add_destruction_listener(on_trace_destruction)
232
233 trace.remove_destruction_listener(handle)
234
235 with self.assertRaisesRegex(
236 ValueError, r'This trace destruction listener was already removed\.'
237 ):
238 trace.remove_destruction_listener(handle)
3db06b1d 239
b6932b96
SM
240 def test_raise_in_destruction_listener(self):
241 def on_trace_destruction(trace):
242 raise ValueError('it hurts')
243
244 trace_class = get_default_trace_class()
245 trace = trace_class()
246 trace.add_destruction_listener(on_trace_destruction)
247
248 del trace
249
3db06b1d
SM
250
251if __name__ == '__main__':
252 unittest.main()
This page took 0.067159 seconds and 4 git commands to generate.