|
1 |
| -import sys |
2 |
| -from typing import Type, Callable, List |
| 1 | +from typing import Iterator |
3 | 2 | import time
|
4 |
| -try: |
5 |
| - import collections.abc as collections_abc |
6 |
| -except ImportError: |
7 |
| - import collections as collections_abc # type: ignore # PY32 and earlier |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +from contextlib import contextmanager |
| 7 | +from threading import Thread |
8 | 8 | from unittest import TestCase, main, skipUnless
|
9 | 9 | from mypy import util
|
10 | 10 |
|
11 | 11 |
|
12 |
| -def create_bad_function(lag: float, exc: BaseException) -> Callable[[], None]: |
13 |
| - start_time = time.perf_counter() |
| 12 | +WIN32 = sys.platform.startswith("win") |
| 13 | + |
| 14 | + |
| 15 | +@contextmanager |
| 16 | +def lock_file(filename: str, duration: float) -> Iterator[Thread]: |
| 17 | + ''' |
| 18 | + Opens filename (which must exist) for reading |
| 19 | + After duration sec, releases the handle |
| 20 | + ''' |
| 21 | + def _lock_file() -> None: |
| 22 | + with open(filename): |
| 23 | + time.sleep(duration) |
| 24 | + t = Thread(target=_lock_file, daemon=True) |
| 25 | + t.start() |
| 26 | + yield t |
| 27 | + t.join() |
| 28 | + |
| 29 | + |
| 30 | +@skipUnless(WIN32, "only relevant for Windows") |
| 31 | +class ReliableReplace(TestCase): |
| 32 | + tmpdir = tempfile.TemporaryDirectory(prefix='mypy-test-', |
| 33 | + dir=os.path.abspath('tmp-test-dirs')) |
| 34 | + src = os.path.join(tmpdir.name, 'tmp1') |
| 35 | + dest = os.path.join(tmpdir.name, 'tmp2') |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def tearDownClass(cls) -> None: |
| 39 | + cls.tmpdir.cleanup() |
| 40 | + |
| 41 | + def setUp(self) -> None: |
| 42 | + # create two temporary files |
| 43 | + for fname in (self.src, self.dest): |
| 44 | + with open(fname, 'w') as f: |
| 45 | + f.write(fname) |
| 46 | + |
| 47 | + def replace_ok(self) -> None: |
| 48 | + util._replace(self.src, self.dest, timeout=0.25) |
| 49 | + self.assertEqual(open(self.dest).read(), self.src, 'replace failed') |
14 | 50 |
|
15 |
| - def f() -> None: |
16 |
| - if time.perf_counter() - start_time < lag: |
17 |
| - raise exc |
18 |
| - else: |
19 |
| - return |
20 |
| - return f |
| 51 | + def test_normal(self) -> None: |
| 52 | + self.replace_ok() |
21 | 53 |
|
| 54 | + def test_problem_exists(self) -> None: |
| 55 | + with lock_file(self.src, 0.1): |
| 56 | + with self.assertRaises(PermissionError): |
| 57 | + os.replace(self.src, self.dest) |
22 | 58 |
|
23 |
| -def create_funcs() -> List[Callable[[], None]]: |
| 59 | + def test_short_lock_src(self) -> None: |
| 60 | + with lock_file(self.src, 0.1): |
| 61 | + self.replace_ok() |
24 | 62 |
|
25 |
| - def linux_function() -> None: pass |
26 |
| - windows_function1 = create_bad_function(0.1, PermissionError()) |
27 |
| - windows_function2 = create_bad_function(0.2, FileExistsError()) |
28 |
| - return [windows_function1, windows_function2, linux_function] |
| 63 | + def test_short_lock_dest(self) -> None: |
| 64 | + with lock_file(self.dest, 0.1): |
| 65 | + self.replace_ok() |
29 | 66 |
|
| 67 | + def test_long_lock_src(self) -> None: |
| 68 | + with lock_file(self.src, 0.4): |
| 69 | + with self.assertRaises(PermissionError): |
| 70 | + self.replace_ok() |
30 | 71 |
|
31 |
| -class WaitRetryTests(TestCase): |
32 |
| - def test_waitfor(self) -> None: |
33 |
| - with self.assertRaises(OSError): |
34 |
| - util.wait_for(create_funcs(), (PermissionError, FileExistsError), 0.1) |
35 |
| - util.wait_for(create_funcs(), (PermissionError, FileExistsError), 1) |
36 |
| - util.wait_for(create_funcs(), (OSError,), 1) |
37 |
| - with self.assertRaises(FileExistsError): |
38 |
| - util.wait_for(create_funcs(), (PermissionError,), 0.4) |
| 72 | + def test_long_lock_dest(self) -> None: |
| 73 | + with lock_file(self.dest, 0.4): |
| 74 | + with self.assertRaises(PermissionError): |
| 75 | + self.replace_ok() |
39 | 76 |
|
40 | 77 |
|
41 | 78 | if __name__ == '__main__':
|
|
0 commit comments