Skip to content

Commit f1ddbb2

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 8a26e46 commit f1ddbb2

1 file changed

Lines changed: 6 additions & 47 deletions

File tree

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, 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 writerai import Writer, AsyncWriter, APIResponseValidationError
2522
from writerai._types import Omit
23+
from writerai._utils import asyncify
2624
from writerai._models import BaseModel, FinalRequestOptions
2725
from writerai._streaming import Stream, AsyncStream
2826
from writerai._exceptions import WriterError, APIStatusError, APITimeoutError, APIResponseValidationError
2927
from writerai._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

@@ -1659,50 +1659,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
16591659

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

1662-
def test_get_platform(self) -> None:
1663-
# A previous implementation of asyncify could leave threads unterminated when
1664-
# used with nest_asyncio.
1665-
#
1666-
# Since nest_asyncio.apply() is global and cannot be un-applied, this
1667-
# test is run in a separate process to avoid affecting other tests.
1668-
test_code = dedent("""
1669-
import asyncio
1670-
import nest_asyncio
1671-
import threading
1672-
1673-
from writerai._utils import asyncify
1674-
from writerai._base_client import get_platform
1675-
1676-
async def test_main() -> None:
1677-
result = await asyncify(get_platform)()
1678-
print(result)
1679-
for thread in threading.enumerate():
1680-
print(thread.name)
1681-
1682-
nest_asyncio.apply()
1683-
asyncio.run(test_main())
1684-
""")
1685-
with subprocess.Popen(
1686-
[sys.executable, "-c", test_code],
1687-
text=True,
1688-
) as process:
1689-
timeout = 10 # seconds
1690-
1691-
start_time = time.monotonic()
1692-
while True:
1693-
return_code = process.poll()
1694-
if return_code is not None:
1695-
if return_code != 0:
1696-
raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code")
1697-
1698-
# success
1699-
break
1700-
1701-
if time.monotonic() - start_time > timeout:
1702-
process.kill()
1703-
raise AssertionError("calling get_platform using asyncify resulted in a hung process")
1704-
1705-
time.sleep(0.1)
1662+
async def test_get_platform(self) -> None:
1663+
platform = await asyncify(get_platform)()
1664+
assert isinstance(platform, (str, OtherPlatform))
17061665

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

0 commit comments

Comments
 (0)