bt2: honor self component or query log level when logging
[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
c4239792 19from bt2 import value
c7eee084
PP
20import unittest
21import copy
22import bt2
23
24
25class QueryExecutorTestCase(unittest.TestCase):
26 def test_query(self):
27 class MySink(bt2._UserSinkComponent):
6a91742b 28 def _user_consume(self):
a01b452b
SM
29 pass
30
c7eee084 31 @classmethod
6a91742b 32 def _user_query(cls, query_exec, obj, params, log_level):
c7eee084
PP
33 nonlocal query_params
34 query_params = params
cfbd7cf3 35 return {'null': None, 'bt2': 'BT2'}
c7eee084
PP
36
37 query_params = None
38 params = {
39 'array': ['coucou', 23, None],
cfbd7cf3 40 'other_map': {'yes': 'yeah', '19': 19, 'minus 1.5': -1.5},
c7eee084
PP
41 'null': None,
42 }
43
44 res = bt2.QueryExecutor().query(MySink, 'obj', params)
45 self.assertEqual(query_params, params)
cfbd7cf3 46 self.assertEqual(res, {'null': None, 'bt2': 'BT2'})
c7eee084
PP
47 del query_params
48
49 def test_query_params_none(self):
50 class MySink(bt2._UserSinkComponent):
6a91742b 51 def _user_consume(self):
a01b452b
SM
52 pass
53
c7eee084 54 @classmethod
6a91742b 55 def _user_query(cls, query_exec, obj, params, log_level):
c7eee084
PP
56 nonlocal query_params
57 query_params = params
58
59 query_params = 23
60 res = bt2.QueryExecutor().query(MySink, 'obj', None)
61 self.assertEqual(query_params, None)
62 del query_params
63
761e1890
PP
64 def test_query_logging_level(self):
65 class MySink(bt2._UserSinkComponent):
6a91742b 66 def _user_consume(self):
a01b452b
SM
67 pass
68
761e1890 69 @classmethod
6a91742b 70 def _user_query(cls, query_exec, obj, params, log_level):
761e1890
PP
71 nonlocal query_log_level
72 query_log_level = log_level
73
74 query_log_level = None
cfbd7cf3 75 res = bt2.QueryExecutor().query(MySink, 'obj', None, bt2.LoggingLevel.INFO)
761e1890
PP
76 self.assertEqual(query_log_level, bt2.LoggingLevel.INFO)
77 del query_log_level
78
c7eee084
PP
79 def test_query_gen_error(self):
80 class MySink(bt2._UserSinkComponent):
6a91742b 81 def _user_consume(self):
a01b452b
SM
82 pass
83
c7eee084 84 @classmethod
6a91742b 85 def _user_query(cls, query_exec, obj, params, log_level):
c7eee084
PP
86 raise ValueError
87
694c792b 88 with self.assertRaises(bt2._Error) as ctx:
c7eee084
PP
89 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23])
90
ce4923b0 91 exc = ctx.exception
a635e507 92 self.assertEqual(len(exc), 2)
ce4923b0 93 cause = exc[0]
3fb99a22 94 self.assertIsInstance(cause, bt2._ComponentClassErrorCause)
ce4923b0
SM
95 self.assertIn('raise ValueError', cause.message)
96 self.assertEqual(cause.component_class_type, bt2.ComponentClassType.SINK)
97 self.assertEqual(cause.component_class_name, 'MySink')
98
76b6c2f7 99 def test_query_unknown_object(self):
c7eee084 100 class MySink(bt2._UserSinkComponent):
6a91742b 101 def _user_consume(self):
a01b452b
SM
102 pass
103
c7eee084 104 @classmethod
6a91742b 105 def _user_query(cls, query_exec, obj, params, log_level):
76b6c2f7 106 raise bt2.UnknownObject
c7eee084 107
76b6c2f7 108 with self.assertRaises(bt2.UnknownObject):
c7eee084
PP
109 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23])
110
761e1890
PP
111 def test_query_logging_level_invalid_type(self):
112 class MySink(bt2._UserSinkComponent):
6a91742b 113 def _user_consume(self):
a01b452b
SM
114 pass
115
761e1890 116 @classmethod
6a91742b 117 def _user_query(cls, query_exec, obj, params, log_level):
761e1890
PP
118 pass
119
120 with self.assertRaises(TypeError):
121 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23], 'yeah')
122
123 def test_query_logging_level_invalid_value(self):
124 class MySink(bt2._UserSinkComponent):
6a91742b 125 def _user_consume(self):
a01b452b
SM
126 pass
127
761e1890 128 @classmethod
6a91742b 129 def _user_query(cls, query_exec, obj, params, log_level):
761e1890
PP
130 pass
131
132 with self.assertRaises(ValueError):
133 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23], 12345)
134
c7eee084
PP
135 def test_query_try_again(self):
136 class MySink(bt2._UserSinkComponent):
6a91742b 137 def _user_consume(self):
a01b452b
SM
138 pass
139
c7eee084 140 @classmethod
6a91742b 141 def _user_query(cls, query_exec, obj, params, log_level):
c7eee084
PP
142 raise bt2.TryAgain
143
144 with self.assertRaises(bt2.TryAgain):
145 res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23])
146
9b4f9b42
PP
147 def test_query_add_interrupter(self):
148 class MySink(bt2._UserSinkComponent):
6a91742b 149 def _user_consume(self):
9b4f9b42
PP
150 pass
151
152 @classmethod
6a91742b 153 def _user_query(cls, query_exec, obj, params, log_level):
9b4f9b42
PP
154 nonlocal interrupter2
155 test_self.assertFalse(query_exec.is_interrupted)
156 interrupter2.set()
157 test_self.assertTrue(query_exec.is_interrupted)
158 interrupter2.reset()
159 test_self.assertFalse(query_exec.is_interrupted)
160
161 interrupter1 = bt2.Interrupter()
162 interrupter2 = bt2.Interrupter()
163 test_self = self
c7eee084 164 query_exec = bt2.QueryExecutor()
9b4f9b42
PP
165 query_exec.add_interrupter(interrupter1)
166 query_exec.add_interrupter(interrupter2)
167 query_exec.query(MySink, 'obj', [17, 23])
c7eee084 168
9b4f9b42 169 def test_query_interrupt(self):
c7eee084 170 class MySink(bt2._UserSinkComponent):
6a91742b 171 def _user_consume(self):
a01b452b
SM
172 pass
173
c7eee084 174 @classmethod
6a91742b 175 def _user_query(cls, query_exec, obj, params, log_level):
9b4f9b42
PP
176 test_self.assertFalse(query_exec.is_interrupted)
177 query_exec.interrupt()
178 test_self.assertTrue(query_exec.is_interrupted)
c7eee084 179
9b4f9b42 180 test_self = self
c7eee084 181 query_exec = bt2.QueryExecutor()
9b4f9b42 182 query_exec.query(MySink, 'obj', [17, 23])
This page took 0.043334 seconds and 4 git commands to generate.