Commit | Line | Data |
---|---|---|
3b2be708 SM |
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 | from bt2 import native_bt | |
20 | import bt2 | |
21 | import unittest | |
22 | ||
23 | ||
24 | class FailingIter(bt2._UserMessageIterator): | |
25 | def __next__(self): | |
26 | raise ValueError('User message iterator is failing') | |
27 | ||
28 | ||
29 | class SourceWithFailingIter( | |
30 | bt2._UserSourceComponent, message_iterator_class=FailingIter | |
31 | ): | |
32 | def __init__(self, params): | |
33 | self._add_output_port('out') | |
34 | ||
35 | ||
36 | class SourceWithFailingInit( | |
37 | bt2._UserSourceComponent, message_iterator_class=FailingIter | |
38 | ): | |
39 | def __init__(self, params): | |
40 | raise ValueError('Source is failing') | |
41 | ||
42 | ||
43 | class WorkingSink(bt2._UserSinkComponent): | |
44 | def __init__(self, params): | |
45 | self._in = self._add_input_port('in') | |
46 | ||
47 | def _graph_is_configured(self): | |
48 | self._iter = self._in.create_message_iterator() | |
49 | ||
50 | def _consume(self): | |
51 | next(self._iter) | |
52 | ||
53 | ||
54 | class SinkWithExceptionChaining(bt2._UserSinkComponent): | |
55 | def __init__(self, params): | |
56 | self._in = self._add_input_port('in') | |
57 | ||
58 | def _graph_is_configured(self): | |
59 | self._iter = self._in.create_message_iterator() | |
60 | ||
61 | def _consume(self): | |
62 | try: | |
63 | print(self._iter.__next__) | |
64 | next(self._iter) | |
65 | except bt2.Error as e: | |
66 | print(hex(id(e))) | |
67 | print(e.__dict__) | |
68 | raise ValueError('oops') from e | |
69 | ||
70 | ||
71 | class SinkWithFailingQuery(bt2._UserSinkComponent): | |
72 | def _graph_is_configured(self): | |
73 | pass | |
74 | ||
75 | def _consume(self): | |
76 | pass | |
77 | ||
78 | @staticmethod | |
79 | def _query(executor, obj, params, log_level): | |
80 | raise ValueError('Query is failing') | |
81 | ||
82 | ||
83 | class ErrorTestCase(unittest.TestCase): | |
84 | def _run_failing_graph(self, source_cc, sink_cc): | |
85 | with self.assertRaises(bt2.Error) as ctx: | |
86 | graph = bt2.Graph() | |
87 | src = graph.add_component(source_cc, 'src') | |
88 | snk = graph.add_component(sink_cc, 'snk') | |
89 | graph.connect_ports(src.output_ports['out'], snk.input_ports['in']) | |
90 | graph.run() | |
91 | ||
92 | return ctx.exception | |
93 | ||
94 | def test_current_thread_error_none(self): | |
95 | # When a bt2.Error is raised, it steals the current thread's error. | |
96 | # Verify that it is now NULL. | |
97 | exc = self._run_failing_graph(SourceWithFailingInit, WorkingSink) | |
98 | self.assertIsNone(native_bt.current_thread_take_error()) | |
99 | ||
100 | def test_len(self): | |
101 | exc = self._run_failing_graph(SourceWithFailingIter, WorkingSink) | |
102 | ||
103 | # The exact number of causes is not too important (it can change if we | |
104 | # append more or less causes along the way), but the idea is to verify is | |
105 | # has a value that makes sense. | |
106 | self.assertEqual(len(exc), 4) | |
107 | ||
108 | def test_iter(self): | |
109 | exc = self._run_failing_graph(SourceWithFailingIter, WorkingSink) | |
110 | ||
111 | for c in exc: | |
112 | # Each cause is an instance of _ErrorCause (including subclasses). | |
113 | self.assertIsInstance(c, bt2.error._ErrorCause) | |
114 | ||
115 | def test_getitem(self): | |
116 | exc = self._run_failing_graph(SourceWithFailingIter, WorkingSink) | |
117 | ||
118 | for i in range(len(exc)): | |
119 | c = exc[i] | |
120 | # Each cause is an instance of _ErrorCause (including subclasses). | |
121 | self.assertIsInstance(c, bt2.error._ErrorCause) | |
122 | ||
123 | def test_getitem_indexerror(self): | |
124 | exc = self._run_failing_graph(SourceWithFailingIter, WorkingSink) | |
125 | ||
126 | with self.assertRaises(IndexError): | |
127 | exc[len(exc)] | |
128 | ||
129 | def test_exception_chaining(self): | |
130 | # Test that if we do: | |
131 | # | |
132 | # try: | |
133 | # ... | |
134 | # except bt2.Error as exc: | |
135 | # raise ValueError('oh noes') from exc | |
136 | # | |
137 | # We are able to fetch the causes of the original bt2.Error in the | |
138 | # exception chain. Also, each exception in the chain should become one | |
139 | # cause once caught. | |
140 | exc = self._run_failing_graph(SourceWithFailingIter, SinkWithExceptionChaining) | |
141 | ||
142 | self.assertEqual(len(exc), 5) | |
143 | ||
144 | self.assertIsInstance(exc[0], bt2.error._MessageIteratorErrorCause) | |
145 | self.assertEqual(exc[0].component_class_name, 'SourceWithFailingIter') | |
146 | self.assertIn('ValueError: User message iterator is failing', exc[0].message) | |
147 | ||
148 | self.assertIsInstance(exc[1], bt2.error._ErrorCause) | |
149 | ||
150 | self.assertIsInstance(exc[2], bt2.error._ComponentErrorCause) | |
151 | self.assertEqual(exc[2].component_class_name, 'SinkWithExceptionChaining') | |
152 | self.assertIn( | |
153 | 'bt2.error.Error: unexpected error: cannot advance the message iterator', | |
154 | exc[2].message, | |
155 | ) | |
156 | ||
157 | self.assertIsInstance(exc[3], bt2.error._ComponentErrorCause) | |
158 | self.assertEqual(exc[3].component_class_name, 'SinkWithExceptionChaining') | |
159 | self.assertIn('ValueError: oops', exc[3].message) | |
160 | ||
161 | self.assertIsInstance(exc[4], bt2.error._ErrorCause) | |
162 | ||
163 | def _common_cause_tests(self, cause): | |
164 | self.assertIsInstance(cause.module_name, str) | |
165 | self.assertIsInstance(cause.file_name, str) | |
166 | self.assertIsInstance(cause.line_number, int) | |
167 | ||
168 | def test_unknown_error_cause(self): | |
169 | exc = self._run_failing_graph(SourceWithFailingIter, SinkWithExceptionChaining) | |
170 | cause = exc[-1] | |
171 | self.assertIs(type(cause), bt2.error._ErrorCause) | |
172 | self._common_cause_tests(cause) | |
173 | ||
174 | def test_component_error_cause(self): | |
175 | exc = self._run_failing_graph(SourceWithFailingInit, SinkWithExceptionChaining) | |
176 | cause = exc[0] | |
177 | self.assertIs(type(cause), bt2.error._ComponentErrorCause) | |
178 | self._common_cause_tests(cause) | |
179 | ||
180 | self.assertIn('Source is failing', cause.message) | |
181 | self.assertEqual(cause.component_name, 'src') | |
182 | self.assertEqual(cause.component_class_type, bt2.ComponentClassType.SOURCE) | |
183 | self.assertEqual(cause.component_class_name, 'SourceWithFailingInit') | |
184 | self.assertIsNone(cause.plugin_name) | |
185 | ||
186 | def test_component_class_error_cause(self): | |
187 | q = bt2.QueryExecutor() | |
188 | ||
189 | with self.assertRaises(bt2.Error) as ctx: | |
190 | q.query(SinkWithFailingQuery, 'hello') | |
191 | ||
192 | cause = ctx.exception[0] | |
193 | self.assertIs(type(cause), bt2.error._ComponentClassErrorCause) | |
194 | self._common_cause_tests(cause) | |
195 | ||
196 | self.assertIn('Query is failing', cause.message) | |
197 | ||
198 | self.assertEqual(cause.component_class_type, bt2.ComponentClassType.SINK) | |
199 | self.assertEqual(cause.component_class_name, 'SinkWithFailingQuery') | |
200 | self.assertIsNone(cause.plugin_name) | |
201 | ||
202 | def test_message_iterator_error_cause(self): | |
203 | exc = self._run_failing_graph(SourceWithFailingIter, SinkWithExceptionChaining) | |
204 | cause = exc[0] | |
205 | self.assertIs(type(cause), bt2.error._MessageIteratorErrorCause) | |
206 | self._common_cause_tests(cause) | |
207 | ||
208 | self.assertIn('User message iterator is failing', cause.message) | |
209 | self.assertEqual(cause.component_name, 'src') | |
210 | self.assertEqual(cause.component_output_port_name, 'out') | |
211 | self.assertEqual(cause.component_class_type, bt2.ComponentClassType.SOURCE) | |
212 | self.assertEqual(cause.component_class_name, 'SourceWithFailingIter') | |
213 | self.assertIsNone(cause.plugin_name) |