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