annotate tests/test-lock.py @ 46924:ca0049946e9a

split: avoid strip if split is a no-op (identical to original) Differential Revision: https://phab.mercurial-scm.org/D10389
author Kyle Lippincott <spectral@google.com>
date Mon, 12 Apr 2021 19:25:34 -0700
parents 89a2afe31e82
children 6000f5b25c9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
1 from __future__ import absolute_import
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
2
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
3 import copy
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
4 import errno
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
5 import tempfile
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
6 import types
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
7 import unittest
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
8
40204
5d50c9ffaebb tests: fix style issue of importing order in test-lock.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 39948
diff changeset
9 import silenttestrunner
5d50c9ffaebb tests: fix style issue of importing order in test-lock.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 39948
diff changeset
10
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
11 from mercurial import (
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
12 encoding,
26498
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
13 error,
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
14 lock,
31249
e067741d4607 vfs: use 'vfs' module directly in 'test-lock'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28027
diff changeset
15 vfs as vfsmod,
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
16 )
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
17
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
18 testlockname = b'testlock'
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
19
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
20 # work around http://bugs.python.org/issue1515
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
21 if types.MethodType not in copy._deepcopy_dispatch:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
22
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
23 def _deepcopy_method(x, memo):
34726
daf12f69699f python3: replace im_{self,func} with __{self,func}__ globally
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
24 return type(x)(x.__func__, copy.deepcopy(x.__self__, memo), x.im_class)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
25
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
26 copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
27
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
28
26384
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
29 class lockwrapper(lock.lock):
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
30 def __init__(self, pidoffset, *args, **kwargs):
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
31 # lock.lock.__init__() calls lock(), so the pidoffset assignment needs
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
32 # to be earlier
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
33 self._pidoffset = pidoffset
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
34 super(lockwrapper, self).__init__(*args, **kwargs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
35
26384
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
36 def _getpid(self):
28027
14033c5dd261 util: enable getpid to be replaced
timeless <timeless@mozdev.org>
parents: 26499
diff changeset
37 return super(lockwrapper, self)._getpid() + self._pidoffset
26384
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
38
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
39
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
40 class teststate(object):
26385
fb1a424e8bff test-lock.py: allow PID to be changed in test state
Siddharth Agarwal <sid0@fb.com>
parents: 26384
diff changeset
41 def __init__(self, testcase, dir, pidoffset=0):
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
42 self._testcase = testcase
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
43 self._acquirecalled = False
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
44 self._releasecalled = False
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
45 self._postreleasecalled = False
31249
e067741d4607 vfs: use 'vfs' module directly in 'test-lock'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28027
diff changeset
46 self.vfs = vfsmod.vfs(dir, audit=False)
26385
fb1a424e8bff test-lock.py: allow PID to be changed in test state
Siddharth Agarwal <sid0@fb.com>
parents: 26384
diff changeset
47 self._pidoffset = pidoffset
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
48
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
49 def makelock(self, *args, **kwargs):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
50 l = lockwrapper(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
51 self._pidoffset,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
52 self.vfs,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
53 testlockname,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
54 releasefn=self.releasefn,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
55 acquirefn=self.acquirefn,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
56 *args,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
57 **kwargs
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
58 )
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
59 l.postrelease.append(self.postreleasefn)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
60 return l
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
61
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
62 def acquirefn(self):
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
63 self._acquirecalled = True
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
64
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
65 def releasefn(self):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
66 self._releasecalled = True
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
67
43778
888bd39ed555 lock: pass "success" boolean to _afterlock callbacks
Kyle Lippincott <spectral@google.com>
parents: 43076
diff changeset
68 def postreleasefn(self, success):
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
69 self._postreleasecalled = True
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
70
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
71 def assertacquirecalled(self, called):
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
72 self._testcase.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
73 self._acquirecalled,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
74 called,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
75 'expected acquire to be %s but was actually %s'
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
76 % (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
77 self._tocalled(called),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
78 self._tocalled(self._acquirecalled),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
79 ),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
80 )
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
81
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
82 def resetacquirefn(self):
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
83 self._acquirecalled = False
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
84
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
85 def assertreleasecalled(self, called):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
86 self._testcase.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
87 self._releasecalled,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
88 called,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
89 'expected release to be %s but was actually %s'
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
90 % (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
91 self._tocalled(called),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
92 self._tocalled(self._releasecalled),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
93 ),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
94 )
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
95
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
96 def assertpostreleasecalled(self, called):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
97 self._testcase.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
98 self._postreleasecalled,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
99 called,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
100 'expected postrelease to be %s but was actually %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
101 % (
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
102 self._tocalled(called),
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
103 self._tocalled(self._postreleasecalled),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
104 ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
105 )
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
106
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
107 def assertlockexists(self, exists):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
108 actual = self.vfs.lexists(testlockname)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
109 self._testcase.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
110 actual,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
111 exists,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
112 'expected lock to %s but actually did %s'
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
113 % (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
114 self._toexists(exists),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
115 self._toexists(actual),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45519
diff changeset
116 ),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
117 )
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
118
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
119 def _tocalled(self, called):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
120 if called:
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
121 return 'called'
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
122 else:
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
123 return 'not called'
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
124
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
125 def _toexists(self, exists):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
126 if exists:
26381
94dc10834b79 test-lock.py: copy-edit assertions about file existing
Siddharth Agarwal <sid0@fb.com>
parents: 26321
diff changeset
127 return 'exist'
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
128 else:
26381
94dc10834b79 test-lock.py: copy-edit assertions about file existing
Siddharth Agarwal <sid0@fb.com>
parents: 26321
diff changeset
129 return 'not exist'
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
130
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
131
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
132 class testlock(unittest.TestCase):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
133 def testlock(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
134 state = teststate(self, tempfile.mkdtemp(dir=encoding.getcwd()))
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
135 lock = state.makelock()
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
136 state.assertacquirecalled(True)
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
137 lock.release()
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
138 state.assertreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
139 state.assertpostreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
140 state.assertlockexists(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
141
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
142 def testrecursivelock(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
143 state = teststate(self, tempfile.mkdtemp(dir=encoding.getcwd()))
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
144 lock = state.makelock()
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
145 state.assertacquirecalled(True)
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
146
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
147 state.resetacquirefn()
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
148 lock.lock()
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
149 # recursive lock should not call acquirefn again
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
150 state.assertacquirecalled(False)
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
151
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
152 lock.release() # brings lock refcount down from 2 to 1
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
153 state.assertreleasecalled(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
154 state.assertpostreleasecalled(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
155 state.assertlockexists(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
156
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
157 lock.release() # releases the lock
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
158 state.assertreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
159 state.assertpostreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
160 state.assertlockexists(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
161
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
162 def testlockfork(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
163 state = teststate(self, tempfile.mkdtemp(dir=encoding.getcwd()))
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
164 lock = state.makelock()
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
165 state.assertacquirecalled(True)
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
166
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
167 # fake a fork
41481
5880b4e762cd tests: perform a shallow copy instead of a deep copy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40204
diff changeset
168 forklock = copy.copy(lock)
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
169 forklock._pidoffset = 1
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
170 forklock.release()
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
171 state.assertreleasecalled(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
172 state.assertpostreleasecalled(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
173 state.assertlockexists(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
174
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
175 # release the actual lock
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
176 lock.release()
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
177 state.assertreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
178 state.assertpostreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
179 state.assertlockexists(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
180
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
181 def testfrequentlockunlock(self):
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
182 """This tests whether lock acquisition fails as expected, even if
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
183 (1) lock can't be acquired (makelock fails by EEXIST), and
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
184 (2) locker info can't be read in (readlock fails by ENOENT) while
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
185 retrying 5 times.
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
186 """
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
187
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
188 d = tempfile.mkdtemp(dir=encoding.getcwd())
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
189 state = teststate(self, d)
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
190
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
191 def emulatefrequentlock(*args):
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
192 raise OSError(errno.EEXIST, "File exists")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
193
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
194 def emulatefrequentunlock(*args):
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
195 raise OSError(errno.ENOENT, "No such file or directory")
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
196
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
197 state.vfs.makelock = emulatefrequentlock
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
198 state.vfs.readlock = emulatefrequentunlock
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
199
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
200 try:
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
201 state.makelock(timeout=0)
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
202 self.fail("unexpected lock acquisition")
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
203 except error.LockHeld as why:
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
204 self.assertTrue(why.errno == errno.ETIMEDOUT)
41482
b58d608ec6a0 tests: compare against a bytes in test-lock.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41481
diff changeset
205 self.assertTrue(why.locker == b"")
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
206 state.assertlockexists(False)
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
207
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
208
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
209 if __name__ == '__main__':
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
210 silenttestrunner.main(__name__)