mirror of
https://github.com/chatmail/relay.git
synced 2026-05-10 16:04:37 +00:00
test: Add retries to test_rewrite_subject() (#670)
- test_rewrite_subject() is prone to failure when it checks for the delivered message, because fetch_all_messages() raises "ValueError: no messages in imap folder". The check has the potential to happen before the server has had a chance to deliver the message to the user's inbox. - Added a function try_n_times() that attempts to call a function the specified number of times, with a 1-second sleep between calls. The call is retried until it doesn't raise an exception. The last call is made without a 'try' block, so that the final exception passes through to the caller if it does not return. - Wrapped call to fetch_all_messages() in try_n_times(), with 5 attempts specified. This should usually allow enough time for the message to get moved from the postfix queue to the user's inbox.
This commit is contained in:
@@ -2,6 +2,7 @@ import datetime
|
||||
import smtplib
|
||||
import socket
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -146,6 +147,16 @@ def test_reject_missing_dkim(cmsetup, maildata, from_addr):
|
||||
s.sendmail(from_addr=from_addr, to_addrs=recipient.addr, msg=msg)
|
||||
|
||||
|
||||
def try_n_times(n, f):
|
||||
for _ in range(n - 1):
|
||||
try:
|
||||
return f()
|
||||
except Exception:
|
||||
time.sleep(1)
|
||||
|
||||
return f()
|
||||
|
||||
|
||||
def test_rewrite_subject(cmsetup, maildata):
|
||||
"""Test that subject gets replaced with [...]."""
|
||||
user1, user2 = cmsetup.gen_users(2)
|
||||
@@ -158,7 +169,8 @@ def test_rewrite_subject(cmsetup, maildata):
|
||||
).as_string()
|
||||
user1.smtp.sendmail(from_addr=user1.addr, to_addrs=[user2.addr], msg=sent_msg)
|
||||
|
||||
messages = user2.imap.fetch_all_messages()
|
||||
# The message may need some time to get delivered by postfix.
|
||||
messages = try_n_times(5, user2.imap.fetch_all_messages)
|
||||
assert len(messages) == 1
|
||||
rcvd_msg = messages[0]
|
||||
assert "Subject: [...]" not in sent_msg
|
||||
|
||||
Reference in New Issue
Block a user