annotate tests/test-lock.py @ 51370:0d414fb8336f

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