|
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 cloudflare import Cloudflare, AsyncCloudflare, APIResponseValidationError
|
25 | 22 | from cloudflare._types import Omit
|
| 23 | +from cloudflare._utils import asyncify |
26 | 24 | from cloudflare._models import BaseModel, FinalRequestOptions
|
27 | 25 | from cloudflare._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError
|
28 | 26 | from cloudflare._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 |
|
@@ -1902,50 +1902,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
|
1902 | 1902 |
|
1903 | 1903 | assert response.http_request.headers.get("x-stainless-retry-count") == "42"
|
1904 | 1904 |
|
1905 |
| - def test_get_platform(self) -> None: |
1906 |
| - # A previous implementation of asyncify could leave threads unterminated when |
1907 |
| - # used with nest_asyncio. |
1908 |
| - # |
1909 |
| - # Since nest_asyncio.apply() is global and cannot be un-applied, this |
1910 |
| - # test is run in a separate process to avoid affecting other tests. |
1911 |
| - test_code = dedent(""" |
1912 |
| - import asyncio |
1913 |
| - import nest_asyncio |
1914 |
| - import threading |
1915 |
| -
|
1916 |
| - from cloudflare._utils import asyncify |
1917 |
| - from cloudflare._base_client import get_platform |
1918 |
| -
|
1919 |
| - async def test_main() -> None: |
1920 |
| - result = await asyncify(get_platform)() |
1921 |
| - print(result) |
1922 |
| - for thread in threading.enumerate(): |
1923 |
| - print(thread.name) |
1924 |
| -
|
1925 |
| - nest_asyncio.apply() |
1926 |
| - asyncio.run(test_main()) |
1927 |
| - """) |
1928 |
| - with subprocess.Popen( |
1929 |
| - [sys.executable, "-c", test_code], |
1930 |
| - text=True, |
1931 |
| - ) as process: |
1932 |
| - timeout = 10 # seconds |
1933 |
| - |
1934 |
| - start_time = time.monotonic() |
1935 |
| - while True: |
1936 |
| - return_code = process.poll() |
1937 |
| - if return_code is not None: |
1938 |
| - if return_code != 0: |
1939 |
| - raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code") |
1940 |
| - |
1941 |
| - # success |
1942 |
| - break |
1943 |
| - |
1944 |
| - if time.monotonic() - start_time > timeout: |
1945 |
| - process.kill() |
1946 |
| - raise AssertionError("calling get_platform using asyncify resulted in a hung process") |
1947 |
| - |
1948 |
| - time.sleep(0.1) |
| 1905 | + async def test_get_platform(self) -> None: |
| 1906 | + platform = await asyncify(get_platform)() |
| 1907 | + assert isinstance(platform, (str, OtherPlatform)) |
1949 | 1908 |
|
1950 | 1909 | async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
1951 | 1910 | # Test that the proxy environment variables are set correctly
|
|
0 commit comments