Skip to content

Commit dadb7af

Browse files
committed
add tests
Signed-off-by: emdneto <[email protected]>
1 parent da57d52 commit dadb7af

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

opentelemetry-api/tests/trace/test_globals.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import unittest
216
from unittest.mock import Mock, patch
317

@@ -13,7 +27,12 @@ class SpanTest(trace.NonRecordingSpan):
1327
recorded_status = Status(status_code=StatusCode.UNSET)
1428

1529
def set_status(self, status, description=None):
16-
self.recorded_status = status
30+
if isinstance(status, Status):
31+
self.recorded_status = status
32+
else:
33+
self.recorded_status = Status(
34+
status_code=status, description=description
35+
)
1736

1837
def end(self, end_time=None):
1938
self.has_ended = True
@@ -143,10 +162,32 @@ class TestUseSpanException(Exception):
143162
raise TestUseSpanException("test error")
144163

145164
self.assertEqual(
146-
test_span.recorded_status.status_code, # type: ignore[reportAttributeAccessIssue]
165+
test_span.recorded_status.status_code,
147166
StatusCode.ERROR,
148167
)
149168
self.assertEqual(
150-
test_span.recorded_status.description, # type: ignore[reportAttributeAccessIssue]
169+
test_span.recorded_status.description,
151170
"TestUseSpanException: test error",
152171
)
172+
173+
def test_use_span_base_exceptions(self):
174+
base_exception_classes = [
175+
BaseException,
176+
GeneratorExit,
177+
SystemExit,
178+
KeyboardInterrupt,
179+
]
180+
181+
for exc_cls in base_exception_classes:
182+
with self.subTest(exc=exc_cls.__name__):
183+
test_span = SpanTest(trace.INVALID_SPAN_CONTEXT)
184+
185+
with self.assertRaises(exc_cls):
186+
with trace.use_span(test_span):
187+
raise exc_cls()
188+
189+
self.assertEqual(
190+
test_span.recorded_status.status_code,
191+
StatusCode.UNSET,
192+
)
193+
self.assertIsNone(test_span.recorded_status.description)

0 commit comments

Comments
 (0)