Skip to content

Commit 76c8585

Browse files
committed
test: add concurrency safety tests for markdown renderers
1 parent 4bf5ec0 commit 76c8585

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

tests/test_formatters.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from concurrent.futures import ThreadPoolExecutor
2+
13
import pytest
24
from flask import Markup
35
from notifications_utils.formatters import (
@@ -12,6 +14,7 @@
1214
nl2li,
1315
normalise_whitespace,
1416
notify_email_markdown,
17+
notify_email_preheader_markdown,
1518
notify_letter_preview_markdown,
1619
notify_plain_text_email_markdown,
1720
remove_language_divs,
@@ -32,6 +35,33 @@
3235
from notifications_utils.template import HTMLEmailTemplate, PlainTextEmailTemplate, SMSMessageTemplate, SMSPreviewTemplate
3336

3437

38+
@pytest.mark.parametrize(
39+
"markdown_function",
40+
[
41+
notify_email_markdown,
42+
notify_plain_text_email_markdown,
43+
notify_email_preheader_markdown,
44+
notify_letter_preview_markdown,
45+
],
46+
)
47+
def test_markdown_renderers_are_safe_under_concurrency(markdown_function):
48+
samples = [
49+
"heartbeat ok",
50+
"# hi\n\nplain text",
51+
"1. a\n2. b\n",
52+
"no markdown here",
53+
"___\nnext",
54+
]
55+
56+
def render(i):
57+
return markdown_function(samples[i % len(samples)])
58+
59+
with ThreadPoolExecutor(max_workers=16) as executor:
60+
results = list(executor.map(render, range(3000)))
61+
62+
assert len(results) == 3000
63+
64+
3565
@pytest.mark.parametrize(
3666
"url",
3767
[

0 commit comments

Comments
 (0)