Mercurial > public > mercurial-scm > hg-stable
annotate tests/test-lock.py @ 26473:5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Review feedback from Pierre-Yves David. This makes the overall code cleaner and
less error-prone, and makes a previously explicitly checked error state
impossible.
author | Siddharth Agarwal <sid0@fb.com> |
---|---|
date | Sun, 04 Oct 2015 20:02:50 -0700 |
parents | e16f80f89a29 |
children | 431094a3b21f |
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 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
4 import os |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
5 import silenttestrunner |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
6 import tempfile |
26386
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
7 import types |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
8 import unittest |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
9 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
10 from mercurial import ( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
11 lock, |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
12 scmutil, |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
13 ) |
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 testlockname = 'testlock' |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
16 |
26386
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
17 # work around http://bugs.python.org/issue1515 |
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
18 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
|
19 def _deepcopy_method(x, memo): |
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
20 return type(x)(x.im_func, copy.deepcopy(x.im_self, memo), x.im_class) |
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
21 copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method |
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
22 |
26384
ad6e56d01c30
test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents:
26382
diff
changeset
|
23 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
|
24 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
|
25 # 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
|
26 # 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
|
27 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
|
28 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
|
29 def _getpid(self): |
ad6e56d01c30
test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents:
26382
diff
changeset
|
30 return os.getpid() + self._pidoffset |
ad6e56d01c30
test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents:
26382
diff
changeset
|
31 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
32 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
|
33 def __init__(self, testcase, dir, pidoffset=0): |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
34 self._testcase = testcase |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
35 self._acquirecalled = False |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
36 self._releasecalled = False |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
37 self._postreleasecalled = False |
26382
b673e89affc9
test-lock.py: move temp dir generation to testcase
Siddharth Agarwal <sid0@fb.com>
parents:
26381
diff
changeset
|
38 self.vfs = scmutil.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
|
39 self._pidoffset = pidoffset |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
40 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
41 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
|
42 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
|
43 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
|
44 *args, **kwargs) |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
45 l.postrelease.append(self.postreleasefn) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
46 return l |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
47 |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
48 def acquirefn(self): |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
49 self._acquirecalled = True |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
50 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
51 def releasefn(self): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
52 self._releasecalled = True |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
53 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
54 def postreleasefn(self): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
55 self._postreleasecalled = True |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
56 |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
57 def assertacquirecalled(self, called): |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
58 self._testcase.assertEqual( |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
59 self._acquirecalled, called, |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
60 '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
|
61 self._tocalled(called), |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
62 self._tocalled(self._acquirecalled), |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
63 )) |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
64 |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
65 def resetacquirefn(self): |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
66 self._acquirecalled = False |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
67 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
68 def assertreleasecalled(self, called): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
69 self._testcase.assertEqual( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
70 self._releasecalled, called, |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
71 '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
|
72 self._tocalled(called), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
73 self._tocalled(self._releasecalled), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
74 )) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
75 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
76 def assertpostreleasecalled(self, called): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
77 self._testcase.assertEqual( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
78 self._postreleasecalled, called, |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
79 '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
|
80 self._tocalled(called), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
81 self._tocalled(self._postreleasecalled), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
82 )) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
83 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
84 def assertlockexists(self, exists): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
85 actual = self.vfs.lexists(testlockname) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
86 self._testcase.assertEqual( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
87 actual, exists, |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
88 'expected lock to %s but actually did %s' % ( |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
89 self._toexists(exists), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
90 self._toexists(actual), |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
91 )) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
92 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
93 def _tocalled(self, called): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
94 if called: |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
95 return 'called' |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
96 else: |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
97 return 'not called' |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
98 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
99 def _toexists(self, exists): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
100 if exists: |
26381
94dc10834b79
test-lock.py: copy-edit assertions about file existing
Siddharth Agarwal <sid0@fb.com>
parents:
26321
diff
changeset
|
101 return 'exist' |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
102 else: |
26381
94dc10834b79
test-lock.py: copy-edit assertions about file existing
Siddharth Agarwal <sid0@fb.com>
parents:
26321
diff
changeset
|
103 return 'not exist' |
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 class testlock(unittest.TestCase): |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
106 def testlock(self): |
26382
b673e89affc9
test-lock.py: move temp dir generation to testcase
Siddharth Agarwal <sid0@fb.com>
parents:
26381
diff
changeset
|
107 state = teststate(self, tempfile.mkdtemp(dir=os.getcwd())) |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
108 lock = state.makelock() |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
109 state.assertacquirecalled(True) |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
110 lock.release() |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
111 state.assertreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
112 state.assertpostreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
113 state.assertlockexists(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
114 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
115 def testrecursivelock(self): |
26382
b673e89affc9
test-lock.py: move temp dir generation to testcase
Siddharth Agarwal <sid0@fb.com>
parents:
26381
diff
changeset
|
116 state = teststate(self, tempfile.mkdtemp(dir=os.getcwd())) |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
117 lock = state.makelock() |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
118 state.assertacquirecalled(True) |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
119 |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
120 state.resetacquirefn() |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
121 lock.lock() |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
122 # 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
|
123 state.assertacquirecalled(False) |
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
124 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
125 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
|
126 state.assertreleasecalled(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
127 state.assertpostreleasecalled(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
128 state.assertlockexists(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
129 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
130 lock.release() # releases the lock |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
131 state.assertreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
132 state.assertpostreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
133 state.assertlockexists(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
134 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
135 def testlockfork(self): |
26382
b673e89affc9
test-lock.py: move temp dir generation to testcase
Siddharth Agarwal <sid0@fb.com>
parents:
26381
diff
changeset
|
136 state = teststate(self, tempfile.mkdtemp(dir=os.getcwd())) |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
137 lock = state.makelock() |
26321
db4c192cb9b3
lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents:
26289
diff
changeset
|
138 state.assertacquirecalled(True) |
26386
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
139 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
140 # fake a fork |
26386
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
141 forklock = copy.deepcopy(lock) |
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
142 forklock._pidoffset = 1 |
146cccdb282b
test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents:
26385
diff
changeset
|
143 forklock.release() |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
144 state.assertreleasecalled(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
145 state.assertpostreleasecalled(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
146 state.assertlockexists(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
147 |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
148 # release the actual lock |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
149 lock.release() |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
150 state.assertreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
151 state.assertpostreleasecalled(True) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
152 state.assertlockexists(False) |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
153 |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
154 def testinheritlock(self): |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
155 d = tempfile.mkdtemp(dir=os.getcwd()) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
156 parentstate = teststate(self, d) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
157 parentlock = parentstate.makelock() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
158 parentstate.assertacquirecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
159 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
160 # set up lock inheritance |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
161 with parentlock.inherit() as lockname: |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
162 parentstate.assertreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
163 parentstate.assertpostreleasecalled(False) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
164 parentstate.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
165 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
166 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
|
167 childlock = childstate.makelock(parentlock=lockname) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
168 childstate.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
169 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
170 childlock.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
171 childstate.assertreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
172 childstate.assertpostreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
173 childstate.assertlockexists(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
174 |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
175 parentstate.resetacquirefn() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
176 |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
177 parentstate.assertacquirecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
178 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
179 parentlock.release() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
180 parentstate.assertreleasecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
181 parentstate.assertpostreleasecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
182 parentstate.assertlockexists(False) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
183 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
184 def testmultilock(self): |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
185 d = tempfile.mkdtemp(dir=os.getcwd()) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
186 state0 = teststate(self, d) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
187 lock0 = state0.makelock() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
188 state0.assertacquirecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
189 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
190 with lock0.inherit() as lock0name: |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
191 state0.assertreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
192 state0.assertpostreleasecalled(False) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
193 state0.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
194 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
195 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
|
196 lock1 = state1.makelock(parentlock=lock0name) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
197 state1.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
198 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
199 # 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
|
200 with lock1.inherit() as lock1name: |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
201 # 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
|
202 # name |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
203 self.assertEqual(lock0name, lock1name) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
204 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
205 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
|
206 lock2 = state2.makelock(parentlock=lock1name) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
207 state2.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
208 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
209 lock2.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
210 state2.assertreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
211 state2.assertpostreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
212 state2.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
213 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
214 state1.resetacquirefn() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
215 |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
216 state1.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
217 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
218 lock1.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
219 state1.assertreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
220 state1.assertpostreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
221 state1.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
222 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
223 lock0.release() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
224 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
225 def testinheritlockfork(self): |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
226 d = tempfile.mkdtemp(dir=os.getcwd()) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
227 parentstate = teststate(self, d) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
228 parentlock = parentstate.makelock() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
229 parentstate.assertacquirecalled(True) |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
230 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
231 # set up lock inheritance |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
232 with parentlock.inherit() as lockname: |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
233 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
|
234 childlock = childstate.makelock(parentlock=lockname) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
235 childstate.assertacquirecalled(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
236 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
237 # fork the child lock |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
238 forkchildlock = copy.deepcopy(childlock) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
239 forkchildlock._pidoffset += 1 |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
240 forkchildlock.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
241 childstate.assertreleasecalled(False) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
242 childstate.assertpostreleasecalled(False) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
243 childstate.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
244 |
26473
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
245 # release the child lock |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
246 childlock.release() |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
247 childstate.assertreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
248 childstate.assertpostreleasecalled(True) |
5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents:
26387
diff
changeset
|
249 childstate.assertlockexists(True) |
26387
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
250 |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
251 parentlock.release() |
e16f80f89a29
lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents:
26386
diff
changeset
|
252 |
26289
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
253 if __name__ == '__main__': |
c4b667a7a51d
tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff
changeset
|
254 silenttestrunner.main(__name__) |