feat: add lock heartbeat to prevent mid-execution expiry (#64)

This commit is contained in:
d3vyce
2026-06-27 13:56:50 +02:00
committed by GitHub
parent e45aa8f977
commit 2c2a3c89f1
7 changed files with 286 additions and 3 deletions
+27 -1
View File
@@ -7,7 +7,7 @@ In CI a Redis service is started before the test step.
import pytest
from taskiq_deduplication import DuplicateTaskError, RedisDeduplicationMiddleware
from taskiq_deduplication.utils import RELEASE_LUA_SCRIPT
from taskiq_deduplication.utils import REFRESH_LUA_SCRIPT, RELEASE_LUA_SCRIPT
@pytest.fixture
@@ -15,6 +15,7 @@ def mw(real_redis):
middleware = RedisDeduplicationMiddleware(redis_url="redis://localhost:6379/15")
middleware._redis = real_redis
middleware._release_script = real_redis.register_script(RELEASE_LUA_SCRIPT)
middleware._refresh_script = real_redis.register_script(REFRESH_LUA_SCRIPT)
return middleware
@@ -68,6 +69,31 @@ async def test_ttl_is_applied(mw, real_redis, make_message):
assert 0 < ttl <= mw.default_ttl
@pytest.mark.integration
async def test_heartbeat_keeps_long_running_lock_alive(
mw, real_redis, make_message, make_result
):
import asyncio
from taskiq_deduplication.middleware import DEDUP_TTL_LABEL
# 1s TTL with a sub-second heartbeat: without refresh the lock would expire.
mw.heartbeat_interval = 0.2
msg = make_message(labels={DEDUP_TTL_LABEL: 1})
await mw.pre_send(msg)
key = mw._build_deduplication_key(msg)
await mw.pre_execute(msg)
try:
# outlive the original TTL; the heartbeat should keep the lock present
await asyncio.sleep(1.5)
assert await real_redis.exists(key)
with pytest.raises(DuplicateTaskError):
await mw.pre_send(make_message(labels={DEDUP_TTL_LABEL: 1}))
finally:
await mw.post_execute(msg, make_result())
assert not await real_redis.exists(key)
@pytest.mark.integration
async def test_explicit_key_end_to_end(mw, real_redis, make_message, make_result):
from taskiq_deduplication.middleware import DEDUP_EXPLICIT_KEY_LABEL