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.
This commit is contained in:
anonsaber 2026-09-30 12:39:21 +08:00
parent 0890eb18f0
commit 7d6ea013b4
1 changed files with 10 additions and 9 deletions

View File

@ -1706,7 +1706,11 @@ mod real_redis_ttl_tests {
let mut conn = state.connection.clone(); let mut conn = state.connection.clone();
// `set_ai_consent` writes an absolute expiry value plus a relative EX. // `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(); state.set_ai_consent(chat_id, 3600).await.unwrap();
let consent_key = format!("consent:ai:{chat_id}"); let consent_key = format!("consent:ai:{chat_id}");
let pttl: i64 = redis::cmd("PTTL") 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 // 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 // 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. // returns before XADD/XACK and no stream or group needs to exist.
let stream_name = "ttlcheck:stream";
let message = StreamMessage { let message = StreamMessage {
id: format!("ttlcheck:{run}:msg"), id: format!("ttlcheck:{run}:msg"),
payload: "payload".into(), 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 assert!(!state
.retry_or_dlq( .retry_or_dlq(stream_name, "ttlcheck:dlq", "ttlcheck:group", &message, 99)
"ttlcheck:stream",
"ttlcheck:dlq",
"ttlcheck:group",
&message,
99
)
.await .await
.unwrap()); .unwrap());
let pttl: i64 = redis::cmd("PTTL") let pttl: i64 = redis::cmd("PTTL")