Skip to content

Commit

Permalink
chore(tests): add more retry tests (#1806)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Oct 22, 2024
1 parent a366120 commit 6634f52
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tracemalloc
from typing import Any, Union, cast
from unittest import mock
from typing_extensions import Literal

import httpx
import pytest
Expand Down Expand Up @@ -764,7 +765,14 @@ def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> Non
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
@mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
def test_retries_taken(self, client: OpenAI, failures_before_success: int, respx_mock: MockRouter) -> None:
@pytest.mark.parametrize("failure_mode", ["status", "exception"])
def test_retries_taken(
self,
client: OpenAI,
failures_before_success: int,
failure_mode: Literal["status", "exception"],
respx_mock: MockRouter,
) -> None:
client = client.with_options(max_retries=4)

nb_retries = 0
Expand All @@ -773,6 +781,8 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
nonlocal nb_retries
if nb_retries < failures_before_success:
nb_retries += 1
if failure_mode == "exception":
raise RuntimeError("oops")
return httpx.Response(500)
return httpx.Response(200)

Expand Down Expand Up @@ -1623,8 +1633,13 @@ async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter)
@mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
@pytest.mark.asyncio
@pytest.mark.parametrize("failure_mode", ["status", "exception"])
async def test_retries_taken(
self, async_client: AsyncOpenAI, failures_before_success: int, respx_mock: MockRouter
self,
async_client: AsyncOpenAI,
failures_before_success: int,
failure_mode: Literal["status", "exception"],
respx_mock: MockRouter,
) -> None:
client = async_client.with_options(max_retries=4)

Expand All @@ -1634,6 +1649,8 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
nonlocal nb_retries
if nb_retries < failures_before_success:
nb_retries += 1
if failure_mode == "exception":
raise RuntimeError("oops")
return httpx.Response(500)
return httpx.Response(200)

Expand Down

0 comments on commit 6634f52

Please sign in to comment.