Skip to content

Commit 046ed94

Browse files
committed
add test for TTL
1 parent 9419ad0 commit 046ed94

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

tests/server/message_queue/test_redis.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async def redis_dispatch():
2323
with patch("mcp.server.message_queue.redis.redis", fake_redis.FakeRedis):
2424
from mcp.server.message_queue.redis import RedisMessageDispatch
2525

26-
dispatch = RedisMessageDispatch()
26+
dispatch = RedisMessageDispatch(session_ttl=5) # Shorter TTL for testing
2727
try:
2828
yield dispatch
2929
finally:
@@ -46,6 +46,39 @@ async def test_session_exists(redis_dispatch):
4646
assert not await redis_dispatch.session_exists(session_id)
4747

4848

49+
@pytest.mark.anyio
50+
async def test_session_ttl(redis_dispatch):
51+
"""Test that session has proper TTL set."""
52+
session_id = uuid4()
53+
54+
async with redis_dispatch.subscribe(session_id, AsyncMock()):
55+
session_key = redis_dispatch._session_key(session_id)
56+
ttl = await redis_dispatch._redis.ttl(session_key) # type: ignore
57+
assert ttl > 0
58+
assert ttl <= redis_dispatch._session_ttl
59+
60+
61+
@pytest.mark.anyio
62+
async def test_session_heartbeat(redis_dispatch):
63+
"""Test that session heartbeat refreshes TTL."""
64+
session_id = uuid4()
65+
66+
async with redis_dispatch.subscribe(session_id, AsyncMock()):
67+
session_key = redis_dispatch._session_key(session_id)
68+
69+
# Initial TTL
70+
initial_ttl = await redis_dispatch._redis.ttl(session_key) # type: ignore
71+
assert initial_ttl > 0
72+
73+
# Wait for heartbeat to run
74+
await anyio.sleep(redis_dispatch._session_ttl / 2 + 0.5)
75+
76+
# TTL should be refreshed
77+
refreshed_ttl = await redis_dispatch._redis.ttl(session_key) # type: ignore
78+
assert refreshed_ttl > 0
79+
assert refreshed_ttl <= redis_dispatch._session_ttl
80+
81+
4982
@pytest.mark.anyio
5083
async def test_subscribe_unsubscribe(redis_dispatch):
5184
"""Test subscribing and unsubscribing from a session."""

0 commit comments

Comments
 (0)