test: fix flaky test_exceed_rate_limit

filtermail rate limiter is using leaky bucket
algorithm (GCRA).
Exceeting the limit requires sending
at least max_user_send_per_minute
messages to exhaust allowed burst,
and then sending messages faster
than the leak rate.
As we don't know how fast is the network
between the server and test runner,
try to send 3 times max_user_send_per_minute
messages to ensure the test does not
fail randomly.
This commit is contained in:
link2xt
2026-01-24 02:57:50 +00:00
committed by l
parent ab3492d9a1
commit 9c7508cc33

View File

@@ -190,22 +190,17 @@ def test_exceed_rate_limit(cmsetup, gencreds, maildata, chatmail_config):
"encrypted.eml", from_addr=user1.addr, to_addr=user2.addr "encrypted.eml", from_addr=user1.addr, to_addr=user2.addr
).as_string() ).as_string()
timestamps = [] for i in range(chatmail_config.max_user_send_per_minute * 3):
i = 0
while len(timestamps) <= chatmail_config.max_user_send_per_minute * 1.7:
print("Sending mail", str(i)) print("Sending mail", str(i))
i += 1
try: try:
user1.smtp.sendmail(user1.addr, [user2.addr], mail) user1.smtp.sendmail(user1.addr, [user2.addr], mail)
timestamps.append(time.time())
except smtplib.SMTPException as e: except smtplib.SMTPException as e:
if len(timestamps) < chatmail_config.max_user_send_per_minute: if i < chatmail_config.max_user_send_per_minute:
pytest.fail(f"rate limit was exceeded too early with msg {i}") pytest.fail(f"rate limit was exceeded too early with msg {i}")
outcome = e.recipients[user2.addr] outcome = e.recipients[user2.addr]
assert outcome[0] == 450 assert outcome[0] == 450
assert b"4.7.1: Too much mail from" in outcome[1] assert b"4.7.1: Too much mail from" in outcome[1]
return return
timestamps[:] = [ts for ts in timestamps if ts >= (time.time() - 60)]
pytest.fail("Rate limit was not exceeded") pytest.fail("Rate limit was not exceeded")