Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
send_email: Fix connection lifetime for long-lived connections.
Alex Vandiverad3404e3bfb7
diff --git a/zerver/tests/test_send_email.py b/zerver/tests/test_send_email.pyindex d317ca75f5a..08302a447c4 100644--- a/zerver/tests/test_send_email.py+++ b/zerver/tests/test_send_email.py@@ -227,6 +227,27 @@ class TestSendEmail(ZulipTestCase):self.assertEqual(close_call.call_count, 0)self.assertEqual(_open_call.call_count, 2)self.assertEqual(old_connection_mock.noop.call_count, 0)+ close_call.reset_mock()+ _open_call.reset_mock()++ # Connections older than 24 hours are properly+ # recycled: timedelta.seconds wraps at 24 hours, but+ # total_seconds() does not.+ with time_machine.travel(+ timezone_now() + timedelta(hours=25),+ tick=False,+ ):+ old_connection_mock = mock.MagicMock(spec=SMTP)+ backend.connection = old_connection_mock+ with mock.patch(+ "django.core.mail.backends.smtp.EmailBackend.open", side_effect=[False, True]+ ):+ initialize_connection(backend)+ self.assertEqual(backend.opened_at, timezone_now())++ # Noop should not have been attempted on the old connection.+ self.assertEqual(old_connection_mock.noop.call_count, 0)+ self.assertEqual(_open_call.call_count, 2)def test_send_email_exceptions(self) -> None:hamlet = self.example_user("hamlet")diff --git a/zproject/email_backends.py b/zproject/email_backends.pyindex bba21fe8e64..daaec6bf3cd 100644--- a/zproject/email_backends.py+++ b/zproject/email_backends.py@@ -130,7 +130,7 @@ class PersistentSMTPEmailBackend(EmailBackend):return Truestatus = None- time_elapsed = (timezone_now() - self.opened_at).seconds / 60+ time_elapsed = (timezone_now() - self.opened_at).total_seconds() / 60if (settings.EMAIL_MAX_CONNECTION_LIFETIME_IN_MINUTES is Noneor time_elapsed <= settings.EMAIL_MAX_CONNECTION_LIFETIME_IN_MINUTES