From 4f5719f590ead38617c19c0474e75193f0dddd66 Mon Sep 17 00:00:00 2001 From: cliffmccarthy <16453869+cliffmccarthy@users.noreply.github.com> Date: Tue, 14 Oct 2025 14:18:15 -0500 Subject: [PATCH] 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. --- cmdeploy/src/cmdeploy/tests/online/test_1_basic.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cmdeploy/src/cmdeploy/tests/online/test_1_basic.py b/cmdeploy/src/cmdeploy/tests/online/test_1_basic.py index 990c4f96..468fb893 100644 --- a/cmdeploy/src/cmdeploy/tests/online/test_1_basic.py +++ b/cmdeploy/src/cmdeploy/tests/online/test_1_basic.py @@ -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