|
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 imagekit import ImageKit, AsyncImageKit, APIResponseValidationError |
25 | 22 | from imagekit._types import Omit |
| 23 | +from imagekit._utils import asyncify |
26 | 24 | from imagekit._models import BaseModel, FinalRequestOptions |
27 | 25 | from imagekit._exceptions import ImageKitError, APIStatusError, APITimeoutError, APIResponseValidationError |
28 | 26 | from imagekit._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 |
|
@@ -1808,50 +1808,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
1808 | 1808 |
|
1809 | 1809 | assert response.http_request.headers.get("x-stainless-retry-count") == "42" |
1810 | 1810 |
|
1811 | | - def test_get_platform(self) -> None: |
1812 | | - # A previous implementation of asyncify could leave threads unterminated when |
1813 | | - # used with nest_asyncio. |
1814 | | - # |
1815 | | - # Since nest_asyncio.apply() is global and cannot be un-applied, this |
1816 | | - # test is run in a separate process to avoid affecting other tests. |
1817 | | - test_code = dedent(""" |
1818 | | - import asyncio |
1819 | | - import nest_asyncio |
1820 | | - import threading |
1821 | | -
|
1822 | | - from imagekit._utils import asyncify |
1823 | | - from imagekit._base_client import get_platform |
1824 | | -
|
1825 | | - async def test_main() -> None: |
1826 | | - result = await asyncify(get_platform)() |
1827 | | - print(result) |
1828 | | - for thread in threading.enumerate(): |
1829 | | - print(thread.name) |
1830 | | -
|
1831 | | - nest_asyncio.apply() |
1832 | | - asyncio.run(test_main()) |
1833 | | - """) |
1834 | | - with subprocess.Popen( |
1835 | | - [sys.executable, "-c", test_code], |
1836 | | - text=True, |
1837 | | - ) as process: |
1838 | | - timeout = 10 # seconds |
1839 | | - |
1840 | | - start_time = time.monotonic() |
1841 | | - while True: |
1842 | | - return_code = process.poll() |
1843 | | - if return_code is not None: |
1844 | | - if return_code != 0: |
1845 | | - raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code") |
1846 | | - |
1847 | | - # success |
1848 | | - break |
1849 | | - |
1850 | | - if time.monotonic() - start_time > timeout: |
1851 | | - process.kill() |
1852 | | - raise AssertionError("calling get_platform using asyncify resulted in a hung process") |
1853 | | - |
1854 | | - time.sleep(0.1) |
| 1811 | + async def test_get_platform(self) -> None: |
| 1812 | + platform = await asyncify(get_platform)() |
| 1813 | + assert isinstance(platform, (str, OtherPlatform)) |
1855 | 1814 |
|
1856 | 1815 | async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None: |
1857 | 1816 | # Test that the proxy environment variables are set correctly |
|
0 commit comments