Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01f03e0

Browse files
chore(tests): simplify get_platform test
`nest_asyncio` is archived and broken on some platforms so it's not worth keeping in our test suite.
1 parent 7b60787 commit 01f03e0

File tree

2 files changed

+7
-49
lines changed

2 files changed

+7
-49
lines changed

requirements-dev.lock

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ filelock==3.12.4
7070
frozenlist==1.7.0
7171
# via aiohttp
7272
# via aiosignal
73-
griffe==1.14.0
73+
griffe==1.13.0
7474
h11==0.16.0
7575
# via httpcore
7676
httpcore==1.0.9
@@ -108,7 +108,6 @@ multidict==6.5.0
108108
mypy==1.14.1
109109
mypy-extensions==1.0.0
110110
# via mypy
111-
nest-asyncio==1.6.0
112111
nodeenv==1.8.0
113112
# via pyright
114113
nox==2023.4.22

tests/test_client.py

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
import os
77
import sys
88
import json
9-
import time
109
import asyncio
1110
import inspect
12-
import subprocess
1311
import tracemalloc
1412
from typing import Any, Union, Protocol, cast
15-
from textwrap import dedent
1613
from unittest import mock
1714
from typing_extensions import Literal
1815

@@ -23,15 +20,18 @@
2320

2421
from openai import OpenAI, AsyncOpenAI, APIResponseValidationError
2522
from openai._types import Omit
23+
from openai._utils import asyncify
2624
from openai._models import BaseModel, FinalRequestOptions
2725
from openai._streaming import Stream, AsyncStream
2826
from openai._exceptions import OpenAIError, APIStatusError, APITimeoutError, APIResponseValidationError
2927
from openai._base_client import (
3028
DEFAULT_TIMEOUT,
3129
HTTPX_DEFAULT_TIMEOUT,
3230
BaseClient,
31+
OtherPlatform,
3332
DefaultHttpxClient,
3433
DefaultAsyncHttpxClient,
34+
get_platform,
3535
make_request_options,
3636
)
3737

@@ -1857,50 +1857,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
18571857
assert response.retries_taken == failures_before_success
18581858
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
18591859

1860-
def test_get_platform(self) -> None:
1861-
# A previous implementation of asyncify could leave threads unterminated when
1862-
# used with nest_asyncio.
1863-
#
1864-
# Since nest_asyncio.apply() is global and cannot be un-applied, this
1865-
# test is run in a separate process to avoid affecting other tests.
1866-
test_code = dedent("""
1867-
import asyncio
1868-
import nest_asyncio
1869-
import threading
1870-
1871-
from openai._utils import asyncify
1872-
from openai._base_client import get_platform
1873-
1874-
async def test_main() -> None:
1875-
result = await asyncify(get_platform)()
1876-
print(result)
1877-
for thread in threading.enumerate():
1878-
print(thread.name)
1879-
1880-
nest_asyncio.apply()
1881-
asyncio.run(test_main())
1882-
""")
1883-
with subprocess.Popen(
1884-
[sys.executable, "-c", test_code],
1885-
text=True,
1886-
) as process:
1887-
timeout = 10 # seconds
1888-
1889-
start_time = time.monotonic()
1890-
while True:
1891-
return_code = process.poll()
1892-
if return_code is not None:
1893-
if return_code != 0:
1894-
raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code")
1895-
1896-
# success
1897-
break
1898-
1899-
if time.monotonic() - start_time > timeout:
1900-
process.kill()
1901-
raise AssertionError("calling get_platform using asyncify resulted in a hung process")
1902-
1903-
time.sleep(0.1)
1860+
async def test_get_platform(self) -> None:
1861+
platform = await asyncify(get_platform)()
1862+
assert isinstance(platform, (str, OtherPlatform))
19041863

19051864
async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None:
19061865
# Test that the proxy environment variables are set correctly

0 commit comments

Comments
 (0)