Skip to content

Commit 2d369c0

Browse files
committed
Wait for lock expiration at the end
1 parent 42c6987 commit 2d369c0

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

mypy/test/testutil.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Iterator, Tuple
1+
from typing import Iterator, List, Tuple
22
import time
33
import os
44
import sys
@@ -13,8 +13,7 @@
1313
WIN32 = sys.platform.startswith("win")
1414

1515

16-
@contextmanager
17-
def lock_file(filename: str, duration: float) -> Iterator[Thread]:
16+
def lock_file(filename: str, duration: float) -> Thread:
1817
'''
1918
Opens filename (which must exist) for reading
2019
After duration sec, releases the handle
@@ -24,22 +23,27 @@ def _lock_file() -> None:
2423
time.sleep(duration)
2524
t = Thread(target=_lock_file, daemon=True)
2625
t.start()
27-
yield t
28-
t.join()
26+
return t
2927

3028

3129
@skipUnless(WIN32, "only relevant for Windows")
3230
class ReliableReplace(TestCase):
3331
# will be cleaned up automatically when this class goes out of scope
3432
tmpdir = tempfile.TemporaryDirectory(prefix='mypy-test-',
3533
dir=os.path.abspath('tmp-test-dirs'))
36-
timeout = 0.5
37-
short_lock = 0.2
38-
long_lock = 1
34+
timeout = 1
35+
short_lock = 0.25
36+
long_lock = 2
37+
38+
threads = [] # type: List[Thread]
39+
40+
@classmethod
41+
def tearDownClass(cls):
42+
for t in cls.threads:
43+
t.join()
3944

40-
@contextmanager
4145
def prepare_src_dest(self, src_lock_duration: float, dest_lock_duration: float
42-
) -> Iterator[Tuple[str, str]]:
46+
) -> Tuple[str, str]:
4347
# create two temporary files
4448
src = os.path.join(self.tmpdir.name, random_string())
4549
dest = os.path.join(self.tmpdir.name, random_string())
@@ -48,23 +52,23 @@ def prepare_src_dest(self, src_lock_duration: float, dest_lock_duration: float
4852
with open(fname, 'w') as f:
4953
f.write(fname)
5054

51-
with lock_file(src, src_lock_duration):
52-
with lock_file(dest, dest_lock_duration):
53-
yield src, dest
55+
self.threads.append(lock_file(src, src_lock_duration))
56+
self.threads.append(lock_file(dest, dest_lock_duration))
57+
return src, dest
5458

5559
def replace_ok(self, src_lock_duration: float, dest_lock_duration: float,
5660
timeout: float) -> None:
57-
with self.prepare_src_dest(src_lock_duration, dest_lock_duration) as (src, dest):
58-
util._replace(src, dest, timeout=timeout)
59-
self.assertEqual(open(dest).read(), src, 'replace failed')
61+
src, dest = self.prepare_src_dest(src_lock_duration, dest_lock_duration)
62+
util._replace(src, dest, timeout=timeout)
63+
self.assertEqual(open(dest).read(), src, 'replace failed')
6064

6165
def test_normal(self) -> None:
6266
self.replace_ok(0, 0, self.timeout)
6367

6468
def test_problem_exists(self) -> None:
65-
with self.prepare_src_dest(self.short_lock, 0) as (src, dest):
66-
with self.assertRaises(PermissionError):
67-
os.replace(src, dest)
69+
src, dest = self.prepare_src_dest(self.short_lock, 0)
70+
with self.assertRaises(PermissionError):
71+
os.replace(src, dest)
6872

6973
def test_short_lock(self) -> None:
7074
self.replace_ok(self.short_lock, 0, self.timeout)

0 commit comments

Comments
 (0)