bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / tests / bindings / python / bt2 / test_trace_class.py
CommitLineData
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 19import unittest
f0a42b33
FD
20from utils import (
21 run_in_component_init,
22 get_default_trace_class,
23 get_const_stream_beginning_message,
24)
25from bt2 import stream_class as bt2_stream_class
26from bt2 import trace_class as bt2_trace_class
fbbe9302
SM
27
28
29class TraceClassTestCase(unittest.TestCase):
5783664e
PP
30 def assertRaisesInComponentInit(self, expected_exc_type, user_code):
31 def f(comp_self):
32 try:
33 user_code(comp_self)
34 except Exception as exc:
35 return type(exc)
36
37 exc_type = run_in_component_init(f)
38 self.assertIsNotNone(exc_type)
39 self.assertEqual(exc_type, expected_exc_type)
40
fbbe9302
SM
41 def test_create_default(self):
42 def f(comp_self):
43 return comp_self._create_trace_class()
44
45 tc = run_in_component_init(f)
46
47 self.assertEqual(len(tc), 0)
f0a42b33 48 self.assertIs(type(tc), bt2_trace_class._TraceClass)
fbbe9302 49 self.assertTrue(tc.assigns_automatic_stream_class_id)
5783664e
PP
50 self.assertEqual(len(tc.user_attributes), 0)
51
52 def test_create_user_attributes(self):
53 def f(comp_self):
54 return comp_self._create_trace_class(user_attributes={'salut': 23})
55
56 tc = run_in_component_init(f)
57 self.assertEqual(tc.user_attributes, {'salut': 23})
58
59 def test_create_invalid_user_attributes(self):
60 def f(comp_self):
61 return comp_self._create_trace_class(user_attributes=object())
62
63 self.assertRaisesInComponentInit(TypeError, f)
64
65 def test_create_invalid_user_attributes_value_type(self):
66 def f(comp_self):
67 return comp_self._create_trace_class(user_attributes=23)
68
69 self.assertRaisesInComponentInit(TypeError, f)
fbbe9302 70
fbbe9302
SM
71 def test_automatic_stream_class_id(self):
72 def f(comp_self):
73 return comp_self._create_trace_class(assigns_automatic_stream_class_id=True)
74
75 tc = run_in_component_init(f)
76 self.assertTrue(tc.assigns_automatic_stream_class_id)
77
78 # This should not throw.
79 sc1 = tc.create_stream_class()
80 sc2 = tc.create_stream_class()
81
f0a42b33
FD
82 self.assertIs(type(sc1), bt2_stream_class._StreamClass)
83 self.assertIs(type(sc2), bt2_stream_class._StreamClass)
fbbe9302
SM
84 self.assertNotEqual(sc1.id, sc2.id)
85
86 def test_automatic_stream_class_id_raises(self):
87 def f(comp_self):
88 return comp_self._create_trace_class(assigns_automatic_stream_class_id=True)
89
90 tc = run_in_component_init(f)
91 self.assertTrue(tc.assigns_automatic_stream_class_id)
92
4430bc80 93 with self.assertRaises(ValueError):
082db648 94 tc.create_stream_class(23)
fbbe9302
SM
95
96 def test_no_assigns_automatic_stream_class_id(self):
97 def f(comp_self):
cfbd7cf3
FD
98 return comp_self._create_trace_class(
99 assigns_automatic_stream_class_id=False
100 )
fbbe9302
SM
101
102 tc = run_in_component_init(f)
103 self.assertFalse(tc.assigns_automatic_stream_class_id)
104
105 sc = tc.create_stream_class(id=28)
106 self.assertEqual(sc.id, 28)
107
108 def test_no_assigns_automatic_stream_class_id_raises(self):
109 def f(comp_self):
cfbd7cf3
FD
110 return comp_self._create_trace_class(
111 assigns_automatic_stream_class_id=False
112 )
fbbe9302
SM
113
114 tc = run_in_component_init(f)
115 self.assertFalse(tc.assigns_automatic_stream_class_id)
116
117 # In this mode, it is required to pass an explicit id.
4430bc80 118 with self.assertRaises(ValueError):
fbbe9302
SM
119 tc.create_stream_class()
120
fbbe9302
SM
121 @staticmethod
122 def _create_trace_class_with_some_stream_classes():
123 def f(comp_self):
cfbd7cf3
FD
124 return comp_self._create_trace_class(
125 assigns_automatic_stream_class_id=False
126 )
fbbe9302
SM
127
128 tc = run_in_component_init(f)
129 sc1 = tc.create_stream_class(id=12)
130 sc2 = tc.create_stream_class(id=54)
131 sc3 = tc.create_stream_class(id=2018)
132 return tc, sc1, sc2, sc3
133
134 def test_getitem(self):
135 tc, _, _, sc3 = self._create_trace_class_with_some_stream_classes()
f0a42b33 136 self.assertIs(type(tc[2018]), bt2_stream_class._StreamClass)
fbbe9302
SM
137 self.assertEqual(tc[2018].addr, sc3.addr)
138
f0a42b33
FD
139 def test_const_getitem(self):
140 const_tc = get_const_stream_beginning_message().stream.trace.cls
141 self.assertIs(type(const_tc[0]), bt2_stream_class._StreamClassConst)
142
fbbe9302
SM
143 def test_getitem_wrong_key_type(self):
144 tc, _, _, _ = self._create_trace_class_with_some_stream_classes()
145 with self.assertRaises(TypeError):
146 tc['hello']
147
148 def test_getitem_wrong_key(self):
149 tc, _, _, _ = self._create_trace_class_with_some_stream_classes()
150 with self.assertRaises(KeyError):
151 tc[4]
152
153 def test_len(self):
154 tc = get_default_trace_class()
155 self.assertEqual(len(tc), 0)
156 tc.create_stream_class()
157 self.assertEqual(len(tc), 1)
158
159 def test_iter(self):
160 tc, sc1, sc2, sc3 = self._create_trace_class_with_some_stream_classes()
161
162 for sc_id, stream_class in tc.items():
fbbe9302 163 if sc_id == 12:
f0a42b33 164 self.assertIs(type(stream_class), bt2_stream_class._StreamClass)
fbbe9302
SM
165 self.assertEqual(stream_class.addr, sc1.addr)
166 elif sc_id == 54:
167 self.assertEqual(stream_class.addr, sc2.addr)
168 elif sc_id == 2018:
169 self.assertEqual(stream_class.addr, sc3.addr)
170
f0a42b33
FD
171 def test_const_iter(self):
172 const_tc = get_const_stream_beginning_message().stream.trace.cls
173 const_sc = list(const_tc.values())[0]
174 self.assertIs(type(const_sc), bt2_stream_class._StreamClassConst)
175
fbbe9302
SM
176 def test_destruction_listener(self):
177 def on_trace_class_destruction(trace_class):
178 nonlocal trace_class_destroyed
179 trace_class_destroyed = True
180
181 trace_class_destroyed = False
182
183 trace_class = get_default_trace_class()
184 trace_class.add_destruction_listener(on_trace_class_destruction)
185
186 self.assertFalse(trace_class_destroyed)
187
188 del trace_class
189
190 self.assertTrue(trace_class_destroyed)
This page took 0.038518 seconds and 4 git commands to generate.