|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""Tests for the Redis Cluster storage adapter without a live cluster.""" |
| 7 | + |
| 8 | +from unittest.mock import MagicMock |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +from trpc_agent_sdk.storage import RedisClusterStorage |
| 12 | +from trpc_agent_sdk.storage import RedisCommand |
| 13 | +from trpc_agent_sdk.storage import RedisCondition |
| 14 | + |
| 15 | + |
| 16 | +class _FakeClusterClient: |
| 17 | + """Minimal async cluster client used to exercise the storage adapter.""" |
| 18 | + |
| 19 | + def __init__(self) -> None: |
| 20 | + self.scan_calls: list[tuple[str, int]] = [] |
| 21 | + self.closed = False |
| 22 | + |
| 23 | + def scan_iter(self, match: str, count: int): |
| 24 | + self.scan_calls.append((match, count)) |
| 25 | + |
| 26 | + async def _iterate(): |
| 27 | + # Duplicate keys are possible while a real cluster is resharding. |
| 28 | + yield b"memory:app/user:one" |
| 29 | + yield b"memory:app/user:two" |
| 30 | + yield b"memory:app/user:one" |
| 31 | + |
| 32 | + return _iterate() |
| 33 | + |
| 34 | + async def type(self, key: str): |
| 35 | + return "list" |
| 36 | + |
| 37 | + async def lrange(self, key: str, start: int, end: int): |
| 38 | + return [f'{key}-event'] |
| 39 | + |
| 40 | + async def hgetall(self, key: str): |
| 41 | + return {b"field": b"value"} |
| 42 | + |
| 43 | + async def close(self): |
| 44 | + self.closed = True |
| 45 | + |
| 46 | + |
| 47 | +class TestRedisClusterStorage: |
| 48 | + async def test_query_scans_all_cluster_keys_and_deduplicates(self): |
| 49 | + storage = RedisClusterStorage(redis_url="redis://seed:6379/0", is_async=True) |
| 50 | + client = _FakeClusterClient() |
| 51 | + storage._redis_client = client |
| 52 | + |
| 53 | + async with storage.create_db_session() as conn: |
| 54 | + results = await storage.query(conn, "memory:app/user:*", RedisCondition(limit=-1)) |
| 55 | + |
| 56 | + assert [key for key, _ in results] == ["memory:app/user:one", "memory:app/user:two"] |
| 57 | + assert client.scan_calls == [("memory:app/user:*", storage._SCAN_COUNT)] |
| 58 | + |
| 59 | + async def test_keys_command_uses_scan_not_node_local_keys(self): |
| 60 | + storage = RedisClusterStorage(redis_url="redis://seed:6379/0", is_async=True) |
| 61 | + client = _FakeClusterClient() |
| 62 | + |
| 63 | + keys = await storage.execute_command(client, RedisCommand(method="keys", args=("session:app:*", ))) |
| 64 | + |
| 65 | + assert keys == ["memory:app/user:one", "memory:app/user:two"] |
| 66 | + assert client.scan_calls == [("session:app:*", storage._SCAN_COUNT)] |
| 67 | + |
| 68 | + async def test_hgetall_normalizes_raw_redis_bytes(self): |
| 69 | + storage = RedisClusterStorage(redis_url="redis://seed:6379/0", is_async=True, decode_responses=False) |
| 70 | + client = _FakeClusterClient() |
| 71 | + |
| 72 | + result = await storage.execute_command(client, RedisCommand(method="hgetall", args=("state", ))) |
| 73 | + |
| 74 | + assert result == {"field": "value"} |
| 75 | + |
| 76 | + async def test_async_client_is_created_from_seed_url(self): |
| 77 | + storage = RedisClusterStorage(redis_url="redis://seed:6379/0", is_async=True, max_connections=20) |
| 78 | + client = MagicMock() |
| 79 | + with patch("trpc_agent_sdk.storage._redis_cluster.AsyncRedisCluster") as client_cls: |
| 80 | + client_cls.from_url.return_value = client |
| 81 | + await storage.create_redis_engine() |
| 82 | + |
| 83 | + client_cls.from_url.assert_called_once_with( |
| 84 | + "redis://seed:6379/0", decode_responses=True, max_connections=20) |
| 85 | + assert storage._redis_client is client |
| 86 | + |
| 87 | + async def test_close_releases_cluster_client(self): |
| 88 | + storage = RedisClusterStorage(redis_url="redis://seed:6379/0", is_async=True) |
| 89 | + client = _FakeClusterClient() |
| 90 | + storage._redis_client = client |
| 91 | + |
| 92 | + await storage.close() |
| 93 | + |
| 94 | + assert client.closed is True |
| 95 | + assert storage._redis_client is None |
0 commit comments