|
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 lmnt import Lmnt, AsyncLmnt, APIResponseValidationError |
25 | 22 | from lmnt._types import Omit |
| 23 | +from lmnt._utils import asyncify |
26 | 24 | from lmnt._models import BaseModel, FinalRequestOptions |
27 | 25 | from lmnt._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError |
28 | 26 | from lmnt._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 |
|
@@ -1593,50 +1593,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
1593 | 1593 |
|
1594 | 1594 | assert response.http_request.headers.get("x-stainless-retry-count") == "42" |
1595 | 1595 |
|
1596 | | - def test_get_platform(self) -> None: |
1597 | | - # A previous implementation of asyncify could leave threads unterminated when |
1598 | | - # used with nest_asyncio. |
1599 | | - # |
1600 | | - # Since nest_asyncio.apply() is global and cannot be un-applied, this |
1601 | | - # test is run in a separate process to avoid affecting other tests. |
1602 | | - test_code = dedent(""" |
1603 | | - import asyncio |
1604 | | - import nest_asyncio |
1605 | | - import threading |
1606 | | -
|
1607 | | - from lmnt._utils import asyncify |
1608 | | - from lmnt._base_client import get_platform |
1609 | | -
|
1610 | | - async def test_main() -> None: |
1611 | | - result = await asyncify(get_platform)() |
1612 | | - print(result) |
1613 | | - for thread in threading.enumerate(): |
1614 | | - print(thread.name) |
1615 | | -
|
1616 | | - nest_asyncio.apply() |
1617 | | - asyncio.run(test_main()) |
1618 | | - """) |
1619 | | - with subprocess.Popen( |
1620 | | - [sys.executable, "-c", test_code], |
1621 | | - text=True, |
1622 | | - ) as process: |
1623 | | - timeout = 10 # seconds |
1624 | | - |
1625 | | - start_time = time.monotonic() |
1626 | | - while True: |
1627 | | - return_code = process.poll() |
1628 | | - if return_code is not None: |
1629 | | - if return_code != 0: |
1630 | | - raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code") |
1631 | | - |
1632 | | - # success |
1633 | | - break |
1634 | | - |
1635 | | - if time.monotonic() - start_time > timeout: |
1636 | | - process.kill() |
1637 | | - raise AssertionError("calling get_platform using asyncify resulted in a hung process") |
1638 | | - |
1639 | | - time.sleep(0.1) |
| 1596 | + async def test_get_platform(self) -> None: |
| 1597 | + platform = await asyncify(get_platform)() |
| 1598 | + assert isinstance(platform, (str, OtherPlatform)) |
1640 | 1599 |
|
1641 | 1600 | async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None: |
1642 | 1601 | # Test that the proxy environment variables are set correctly |
|
0 commit comments