Fix: src.ctf.fs: initialize the other_entry variable
[babeltrace.git] / tests / bindings / python / bt2 / test_query_executor.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
c7eee084 19import unittest
c7eee084 20import bt2
7c14d641 21import re
c7eee084
PP
22
23
24class QueryExecutorTestCase(unittest.TestCase):
88d1a0b7
SM
25 def test_default_interrupter(self):
26 class MySink(bt2._UserSinkComponent):
27 def _user_consume(self):
28 pass
29
30 query_exec = bt2.QueryExecutor(MySink, 'obj')
31 interrupter = query_exec.default_interrupter
32 self.assertIs(type(interrupter), bt2.Interrupter)
33
c7eee084
PP
34 def test_query(self):
35 class MySink(bt2._UserSinkComponent):
6a91742b 36 def _user_consume(self):
a01b452b
SM
37 pass
38
c7eee084 39 @classmethod
7c14d641 40 def _user_query(cls, priv_query_exec, obj, params, method_obj):
c7eee084
PP
41 nonlocal query_params
42 query_params = params
cfbd7cf3 43 return {'null': None, 'bt2': 'BT2'}
c7eee084
PP
44
45 query_params = None
46 params = {
47 'array': ['coucou', 23, None],
cfbd7cf3 48 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
c7eee084
PP
49 'null': None,
50 }
51
3c729b9a 52 res = bt2.QueryExecutor(MySink, 'obj', params).query()
e42e1587
FD
53 self.assertIs(type(res), bt2._MapValueConst)
54 self.assertIs(type(res['bt2']), bt2._StringValueConst)
c7eee084 55 self.assertEqual(query_params, params)
cfbd7cf3 56 self.assertEqual(res, {'null': None, 'bt2': 'BT2'})
c7eee084
PP
57 del query_params
58
59 def test_query_params_none(self):
60 class MySink(bt2._UserSinkComponent):
6a91742b 61 def _user_consume(self):
a01b452b
SM
62 pass
63
c7eee084 64 @classmethod
7c14d641 65 def _user_query(cls, priv_query_exec, obj, params, method_obj):
c7eee084
PP
66 nonlocal query_params
67 query_params = params
68
69 query_params = 23
082db648 70 bt2.QueryExecutor(MySink, 'obj', None).query()
3c729b9a
PP
71 self.assertIs(query_params, None)
72 del query_params
73
74 def test_query_no_params(self):
75 class MySink(bt2._UserSinkComponent):
76 def _user_consume(self):
77 pass
78
79 @classmethod
7c14d641 80 def _user_query(cls, priv_query_exec, obj, params, method_obj):
3c729b9a
PP
81 nonlocal query_params
82 query_params = params
83
84 query_params = 23
082db648 85 bt2.QueryExecutor(MySink, 'obj').query()
3c729b9a 86 self.assertIs(query_params, None)
c7eee084
PP
87 del query_params
88
7c14d641
PP
89 def test_query_with_method_obj(self):
90 class MySink(bt2._UserSinkComponent):
91 def _user_consume(self):
92 pass
93
94 @classmethod
95 def _user_query(cls, priv_query_exec, obj, params, method_obj):
96 nonlocal query_method_obj
97 query_method_obj = method_obj
98
99 query_method_obj = None
100 method_obj = object()
082db648 101 bt2.QueryExecutor(MySink, 'obj', method_obj=method_obj).query()
7c14d641
PP
102 self.assertIs(query_method_obj, method_obj)
103 del query_method_obj
104
105 def test_query_with_method_obj_del_ref(self):
106 class MySink(bt2._UserSinkComponent):
107 def _user_consume(self):
108 pass
109
110 @classmethod
111 def _user_query(cls, priv_query_exec, obj, params, method_obj):
112 nonlocal query_method_obj
113 query_method_obj = method_obj
114
115 class Custom:
116 pass
117
118 query_method_obj = None
119 method_obj = Custom()
120 method_obj.hola = 'hello'
121 query_exec = bt2.QueryExecutor(MySink, 'obj', method_obj=method_obj)
122 del method_obj
123 query_exec.query()
124 self.assertIsInstance(query_method_obj, Custom)
125 self.assertEqual(query_method_obj.hola, 'hello')
126 del query_method_obj
127
128 def test_query_with_none_method_obj(self):
129 class MySink(bt2._UserSinkComponent):
130 def _user_consume(self):
131 pass
132
133 @classmethod
134 def _user_query(cls, priv_query_exec, obj, params, method_obj):
135 nonlocal query_method_obj
136 query_method_obj = method_obj
137
138 query_method_obj = object()
082db648 139 bt2.QueryExecutor(MySink, 'obj').query()
7c14d641
PP
140 self.assertIsNone(query_method_obj)
141 del query_method_obj
142
143 def test_query_with_method_obj_non_python_comp_cls(self):
144 plugin = bt2.find_plugin('text', find_in_user_dir=False, find_in_sys_dir=False)
145 assert plugin is not None
146 cc = plugin.source_component_classes['dmesg']
147 assert cc is not None
148
149 with self.assertRaisesRegex(
150 ValueError,
151 re.escape(r'cannot pass a Python object to a non-Python component class'),
152 ):
153 bt2.QueryExecutor(cc, 'obj', method_obj=object()).query()
154
761e1890
PP
155 def test_query_logging_level(self):
156 class MySink(bt2._UserSinkComponent):
6a91742b 157 def _user_consume(self):
a01b452b
SM
158 pass
159
761e1890 160 @classmethod
7c14d641 161 def _user_query(cls, priv_query_exec, obj, params, method_obj):
761e1890 162 nonlocal query_log_level
3c729b9a 163 query_log_level = priv_query_exec.logging_level
761e1890
PP
164
165 query_log_level = None
3c729b9a
PP
166 query_exec = bt2.QueryExecutor(MySink, 'obj', None)
167 query_exec.logging_level = bt2.LoggingLevel.INFO
168 query_exec.query()
761e1890
PP
169 self.assertEqual(query_log_level, bt2.LoggingLevel.INFO)
170 del query_log_level
171
c7eee084
PP
172 def test_query_gen_error(self):
173 class MySink(bt2._UserSinkComponent):
6a91742b 174 def _user_consume(self):
a01b452b
SM
175 pass
176
c7eee084 177 @classmethod
7c14d641 178 def _user_query(cls, priv_query_exec, obj, params, method_obj):
c7eee084
PP
179 raise ValueError
180
694c792b 181 with self.assertRaises(bt2._Error) as ctx:
082db648 182 bt2.QueryExecutor(MySink, 'obj', [17, 23]).query()
c7eee084 183
ce4923b0 184 exc = ctx.exception
561e7049 185 self.assertEqual(len(exc), 3)
ce4923b0 186 cause = exc[0]
3fb99a22 187 self.assertIsInstance(cause, bt2._ComponentClassErrorCause)
ce4923b0
SM
188 self.assertIn('raise ValueError', cause.message)
189 self.assertEqual(cause.component_class_type, bt2.ComponentClassType.SINK)
190 self.assertEqual(cause.component_class_name, 'MySink')
191
76b6c2f7 192 def test_query_unknown_object(self):
c7eee084 193 class MySink(bt2._UserSinkComponent):
6a91742b 194 def _user_consume(self):
a01b452b
SM
195 pass
196
c7eee084 197 @classmethod
7c14d641 198 def _user_query(cls, priv_query_exec, obj, params, method_obj):
76b6c2f7 199 raise bt2.UnknownObject
c7eee084 200
76b6c2f7 201 with self.assertRaises(bt2.UnknownObject):
082db648 202 bt2.QueryExecutor(MySink, 'obj', [17, 23]).query()
c7eee084 203
761e1890
PP
204 def test_query_logging_level_invalid_type(self):
205 class MySink(bt2._UserSinkComponent):
6a91742b 206 def _user_consume(self):
a01b452b
SM
207 pass
208
761e1890 209 @classmethod
7c14d641 210 def _user_query(cls, priv_query_exec, obj, params, method_obj):
761e1890
PP
211 pass
212
3c729b9a
PP
213 query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23])
214
761e1890 215 with self.assertRaises(TypeError):
3c729b9a 216 query_exec.logging_level = 'yeah'
761e1890
PP
217
218 def test_query_logging_level_invalid_value(self):
219 class MySink(bt2._UserSinkComponent):
6a91742b 220 def _user_consume(self):
a01b452b
SM
221 pass
222
761e1890 223 @classmethod
7c14d641 224 def _user_query(cls, priv_query_exec, obj, params, method_obj):
761e1890
PP
225 pass
226
3c729b9a
PP
227 query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23])
228
761e1890 229 with self.assertRaises(ValueError):
3c729b9a 230 query_exec.logging_level = 12345
761e1890 231
c7eee084
PP
232 def test_query_try_again(self):
233 class MySink(bt2._UserSinkComponent):
6a91742b 234 def _user_consume(self):
a01b452b
SM
235 pass
236
c7eee084 237 @classmethod
7c14d641 238 def _user_query(cls, priv_query_exec, obj, params, method_obj):
c7eee084
PP
239 raise bt2.TryAgain
240
241 with self.assertRaises(bt2.TryAgain):
082db648 242 bt2.QueryExecutor(MySink, 'obj', [17, 23]).query()
c7eee084 243
9b4f9b42
PP
244 def test_query_add_interrupter(self):
245 class MySink(bt2._UserSinkComponent):
6a91742b 246 def _user_consume(self):
9b4f9b42
PP
247 pass
248
249 @classmethod
7c14d641 250 def _user_query(cls, priv_query_exec, obj, params, method_obj):
9b4f9b42
PP
251 nonlocal interrupter2
252 test_self.assertFalse(query_exec.is_interrupted)
253 interrupter2.set()
254 test_self.assertTrue(query_exec.is_interrupted)
255 interrupter2.reset()
256 test_self.assertFalse(query_exec.is_interrupted)
257
258 interrupter1 = bt2.Interrupter()
259 interrupter2 = bt2.Interrupter()
260 test_self = self
3c729b9a 261 query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23])
9b4f9b42
PP
262 query_exec.add_interrupter(interrupter1)
263 query_exec.add_interrupter(interrupter2)
3c729b9a 264 query_exec.query()
c7eee084 265
9b4f9b42 266 def test_query_interrupt(self):
c7eee084 267 class MySink(bt2._UserSinkComponent):
6a91742b 268 def _user_consume(self):
a01b452b
SM
269 pass
270
c7eee084 271 @classmethod
7c14d641 272 def _user_query(cls, priv_query_exec, obj, params, method_obj):
9b4f9b42 273 test_self.assertFalse(query_exec.is_interrupted)
88d1a0b7 274 query_exec.default_interrupter.set()
9b4f9b42 275 test_self.assertTrue(query_exec.is_interrupted)
c7eee084 276
9b4f9b42 277 test_self = self
3c729b9a
PP
278 query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23])
279 query_exec.query()
280
281 def test_query_priv_executor_invalid_after(self):
282 class MySink(bt2._UserSinkComponent):
283 def _user_consume(self):
284 pass
285
286 @classmethod
7c14d641 287 def _user_query(cls, priv_query_exec, obj, params, method_obj):
3c729b9a
PP
288 nonlocal test_priv_query_exec
289 test_priv_query_exec = priv_query_exec
290
291 test_priv_query_exec = None
292 query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23])
293 query_exec.query()
294 assert test_priv_query_exec is not None
295
296 with self.assertRaises(RuntimeError):
297 test_priv_query_exec.logging_level
298
299 del test_priv_query_exec
d14ddbba
SM
300
301
302if __name__ == '__main__':
303 unittest.main()
This page took 0.064075 seconds and 4 git commands to generate.