From 7d6ea013b435ece1b63a79bcbe8937d80b2e8b19 Mon Sep 17 00:00:00 2001 From: anonsaber Date: Wed, 30 Sep 2026 12:39:21 +0800 Subject: [PATCH] fix: make the real-Redis TTL test safe to point at a shared database Two latent bugs surfaced while preparing to run these against Upstash: 1. The retry key was mis-constructed. builds its counter as , so the test had to read . It read , a key the call never writes, so the assertion would have failed on the first real run (PTTL of a missing key is -2, and the test's own guard rejects it). Derive the key from the same stream name and message id the call receives instead of re-spelling it by hand. 2. The consent probe used the process id as a Telegram chat_id. A private Telegram chat_id is a plain positive integer, so a small pid can legitimately be a real user. If the test is pointed at the live database it would have overwritten that user's AI consent flag with a 1h expiry. Anchor at -4e18 + pid instead: real Telegram ids stay under ~1e11 in magnitude, so no live flag can be shadowed. All test keys remain prefixed and are deleted at the end of each test. --- src/state.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/state.rs b/src/state.rs index 7fda45c..51c8a3a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1706,7 +1706,11 @@ mod real_redis_ttl_tests { let mut conn = state.connection.clone(); // `set_ai_consent` writes an absolute expiry value plus a relative EX. - let chat_id: i64 = run.split(':').next().unwrap_or("0").parse().unwrap_or(0); + // Telegram ids stay under ~1e11 in magnitude, so anchor far outside that + // range: this test may target a shared database and must never shadow a + // real chat's consent flag. + let pid: i64 = std::process::id().into(); + let chat_id: i64 = -(4_000_000_000_000_000_000) + pid; state.set_ai_consent(chat_id, 3600).await.unwrap(); let consent_key = format!("consent:ai:{chat_id}"); let pttl: i64 = redis::cmd("PTTL") @@ -1723,19 +1727,16 @@ mod real_redis_ttl_tests { // site, so it is asserted through `retry_or_dlq` rather than read back // from a constant. max_attempts=99 keeps n below the cap, so the script // returns before XADD/XACK and no stream or group needs to exist. + let stream_name = "ttlcheck:stream"; let message = StreamMessage { id: format!("ttlcheck:{run}:msg"), payload: "payload".into(), }; - let retry_key = format!("retry:ttlcheck:{run}:msg"); + // Mirror `retry_or_dlq`'s own key format exactly, or the PTTL would be + // read from a key the call never wrote. + let retry_key = format!("retry:{stream_name}:{}", message.id); assert!(!state - .retry_or_dlq( - "ttlcheck:stream", - "ttlcheck:dlq", - "ttlcheck:group", - &message, - 99 - ) + .retry_or_dlq(stream_name, "ttlcheck:dlq", "ttlcheck:group", &message, 99) .await .unwrap()); let pttl: i64 = redis::cmd("PTTL")