Skip to content

Commit 6ae8d62

Browse files
committed
[asgi] add test for #1883
1 parent 7603a1f commit 6ae8d62

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

+34
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,38 @@ async def wrapped_app(scope, receive, send):
796797
)
797798

798799

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

0 commit comments

Comments
 (0)