|
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 openlayer import Openlayer, AsyncOpenlayer, APIResponseValidationError |
25 | 22 | from openlayer._types import Omit |
| 23 | +from openlayer._utils import asyncify |
26 | 24 | from openlayer._models import BaseModel, FinalRequestOptions |
27 | 25 | from openlayer._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError |
28 | 26 | from openlayer._base_client import ( |
29 | 27 | DEFAULT_TIMEOUT, |
30 | 28 | HTTPX_DEFAULT_TIMEOUT, |
31 | 29 | BaseClient, |
| 30 | + OtherPlatform, |
32 | 31 | DefaultHttpxClient, |
33 | 32 | DefaultAsyncHttpxClient, |
| 33 | + get_platform, |
34 | 34 | make_request_options, |
35 | 35 | ) |
36 | 36 |
|
@@ -1791,50 +1791,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
1791 | 1791 |
|
1792 | 1792 | assert response.http_request.headers.get("x-stainless-retry-count") == "42" |
1793 | 1793 |
|
1794 | | - def test_get_platform(self) -> None: |
1795 | | - # A previous implementation of asyncify could leave threads unterminated when |
1796 | | - # used with nest_asyncio. |
1797 | | - # |
1798 | | - # Since nest_asyncio.apply() is global and cannot be un-applied, this |
1799 | | - # test is run in a separate process to avoid affecting other tests. |
1800 | | - test_code = dedent(""" |
1801 | | - import asyncio |
1802 | | - import nest_asyncio |
1803 | | - import threading |
1804 | | -
|
1805 | | - from openlayer._utils import asyncify |
1806 | | - from openlayer._base_client import get_platform |
1807 | | -
|
1808 | | - async def test_main() -> None: |
1809 | | - result = await asyncify(get_platform)() |
1810 | | - print(result) |
1811 | | - for thread in threading.enumerate(): |
1812 | | - print(thread.name) |
1813 | | -
|
1814 | | - nest_asyncio.apply() |
1815 | | - asyncio.run(test_main()) |
1816 | | - """) |
1817 | | - with subprocess.Popen( |
1818 | | - [sys.executable, "-c", test_code], |
1819 | | - text=True, |
1820 | | - ) as process: |
1821 | | - timeout = 10 # seconds |
1822 | | - |
1823 | | - start_time = time.monotonic() |
1824 | | - while True: |
1825 | | - return_code = process.poll() |
1826 | | - if return_code is not None: |
1827 | | - if return_code != 0: |
1828 | | - raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code") |
1829 | | - |
1830 | | - # success |
1831 | | - break |
1832 | | - |
1833 | | - if time.monotonic() - start_time > timeout: |
1834 | | - process.kill() |
1835 | | - raise AssertionError("calling get_platform using asyncify resulted in a hung process") |
1836 | | - |
1837 | | - time.sleep(0.1) |
| 1794 | + async def test_get_platform(self) -> None: |
| 1795 | + platform = await asyncify(get_platform)() |
| 1796 | + assert isinstance(platform, (str, OtherPlatform)) |
1838 | 1797 |
|
1839 | 1798 | async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None: |
1840 | 1799 | # Test that the proxy environment variables are set correctly |
|
0 commit comments