|
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 opencode_ai import Opencode, AsyncOpencode, APIResponseValidationError |
25 | 22 | from opencode_ai._types import Omit |
| 23 | +from opencode_ai._utils import asyncify |
26 | 24 | from opencode_ai._models import BaseModel, FinalRequestOptions |
27 | 25 | from opencode_ai._streaming import Stream, AsyncStream |
28 | 26 | from opencode_ai._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError |
29 | 27 | from opencode_ai._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 |
|
@@ -1566,50 +1566,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
1566 | 1566 |
|
1567 | 1567 | assert response.http_request.headers.get("x-stainless-retry-count") == "42" |
1568 | 1568 |
|
1569 | | - def test_get_platform(self) -> None: |
1570 | | - # A previous implementation of asyncify could leave threads unterminated when |
1571 | | - # used with nest_asyncio. |
1572 | | - # |
1573 | | - # Since nest_asyncio.apply() is global and cannot be un-applied, this |
1574 | | - # test is run in a separate process to avoid affecting other tests. |
1575 | | - test_code = dedent(""" |
1576 | | - import asyncio |
1577 | | - import nest_asyncio |
1578 | | - import threading |
1579 | | -
|
1580 | | - from opencode_ai._utils import asyncify |
1581 | | - from opencode_ai._base_client import get_platform |
1582 | | -
|
1583 | | - async def test_main() -> None: |
1584 | | - result = await asyncify(get_platform)() |
1585 | | - print(result) |
1586 | | - for thread in threading.enumerate(): |
1587 | | - print(thread.name) |
1588 | | -
|
1589 | | - nest_asyncio.apply() |
1590 | | - asyncio.run(test_main()) |
1591 | | - """) |
1592 | | - with subprocess.Popen( |
1593 | | - [sys.executable, "-c", test_code], |
1594 | | - text=True, |
1595 | | - ) as process: |
1596 | | - timeout = 10 # seconds |
1597 | | - |
1598 | | - start_time = time.monotonic() |
1599 | | - while True: |
1600 | | - return_code = process.poll() |
1601 | | - if return_code is not None: |
1602 | | - if return_code != 0: |
1603 | | - raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code") |
1604 | | - |
1605 | | - # success |
1606 | | - break |
1607 | | - |
1608 | | - if time.monotonic() - start_time > timeout: |
1609 | | - process.kill() |
1610 | | - raise AssertionError("calling get_platform using asyncify resulted in a hung process") |
1611 | | - |
1612 | | - time.sleep(0.1) |
| 1569 | + async def test_get_platform(self) -> None: |
| 1570 | + platform = await asyncify(get_platform)() |
| 1571 | + assert isinstance(platform, (str, OtherPlatform)) |
1613 | 1572 |
|
1614 | 1573 | async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None: |
1615 | 1574 | # Test that the proxy environment variables are set correctly |
|
0 commit comments