| 1 |
import re |
| 2 |
import unittest |
| 3 |
|
| 4 |
from test.support import threading_helper |
| 5 |
from test.support.threading_helper import run_concurrently |
| 6 |
|
| 7 |
|
| 8 |
NTHREADS = 10 |
| 9 |
|
| 10 |
|
| 11 |
@threading_helper.requires_working_threading() |
| 12 |
class TestRe(unittest.TestCase): |
| 13 |
def test_pattern_sub(self): |
| 14 |
"""Pattern substitution should work across threads""" |
| 15 |
pattern = re.compile(r"\w+@\w+\.\w+") |
| 16 |
text = "e-mail: [email protected] or [email protected]. " * 5 |
| 17 |
results = [] |
| 18 |
|
| 19 |
def worker(): |
| 20 |
substituted = pattern.sub("(redacted)", text) |
| 21 |
results.append(substituted.count("(redacted)")) |
| 22 |
|
| 23 |
run_concurrently(worker_func=worker, nthreads=NTHREADS) |
| 24 |
self.assertEqual(results, [2 * 5] * NTHREADS) |
| 25 |
|
| 26 |
def test_pattern_search(self): |
| 27 |
"""Pattern search should work across threads.""" |
| 28 |
emails = ["[email protected]", "[email protected]"] * 10 |
| 29 |
pattern = re.compile(r"\w+@\w+\.\w+") |
| 30 |
results = [] |
| 31 |
|
| 32 |
def worker(): |
| 33 |
matches = [pattern.search(e).group() for e in emails] |
| 34 |
results.append(len(matches)) |
| 35 |
|
| 36 |
run_concurrently(worker_func=worker, nthreads=NTHREADS) |
| 37 |
self.assertEqual(results, [2 * 10] * NTHREADS) |
| 38 |
|
| 39 |
def test_scanner_concurrent_access(self): |
| 40 |
"""Shared scanner should reject concurrent access.""" |
| 41 |
pattern = re.compile(r"\w+") |
| 42 |
scanner = pattern.scanner("word " * 10) |
| 43 |
|
| 44 |
def worker(): |
| 45 |
for _ in range(100): |
| 46 |
try: |
| 47 |
scanner.search() |
| 48 |
except ValueError as e: |
| 49 |
if "already executing" in str(e): |
| 50 |
pass |
| 51 |
else: |
| 52 |
raise |
| 53 |
|
| 54 |
run_concurrently(worker_func=worker, nthreads=NTHREADS) |
| 55 |
# This test has no assertions. Its purpose is to catch crashes and |
| 56 |
# enable thread sanitizer to detect race conditions. While "already |
| 57 |
# executing" errors are very likely, they're not guaranteed due to |
| 58 |
# non-deterministic thread scheduling, so we can't assert errors > 0. |
| 59 |
|
| 60 |
|
| 61 |
if __name__ == "__main__": |
| 62 |
unittest.main() |