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