From 4d19d5f68d0752c22ed4fbd5d822c8fccf59e23f Mon Sep 17 00:00:00 2001 From: EMRG Evolution Date: Tue, 11 Aug 2026 00:40:48 +0800 Subject: [PATCH] emrg: speed up daemon spawn-wait unit tests by mocking asyncio.sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two timeout-path tests (test_raises_on_timeout, test_start_daemon_throttles_after_max_attempts) waited on the real 15x0.3s spawn-ready deadline loop (4.5s per start_daemon call), costing ~18s of pure wall-clock time per suite run. The assertions only cover call counts, exception types and throttle state — real timing adds nothing. Patching emrg.client.daemon_manager.asyncio.sleep to an AsyncMock collapses the wait while preserving full discriminative power. Suite runtime for these tests: ~18s -> 0.25s. --- tests/test_daemon_manager.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_daemon_manager.py b/tests/test_daemon_manager.py index fef538c..1f75e28 100644 --- a/tests/test_daemon_manager.py +++ b/tests/test_daemon_manager.py @@ -99,11 +99,13 @@ async def _run(): assert args[0].endswith("python") or "python" in args[0] assert args[1:] == ("-m", "emrg.server") + @patch("emrg.client.daemon_manager.asyncio.sleep", new_callable=AsyncMock) @patch("emrg.client.daemon_manager.is_running", return_value=False) @patch("emrg.client.daemon_manager.cleanup_server") @patch("emrg.client.daemon_manager.asyncio.create_subprocess_exec", new_callable=AsyncMock) - def test_raises_on_timeout(self, mock_spawn, mock_cleanup, mock_is_running): + def test_raises_on_timeout(self, mock_spawn, mock_cleanup, mock_is_running, + mock_sleep): proc = MagicMock(pid=1234) mock_spawn.return_value = proc @@ -247,12 +249,14 @@ async def _run(): # _MAX_SPAWN_ATTEMPTS 次,超限抛节流错误;成功连接后归零。 class TestSpawnThrottle: + @patch("emrg.client.daemon_manager.asyncio.sleep", new_callable=AsyncMock) @patch("emrg.client.daemon_manager.is_running", return_value=False) @patch("emrg.client.daemon_manager.cleanup_server") @patch("emrg.client.daemon_manager.asyncio.create_subprocess_exec", new_callable=AsyncMock) def test_start_daemon_throttles_after_max_attempts(self, mock_spawn, - mock_cleanup, mock_is_running): + mock_cleanup, mock_is_running, + mock_sleep): proc = MagicMock(pid=1234) mock_spawn.return_value = proc