forked from census-instrumentation/opencensus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_stack_trace.py
More file actions
217 lines (176 loc) · 7.21 KB
/
Copy pathtest_stack_trace.py
File metadata and controls
217 lines (176 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright 2017, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import unittest
import mock
from opencensus.trace import stack_trace as stack_trace_module
class TestStackFrame(unittest.TestCase):
def test_constructor(self):
func_name = 'func name'
original_func_name = 'original func name'
file_name = 'file name'
line_num = 10
col_num = 1
load_module = 'module'
build_id = 100
source_version = 'source version'
stack_frame = stack_trace_module.StackFrame(
func_name,
original_func_name,
file_name,
line_num,
col_num,
load_module,
build_id,
source_version)
self.assertEqual(stack_frame.func_name, func_name)
self.assertEqual(stack_frame.original_func_name, original_func_name)
self.assertEqual(stack_frame.file_name, file_name)
self.assertEqual(stack_frame.line_num, line_num)
self.assertEqual(stack_frame.col_num, col_num)
self.assertEqual(stack_frame.load_module, load_module)
self.assertEqual(stack_frame.build_id, build_id)
self.assertEqual(stack_frame.source_version, source_version)
def test_format_stack_frame_json(self):
func_name = 'func name'
original_func_name = 'original func name'
file_name = 'file name'
line_num = 10
col_num = 1
load_module = 'module'
build_id = 100
source_version = 'source version'
stack_frame = stack_trace_module.StackFrame(
func_name,
original_func_name,
file_name,
line_num,
col_num,
load_module,
build_id,
source_version)
def mock_get_truncatable_str(str):
return str
patch = mock.patch(
'opencensus.trace.stack_trace._get_truncatable_str',
mock_get_truncatable_str)
expected_stack_frame_json = {
'function_name': func_name,
'original_function_name': original_func_name,
'file_name': file_name,
'line_number': line_num,
'column_number': col_num,
'load_module': {
'module': load_module,
'build_id': build_id
},
'source_version': source_version
}
with patch:
stack_frame_json = stack_frame.format_stack_frame_json()
self.assertEqual(stack_frame_json, expected_stack_frame_json)
class TestStackTrace(unittest.TestCase):
def test_constructor_default(self):
hash_id = 1100
patch = mock.patch(
'opencensus.trace.stack_trace.generate_hash_id',
return_value=hash_id)
with patch:
stack_trace = stack_trace_module.StackTrace()
self.assertEqual(stack_trace.stack_frames, [])
self.assertEqual(stack_trace.stack_trace_hash_id, hash_id)
def test_constructor_explicit(self):
stack_frames = [mock.Mock()]
hash_id = 1100
stack_trace = stack_trace_module.StackTrace(stack_frames, hash_id)
self.assertEqual(stack_trace.stack_frames, stack_frames)
self.assertEqual(stack_trace.stack_trace_hash_id, hash_id)
def test_constructor_max_frames(self):
stack_frames = [mock.Mock()] * (stack_trace_module.MAX_FRAMES + 1)
stack_trace = stack_trace_module.StackTrace(stack_frames, 100)
self.assertEqual(stack_trace.dropped_frames_count, 1)
self.assertEqual(
len(stack_trace.stack_frames),
stack_trace_module.MAX_FRAMES
)
def test_add_stack_frame(self):
stack_trace = stack_trace_module.StackTrace()
stack_frame = mock.Mock()
stack_frame_json = 'test stack frame'
stack_frame.format_stack_frame_json.return_value = stack_frame_json
stack_trace.add_stack_frame(stack_frame)
self.assertEqual(stack_trace.stack_frames, [stack_frame_json])
def test_format_stack_trace_json_with_stack_frame(self):
hash_id = 1100
stack_frame = [mock.Mock()]
stack_trace = stack_trace_module.StackTrace(
stack_frames=stack_frame,
stack_trace_hash_id=hash_id)
stack_trace_json = stack_trace.format_stack_trace_json()
expected_stack_trace_json = {
'stack_frames': {
'frame': stack_frame,
'dropped_frames_count': 0
},
'stack_trace_hash_id': hash_id
}
self.assertEqual(expected_stack_trace_json, stack_trace_json)
def test_format_stack_trace_json_without_stack_frame(self):
hash_id = 1100
stack_trace = stack_trace_module.StackTrace(
stack_trace_hash_id=hash_id)
stack_trace_json = stack_trace.format_stack_trace_json()
expected_stack_trace_json = {
'stack_trace_hash_id': hash_id
}
self.assertEqual(expected_stack_trace_json, stack_trace_json)
def test_create_from_traceback(self):
try:
raise AssertionError('something went wrong')
except AssertionError:
_, _, tb = sys.exc_info()
stack_trace = stack_trace_module.StackTrace.from_traceback(tb)
self.assertIsNotNone(stack_trace)
self.assertIsNotNone(stack_trace.stack_trace_hash_id)
self.assertEqual(len(stack_trace.stack_frames), 1)
stack_frame = stack_trace.stack_frames[0]
self.assertEqual(stack_frame['file_name']['value'], __file__)
self.assertEqual(
stack_frame['function_name']['value'], 'test_create_from_traceback'
)
self.assertEqual(
stack_frame['load_module']['module']['value'], __file__
)
self.assertEqual(
stack_frame['original_function_name']['value'],
'test_create_from_traceback'
)
self.assertIsNotNone(stack_frame['source_version']['value'])
self.assertIsNotNone(stack_frame['load_module']['build_id']['value'])
def test_dropped_frames(self):
"""Make sure the limit of 128 frames is enforced"""
def recur(max_depth):
def _recur_helper(depth):
if depth >= max_depth:
raise AssertionError('reached max depth')
_recur_helper(depth+1)
_recur_helper(0)
try:
recur(stack_trace_module.MAX_FRAMES)
except AssertionError:
_, _, tb = sys.exc_info()
stack_trace = stack_trace_module.StackTrace.from_traceback(tb)
# total frames should be MAX_FRAMES + 3 (1 for test function,
# 1 for recursion start, one for exception, MAX_FRAMES in helper)
self.assertEqual(stack_trace.dropped_frames_count, 3)