| 1 |
import sys |
| 2 |
import time |
| 3 |
import unittest |
| 4 |
import threading |
| 5 |
import concurrent.futures |
| 6 |
|
| 7 |
from test.support import setswitchinterval, threading_helper |
| 8 |
from unittest.mock import patch, ThreadingMock |
| 9 |
|
| 10 |
|
| 11 |
threading_helper.requires_working_threading(module=True) |
| 12 |
|
| 13 |
VERY_SHORT_TIMEOUT = 0.1 |
| 14 |
|
| 15 |
|
| 16 |
class Something: |
| 17 |
def method_1(self): |
| 18 |
pass # pragma: no cover |
| 19 |
|
| 20 |
def method_2(self): |
| 21 |
pass # pragma: no cover |
| 22 |
|
| 23 |
|
| 24 |
class TestThreadingMock(unittest.TestCase): |
| 25 |
def _call_after_delay(self, func, /, *args, **kwargs): |
| 26 |
time.sleep(kwargs.pop("delay")) |
| 27 |
func(*args, **kwargs) |
| 28 |
|
| 29 |
def setUp(self): |
| 30 |
self._executor = concurrent.futures.ThreadPoolExecutor(max_workers=5) |
| 31 |
|
| 32 |
def tearDown(self): |
| 33 |
self._executor.shutdown() |
| 34 |
|
| 35 |
def run_async(self, func, /, *args, delay=0, **kwargs): |
| 36 |
self._executor.submit( |
| 37 |
self._call_after_delay, func, *args, **kwargs, delay=delay |
| 38 |
) |
| 39 |
|
| 40 |
def _make_mock(self, *args, **kwargs): |
| 41 |
return ThreadingMock(*args, **kwargs) |
| 42 |
|
| 43 |
def test_spec(self): |
| 44 |
waitable_mock = self._make_mock(spec=Something) |
| 45 |
|
| 46 |
with patch(f"{__name__}.Something", waitable_mock) as m: |
| 47 |
something = m() |
| 48 |
|
| 49 |
self.assertIsInstance(something.method_1, ThreadingMock) |
| 50 |
self.assertIsInstance(something.method_1().method_2(), ThreadingMock) |
| 51 |
|
| 52 |
with self.assertRaises(AttributeError): |
| 53 |
m.test |
| 54 |
|
| 55 |
def test_side_effect(self): |
| 56 |
waitable_mock = self._make_mock() |
| 57 |
|
| 58 |
with patch(f"{__name__}.Something", waitable_mock): |
| 59 |
something = Something() |
| 60 |
something.method_1.side_effect = [1] |
| 61 |
|
| 62 |
self.assertEqual(something.method_1(), 1) |
| 63 |
|
| 64 |
def test_instance_check(self): |
| 65 |
waitable_mock = self._make_mock() |
| 66 |
|
| 67 |
with patch(f"{__name__}.Something", waitable_mock): |
| 68 |
something = Something() |
| 69 |
|
| 70 |
self.assertIsInstance(something.method_1, ThreadingMock) |
| 71 |
self.assertIsInstance(something.method_1().method_2(), ThreadingMock) |
| 72 |
|
| 73 |
def test_dynamic_child_mocks_are_threading_mocks(self): |
| 74 |
waitable_mock = self._make_mock() |
| 75 |
self.assertIsInstance(waitable_mock.child, ThreadingMock) |
| 76 |
|
| 77 |
def test_dynamic_child_mocks_inherit_timeout(self): |
| 78 |
mock1 = self._make_mock() |
| 79 |
self.assertIs(mock1._mock_wait_timeout, None) |
| 80 |
mock2 = self._make_mock(timeout=2) |
| 81 |
self.assertEqual(mock2._mock_wait_timeout, 2) |
| 82 |
mock3 = self._make_mock(timeout=3) |
| 83 |
self.assertEqual(mock3._mock_wait_timeout, 3) |
| 84 |
|
| 85 |
self.assertIs(mock1.child._mock_wait_timeout, None) |
| 86 |
self.assertEqual(mock2.child._mock_wait_timeout, 2) |
| 87 |
self.assertEqual(mock3.child._mock_wait_timeout, 3) |
| 88 |
|
| 89 |
self.assertEqual(mock2.really().__mul__().complex._mock_wait_timeout, 2) |
| 90 |
|
| 91 |
def test_no_name_clash(self): |
| 92 |
waitable_mock = self._make_mock() |
| 93 |
waitable_mock._event = "myevent" |
| 94 |
waitable_mock.event = "myevent" |
| 95 |
waitable_mock.timeout = "mytimeout" |
| 96 |
waitable_mock("works") |
| 97 |
waitable_mock.wait_until_called() |
| 98 |
waitable_mock.wait_until_any_call_with("works") |
| 99 |
|
| 100 |
def test_patch(self): |
| 101 |
waitable_mock = self._make_mock(spec=Something) |
| 102 |
|
| 103 |
with patch(f"{__name__}.Something", waitable_mock): |
| 104 |
something = Something() |
| 105 |
something.method_1() |
| 106 |
something.method_1.wait_until_called() |
| 107 |
|
| 108 |
def test_wait_already_called_success(self): |
| 109 |
waitable_mock = self._make_mock(spec=Something) |
| 110 |
waitable_mock.method_1() |
| 111 |
waitable_mock.method_1.wait_until_called() |
| 112 |
waitable_mock.method_1.wait_until_any_call_with() |
| 113 |
waitable_mock.method_1.assert_called() |
| 114 |
|
| 115 |
def test_wait_until_called_success(self): |
| 116 |
waitable_mock = self._make_mock(spec=Something) |
| 117 |
self.run_async(waitable_mock.method_1, delay=VERY_SHORT_TIMEOUT) |
| 118 |
waitable_mock.method_1.wait_until_called() |
| 119 |
|
| 120 |
def test_wait_until_called_method_timeout(self): |
| 121 |
waitable_mock = self._make_mock(spec=Something) |
| 122 |
with self.assertRaises(AssertionError): |
| 123 |
waitable_mock.method_1.wait_until_called(timeout=VERY_SHORT_TIMEOUT) |
| 124 |
|
| 125 |
def test_wait_until_called_instance_timeout(self): |
| 126 |
waitable_mock = self._make_mock(spec=Something, timeout=VERY_SHORT_TIMEOUT) |
| 127 |
with self.assertRaises(AssertionError): |
| 128 |
waitable_mock.method_1.wait_until_called() |
| 129 |
|
| 130 |
def test_wait_until_called_global_timeout(self): |
| 131 |
with patch.object(ThreadingMock, "DEFAULT_TIMEOUT"): |
| 132 |
ThreadingMock.DEFAULT_TIMEOUT = VERY_SHORT_TIMEOUT |
| 133 |
waitable_mock = self._make_mock(spec=Something) |
| 134 |
with self.assertRaises(AssertionError): |
| 135 |
waitable_mock.method_1.wait_until_called() |
| 136 |
|
| 137 |
def test_wait_until_any_call_with_success(self): |
| 138 |
waitable_mock = self._make_mock() |
| 139 |
self.run_async(waitable_mock, delay=VERY_SHORT_TIMEOUT) |
| 140 |
waitable_mock.wait_until_any_call_with() |
| 141 |
|
| 142 |
def test_wait_until_any_call_with_instance_timeout(self): |
| 143 |
waitable_mock = self._make_mock(timeout=VERY_SHORT_TIMEOUT) |
| 144 |
with self.assertRaises(AssertionError): |
| 145 |
waitable_mock.wait_until_any_call_with() |
| 146 |
|
| 147 |
def test_wait_until_any_call_global_timeout(self): |
| 148 |
with patch.object(ThreadingMock, "DEFAULT_TIMEOUT"): |
| 149 |
ThreadingMock.DEFAULT_TIMEOUT = VERY_SHORT_TIMEOUT |
| 150 |
waitable_mock = self._make_mock() |
| 151 |
with self.assertRaises(AssertionError): |
| 152 |
waitable_mock.wait_until_any_call_with() |
| 153 |
|
| 154 |
def test_wait_until_any_call_positional(self): |
| 155 |
waitable_mock = self._make_mock(timeout=VERY_SHORT_TIMEOUT) |
| 156 |
waitable_mock.method_1(1, 2, 3) |
| 157 |
waitable_mock.method_1.wait_until_any_call_with(1, 2, 3) |
| 158 |
with self.assertRaises(AssertionError): |
| 159 |
waitable_mock.method_1.wait_until_any_call_with(2, 3, 1) |
| 160 |
with self.assertRaises(AssertionError): |
| 161 |
waitable_mock.method_1.wait_until_any_call_with() |
| 162 |
|
| 163 |
def test_wait_until_any_call_kw(self): |
| 164 |
waitable_mock = self._make_mock(timeout=VERY_SHORT_TIMEOUT) |
| 165 |
waitable_mock.method_1(a=1, b=2) |
| 166 |
waitable_mock.method_1.wait_until_any_call_with(a=1, b=2) |
| 167 |
with self.assertRaises(AssertionError): |
| 168 |
waitable_mock.method_1.wait_until_any_call_with(a=2, b=1) |
| 169 |
with self.assertRaises(AssertionError): |
| 170 |
waitable_mock.method_1.wait_until_any_call_with() |
| 171 |
|
| 172 |
def test_magic_methods_success(self): |
| 173 |
waitable_mock = self._make_mock() |
| 174 |
str(waitable_mock) |
| 175 |
waitable_mock.__str__.wait_until_called() |
| 176 |
waitable_mock.__str__.assert_called() |
| 177 |
|
| 178 |
def test_reset_mock_resets_wait(self): |
| 179 |
m = self._make_mock(timeout=VERY_SHORT_TIMEOUT) |
| 180 |
|
| 181 |
with self.assertRaises(AssertionError): |
| 182 |
m.wait_until_called() |
| 183 |
with self.assertRaises(AssertionError): |
| 184 |
m.wait_until_any_call_with() |
| 185 |
m() |
| 186 |
m.wait_until_called() |
| 187 |
m.wait_until_any_call_with() |
| 188 |
m.assert_called_once() |
| 189 |
|
| 190 |
m.reset_mock() |
| 191 |
|
| 192 |
with self.assertRaises(AssertionError): |
| 193 |
m.wait_until_called() |
| 194 |
with self.assertRaises(AssertionError): |
| 195 |
m.wait_until_any_call_with() |
| 196 |
m() |
| 197 |
m.wait_until_called() |
| 198 |
m.wait_until_any_call_with() |
| 199 |
m.assert_called_once() |
| 200 |
|
| 201 |
def test_call_count_thread_safe(self): |
| 202 |
# See https://github.com/python/cpython/issues/142651. |
| 203 |
m = ThreadingMock() |
| 204 |
LOOPS = 100 |
| 205 |
THREADS = 10 |
| 206 |
def test_function(): |
| 207 |
for _ in range(LOOPS): |
| 208 |
m() |
| 209 |
|
| 210 |
oldswitchinterval = sys.getswitchinterval() |
| 211 |
setswitchinterval(1e-6) |
| 212 |
try: |
| 213 |
threads = [threading.Thread(target=test_function) for _ in range(THREADS)] |
| 214 |
with threading_helper.start_threads(threads): |
| 215 |
pass |
| 216 |
finally: |
| 217 |
sys.setswitchinterval(oldswitchinterval) |
| 218 |
|
| 219 |
self.assertEqual(m.call_count, LOOPS * THREADS) |
| 220 |
|
| 221 |
|
| 222 |
def test_call_args_thread_safe(self): |
| 223 |
m = ThreadingMock() |
| 224 |
LOOPS = 100 |
| 225 |
THREADS = 10 |
| 226 |
def test_function(thread_id): |
| 227 |
for i in range(LOOPS): |
| 228 |
m(thread_id, i) |
| 229 |
|
| 230 |
oldswitchinterval = sys.getswitchinterval() |
| 231 |
setswitchinterval(1e-6) |
| 232 |
try: |
| 233 |
threads = [ |
| 234 |
threading.Thread(target=test_function, args=(thread_id,)) |
| 235 |
for thread_id in range(THREADS) |
| 236 |
] |
| 237 |
with threading_helper.start_threads(threads): |
| 238 |
pass |
| 239 |
finally: |
| 240 |
sys.setswitchinterval(oldswitchinterval) |
| 241 |
expected_calls = { |
| 242 |
(thread_id, i) |
| 243 |
for thread_id in range(THREADS) |
| 244 |
for i in range(LOOPS) |
| 245 |
} |
| 246 |
self.assertSetEqual({call.args for call in m.call_args_list}, expected_calls) |
| 247 |
|
| 248 |
def test_method_calls_thread_safe(self): |
| 249 |
m = ThreadingMock() |
| 250 |
LOOPS = 100 |
| 251 |
THREADS = 10 |
| 252 |
def test_function(thread_id): |
| 253 |
for i in range(LOOPS): |
| 254 |
getattr(m, f"method_{thread_id}")(i) |
| 255 |
|
| 256 |
oldswitchinterval = sys.getswitchinterval() |
| 257 |
setswitchinterval(1e-6) |
| 258 |
try: |
| 259 |
threads = [ |
| 260 |
threading.Thread(target=test_function, args=(thread_id,)) |
| 261 |
for thread_id in range(THREADS) |
| 262 |
] |
| 263 |
with threading_helper.start_threads(threads): |
| 264 |
pass |
| 265 |
finally: |
| 266 |
sys.setswitchinterval(oldswitchinterval) |
| 267 |
for thread_id in range(THREADS): |
| 268 |
self.assertEqual(getattr(m, f"method_{thread_id}").call_count, LOOPS) |
| 269 |
self.assertEqual({call.args for call in getattr(m, f"method_{thread_id}").call_args_list}, |
| 270 |
{(i,) for i in range(LOOPS)}) |
| 271 |
|
| 272 |
def test_mock_calls_thread_safe(self): |
| 273 |
m = ThreadingMock() |
| 274 |
LOOPS = 100 |
| 275 |
THREADS = 10 |
| 276 |
def test_function(thread_id): |
| 277 |
for i in range(LOOPS): |
| 278 |
m(thread_id, i) |
| 279 |
|
| 280 |
oldswitchinterval = sys.getswitchinterval() |
| 281 |
setswitchinterval(1e-6) |
| 282 |
try: |
| 283 |
threads = [ |
| 284 |
threading.Thread(target=test_function, args=(thread_id,)) |
| 285 |
for thread_id in range(THREADS) |
| 286 |
] |
| 287 |
with threading_helper.start_threads(threads): |
| 288 |
pass |
| 289 |
finally: |
| 290 |
sys.setswitchinterval(oldswitchinterval) |
| 291 |
expected_calls = { |
| 292 |
(thread_id, i) |
| 293 |
for thread_id in range(THREADS) |
| 294 |
for i in range(LOOPS) |
| 295 |
} |
| 296 |
self.assertSetEqual({call.args for call in m.mock_calls}, expected_calls) |
| 297 |
|
| 298 |
if __name__ == "__main__": |
| 299 |
unittest.main() |