From 4378944459e9b1fed4d9076e4e5f2b3eafea0c62 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Thu, 25 Jun 2026 11:05:29 +0800 Subject: [PATCH] test: speed up test suite --- nanobot/channels/email.py | 2 ++ tests/channels/test_feishu_lazy_import.py | 13 +++++++++++++ tests/channels/test_websocket_channel.py | 2 +- tests/channels/test_websocket_http_routes.py | 5 ++--- tests/channels/test_websocket_media_route.py | 2 +- tests/providers/test_litellm_kwargs.py | 2 +- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/nanobot/channels/email.py b/nanobot/channels/email.py index 7d1abf24..a7476451 100644 --- a/nanobot/channels/email.py +++ b/nanobot/channels/email.py @@ -199,6 +199,8 @@ class EmailChannel(BaseChannel): except Exception: self.logger.exception("Polling error") + if not self._running: + break await asyncio.sleep(poll_seconds) async def stop(self) -> None: diff --git a/tests/channels/test_feishu_lazy_import.py b/tests/channels/test_feishu_lazy_import.py index d43c39eb..05915c27 100644 --- a/tests/channels/test_feishu_lazy_import.py +++ b/tests/channels/test_feishu_lazy_import.py @@ -35,7 +35,20 @@ def test_feishu_channel_constructor_does_not_import_lark_oapi(): def test_lark_runtime_thread_import_clears_sdk_import_loop(): out = _run_import_probe( "import asyncio\n" + "import sys\n" + "import tempfile\n" + "from pathlib import Path\n" "from nanobot.channels.feishu import _load_lark_runtime\n" + "root = Path(tempfile.mkdtemp())\n" + "pkg = root / 'lark_oapi'\n" + "(pkg / 'ws').mkdir(parents=True)\n" + "(pkg / 'core').mkdir(parents=True)\n" + "(pkg / '__init__.py').write_text('class LogLevel:\\n INFO = 20\\n')\n" + "(pkg / 'ws' / '__init__.py').write_text('')\n" + "(pkg / 'ws' / 'client.py').write_text('import asyncio\\nloop = asyncio.new_event_loop()\\n')\n" + "(pkg / 'core' / '__init__.py').write_text('')\n" + "(pkg / 'core' / 'const.py').write_text(\"FEISHU_DOMAIN = 'feishu'\\nLARK_DOMAIN = 'lark'\\n\")\n" + "sys.path.insert(0, str(root))\n" "async def main():\n" " await asyncio.to_thread(_load_lark_runtime)\n" " import lark_oapi.ws.client as ws\n" diff --git a/tests/channels/test_websocket_channel.py b/tests/channels/test_websocket_channel.py index 9ca0a334..2d8e92a2 100644 --- a/tests/channels/test_websocket_channel.py +++ b/tests/channels/test_websocket_channel.py @@ -136,7 +136,7 @@ def isolate_webui_workspace_state(tmp_path, monkeypatch) -> None: async def _http_get(url: str, headers: dict[str, str] | None = None) -> httpx.Response: """Run GET in a thread to avoid blocking the asyncio loop shared with websockets.""" return await asyncio.to_thread( - functools.partial(httpx.get, url, headers=headers or {}, timeout=5.0) + functools.partial(httpx.get, url, headers=headers or {}, timeout=5.0, trust_env=False) ) diff --git a/tests/channels/test_websocket_http_routes.py b/tests/channels/test_websocket_http_routes.py index 16674092..d767b6f1 100644 --- a/tests/channels/test_websocket_http_routes.py +++ b/tests/channels/test_websocket_http_routes.py @@ -109,7 +109,7 @@ async def _http_get( url: str, headers: dict[str, str] | None = None ) -> httpx.Response: return await asyncio.to_thread( - functools.partial(httpx.get, url, headers=headers or {}, timeout=5.0) + functools.partial(httpx.get, url, headers=headers or {}, timeout=5.0, trust_env=False) ) @@ -506,12 +506,11 @@ async def test_cli_apps_catalog_does_not_block_other_webui_http_routes( token = boot.json()["token"] auth = {"Authorization": f"Bearer {token}"} - started = time.perf_counter() catalog_task = asyncio.create_task( _http_get("http://127.0.0.1:29935/api/settings/cli-apps", headers=auth) ) assert await asyncio.wait_for(entered.wait(), 2.0) - assert time.perf_counter() - started < 1.0 + assert not catalog_task.done() workspaces_started = time.perf_counter() workspaces = await _http_get("http://127.0.0.1:29935/api/workspaces", headers=auth) diff --git a/tests/channels/test_websocket_media_route.py b/tests/channels/test_websocket_media_route.py index d539dd91..0b8b7cd6 100644 --- a/tests/channels/test_websocket_media_route.py +++ b/tests/channels/test_websocket_media_route.py @@ -90,7 +90,7 @@ async def _http_get( url: str, headers: dict[str, str] | None = None ) -> httpx.Response: return await asyncio.to_thread( - functools.partial(httpx.get, url, headers=headers or {}, timeout=5.0) + functools.partial(httpx.get, url, headers=headers or {}, timeout=5.0, trust_env=False) ) diff --git a/tests/providers/test_litellm_kwargs.py b/tests/providers/test_litellm_kwargs.py index 872e1b51..61577c40 100644 --- a/tests/providers/test_litellm_kwargs.py +++ b/tests/providers/test_litellm_kwargs.py @@ -1228,7 +1228,7 @@ def test_openai_compat_defaults_missing_tool_arguments_to_empty_object() -> None @pytest.mark.asyncio async def test_openai_compat_stream_watchdog_returns_error_on_stall(monkeypatch) -> None: - monkeypatch.setenv("NANOBOT_STREAM_IDLE_TIMEOUT_S", "0") + monkeypatch.setenv("NANOBOT_STREAM_IDLE_TIMEOUT_S", "0.01") mock_create = AsyncMock(return_value=_StalledStream()) spec = find_by_name("openai")