Skip to content

Commit b839e4b

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 25c0448 commit b839e4b

File tree

3 files changed

+6
-51
lines changed

3 files changed

+6
-51
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ dev-dependencies = [
5454
"dirty-equals>=0.6.0",
5555
"importlib-metadata>=6.7.0",
5656
"rich>=13.7.1",
57-
"nest_asyncio==1.6.0",
5857
"pytest-xdist>=3.6.1",
5958
]
6059

requirements-dev.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ multidict==6.4.4
7575
mypy==1.14.1
7676
mypy-extensions==1.0.0
7777
# via mypy
78-
nest-asyncio==1.6.0
7978
nodeenv==1.8.0
8079
# via pyright
8180
nox==2023.4.22

tests/test_client.py

Lines changed: 6 additions & 49 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, cast
15-
from textwrap import dedent
1613
from unittest import mock
1714
from typing_extensions import Literal
1815

@@ -23,6 +20,7 @@
2320

2421
from gradient import Gradient, AsyncGradient, APIResponseValidationError
2522
from gradient._types import Omit
23+
from gradient._utils import asyncify
2624
from gradient._models import BaseModel, FinalRequestOptions
2725
from gradient._streaming import Stream, AsyncStream
2826
from gradient._exceptions import (
@@ -34,8 +32,10 @@
3432
DEFAULT_TIMEOUT,
3533
HTTPX_DEFAULT_TIMEOUT,
3634
BaseClient,
35+
OtherPlatform,
3736
DefaultHttpxClient,
3837
DefaultAsyncHttpxClient,
38+
get_platform,
3939
make_request_options,
4040
)
4141

@@ -2058,52 +2058,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
20582058

20592059
assert response.http_request.headers.get("x-stainless-retry-count") == "42"
20602060

2061-
def test_get_platform(self) -> None:
2062-
# A previous implementation of asyncify could leave threads unterminated when
2063-
# used with nest_asyncio.
2064-
#
2065-
# Since nest_asyncio.apply() is global and cannot be un-applied, this
2066-
# test is run in a separate process to avoid affecting other tests.
2067-
test_code = dedent(
2068-
"""
2069-
import asyncio
2070-
import nest_asyncio
2071-
import threading
2072-
2073-
from gradient._utils import asyncify
2074-
from gradient._base_client import get_platform
2075-
2076-
async def test_main() -> None:
2077-
result = await asyncify(get_platform)()
2078-
print(result)
2079-
for thread in threading.enumerate():
2080-
print(thread.name)
2081-
2082-
nest_asyncio.apply()
2083-
asyncio.run(test_main())
2084-
"""
2085-
)
2086-
with subprocess.Popen(
2087-
[sys.executable, "-c", test_code],
2088-
text=True,
2089-
) as process:
2090-
timeout = 10 # seconds
2091-
2092-
start_time = time.monotonic()
2093-
while True:
2094-
return_code = process.poll()
2095-
if return_code is not None:
2096-
if return_code != 0:
2097-
raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code")
2098-
2099-
# success
2100-
break
2101-
2102-
if time.monotonic() - start_time > timeout:
2103-
process.kill()
2104-
raise AssertionError("calling get_platform using asyncify resulted in a hung process")
2105-
2106-
time.sleep(0.1)
2061+
async def test_get_platform(self) -> None:
2062+
platform = await asyncify(get_platform)()
2063+
assert isinstance(platform, (str, OtherPlatform))
21072064

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

0 commit comments

Comments
 (0)