|  | 
| 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 | import urllib.parse | 
| 15 | 13 | from typing import Any, Union, cast | 
| 16 |  | -from textwrap import dedent | 
| 17 | 14 | from unittest import mock | 
| 18 | 15 | from typing_extensions import Literal | 
| 19 | 16 | 
 | 
|  | 
| 24 | 21 | 
 | 
| 25 | 22 | from onebusaway import OnebusawaySDK, AsyncOnebusawaySDK, APIResponseValidationError | 
| 26 | 23 | from onebusaway._types import Omit | 
|  | 24 | +from onebusaway._utils import asyncify | 
| 27 | 25 | from onebusaway._models import BaseModel, FinalRequestOptions | 
| 28 | 26 | from onebusaway._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError | 
| 29 | 27 | from onebusaway._base_client import ( | 
| 30 | 28 |     DEFAULT_TIMEOUT, | 
| 31 | 29 |     HTTPX_DEFAULT_TIMEOUT, | 
| 32 | 30 |     BaseClient, | 
|  | 31 | +    OtherPlatform, | 
| 33 | 32 |     DefaultHttpxClient, | 
| 34 | 33 |     DefaultAsyncHttpxClient, | 
|  | 34 | +    get_platform, | 
| 35 | 35 |     make_request_options, | 
| 36 | 36 | ) | 
| 37 | 37 | 
 | 
| @@ -1628,50 +1628,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: | 
| 1628 | 1628 | 
 | 
| 1629 | 1629 |         assert response.http_request.headers.get("x-stainless-retry-count") == "42" | 
| 1630 | 1630 | 
 | 
| 1631 |  | -    def test_get_platform(self) -> None: | 
| 1632 |  | -        # A previous implementation of asyncify could leave threads unterminated when | 
| 1633 |  | -        # used with nest_asyncio. | 
| 1634 |  | -        # | 
| 1635 |  | -        # Since nest_asyncio.apply() is global and cannot be un-applied, this | 
| 1636 |  | -        # test is run in a separate process to avoid affecting other tests. | 
| 1637 |  | -        test_code = dedent(""" | 
| 1638 |  | -        import asyncio | 
| 1639 |  | -        import nest_asyncio | 
| 1640 |  | -        import threading | 
| 1641 |  | -
 | 
| 1642 |  | -        from onebusaway._utils import asyncify | 
| 1643 |  | -        from onebusaway._base_client import get_platform | 
| 1644 |  | -
 | 
| 1645 |  | -        async def test_main() -> None: | 
| 1646 |  | -            result = await asyncify(get_platform)() | 
| 1647 |  | -            print(result) | 
| 1648 |  | -            for thread in threading.enumerate(): | 
| 1649 |  | -                print(thread.name) | 
| 1650 |  | -
 | 
| 1651 |  | -        nest_asyncio.apply() | 
| 1652 |  | -        asyncio.run(test_main()) | 
| 1653 |  | -        """) | 
| 1654 |  | -        with subprocess.Popen( | 
| 1655 |  | -            [sys.executable, "-c", test_code], | 
| 1656 |  | -            text=True, | 
| 1657 |  | -        ) as process: | 
| 1658 |  | -            timeout = 10  # seconds | 
| 1659 |  | - | 
| 1660 |  | -            start_time = time.monotonic() | 
| 1661 |  | -            while True: | 
| 1662 |  | -                return_code = process.poll() | 
| 1663 |  | -                if return_code is not None: | 
| 1664 |  | -                    if return_code != 0: | 
| 1665 |  | -                        raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code") | 
| 1666 |  | - | 
| 1667 |  | -                    # success | 
| 1668 |  | -                    break | 
| 1669 |  | - | 
| 1670 |  | -                if time.monotonic() - start_time > timeout: | 
| 1671 |  | -                    process.kill() | 
| 1672 |  | -                    raise AssertionError("calling get_platform using asyncify resulted in a hung process") | 
| 1673 |  | - | 
| 1674 |  | -                time.sleep(0.1) | 
|  | 1631 | +    async def test_get_platform(self) -> None: | 
|  | 1632 | +        platform = await asyncify(get_platform)() | 
|  | 1633 | +        assert isinstance(platform, (str, OtherPlatform)) | 
| 1675 | 1634 | 
 | 
| 1676 | 1635 |     async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None: | 
| 1677 | 1636 |         # Test that the proxy environment variables are set correctly | 
|  | 
0 commit comments