Skip to content

Commit e129541

Browse files
committed
[asgi] add test for #1883
1 parent cfdd4ae commit e129541

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

+30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# pylint: disable=too-many-lines
1616

17+
import asyncio
1718
import sys
1819
import unittest
1920
from timeit import default_timer
@@ -796,5 +797,34 @@ async def wrapped_app(scope, receive, send):
796797
)
797798

798799

800+
class TestAsgiApplicationRaisingError(AsgiTestBase):
801+
802+
def tearDown(self):
803+
pass
804+
805+
@mock.patch(
806+
"opentelemetry.instrumentation.asgi.collect_custom_request_headers_attributes",
807+
side_effect=ValueError("whatever"),
808+
)
809+
def test_asgi_issue_1883(self, mock_collect_custom_request_headers_attributes):
810+
"""
811+
Test that exception UnboundLocalError local variable 'start' referenced before assignment is not raises
812+
See https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1883
813+
"""
814+
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
815+
self.seed_app(app)
816+
self.send_default_request()
817+
try:
818+
asyncio.get_event_loop().run_until_complete(
819+
self.communicator.stop()
820+
)
821+
except ValueError as exc_info:
822+
self.assertEqual(exc_info.args[0], "whatever")
823+
except Exception as exc_info:
824+
self.fail("expecting ValueError('whatever'), received instead: " + str(exc_info))
825+
else:
826+
self.fail("expecting ValueError('whatever')")
827+
828+
799829
if __name__ == "__main__":
800830
unittest.main()

0 commit comments

Comments
 (0)