From c1e40c9fd31e59f23663b8d14703ecb4845e6280 Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Tue, 17 Nov 2026 23:18:41 +0800 Subject: [PATCH 1/2] test(api): await Redis listener delivery in subscription tests --- .../redis/test_pubsub_channel.py | 40 ++++++------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/api/tests/unit_tests/libs/broadcast_channel/redis/test_pubsub_channel.py b/api/tests/unit_tests/libs/broadcast_channel/redis/test_pubsub_channel.py index 2d75525bea7234..6097665be589c7 100644 --- a/api/tests/unit_tests/libs/broadcast_channel/redis/test_pubsub_channel.py +++ b/api/tests/unit_tests/libs/broadcast_channel/redis/test_pubsub_channel.py @@ -13,7 +13,7 @@ import threading import time from collections.abc import Generator -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock import pytest @@ -434,24 +434,15 @@ def test_enqueue_message_with_full_queue(self, started_subscription: _RedisSubsc # ==================== Listener Thread Tests ==================== - @patch("time.sleep", side_effect=lambda x: None, autospec=True) # Speed up test - def test_listener_thread_normal_operation( - self, mock_sleep, subscription: _RedisSubscription, mock_pubsub: MagicMock - ): - """Test listener thread normal operation.""" + def test_listener_thread_normal_operation(self, subscription: _RedisSubscription, mock_pubsub: MagicMock) -> None: + """Given a Redis message, receive waits for the listener and returns its payload.""" # Mock message from Redis mock_message = {"type": "message", "channel": "test-topic", "data": b"test payload"} mock_pubsub.get_message.return_value = mock_message - # Start listener - subscription._start_if_needed() - - # Wait a bit for processing - time.sleep(0.1) - - # Verify message was processed - assert not subscription._queue.empty() - assert subscription._queue.get_nowait() == b"test payload" + # receive() waits for the listener to deliver the Redis payload instead of + # assuming the background thread has run as soon as it starts. + assert subscription.receive(timeout=1) == b"test payload" def test_listener_thread_ignores_subscribe_messages(self, subscription: _RedisSubscription, mock_pubsub: MagicMock): """Test that listener thread ignores subscribe/unsubscribe messages.""" @@ -863,24 +854,17 @@ def test_enqueue_message_with_full_queue(self, started_sharded_subscription: _Re # ==================== Listener Thread Tests ==================== - @patch("time.sleep", side_effect=lambda x: None, autospec=True) # Speed up test def test_listener_thread_normal_operation( - self, mock_sleep, sharded_subscription: _RedisShardedSubscription, mock_pubsub: MagicMock - ): - """Test sharded listener thread normal operation.""" + self, sharded_subscription: _RedisShardedSubscription, mock_pubsub: MagicMock + ) -> None: + """Given a sharded Redis message, receive waits for the listener and returns its payload.""" # Mock sharded message from Redis mock_message = {"type": "smessage", "channel": "test-sharded-topic", "data": b"test sharded payload"} mock_pubsub.get_sharded_message.return_value = mock_message - # Start listener - sharded_subscription._start_if_needed() - - # Wait a bit for processing - time.sleep(0.1) - - # Verify message was processed - assert not sharded_subscription._queue.empty() - assert sharded_subscription._queue.get_nowait() == b"test sharded payload" + # receive() waits for the listener to deliver the Redis payload instead of + # assuming the background thread has run as soon as it starts. + assert sharded_subscription.receive(timeout=1) == b"test sharded payload" def test_get_message_uses_target_node_for_cluster_client( self, mock_pubsub: MagicMock, monkeypatch: pytest.MonkeyPatch From 8880273048c21fb259fc58fc627e7e995509d317 Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Tue, 17 Nov 2026 23:22:47 +0800 Subject: [PATCH 2/2] test(api): clear fake vector factories after each test --- .../core/rag/datasource/vdb/test_vector_factory.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/api/tests/unit_tests/core/rag/datasource/vdb/test_vector_factory.py b/api/tests/unit_tests/core/rag/datasource/vdb/test_vector_factory.py index 5b84bd5fb49869..eef13070089450 100644 --- a/api/tests/unit_tests/core/rag/datasource/vdb/test_vector_factory.py +++ b/api/tests/unit_tests/core/rag/datasource/vdb/test_vector_factory.py @@ -3,6 +3,7 @@ import logging import sys import types +from collections.abc import Iterator from datetime import UTC, datetime from types import SimpleNamespace from unittest.mock import MagicMock, patch @@ -45,7 +46,8 @@ def _register_fake_factory_module(monkeypatch: pytest.MonkeyPatch, module_path: @pytest.fixture -def vector_factory_module(): +def vector_factory_module() -> Iterator[types.ModuleType]: + """Reload the factory module and leave no cached test factories after the case.""" import importlib from core.rag.datasource.vdb import vector_backend_registry as reg @@ -53,7 +55,11 @@ def vector_factory_module(): reg.clear_vector_factory_cache() import core.rag.datasource.vdb.vector_factory as module - return importlib.reload(module) + try: + yield importlib.reload(module) + finally: + # Restoring sys.modules does not evict fake classes cached by the registry. + reg.clear_vector_factory_cache() def test_gen_index_struct_dict(vector_factory_module):