Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincarrogan committed Dec 17, 2024
1 parent 58fff96 commit a463ae4
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions mail/tests/test_celery_tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import concurrent.futures
import email.mime.multipart
import time
from datetime import datetime, timezone
from email.mime.multipart import MIMEMultipart
from smtplib import SMTPException
Expand Down Expand Up @@ -294,3 +296,50 @@ def test_sends_email_max_retry_failures(self, exception_class, mock_get_smtp_con
)
self.assertEqual(mock_conn.quit.call_count, MAX_RETRIES + 1)
mock_cache.lock.assert_called_with("global_send_email_lock", timeout=600)

@mock.patch("mail.servers.get_smtp_connection")
def test_locking(self, mock_get_smtp_connection):
results = []

def _sleepy(message):
call = {}
call["start"] = {
"message": message,
"time": time.monotonic(),
}
time.sleep(1)
call["end"] = {
"message": message,
"time": time.monotonic(),
}
results.append(call)

mock_conn = mock_get_smtp_connection()
mock_conn.send_message.side_effect = _sleepy

with concurrent.futures.ThreadPoolExecutor() as executor:
message_1 = {
"From": "[email protected]",
"To": "[email protected]",
}
future_1 = executor.submit(send_email_task.apply, args=[message_1])

message_2 = {
"From": "[email protected]",
"To": "[email protected]",
}
future_2 = executor.submit(send_email_task.apply, args=[message_2])

future_1.result()
future_2.result()

first_call, second_call = results

assert first_call["start"]["message"] == {"From": "[email protected]", "To": "[email protected]"}
assert first_call["end"]["message"] == {"From": "[email protected]", "To": "[email protected]"}

assert second_call["start"]["message"] == {"From": "[email protected]", "To": "[email protected]"}
assert second_call["end"]["message"] == {"From": "[email protected]", "To": "[email protected]"}

assert second_call["start"]["time"] > first_call["end"]["time"]
assert second_call["start"]["time"] > first_call["start"]["time"] + 1

0 comments on commit a463ae4

Please sign in to comment.