|
| 1 | +import time |
| 2 | +import unittest |
| 3 | +import concurrent.futures |
| 4 | + |
| 5 | +from unittest.mock import patch, ThreadingMock, call |
| 6 | + |
| 7 | + |
| 8 | +class Something: |
| 9 | + |
| 10 | + def method_1(self): |
| 11 | + pass |
| 12 | + |
| 13 | + def method_2(self): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +class TestThreadingMock(unittest.TestCase): |
| 18 | + |
| 19 | + def _call_after_delay(self, func, /, *args, **kwargs): |
| 20 | + time.sleep(kwargs.pop('delay')) |
| 21 | + func(*args, **kwargs) |
| 22 | + |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + self._executor = concurrent.futures.ThreadPoolExecutor(max_workers=5) |
| 26 | + |
| 27 | + def tearDown(self): |
| 28 | + self._executor.shutdown() |
| 29 | + |
| 30 | + def run_async(self, func, /, *args, delay=0, **kwargs): |
| 31 | + self._executor.submit(self._call_after_delay, func, *args, **kwargs, delay=delay) |
| 32 | + |
| 33 | + def _make_mock(self, *args, **kwargs): |
| 34 | + return ThreadingMock(*args, **kwargs) |
| 35 | + |
| 36 | + def test_instance_check(self): |
| 37 | + waitable_mock = self._make_mock() |
| 38 | + |
| 39 | + with patch(f'{__name__}.Something', waitable_mock): |
| 40 | + something = Something() |
| 41 | + |
| 42 | + self.assertIsInstance(something.method_1, ThreadingMock) |
| 43 | + self.assertIsInstance( |
| 44 | + something.method_1().method_2(), ThreadingMock) |
| 45 | + |
| 46 | + |
| 47 | + def test_side_effect(self): |
| 48 | + waitable_mock = self._make_mock() |
| 49 | + |
| 50 | + with patch(f'{__name__}.Something', waitable_mock): |
| 51 | + something = Something() |
| 52 | + something.method_1.side_effect = [1] |
| 53 | + |
| 54 | + self.assertEqual(something.method_1(), 1) |
| 55 | + |
| 56 | + |
| 57 | + def test_spec(self): |
| 58 | + waitable_mock = self._make_mock(spec=Something) |
| 59 | + |
| 60 | + with patch(f'{__name__}.Something', waitable_mock) as m: |
| 61 | + something = m() |
| 62 | + |
| 63 | + self.assertIsInstance(something.method_1, ThreadingMock) |
| 64 | + self.assertIsInstance( |
| 65 | + something.method_1().method_2(), ThreadingMock) |
| 66 | + |
| 67 | + with self.assertRaises(AttributeError): |
| 68 | + m.test |
| 69 | + |
| 70 | + |
| 71 | + def test_wait_until_called(self): |
| 72 | + waitable_mock = self._make_mock(spec=Something) |
| 73 | + |
| 74 | + with patch(f'{__name__}.Something', waitable_mock): |
| 75 | + something = Something() |
| 76 | + self.run_async(something.method_1, delay=0.01) |
| 77 | + something.method_1.wait_until_called() |
| 78 | + something.method_1.assert_called_once() |
| 79 | + |
| 80 | + |
| 81 | + def test_wait_until_called_called_before(self): |
| 82 | + waitable_mock = self._make_mock(spec=Something) |
| 83 | + |
| 84 | + with patch(f'{__name__}.Something', waitable_mock): |
| 85 | + something = Something() |
| 86 | + something.method_1() |
| 87 | + something.method_1.wait_until_called() |
| 88 | + something.method_1.assert_called_once() |
| 89 | + |
| 90 | + |
| 91 | + def test_wait_until_called_magic_method(self): |
| 92 | + waitable_mock = self._make_mock(spec=Something) |
| 93 | + |
| 94 | + with patch(f'{__name__}.Something', waitable_mock): |
| 95 | + something = Something() |
| 96 | + self.run_async(something.method_1.__str__, delay=0.01) |
| 97 | + something.method_1.__str__.wait_until_called() |
| 98 | + something.method_1.__str__.assert_called_once() |
| 99 | + |
| 100 | + |
| 101 | + def test_wait_until_called_timeout(self): |
| 102 | + waitable_mock = self._make_mock(spec=Something) |
| 103 | + |
| 104 | + with patch(f'{__name__}.Something', waitable_mock): |
| 105 | + something = Something() |
| 106 | + self.run_async(something.method_1, delay=0.2) |
| 107 | + with self.assertRaises(AssertionError): |
| 108 | + something.method_1.wait_until_called(mock_timeout=0.1) |
| 109 | + something.method_1.assert_not_called() |
| 110 | + |
| 111 | + something.method_1.wait_until_called() |
| 112 | + something.method_1.assert_called_once() |
| 113 | + |
| 114 | + |
| 115 | + def test_wait_until_any_call_positional(self): |
| 116 | + waitable_mock = self._make_mock(spec=Something) |
| 117 | + |
| 118 | + with patch(f'{__name__}.Something', waitable_mock): |
| 119 | + something = Something() |
| 120 | + self.run_async(something.method_1, 1, delay=0.1) |
| 121 | + self.run_async(something.method_1, 2, delay=0.2) |
| 122 | + self.run_async(something.method_1, 3, delay=0.3) |
| 123 | + self.assertNotIn(call(1), something.method_1.mock_calls) |
| 124 | + |
| 125 | + something.method_1.wait_until_any_call(1) |
| 126 | + something.method_1.assert_called_once_with(1) |
| 127 | + self.assertNotIn(call(2), something.method_1.mock_calls) |
| 128 | + self.assertNotIn(call(3), something.method_1.mock_calls) |
| 129 | + |
| 130 | + something.method_1.wait_until_any_call(3) |
| 131 | + self.assertIn(call(2), something.method_1.mock_calls) |
| 132 | + something.method_1.wait_until_any_call(2) |
| 133 | + |
| 134 | + |
| 135 | + def test_wait_until_any_call_keywords(self): |
| 136 | + waitable_mock = self._make_mock(spec=Something) |
| 137 | + |
| 138 | + with patch(f'{__name__}.Something', waitable_mock): |
| 139 | + something = Something() |
| 140 | + self.run_async(something.method_1, a=1, delay=0.1) |
| 141 | + self.run_async(something.method_1, b=2, delay=0.2) |
| 142 | + self.run_async(something.method_1, c=3, delay=0.3) |
| 143 | + self.assertNotIn(call(a=1), something.method_1.mock_calls) |
| 144 | + |
| 145 | + something.method_1.wait_until_any_call(a=1) |
| 146 | + something.method_1.assert_called_once_with(a=1) |
| 147 | + self.assertNotIn(call(b=2), something.method_1.mock_calls) |
| 148 | + self.assertNotIn(call(c=3), something.method_1.mock_calls) |
| 149 | + |
| 150 | + something.method_1.wait_until_any_call(c=3) |
| 151 | + self.assertIn(call(b=2), something.method_1.mock_calls) |
| 152 | + something.method_1.wait_until_any_call(b=2) |
| 153 | + |
| 154 | + def test_wait_until_any_call_no_argument(self): |
| 155 | + waitable_mock = self._make_mock(spec=Something) |
| 156 | + |
| 157 | + with patch(f'{__name__}.Something', waitable_mock): |
| 158 | + something = Something() |
| 159 | + something.method_1(1) |
| 160 | + |
| 161 | + something.method_1.assert_called_once_with(1) |
| 162 | + with self.assertRaises(AssertionError): |
| 163 | + something.method_1.wait_until_any_call(mock_timeout=0.1) |
| 164 | + |
| 165 | + something.method_1() |
| 166 | + something.method_1.wait_until_any_call() |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + unittest.main() |
0 commit comments