Show More
@@ -1,137 +1,136 b'' | |||
|
1 | 1 | from __future__ import absolute_import |
|
2 | 2 | |
|
3 | 3 | import os |
|
4 | 4 | import silenttestrunner |
|
5 | 5 | import tempfile |
|
6 | 6 | import unittest |
|
7 | 7 | |
|
8 | 8 | from mercurial import ( |
|
9 | 9 | lock, |
|
10 | 10 | scmutil, |
|
11 | 11 | ) |
|
12 | 12 | |
|
13 | 13 | testlockname = 'testlock' |
|
14 | 14 | |
|
15 | 15 | class teststate(object): |
|
16 | def __init__(self, testcase): | |
|
16 | def __init__(self, testcase, dir): | |
|
17 | 17 | self._testcase = testcase |
|
18 | 18 | self._acquirecalled = False |
|
19 | 19 | self._releasecalled = False |
|
20 | 20 | self._postreleasecalled = False |
|
21 | d = tempfile.mkdtemp(dir=os.getcwd()) | |
|
22 | self.vfs = scmutil.vfs(d, audit=False) | |
|
21 | self.vfs = scmutil.vfs(dir, audit=False) | |
|
23 | 22 | |
|
24 | 23 | def makelock(self, *args, **kwargs): |
|
25 | 24 | l = lock.lock(self.vfs, testlockname, releasefn=self.releasefn, |
|
26 | 25 | acquirefn=self.acquirefn, *args, **kwargs) |
|
27 | 26 | l.postrelease.append(self.postreleasefn) |
|
28 | 27 | return l |
|
29 | 28 | |
|
30 | 29 | def acquirefn(self): |
|
31 | 30 | self._acquirecalled = True |
|
32 | 31 | |
|
33 | 32 | def releasefn(self): |
|
34 | 33 | self._releasecalled = True |
|
35 | 34 | |
|
36 | 35 | def postreleasefn(self): |
|
37 | 36 | self._postreleasecalled = True |
|
38 | 37 | |
|
39 | 38 | def assertacquirecalled(self, called): |
|
40 | 39 | self._testcase.assertEqual( |
|
41 | 40 | self._acquirecalled, called, |
|
42 | 41 | 'expected acquire to be %s but was actually %s' % ( |
|
43 | 42 | self._tocalled(called), |
|
44 | 43 | self._tocalled(self._acquirecalled), |
|
45 | 44 | )) |
|
46 | 45 | |
|
47 | 46 | def resetacquirefn(self): |
|
48 | 47 | self._acquirecalled = False |
|
49 | 48 | |
|
50 | 49 | def assertreleasecalled(self, called): |
|
51 | 50 | self._testcase.assertEqual( |
|
52 | 51 | self._releasecalled, called, |
|
53 | 52 | 'expected release to be %s but was actually %s' % ( |
|
54 | 53 | self._tocalled(called), |
|
55 | 54 | self._tocalled(self._releasecalled), |
|
56 | 55 | )) |
|
57 | 56 | |
|
58 | 57 | def assertpostreleasecalled(self, called): |
|
59 | 58 | self._testcase.assertEqual( |
|
60 | 59 | self._postreleasecalled, called, |
|
61 | 60 | 'expected postrelease to be %s but was actually %s' % ( |
|
62 | 61 | self._tocalled(called), |
|
63 | 62 | self._tocalled(self._postreleasecalled), |
|
64 | 63 | )) |
|
65 | 64 | |
|
66 | 65 | def assertlockexists(self, exists): |
|
67 | 66 | actual = self.vfs.lexists(testlockname) |
|
68 | 67 | self._testcase.assertEqual( |
|
69 | 68 | actual, exists, |
|
70 | 69 | 'expected lock to %s but actually did %s' % ( |
|
71 | 70 | self._toexists(exists), |
|
72 | 71 | self._toexists(actual), |
|
73 | 72 | )) |
|
74 | 73 | |
|
75 | 74 | def _tocalled(self, called): |
|
76 | 75 | if called: |
|
77 | 76 | return 'called' |
|
78 | 77 | else: |
|
79 | 78 | return 'not called' |
|
80 | 79 | |
|
81 | 80 | def _toexists(self, exists): |
|
82 | 81 | if exists: |
|
83 | 82 | return 'exist' |
|
84 | 83 | else: |
|
85 | 84 | return 'not exist' |
|
86 | 85 | |
|
87 | 86 | class testlock(unittest.TestCase): |
|
88 | 87 | def testlock(self): |
|
89 | state = teststate(self) | |
|
88 | state = teststate(self, tempfile.mkdtemp(dir=os.getcwd())) | |
|
90 | 89 | lock = state.makelock() |
|
91 | 90 | state.assertacquirecalled(True) |
|
92 | 91 | lock.release() |
|
93 | 92 | state.assertreleasecalled(True) |
|
94 | 93 | state.assertpostreleasecalled(True) |
|
95 | 94 | state.assertlockexists(False) |
|
96 | 95 | |
|
97 | 96 | def testrecursivelock(self): |
|
98 | state = teststate(self) | |
|
97 | state = teststate(self, tempfile.mkdtemp(dir=os.getcwd())) | |
|
99 | 98 | lock = state.makelock() |
|
100 | 99 | state.assertacquirecalled(True) |
|
101 | 100 | |
|
102 | 101 | state.resetacquirefn() |
|
103 | 102 | lock.lock() |
|
104 | 103 | # recursive lock should not call acquirefn again |
|
105 | 104 | state.assertacquirecalled(False) |
|
106 | 105 | |
|
107 | 106 | lock.release() # brings lock refcount down from 2 to 1 |
|
108 | 107 | state.assertreleasecalled(False) |
|
109 | 108 | state.assertpostreleasecalled(False) |
|
110 | 109 | state.assertlockexists(True) |
|
111 | 110 | |
|
112 | 111 | lock.release() # releases the lock |
|
113 | 112 | state.assertreleasecalled(True) |
|
114 | 113 | state.assertpostreleasecalled(True) |
|
115 | 114 | state.assertlockexists(False) |
|
116 | 115 | |
|
117 | 116 | def testlockfork(self): |
|
118 | state = teststate(self) | |
|
117 | state = teststate(self, tempfile.mkdtemp(dir=os.getcwd())) | |
|
119 | 118 | lock = state.makelock() |
|
120 | 119 | state.assertacquirecalled(True) |
|
121 | 120 | lock.lock() |
|
122 | 121 | # fake a fork |
|
123 | 122 | lock.pid += 1 |
|
124 | 123 | lock.release() |
|
125 | 124 | state.assertreleasecalled(False) |
|
126 | 125 | state.assertpostreleasecalled(False) |
|
127 | 126 | state.assertlockexists(True) |
|
128 | 127 | |
|
129 | 128 | # release the actual lock |
|
130 | 129 | lock.pid -= 1 |
|
131 | 130 | lock.release() |
|
132 | 131 | state.assertreleasecalled(True) |
|
133 | 132 | state.assertpostreleasecalled(True) |
|
134 | 133 | state.assertlockexists(False) |
|
135 | 134 | |
|
136 | 135 | if __name__ == '__main__': |
|
137 | 136 | silenttestrunner.main(__name__) |
General Comments 0
You need to be logged in to leave comments.
Login now