|
6 | 6 | import os |
7 | 7 | import sys |
8 | 8 | import json |
9 | | -import time |
10 | 9 | import asyncio |
11 | 10 | import inspect |
12 | | -import subprocess |
13 | 11 | import tracemalloc |
14 | 12 | from typing import Any, Union, cast |
15 | | -from textwrap import dedent |
16 | 13 | from unittest import mock |
17 | 14 | from typing_extensions import Literal |
18 | 15 |
|
|
23 | 20 |
|
24 | 21 | from gradient import Gradient, AsyncGradient, APIResponseValidationError |
25 | 22 | from gradient._types import Omit |
| 23 | +from gradient._utils import asyncify |
26 | 24 | from gradient._models import BaseModel, FinalRequestOptions |
27 | 25 | from gradient._streaming import Stream, AsyncStream |
28 | 26 | from gradient._exceptions import ( |
|
34 | 32 | DEFAULT_TIMEOUT, |
35 | 33 | HTTPX_DEFAULT_TIMEOUT, |
36 | 34 | BaseClient, |
| 35 | + OtherPlatform, |
37 | 36 | DefaultHttpxClient, |
38 | 37 | DefaultAsyncHttpxClient, |
| 38 | + get_platform, |
39 | 39 | make_request_options, |
40 | 40 | ) |
41 | 41 |
|
@@ -2058,52 +2058,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
2058 | 2058 |
|
2059 | 2059 | assert response.http_request.headers.get("x-stainless-retry-count") == "42" |
2060 | 2060 |
|
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)) |
2107 | 2064 |
|
2108 | 2065 | async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None: |
2109 | 2066 | # Test that the proxy environment variables are set correctly |
|
0 commit comments