@@ -749,6 +749,62 @@ def _prepared_split_request(hook_ctx, request):
749749 tempdir .cleanup .assert_called_once ()
750750
751751
752+ @pytest .mark .asyncio
753+ async def test_unit_do_request_async_cancellation_logs_cancelled_cleanup (
754+ caplog : pytest .LogCaptureFixture ,
755+ ):
756+ caplog .set_level (logging .DEBUG , logger = "test" )
757+ operation_id = "cancelled-cleanup"
758+
759+ class PreparedRequestHook :
760+ def before_request (self , hook_ctx , request ):
761+ del hook_ctx , request
762+ return httpx .Request (
763+ "GET" ,
764+ "http://localhost:8888/general/docs" ,
765+ headers = {"operation_id" : operation_id },
766+ extensions = {"split_pdf_operation_id" : operation_id },
767+ )
768+
769+ class CancelledCleanupHook :
770+ async def after_error_async (self , hook_ctx , response , error ):
771+ del hook_ctx , response , error
772+ raise asyncio .CancelledError ()
773+
774+ def after_error (self , hook_ctx , response , error ): # pragma: no cover - dispatch guard
775+ raise AssertionError ("async hook should be awaited" )
776+
777+ hooks = SDKHooks ()
778+ hooks .before_request_hooks = [PreparedRequestHook ()] # type: ignore[list-item]
779+ hooks .after_error_hooks = [CancelledCleanupHook ()] # type: ignore[list-item]
780+
781+ client = _BlockingAsyncClient ()
782+ config = SDKConfiguration (
783+ client = None ,
784+ client_supplied = False ,
785+ async_client = client , # type: ignore[arg-type]
786+ async_client_supplied = True ,
787+ debug_logger = logging .getLogger ("test" ),
788+ )
789+ config .__dict__ ["_hooks" ] = hooks
790+ sdk = BaseSDK (config )
791+ task = asyncio .create_task (
792+ sdk .do_request_async (
793+ _make_sdk_hook_context (),
794+ httpx .Request ("POST" , "http://localhost:8888/general/v0/general" ),
795+ error_status_codes = [],
796+ )
797+ )
798+
799+ await client .started .wait ()
800+ task .cancel ()
801+
802+ with pytest .raises (asyncio .CancelledError ):
803+ await task
804+
805+ assert "Cancellation cleanup cancelled" in caplog .text
806+
807+
752808def test_before_request_returns_dummy_with_timeout_and_operation_id ():
753809 hook , mock_hook_ctx , result = _make_hook_with_split_request ()
754810 operation_id = result .headers ["operation_id" ]
0 commit comments