1
- from typing import Iterator , Tuple
1
+ from typing import Iterator , List , Tuple
2
2
import time
3
3
import os
4
4
import sys
13
13
WIN32 = sys .platform .startswith ("win" )
14
14
15
15
16
- @contextmanager
17
- def lock_file (filename : str , duration : float ) -> Iterator [Thread ]:
16
+ def lock_file (filename : str , duration : float ) -> Thread :
18
17
'''
19
18
Opens filename (which must exist) for reading
20
19
After duration sec, releases the handle
@@ -24,22 +23,27 @@ def _lock_file() -> None:
24
23
time .sleep (duration )
25
24
t = Thread (target = _lock_file , daemon = True )
26
25
t .start ()
27
- yield t
28
- t .join ()
26
+ return t
29
27
30
28
31
29
@skipUnless (WIN32 , "only relevant for Windows" )
32
30
class ReliableReplace (TestCase ):
33
31
# will be cleaned up automatically when this class goes out of scope
34
32
tmpdir = tempfile .TemporaryDirectory (prefix = 'mypy-test-' ,
35
33
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 ()
39
44
40
- @contextmanager
41
45
def prepare_src_dest (self , src_lock_duration : float , dest_lock_duration : float
42
- ) -> Iterator [ Tuple [str , str ] ]:
46
+ ) -> Tuple [str , str ]:
43
47
# create two temporary files
44
48
src = os .path .join (self .tmpdir .name , random_string ())
45
49
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
48
52
with open (fname , 'w' ) as f :
49
53
f .write (fname )
50
54
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
54
58
55
59
def replace_ok (self , src_lock_duration : float , dest_lock_duration : float ,
56
60
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' )
60
64
61
65
def test_normal (self ) -> None :
62
66
self .replace_ok (0 , 0 , self .timeout )
63
67
64
68
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 )
68
72
69
73
def test_short_lock (self ) -> None :
70
74
self .replace_ok (self .short_lock , 0 , self .timeout )
0 commit comments