Mercurial > hg
comparison tests/test-lock.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | b58d608ec6a0 |
children | 888bd39ed555 |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
17 | 17 |
18 testlockname = b'testlock' | 18 testlockname = b'testlock' |
19 | 19 |
20 # work around http://bugs.python.org/issue1515 | 20 # work around http://bugs.python.org/issue1515 |
21 if types.MethodType not in copy._deepcopy_dispatch: | 21 if types.MethodType not in copy._deepcopy_dispatch: |
22 | |
22 def _deepcopy_method(x, memo): | 23 def _deepcopy_method(x, memo): |
23 return type(x)(x.__func__, copy.deepcopy(x.__self__, memo), x.im_class) | 24 return type(x)(x.__func__, copy.deepcopy(x.__self__, memo), x.im_class) |
25 | |
24 copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method | 26 copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method |
27 | |
25 | 28 |
26 class lockwrapper(lock.lock): | 29 class lockwrapper(lock.lock): |
27 def __init__(self, pidoffset, *args, **kwargs): | 30 def __init__(self, pidoffset, *args, **kwargs): |
28 # lock.lock.__init__() calls lock(), so the pidoffset assignment needs | 31 # lock.lock.__init__() calls lock(), so the pidoffset assignment needs |
29 # to be earlier | 32 # to be earlier |
30 self._pidoffset = pidoffset | 33 self._pidoffset = pidoffset |
31 super(lockwrapper, self).__init__(*args, **kwargs) | 34 super(lockwrapper, self).__init__(*args, **kwargs) |
35 | |
32 def _getpid(self): | 36 def _getpid(self): |
33 return super(lockwrapper, self)._getpid() + self._pidoffset | 37 return super(lockwrapper, self)._getpid() + self._pidoffset |
38 | |
34 | 39 |
35 class teststate(object): | 40 class teststate(object): |
36 def __init__(self, testcase, dir, pidoffset=0): | 41 def __init__(self, testcase, dir, pidoffset=0): |
37 self._testcase = testcase | 42 self._testcase = testcase |
38 self._acquirecalled = False | 43 self._acquirecalled = False |
40 self._postreleasecalled = False | 45 self._postreleasecalled = False |
41 self.vfs = vfsmod.vfs(dir, audit=False) | 46 self.vfs = vfsmod.vfs(dir, audit=False) |
42 self._pidoffset = pidoffset | 47 self._pidoffset = pidoffset |
43 | 48 |
44 def makelock(self, *args, **kwargs): | 49 def makelock(self, *args, **kwargs): |
45 l = lockwrapper(self._pidoffset, self.vfs, testlockname, | 50 l = lockwrapper( |
46 releasefn=self.releasefn, acquirefn=self.acquirefn, | 51 self._pidoffset, |
47 *args, **kwargs) | 52 self.vfs, |
53 testlockname, | |
54 releasefn=self.releasefn, | |
55 acquirefn=self.acquirefn, | |
56 *args, | |
57 **kwargs | |
58 ) | |
48 l.postrelease.append(self.postreleasefn) | 59 l.postrelease.append(self.postreleasefn) |
49 return l | 60 return l |
50 | 61 |
51 def acquirefn(self): | 62 def acquirefn(self): |
52 self._acquirecalled = True | 63 self._acquirecalled = True |
57 def postreleasefn(self): | 68 def postreleasefn(self): |
58 self._postreleasecalled = True | 69 self._postreleasecalled = True |
59 | 70 |
60 def assertacquirecalled(self, called): | 71 def assertacquirecalled(self, called): |
61 self._testcase.assertEqual( | 72 self._testcase.assertEqual( |
62 self._acquirecalled, called, | 73 self._acquirecalled, |
63 'expected acquire to be %s but was actually %s' % ( | 74 called, |
64 self._tocalled(called), | 75 'expected acquire to be %s but was actually %s' |
65 self._tocalled(self._acquirecalled), | 76 % (self._tocalled(called), self._tocalled(self._acquirecalled),), |
66 )) | 77 ) |
67 | 78 |
68 def resetacquirefn(self): | 79 def resetacquirefn(self): |
69 self._acquirecalled = False | 80 self._acquirecalled = False |
70 | 81 |
71 def assertreleasecalled(self, called): | 82 def assertreleasecalled(self, called): |
72 self._testcase.assertEqual( | 83 self._testcase.assertEqual( |
73 self._releasecalled, called, | 84 self._releasecalled, |
74 'expected release to be %s but was actually %s' % ( | 85 called, |
75 self._tocalled(called), | 86 'expected release to be %s but was actually %s' |
76 self._tocalled(self._releasecalled), | 87 % (self._tocalled(called), self._tocalled(self._releasecalled),), |
77 )) | 88 ) |
78 | 89 |
79 def assertpostreleasecalled(self, called): | 90 def assertpostreleasecalled(self, called): |
80 self._testcase.assertEqual( | 91 self._testcase.assertEqual( |
81 self._postreleasecalled, called, | 92 self._postreleasecalled, |
82 'expected postrelease to be %s but was actually %s' % ( | 93 called, |
94 'expected postrelease to be %s but was actually %s' | |
95 % ( | |
83 self._tocalled(called), | 96 self._tocalled(called), |
84 self._tocalled(self._postreleasecalled), | 97 self._tocalled(self._postreleasecalled), |
85 )) | 98 ), |
99 ) | |
86 | 100 |
87 def assertlockexists(self, exists): | 101 def assertlockexists(self, exists): |
88 actual = self.vfs.lexists(testlockname) | 102 actual = self.vfs.lexists(testlockname) |
89 self._testcase.assertEqual( | 103 self._testcase.assertEqual( |
90 actual, exists, | 104 actual, |
91 'expected lock to %s but actually did %s' % ( | 105 exists, |
92 self._toexists(exists), | 106 'expected lock to %s but actually did %s' |
93 self._toexists(actual), | 107 % (self._toexists(exists), self._toexists(actual),), |
94 )) | 108 ) |
95 | 109 |
96 def _tocalled(self, called): | 110 def _tocalled(self, called): |
97 if called: | 111 if called: |
98 return 'called' | 112 return 'called' |
99 else: | 113 else: |
102 def _toexists(self, exists): | 116 def _toexists(self, exists): |
103 if exists: | 117 if exists: |
104 return 'exist' | 118 return 'exist' |
105 else: | 119 else: |
106 return 'not exist' | 120 return 'not exist' |
121 | |
107 | 122 |
108 class testlock(unittest.TestCase): | 123 class testlock(unittest.TestCase): |
109 def testlock(self): | 124 def testlock(self): |
110 state = teststate(self, tempfile.mkdtemp(dir=encoding.getcwd())) | 125 state = teststate(self, tempfile.mkdtemp(dir=encoding.getcwd())) |
111 lock = state.makelock() | 126 lock = state.makelock() |
123 state.resetacquirefn() | 138 state.resetacquirefn() |
124 lock.lock() | 139 lock.lock() |
125 # recursive lock should not call acquirefn again | 140 # recursive lock should not call acquirefn again |
126 state.assertacquirecalled(False) | 141 state.assertacquirecalled(False) |
127 | 142 |
128 lock.release() # brings lock refcount down from 2 to 1 | 143 lock.release() # brings lock refcount down from 2 to 1 |
129 state.assertreleasecalled(False) | 144 state.assertreleasecalled(False) |
130 state.assertpostreleasecalled(False) | 145 state.assertpostreleasecalled(False) |
131 state.assertlockexists(True) | 146 state.assertlockexists(True) |
132 | 147 |
133 lock.release() # releases the lock | 148 lock.release() # releases the lock |
134 state.assertreleasecalled(True) | 149 state.assertreleasecalled(True) |
135 state.assertpostreleasecalled(True) | 150 state.assertpostreleasecalled(True) |
136 state.assertlockexists(False) | 151 state.assertlockexists(False) |
137 | 152 |
138 def testlockfork(self): | 153 def testlockfork(self): |
254 parentlock.release() | 269 parentlock.release() |
255 | 270 |
256 def testinheritcheck(self): | 271 def testinheritcheck(self): |
257 d = tempfile.mkdtemp(dir=encoding.getcwd()) | 272 d = tempfile.mkdtemp(dir=encoding.getcwd()) |
258 state = teststate(self, d) | 273 state = teststate(self, d) |
274 | |
259 def check(): | 275 def check(): |
260 raise error.LockInheritanceContractViolation('check failed') | 276 raise error.LockInheritanceContractViolation('check failed') |
277 | |
261 lock = state.makelock(inheritchecker=check) | 278 lock = state.makelock(inheritchecker=check) |
262 state.assertacquirecalled(True) | 279 state.assertacquirecalled(True) |
263 | 280 |
264 with self.assertRaises(error.LockInheritanceContractViolation): | 281 with self.assertRaises(error.LockInheritanceContractViolation): |
265 with lock.inherit(): | 282 with lock.inherit(): |
277 d = tempfile.mkdtemp(dir=encoding.getcwd()) | 294 d = tempfile.mkdtemp(dir=encoding.getcwd()) |
278 state = teststate(self, d) | 295 state = teststate(self, d) |
279 | 296 |
280 def emulatefrequentlock(*args): | 297 def emulatefrequentlock(*args): |
281 raise OSError(errno.EEXIST, "File exists") | 298 raise OSError(errno.EEXIST, "File exists") |
299 | |
282 def emulatefrequentunlock(*args): | 300 def emulatefrequentunlock(*args): |
283 raise OSError(errno.ENOENT, "No such file or directory") | 301 raise OSError(errno.ENOENT, "No such file or directory") |
284 | 302 |
285 state.vfs.makelock = emulatefrequentlock | 303 state.vfs.makelock = emulatefrequentlock |
286 state.vfs.readlock = emulatefrequentunlock | 304 state.vfs.readlock = emulatefrequentunlock |
291 except error.LockHeld as why: | 309 except error.LockHeld as why: |
292 self.assertTrue(why.errno == errno.ETIMEDOUT) | 310 self.assertTrue(why.errno == errno.ETIMEDOUT) |
293 self.assertTrue(why.locker == b"") | 311 self.assertTrue(why.locker == b"") |
294 state.assertlockexists(False) | 312 state.assertlockexists(False) |
295 | 313 |
314 | |
296 if __name__ == '__main__': | 315 if __name__ == '__main__': |
297 silenttestrunner.main(__name__) | 316 silenttestrunner.main(__name__) |