Browse Source

Enable retry for SMTPDataError

pull/563/head
Pēteris Caune 3 years ago
parent
commit
1b0d0eac6a
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
2 changed files with 12 additions and 3 deletions
  1. +2
    -2
      hc/lib/emails.py
  2. +10
    -1
      hc/lib/tests/test_emails.py

+ 2
- 2
hc/lib/emails.py View File

@ -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


+ 10
- 1
hc/lib/tests/test_emails.py View File

@ -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)

Loading…
Cancel
Save