From 1b0d0eac6a16ba0ce01662afa3c552addb308462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 23 Sep 2021 15:20:42 +0300 Subject: [PATCH] Enable retry for SMTPDataError --- hc/lib/emails.py | 4 ++-- hc/lib/tests/test_emails.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/hc/lib/emails.py b/hc/lib/emails.py index fcbd0da2..04c44be1 100644 --- a/hc/lib/emails.py +++ b/hc/lib/emails.py @@ -1,4 +1,4 @@ -import smtplib +from smtplib import SMTPServerDisconnected, SMTPDataError from threading import Thread import time @@ -22,7 +22,7 @@ class EmailThread(Thread): self.message.send() # No exception--great! Return from the retry loop return - except smtplib.SMTPServerDisconnected as e: + except (SMTPServerDisconnected, SMTPDataError) as e: if attempt + 1 == self.MAX_TRIES: # This was the last attempt and it failed: # re-raise the exception diff --git a/hc/lib/tests/test_emails.py b/hc/lib/tests/test_emails.py index a0c5387d..cffc8ba1 100644 --- a/hc/lib/tests/test_emails.py +++ b/hc/lib/tests/test_emails.py @@ -1,4 +1,4 @@ -from smtplib import SMTPServerDisconnected +from smtplib import SMTPServerDisconnected, SMTPDataError from unittest.mock import Mock, patch from django.test import TestCase @@ -25,3 +25,12 @@ class EmailsTestCase(TestCase): t.run() self.assertEqual(mock_msg.send.call_count, 3) + + def test_it_retries_smtp_data_error(self, mock_time): + mock_msg = Mock() + mock_msg.send = Mock(side_effect=[SMTPDataError(454, "hello"), None]) + + t = EmailThread(mock_msg) + t.run() + + self.assertEqual(mock_msg.send.call_count, 2)