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