10
10
import tracemalloc
11
11
from typing import Any , Union , cast
12
12
from unittest import mock
13
+ from typing_extensions import Literal
13
14
14
15
import httpx
15
16
import pytest
@@ -824,7 +825,14 @@ def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str
824
825
@pytest .mark .parametrize ("failures_before_success" , [0 , 2 , 4 ])
825
826
@mock .patch ("finch._base_client.BaseClient._calculate_retry_timeout" , _low_retry_timeout )
826
827
@pytest .mark .respx (base_url = base_url )
827
- def test_retries_taken (self , client : Finch , failures_before_success : int , respx_mock : MockRouter ) -> None :
828
+ @pytest .mark .parametrize ("failure_mode" , ["status" , "exception" ])
829
+ def test_retries_taken (
830
+ self ,
831
+ client : Finch ,
832
+ failures_before_success : int ,
833
+ failure_mode : Literal ["status" , "exception" ],
834
+ respx_mock : MockRouter ,
835
+ ) -> None :
828
836
client = client .with_options (max_retries = 4 )
829
837
830
838
nb_retries = 0
@@ -833,6 +841,8 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
833
841
nonlocal nb_retries
834
842
if nb_retries < failures_before_success :
835
843
nb_retries += 1
844
+ if failure_mode == "exception" :
845
+ raise RuntimeError ("oops" )
836
846
return httpx .Response (500 )
837
847
return httpx .Response (200 )
838
848
@@ -1704,8 +1714,13 @@ async def test_parse_retry_after_header(self, remaining_retries: int, retry_afte
1704
1714
@mock .patch ("finch._base_client.BaseClient._calculate_retry_timeout" , _low_retry_timeout )
1705
1715
@pytest .mark .respx (base_url = base_url )
1706
1716
@pytest .mark .asyncio
1717
+ @pytest .mark .parametrize ("failure_mode" , ["status" , "exception" ])
1707
1718
async def test_retries_taken (
1708
- self , async_client : AsyncFinch , failures_before_success : int , respx_mock : MockRouter
1719
+ self ,
1720
+ async_client : AsyncFinch ,
1721
+ failures_before_success : int ,
1722
+ failure_mode : Literal ["status" , "exception" ],
1723
+ respx_mock : MockRouter ,
1709
1724
) -> None :
1710
1725
client = async_client .with_options (max_retries = 4 )
1711
1726
@@ -1715,6 +1730,8 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
1715
1730
nonlocal nb_retries
1716
1731
if nb_retries < failures_before_success :
1717
1732
nb_retries += 1
1733
+ if failure_mode == "exception" :
1734
+ raise RuntimeError ("oops" )
1718
1735
return httpx .Response (500 )
1719
1736
return httpx .Response (200 )
1720
1737
0 commit comments