Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_unittest/testmock/testthreadingmock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
import unittest
import threading
import concurrent.futures

from test.support import threading_helper
Expand Down Expand Up @@ -196,6 +197,22 @@ def test_reset_mock_resets_wait(self):
m.wait_until_any_call_with()
m.assert_called_once()

def test_call_count_thread_safe(self):
m = ThreadingMock()
# 3k loops reliably reproduces the issue while keeping runtime ~0.6s
LOOPS = 3_000
THREADS = 10
def test_function():
for _ in range(LOOPS):
m()
threads = [threading.Thread(target=test_function) for _ in range(THREADS)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()

self.assertEqual(m.call_count, LOOPS * THREADS)


if __name__ == "__main__":
unittest.main()
Loading