Mercurial > hg
annotate tests/test-lock.py @ 42044:bb271ec2fbfb
compression: introduce a `storage.revlog.zstd.level` configuration
This option control the zstd compression level used when compressing revlog
chunk. The usage of zstd for revlog compression has not graduated from
experimental yet, but we intend to fix that soon.
The option name for the compression level is more straight forward to pick, so
this changesets comes first. Having a dedicated option for each compression
engine is useful because they don't support the same range of values.
I ran the same measurement as for the zlib compression level (in the parent
changesets). The variation in repository size is stay mostly in the same (small)
range. The "read/write" performance see smallish variation, but are overall much
better than zlib. Write performance show the same tend of having better write
performance for when reaching high-end compression.
Again, we don't intend to change the default zstd compression level (currently:
3) in this series. However this is worth investigating in the future.
The Performance comparison of zlib vs zstd is quite impressive. The repository
size stay in the same range, but the performance are much better in all
situations.
Comparison summary
==================
We are looking at:
- performance range for zlib
- performance range for zstd
- comparison of default zstd (level-3) to default zlib (level 6)
- comparison of the slowest zstd time to the fastest zlib time
Read performance:
-----------------
| zlib | zstd | cmp | f2s
mercurial | 0.170159 - 0.189219 | 0.144127 - 0.149624 | 80% | 88%
pypy | 2.679217 - 2.768691 | 1.532317 - 1.705044 | 60% | 63%
netbeans | 122.477027 - 141.620281 | 72.996346 - 89.731560 | 58% | 73%
mozilla | 147.867662 - 170.572118 | 91.700995 - 105.853099 | 56% | 71%
Write performance:
------------------
| zlib | zstd | cmp | f2s
mercurial | 53.250304 - 56.2936129 | 40.877025 - 45.677286 | 75% | 86%
pypy | 460.721984 - 476.589918 | 270.545409 - 301.002219 | 63% | 65%
netbeans | 520.560316 - 715.930400 | 370.356311 - 428.329652 | 55% | 82%
mozilla | 739.803002 - 987.056093 | 505.152906 - 591.930683 | 57% | 80%
Raw data
--------
repo alg lvl .hg/store size 00manifest.d read write
mercurial zlib 1 49,402,813 5,963,475 0.170159 53.250304
mercurial zlib 6 47,197,397 5,875,730 0.182820 56.264320
mercurial zlib 9 47,121,596 5,849,781 0.189219 56.293612
mercurial zstd 1 49,737,084 5,966,355 0.144127 40.877025
mercurial zstd 3 48,961,867 5,895,208 0.146376 42.268142
mercurial zstd 5 48,200,592 5,938,676 0.149624 43.162875
mercurial zstd 10 47,833,520 5,913,353 0.145185 44.012489
mercurial zstd 15 47,314,604 5,728,679 0.147686 45.677286
mercurial zstd 20 47,330,502 5,830,539 0.145789 45.025407
mercurial zstd 22 47,330,076 5,830,539 0.143996 44.690460
pypy zlib 1 370,830,572 28,462,425 2.679217 460.721984
pypy zlib 6 340,112,317 27,648,747 2.768691 467.537158
pypy zlib 9 338,360,736 27,639,003 2.763495 476.589918
pypy zstd 1 362,377,479 27,916,214 1.532317 270.545409
pypy zstd 3 354,137,693 27,905,988 1.686718 294.951509
pypy zstd 5 342,640,043 27,655,774 1.705044 301.002219
pypy zstd 10 334,224,327 27,164,493 1.567287 285.186239
pypy zstd 15 329,000,363 26,645,965 1.637729 299.561332
pypy zstd 20 324,534,039 26,199,547 1.526813 302.149827
pypy zstd 22 324,530,595 26,198,932 1.525718 307.821218
netbeans zlib 1 1,281,847,810 165,495,457 122.477027 520.560316
netbeans zlib 6 1,205,284,353 159,161,207 139.876147 715.930400
netbeans zlib 9 1,197,135,671 155,034,586 141.620281 678.297064
netbeans zstd 1 1,259,581,737 160,840,613 72.996346 370.356311
netbeans zstd 3 1,232,978,122 157,691,551 81.622317 396.733087
netbeans zstd 5 1,208,034,075 160,246,880 83.080549 364.342626
netbeans zstd 10 1,188,624,176 156,083,417 79.323935 403.594602
netbeans zstd 15 1,176,973,589 153,859,477 89.731560 428.329652
netbeans zstd 20 1,162,958,258 151,147,535 82.842667 392.335349
netbeans zstd 22 1,162,707,029 151,150,220 82.565695 402.840655
mozilla zlib 1 2,775,497,186 298,527,987 147.867662 751.263721
mozilla zlib 6 2,596,856,420 286,597,671 170.572118 987.056093
mozilla zlib 9 2,587,542,494 287,018,264 163.622338 739.803002
mozilla zstd 1 2,723,159,348 286,617,532 91.700995 570.042751
mozilla zstd 3 2,665,055,001 286,152,013 95.240155 561.412805
mozilla zstd 5 2,607,819,817 288,060,030 101.978048 505.152906
mozilla zstd 10 2,558,761,085 283,967,648 104.113481 497.771202
mozilla zstd 15 2,526,216,060 275,581,300 105.853099 591.930683
mozilla zstd 20 2,485,114,806 266,478,859 95.268795 576.515389
mozilla zstd 22 2,484,869,080 266,456,505 94.429282 572.785537
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Wed, 27 Mar 2019 18:35:59 +0100 |
parents | b58d608ec6a0 |
children | 2372284d9457 |
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: |
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
22 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
|
23 return type(x)(x.__func__, copy.deepcopy(x.__self__, memo), x.im_class) |
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 |
26384
ad6e56d01c30
test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents:
26382
diff
changeset
|
26 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
|
27 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
|
28 # 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
|
29 # 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
|
30 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
|
31 super(lockwrapper, self).__init__(*args, **kwargs) |
ad6e56d01c30
test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents:
26382
diff
changeset
|
32 def _getpid(self): |
28027
14033c5dd261
util: enable getpid to be replaced
timeless <timeless@mozdev.org>
parents:
26499
diff
changeset
|
33 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
|
34 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
35 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
|
36 def __init__(self, testcase, dir, pidoffset=0): |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
37 self._testcase = testcase |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
38 self._acquirecalled = False |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
39 self._releasecalled = False |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
40 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
|
41 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
|
42 self._pidoffset = pidoffset |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
43 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
44 def makelock(self, *args, **kwargs): |
26385
fb1a424e8bff
test-lock.py: allow PID to be changed in test state
Siddharth Agarwal <sid0@fb.com>
parents:
26384
diff
changeset
|
45 l = lockwrapper(self._pidoffset, self.vfs, testlockname, |
fb1a424e8bff
test-lock.py: allow PID to be changed in test state
Siddharth Agarwal <sid0@fb.com>
parents:
26384
diff
changeset
|
46 releasefn=self.releasefn, acquirefn=self.acquirefn, |
fb1a424e8bff
test-lock.py: allow PID to be changed in test state
Siddharth Agarwal <sid0@fb.com>
parents:
26384
diff
changeset
|
47 *args, **kwargs) |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
48 l.postrelease.append(self.postreleasefn) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
49 return l |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
50 |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
51 def acquirefn(self): |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
52 self._acquirecalled = True |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
53 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
54 def releasefn(self): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
55 self._releasecalled = True |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
56 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
57 def postreleasefn(self): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
58 self._postreleasecalled = True |
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 assertacquirecalled(self, called): |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
61 self._testcase.assertEqual( |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
62 self._acquirecalled, called, |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
63 'expected acquire to be %s but was actually %s' % ( |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
64 self._tocalled(called), |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
65 self._tocalled(self._acquirecalled), |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
66 )) |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
67 |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
68 def resetacquirefn(self): |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
69 self._acquirecalled = False |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
70 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
71 def assertreleasecalled(self, called): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
72 self._testcase.assertEqual( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
73 self._releasecalled, called, |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
74 'expected release to be %s but was actually %s' % ( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
75 self._tocalled(called), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
76 self._tocalled(self._releasecalled), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
77 )) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
78 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
79 def assertpostreleasecalled(self, called): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
80 self._testcase.assertEqual( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
81 self._postreleasecalled, called, |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
82 'expected postrelease to be %s but was actually %s' % ( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
83 self._tocalled(called), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
84 self._tocalled(self._postreleasecalled), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
85 )) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
86 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
87 def assertlockexists(self, exists): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
88 actual = self.vfs.lexists(testlockname) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
89 self._testcase.assertEqual( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
90 actual, exists, |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
91 'expected lock to %s but actually did %s' % ( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
92 self._toexists(exists), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
93 self._toexists(actual), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
94 )) |
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 _tocalled(self, called): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
97 if called: |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
98 return 'called' |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
99 else: |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
100 return 'not called' |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
101 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
102 def _toexists(self, exists): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
103 if exists: |
26381
94dc10834b79
test-lock.py: copy-edit assertions about file existing
Siddharth Agarwal <sid0@fb.com>
parents:
26321
diff
changeset
|
104 return 'exist' |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
105 else: |
26381
94dc10834b79
test-lock.py: copy-edit assertions about file existing
Siddharth Agarwal <sid0@fb.com>
parents:
26321
diff
changeset
|
106 return 'not exist' |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
107 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
108 class testlock(unittest.TestCase): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
109 def testlock(self): |
39948
5ee3146c1b20
py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
34726
diff
changeset
|
110 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
|
111 lock = state.makelock() |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
112 state.assertacquirecalled(True) |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
113 lock.release() |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
114 state.assertreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
115 state.assertpostreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
116 state.assertlockexists(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
117 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
118 def testrecursivelock(self): |
39948
5ee3146c1b20
py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
34726
diff
changeset
|
119 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
|
120 lock = state.makelock() |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
121 state.assertacquirecalled(True) |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
122 |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
123 state.resetacquirefn() |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
124 lock.lock() |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
125 # 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
|
126 state.assertacquirecalled(False) |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
127 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
128 lock.release() # brings lock refcount down from 2 to 1 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
129 state.assertreleasecalled(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
130 state.assertpostreleasecalled(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
131 state.assertlockexists(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
132 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
133 lock.release() # releases the lock |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
134 state.assertreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
135 state.assertpostreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
136 state.assertlockexists(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
137 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
138 def testlockfork(self): |
39948
5ee3146c1b20
py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
34726
diff
changeset
|
139 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
|
140 lock = state.makelock() |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
141 state.assertacquirecalled(True) |
26386
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
142 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
143 # 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
|
144 forklock = copy.copy(lock) |
26386
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
145 forklock._pidoffset = 1 |
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
146 forklock.release() |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
147 state.assertreleasecalled(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
148 state.assertpostreleasecalled(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
149 state.assertlockexists(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
150 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
151 # release the actual lock |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
152 lock.release() |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
153 state.assertreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
154 state.assertpostreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
155 state.assertlockexists(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
156 |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
157 def testinheritlock(self): |
39948
5ee3146c1b20
py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
34726
diff
changeset
|
158 d = tempfile.mkdtemp(dir=encoding.getcwd()) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
159 parentstate = teststate(self, d) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
160 parentlock = parentstate.makelock() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
161 parentstate.assertacquirecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
162 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
163 # set up lock inheritance |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
164 with parentlock.inherit() as lockname: |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
165 parentstate.assertreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
166 parentstate.assertpostreleasecalled(False) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
167 parentstate.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
168 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
169 childstate = teststate(self, d, pidoffset=1) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
170 childlock = childstate.makelock(parentlock=lockname) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
171 childstate.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
172 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
173 childlock.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
174 childstate.assertreleasecalled(True) |
26474
431094a3b21f
lock.release: don't call postrelease functions for inherited locks
Siddharth Agarwal <sid0@fb.com>
parents:
26473
diff
changeset
|
175 childstate.assertpostreleasecalled(False) |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
176 childstate.assertlockexists(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
177 |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
178 parentstate.resetacquirefn() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
179 |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
180 parentstate.assertacquirecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
181 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
182 parentlock.release() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
183 parentstate.assertreleasecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
184 parentstate.assertpostreleasecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
185 parentstate.assertlockexists(False) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
186 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
187 def testmultilock(self): |
39948
5ee3146c1b20
py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
34726
diff
changeset
|
188 d = tempfile.mkdtemp(dir=encoding.getcwd()) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
189 state0 = teststate(self, d) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
190 lock0 = state0.makelock() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
191 state0.assertacquirecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
192 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
193 with lock0.inherit() as lock0name: |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
194 state0.assertreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
195 state0.assertpostreleasecalled(False) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
196 state0.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
197 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
198 state1 = teststate(self, d, pidoffset=1) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
199 lock1 = state1.makelock(parentlock=lock0name) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
200 state1.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
201 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
202 # from within lock1, acquire another lock |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
203 with lock1.inherit() as lock1name: |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
204 # since the file on disk is lock0's this should have the same |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
205 # name |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
206 self.assertEqual(lock0name, lock1name) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
207 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
208 state2 = teststate(self, d, pidoffset=2) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
209 lock2 = state2.makelock(parentlock=lock1name) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
210 state2.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
211 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
212 lock2.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
213 state2.assertreleasecalled(True) |
26474
431094a3b21f
lock.release: don't call postrelease functions for inherited locks
Siddharth Agarwal <sid0@fb.com>
parents:
26473
diff
changeset
|
214 state2.assertpostreleasecalled(False) |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
215 state2.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
216 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
217 state1.resetacquirefn() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
218 |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
219 state1.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
220 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
221 lock1.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
222 state1.assertreleasecalled(True) |
26474
431094a3b21f
lock.release: don't call postrelease functions for inherited locks
Siddharth Agarwal <sid0@fb.com>
parents:
26473
diff
changeset
|
223 state1.assertpostreleasecalled(False) |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
224 state1.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
225 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
226 lock0.release() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
227 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
228 def testinheritlockfork(self): |
39948
5ee3146c1b20
py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
34726
diff
changeset
|
229 d = tempfile.mkdtemp(dir=encoding.getcwd()) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
230 parentstate = teststate(self, d) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
231 parentlock = parentstate.makelock() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
232 parentstate.assertacquirecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
233 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
234 # set up lock inheritance |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
235 with parentlock.inherit() as lockname: |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
236 childstate = teststate(self, d, pidoffset=1) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
237 childlock = childstate.makelock(parentlock=lockname) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
238 childstate.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
239 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
240 # fork the child lock |
41481
5880b4e762cd
tests: perform a shallow copy instead of a deep copy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40204
diff
changeset
|
241 forkchildlock = copy.copy(childlock) |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
242 forkchildlock._pidoffset += 1 |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
243 forkchildlock.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
244 childstate.assertreleasecalled(False) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
245 childstate.assertpostreleasecalled(False) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
246 childstate.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
247 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
248 # release the child lock |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
249 childlock.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
250 childstate.assertreleasecalled(True) |
26474
431094a3b21f
lock.release: don't call postrelease functions for inherited locks
Siddharth Agarwal <sid0@fb.com>
parents:
26473
diff
changeset
|
251 childstate.assertpostreleasecalled(False) |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
252 childstate.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
253 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
254 parentlock.release() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
255 |
26498
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
256 def testinheritcheck(self): |
39948
5ee3146c1b20
py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
34726
diff
changeset
|
257 d = tempfile.mkdtemp(dir=encoding.getcwd()) |
26498
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
258 state = teststate(self, d) |
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
259 def check(): |
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
260 raise error.LockInheritanceContractViolation('check failed') |
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
261 lock = state.makelock(inheritchecker=check) |
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
262 state.assertacquirecalled(True) |
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
263 |
32279
68c43a416585
tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32088
diff
changeset
|
264 with self.assertRaises(error.LockInheritanceContractViolation): |
26499
e72b62b154b0
localrepo: prevent wlock from being inherited when a transaction is running
Siddharth Agarwal <sid0@fb.com>
parents:
26498
diff
changeset
|
265 with lock.inherit(): |
26498
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
266 pass |
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
267 |
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
268 lock.release() |
e8564e04382d
lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents:
26474
diff
changeset
|
269 |
32088
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
270 def testfrequentlockunlock(self): |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
271 """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
|
272 (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
|
273 (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
|
274 retrying 5 times. |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
275 """ |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
276 |
39948
5ee3146c1b20
py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
34726
diff
changeset
|
277 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
|
278 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
|
279 |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
280 def emulatefrequentlock(*args): |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
281 raise OSError(errno.EEXIST, "File exists") |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
282 def emulatefrequentunlock(*args): |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
283 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
|
284 |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
285 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
|
286 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
|
287 |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
288 try: |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
289 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
|
290 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
|
291 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
|
292 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
|
293 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
|
294 state.assertlockexists(False) |
0d892d820a51
lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
31249
diff
changeset
|
295 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
296 if __name__ == '__main__': |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
297 silenttestrunner.main(__name__) |