|
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 llama_api_client import LlamaAPIClient, AsyncLlamaAPIClient, APIResponseValidationError |
25 | 22 | from llama_api_client._types import Omit |
| 23 | +from llama_api_client._utils import asyncify |
26 | 24 | from llama_api_client._models import BaseModel, FinalRequestOptions |
27 | 25 | from llama_api_client._exceptions import ( |
28 | 26 | APIStatusError, |
|
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 |
|
@@ -1728,50 +1728,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
1728 | 1728 |
|
1729 | 1729 | assert response.http_request.headers.get("x-stainless-retry-count") == "42" |
1730 | 1730 |
|
1731 | | - def test_get_platform(self) -> None: |
1732 | | - # A previous implementation of asyncify could leave threads unterminated when |
1733 | | - # used with nest_asyncio. |
1734 | | - # |
1735 | | - # Since nest_asyncio.apply() is global and cannot be un-applied, this |
1736 | | - # test is run in a separate process to avoid affecting other tests. |
1737 | | - test_code = dedent(""" |
1738 | | - import asyncio |
1739 | | - import nest_asyncio |
1740 | | - import threading |
1741 | | -
|
1742 | | - from llama_api_client._utils import asyncify |
1743 | | - from llama_api_client._base_client import get_platform |
1744 | | -
|
1745 | | - async def test_main() -> None: |
1746 | | - result = await asyncify(get_platform)() |
1747 | | - print(result) |
1748 | | - for thread in threading.enumerate(): |
1749 | | - print(thread.name) |
1750 | | -
|
1751 | | - nest_asyncio.apply() |
1752 | | - asyncio.run(test_main()) |
1753 | | - """) |
1754 | | - with subprocess.Popen( |
1755 | | - [sys.executable, "-c", test_code], |
1756 | | - text=True, |
1757 | | - ) as process: |
1758 | | - timeout = 10 # seconds |
1759 | | - |
1760 | | - start_time = time.monotonic() |
1761 | | - while True: |
1762 | | - return_code = process.poll() |
1763 | | - if return_code is not None: |
1764 | | - if return_code != 0: |
1765 | | - raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code") |
1766 | | - |
1767 | | - # success |
1768 | | - break |
1769 | | - |
1770 | | - if time.monotonic() - start_time > timeout: |
1771 | | - process.kill() |
1772 | | - raise AssertionError("calling get_platform using asyncify resulted in a hung process") |
1773 | | - |
1774 | | - time.sleep(0.1) |
| 1731 | + async def test_get_platform(self) -> None: |
| 1732 | + platform = await asyncify(get_platform)() |
| 1733 | + assert isinstance(platform, (str, OtherPlatform)) |
1775 | 1734 |
|
1776 | 1735 | async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None: |
1777 | 1736 | # Test that the proxy environment variables are set correctly |
|
0 commit comments