2872.5.1
by Martin Pool
Avoid internal error tracebacks on failure to lock on readonly transport (#129701). |
1 |
# Copyright (C) 2006, 2007 Canonical Ltd
|
1887.1.1
by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines, |
2 |
#
|
1553.5.12
by Martin Pool
New LockDir locking mechanism |
3 |
# This program is free software; you can redistribute it and/or modify
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
1887.1.1
by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines, |
7 |
#
|
1553.5.12
by Martin Pool
New LockDir locking mechanism |
8 |
# This program is distributed in the hope that it will be useful,
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
1887.1.1
by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines, |
12 |
#
|
1553.5.12
by Martin Pool
New LockDir locking mechanism |
13 |
# You should have received a copy of the GNU General Public License
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""Tests for LockDir"""
|
|
18 |
||
1687.1.5
by Robert Collins
Add break_lock utility function to LockDir. |
19 |
from cStringIO import StringIO |
1551.10.3
by Aaron Bentley
Lock attempts don't treat permission problems as lock contention |
20 |
import os |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
21 |
from threading import Thread, Lock |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
22 |
import time |
23 |
||
1687.1.5
by Robert Collins
Add break_lock utility function to LockDir. |
24 |
import bzrlib |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
25 |
from bzrlib import ( |
2055.2.1
by John Arbash Meinel
Make LockDir less sensitive to invalid configuration of email |
26 |
config, |
1551.10.3
by Aaron Bentley
Lock attempts don't treat permission problems as lock contention |
27 |
errors, |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
28 |
osutils, |
1551.10.4
by Aaron Bentley
Update to skip on win32 |
29 |
tests, |
2555.3.9
by Martin Pool
Add test and fix for locking robustly when rename of directories doesn't act as a mutex (thank John) |
30 |
transport, |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
31 |
)
|
1553.5.27
by Martin Pool
Confirm that only the intended holder of a lock was broken. |
32 |
from bzrlib.errors import ( |
2872.5.1
by Martin Pool
Avoid internal error tracebacks on failure to lock on readonly transport (#129701). |
33 |
LockBreakMismatch, |
34 |
LockBroken, |
|
35 |
LockContention, |
|
36 |
LockError, |
|
37 |
LockFailed, |
|
38 |
LockNotHeld, |
|
39 |
)
|
|
2381.1.4
by Robert Collins
Unbreak lockdir tests after adding fast lockdir timeouts to the test suite default environment. |
40 |
from bzrlib.lockdir import LockDir |
1553.5.33
by Martin Pool
LockDir review comment fixes |
41 |
from bzrlib.tests import TestCaseWithTransport |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
42 |
from bzrlib.trace import note |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
43 |
|
44 |
# These tests sometimes use threads to test the behaviour of lock files with
|
|
45 |
# concurrent actors. This is not a typical (or necessarily supported) use;
|
|
46 |
# they're really meant for guarding between processes.
|
|
47 |
||
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
48 |
# These tests are run on the default transport provided by the test framework
|
49 |
# (typically a local disk transport). That can be changed by the --transport
|
|
50 |
# option to bzr selftest. The required properties of the transport
|
|
51 |
# implementation are tested separately. (The main requirement is just that
|
|
52 |
# they don't allow overwriting nonempty directories.)
|
|
53 |
||
1553.5.12
by Martin Pool
New LockDir locking mechanism |
54 |
class TestLockDir(TestCaseWithTransport): |
55 |
"""Test LockDir operations"""
|
|
56 |
||
1957.1.1
by John Arbash Meinel
Report to the user when we are spinning on a lock |
57 |
def logging_report_function(self, fmt, *args): |
58 |
self._logged_reports.append((fmt, args)) |
|
59 |
||
60 |
def setup_log_reporter(self, lock_dir): |
|
61 |
self._logged_reports = [] |
|
62 |
lock_dir._report_function = self.logging_report_function |
|
63 |
||
1553.5.12
by Martin Pool
New LockDir locking mechanism |
64 |
def test_00_lock_creation(self): |
65 |
"""Creation of lock file on a transport"""
|
|
66 |
t = self.get_transport() |
|
67 |
lf = LockDir(t, 'test_lock') |
|
1553.5.13
by Martin Pool
New Transport.rename that mustn't overwrite |
68 |
self.assertFalse(lf.is_held) |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
69 |
|
70 |
def test_01_lock_repr(self): |
|
71 |
"""Lock string representation"""
|
|
72 |
lf = LockDir(self.get_transport(), 'test_lock') |
|
73 |
r = repr(lf) |
|
74 |
self.assertContainsRe(r, r'^LockDir\(.*/test_lock\)$') |
|
75 |
||
76 |
def test_02_unlocked_peek(self): |
|
77 |
lf = LockDir(self.get_transport(), 'test_lock') |
|
78 |
self.assertEqual(lf.peek(), None) |
|
79 |
||
1687.1.3
by Robert Collins
Make LockDir.unlock check the lock is still intact. |
80 |
def get_lock(self): |
81 |
return LockDir(self.get_transport(), 'test_lock') |
|
82 |
||
83 |
def test_unlock_after_break_raises(self): |
|
84 |
ld = self.get_lock() |
|
85 |
ld2 = self.get_lock() |
|
86 |
ld.create() |
|
87 |
ld.attempt_lock() |
|
88 |
ld2.force_break(ld2.peek()) |
|
89 |
self.assertRaises(LockBroken, ld.unlock) |
|
90 |
||
1553.5.12
by Martin Pool
New LockDir locking mechanism |
91 |
def test_03_readonly_peek(self): |
92 |
lf = LockDir(self.get_readonly_transport(), 'test_lock') |
|
93 |
self.assertEqual(lf.peek(), None) |
|
94 |
||
95 |
def test_10_lock_uncontested(self): |
|
96 |
"""Acquire and release a lock"""
|
|
97 |
t = self.get_transport() |
|
98 |
lf = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
99 |
lf.create() |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
100 |
lf.attempt_lock() |
101 |
try: |
|
1553.5.13
by Martin Pool
New Transport.rename that mustn't overwrite |
102 |
self.assertTrue(lf.is_held) |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
103 |
finally: |
104 |
lf.unlock() |
|
1553.5.13
by Martin Pool
New Transport.rename that mustn't overwrite |
105 |
self.assertFalse(lf.is_held) |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
106 |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
107 |
def test_11_create_readonly_transport(self): |
108 |
"""Fail to create lock on readonly transport"""
|
|
109 |
t = self.get_readonly_transport() |
|
110 |
lf = LockDir(t, 'test_lock') |
|
2872.5.1
by Martin Pool
Avoid internal error tracebacks on failure to lock on readonly transport (#129701). |
111 |
self.assertRaises(LockFailed, lf.create) |
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
112 |
|
113 |
def test_12_lock_readonly_transport(self): |
|
1553.5.12
by Martin Pool
New LockDir locking mechanism |
114 |
"""Fail to lock on readonly transport"""
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
115 |
lf = LockDir(self.get_transport(), 'test_lock') |
116 |
lf.create() |
|
117 |
lf = LockDir(self.get_readonly_transport(), 'test_lock') |
|
2872.5.1
by Martin Pool
Avoid internal error tracebacks on failure to lock on readonly transport (#129701). |
118 |
self.assertRaises(LockFailed, lf.attempt_lock) |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
119 |
|
120 |
def test_20_lock_contested(self): |
|
121 |
"""Contention to get a lock"""
|
|
122 |
t = self.get_transport() |
|
123 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
124 |
lf1.create() |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
125 |
lf1.attempt_lock() |
126 |
lf2 = LockDir(t, 'test_lock') |
|
127 |
try: |
|
128 |
# locking is between LockDir instances; aliases within
|
|
129 |
# a single process are not detected
|
|
130 |
lf2.attempt_lock() |
|
131 |
self.fail('Failed to detect lock collision') |
|
132 |
except LockContention, e: |
|
133 |
self.assertEqual(e.lock, lf2) |
|
134 |
self.assertContainsRe(str(e), |
|
135 |
r'^Could not acquire.*test_lock.*$') |
|
136 |
lf1.unlock() |
|
137 |
||
138 |
def test_20_lock_peek(self): |
|
139 |
"""Peek at the state of a lock"""
|
|
140 |
t = self.get_transport() |
|
141 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
142 |
lf1.create() |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
143 |
lf1.attempt_lock() |
144 |
# lock is held, should get some info on it
|
|
145 |
info1 = lf1.peek() |
|
146 |
self.assertEqual(set(info1.keys()), |
|
147 |
set(['user', 'nonce', 'hostname', 'pid', 'start_time'])) |
|
148 |
# should get the same info if we look at it through a different
|
|
149 |
# instance
|
|
150 |
info2 = LockDir(t, 'test_lock').peek() |
|
151 |
self.assertEqual(info1, info2) |
|
152 |
# locks which are never used should be not-held
|
|
153 |
self.assertEqual(LockDir(t, 'other_lock').peek(), None) |
|
154 |
||
155 |
def test_21_peek_readonly(self): |
|
156 |
"""Peek over a readonly transport"""
|
|
157 |
t = self.get_transport() |
|
158 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
159 |
lf1.create() |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
160 |
lf2 = LockDir(self.get_readonly_transport(), 'test_lock') |
161 |
self.assertEqual(lf2.peek(), None) |
|
162 |
lf1.attempt_lock() |
|
163 |
info2 = lf2.peek() |
|
164 |
self.assertTrue(info2) |
|
165 |
self.assertEqual(info2['nonce'], lf1.nonce) |
|
166 |
||
167 |
def test_30_lock_wait_fail(self): |
|
168 |
"""Wait on a lock, then fail
|
|
169 |
|
|
170 |
We ask to wait up to 400ms; this should fail within at most one
|
|
171 |
second. (Longer times are more realistic but we don't want the test
|
|
172 |
suite to take too long, and this should do for now.)
|
|
173 |
"""
|
|
174 |
t = self.get_transport() |
|
175 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
176 |
lf1.create() |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
177 |
lf2 = LockDir(t, 'test_lock') |
1957.1.1
by John Arbash Meinel
Report to the user when we are spinning on a lock |
178 |
self.setup_log_reporter(lf2) |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
179 |
lf1.attempt_lock() |
180 |
try: |
|
181 |
before = time.time() |
|
182 |
self.assertRaises(LockContention, lf2.wait_lock, |
|
183 |
timeout=0.4, poll=0.1) |
|
184 |
after = time.time() |
|
1704.2.1
by Martin Pool
Fix time-dependency in LockDir tests -- allow more margin for error in time to detect lock contention |
185 |
# it should only take about 0.4 seconds, but we allow more time in
|
186 |
# case the machine is heavily loaded
|
|
187 |
self.assertTrue(after - before <= 8.0, |
|
188 |
"took %f seconds to detect lock contention" % (after - before)) |
|
1553.5.12
by Martin Pool
New LockDir locking mechanism |
189 |
finally: |
190 |
lf1.unlock() |
|
1957.1.1
by John Arbash Meinel
Report to the user when we are spinning on a lock |
191 |
lock_base = lf2.transport.abspath(lf2.path) |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
192 |
self.assertEqual(1, len(self._logged_reports)) |
1957.1.9
by John Arbash Meinel
Change default timeouts, and report differently the first failure |
193 |
self.assertEqual('%s %s\n' |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
194 |
'%s\n%s\n' |
1957.1.13
by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock |
195 |
'Will continue to try until %s\n', |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
196 |
self._logged_reports[0][0]) |
197 |
args = self._logged_reports[0][1] |
|
1957.1.9
by John Arbash Meinel
Change default timeouts, and report differently the first failure |
198 |
self.assertEqual('Unable to obtain', args[0]) |
199 |
self.assertEqual('lock %s' % (lock_base,), args[1]) |
|
200 |
self.assertStartsWith(args[2], 'held by ') |
|
201 |
self.assertStartsWith(args[3], 'locked ') |
|
202 |
self.assertEndsWith(args[3], ' ago') |
|
1957.1.13
by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock |
203 |
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d') |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
204 |
|
205 |
def test_31_lock_wait_easy(self): |
|
206 |
"""Succeed when waiting on a lock with no contention.
|
|
207 |
"""
|
|
208 |
t = self.get_transport() |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
209 |
lf1 = LockDir(t, 'test_lock') |
210 |
lf1.create() |
|
1957.1.1
by John Arbash Meinel
Report to the user when we are spinning on a lock |
211 |
self.setup_log_reporter(lf1) |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
212 |
try: |
213 |
before = time.time() |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
214 |
lf1.wait_lock(timeout=0.4, poll=0.1) |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
215 |
after = time.time() |
216 |
self.assertTrue(after - before <= 1.0) |
|
217 |
finally: |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
218 |
lf1.unlock() |
1957.1.1
by John Arbash Meinel
Report to the user when we are spinning on a lock |
219 |
self.assertEqual([], self._logged_reports) |
1553.5.12
by Martin Pool
New LockDir locking mechanism |
220 |
|
1551.15.18
by Aaron Bentley
Skip itermittently-failing test instead of deleting it |
221 |
def test_32_lock_wait_succeed(self): |
222 |
"""Succeed when trying to acquire a lock that gets released
|
|
223 |
||
224 |
One thread holds on a lock and then releases it; another
|
|
225 |
tries to lock it.
|
|
226 |
"""
|
|
1551.15.22
by Aaron Bentley
Redo test skip |
227 |
# This test sometimes fails like this:
|
228 |
# Traceback (most recent call last):
|
|
229 |
||
230 |
# File "/home/pqm/bzr-pqm-workdir/home/+trunk/bzrlib/tests/
|
|
231 |
# test_lockdir.py", line 247, in test_32_lock_wait_succeed
|
|
232 |
# self.assertEqual(1, len(self._logged_reports))
|
|
233 |
# AssertionError: not equal:
|
|
234 |
# a = 1
|
|
235 |
# b = 0
|
|
236 |
raise tests.TestSkipped("Test fails intermittently") |
|
1551.15.18
by Aaron Bentley
Skip itermittently-failing test instead of deleting it |
237 |
t = self.get_transport() |
238 |
lf1 = LockDir(t, 'test_lock') |
|
239 |
lf1.create() |
|
240 |
lf1.attempt_lock() |
|
241 |
||
242 |
def wait_and_unlock(): |
|
243 |
time.sleep(0.1) |
|
244 |
lf1.unlock() |
|
245 |
unlocker = Thread(target=wait_and_unlock) |
|
246 |
unlocker.start() |
|
247 |
try: |
|
248 |
lf2 = LockDir(t, 'test_lock') |
|
249 |
self.setup_log_reporter(lf2) |
|
250 |
before = time.time() |
|
251 |
# wait and then lock
|
|
252 |
lf2.wait_lock(timeout=0.4, poll=0.1) |
|
253 |
after = time.time() |
|
254 |
self.assertTrue(after - before <= 1.0) |
|
255 |
finally: |
|
256 |
unlocker.join() |
|
257 |
||
258 |
# There should be only 1 report, even though it should have to
|
|
259 |
# wait for a while
|
|
260 |
lock_base = lf2.transport.abspath(lf2.path) |
|
261 |
self.assertEqual(1, len(self._logged_reports)) |
|
262 |
self.assertEqual('%s %s\n' |
|
263 |
'%s\n%s\n' |
|
264 |
'Will continue to try until %s\n', |
|
265 |
self._logged_reports[0][0]) |
|
266 |
args = self._logged_reports[0][1] |
|
267 |
self.assertEqual('Unable to obtain', args[0]) |
|
268 |
self.assertEqual('lock %s' % (lock_base,), args[1]) |
|
269 |
self.assertStartsWith(args[2], 'held by ') |
|
270 |
self.assertStartsWith(args[3], 'locked ') |
|
271 |
self.assertEndsWith(args[3], ' ago') |
|
272 |
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d') |
|
273 |
||
1957.1.2
by John Arbash Meinel
Switch the default from instantly aborting, to waiting as long as 1 minute (down from 5 minutes) |
274 |
def test_34_lock_write_waits(self): |
275 |
"""LockDir.lock_write() will wait for the lock."""
|
|
2381.1.4
by Robert Collins
Unbreak lockdir tests after adding fast lockdir timeouts to the test suite default environment. |
276 |
# the test suite sets the default to 0 to make deadlocks fail fast.
|
277 |
# change it for this test, as we want to try a manual deadlock.
|
|
2631.1.4
by Aaron Bentley
Fix test skip |
278 |
raise tests.TestSkipped('Timing-sensitive test') |
2381.1.4
by Robert Collins
Unbreak lockdir tests after adding fast lockdir timeouts to the test suite default environment. |
279 |
bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 300 |
1957.1.2
by John Arbash Meinel
Switch the default from instantly aborting, to waiting as long as 1 minute (down from 5 minutes) |
280 |
t = self.get_transport() |
281 |
lf1 = LockDir(t, 'test_lock') |
|
282 |
lf1.create() |
|
283 |
lf1.attempt_lock() |
|
284 |
||
285 |
def wait_and_unlock(): |
|
286 |
time.sleep(0.1) |
|
287 |
lf1.unlock() |
|
288 |
unlocker = Thread(target=wait_and_unlock) |
|
289 |
unlocker.start() |
|
290 |
try: |
|
291 |
lf2 = LockDir(t, 'test_lock') |
|
292 |
self.setup_log_reporter(lf2) |
|
293 |
before = time.time() |
|
294 |
# wait and then lock
|
|
295 |
lf2.lock_write() |
|
296 |
after = time.time() |
|
297 |
finally: |
|
298 |
unlocker.join() |
|
299 |
||
300 |
# There should be only 1 report, even though it should have to
|
|
301 |
# wait for a while
|
|
302 |
lock_base = lf2.transport.abspath(lf2.path) |
|
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
303 |
self.assertEqual(1, len(self._logged_reports)) |
1957.1.9
by John Arbash Meinel
Change default timeouts, and report differently the first failure |
304 |
self.assertEqual('%s %s\n' |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
305 |
'%s\n%s\n' |
1957.1.13
by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock |
306 |
'Will continue to try until %s\n', |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
307 |
self._logged_reports[0][0]) |
308 |
args = self._logged_reports[0][1] |
|
1957.1.9
by John Arbash Meinel
Change default timeouts, and report differently the first failure |
309 |
self.assertEqual('Unable to obtain', args[0]) |
310 |
self.assertEqual('lock %s' % (lock_base,), args[1]) |
|
311 |
self.assertStartsWith(args[2], 'held by ') |
|
312 |
self.assertStartsWith(args[3], 'locked ') |
|
313 |
self.assertEndsWith(args[3], ' ago') |
|
1957.1.13
by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock |
314 |
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d') |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
315 |
|
316 |
def test_35_wait_lock_changing(self): |
|
317 |
"""LockDir.wait_lock() will report if the lock changes underneath.
|
|
318 |
|
|
319 |
This is the stages we want to happen:
|
|
320 |
||
321 |
0) Synchronization locks are created and locked.
|
|
322 |
1) Lock1 obtains the lockdir, and releases the 'check' lock.
|
|
323 |
2) Lock2 grabs the 'check' lock, and checks the lockdir.
|
|
324 |
It sees the lockdir is already acquired, reports the fact,
|
|
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
325 |
and unsets the 'checked' lock.
|
326 |
3) Thread1 blocks on acquiring the 'checked' lock, and then tells
|
|
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
327 |
Lock1 to release and acquire the lockdir. This resets the 'check'
|
328 |
lock.
|
|
329 |
4) Lock2 acquires the 'check' lock, and checks again. It notices
|
|
330 |
that the holder of the lock has changed, and so reports a new
|
|
331 |
lock holder.
|
|
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
332 |
5) Thread1 blocks on the 'checked' lock, this time, it completely
|
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
333 |
unlocks the lockdir, allowing Lock2 to acquire the lock.
|
334 |
"""
|
|
335 |
||
336 |
wait_to_check_lock = Lock() |
|
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
337 |
wait_until_checked_lock = Lock() |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
338 |
|
339 |
wait_to_check_lock.acquire() |
|
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
340 |
wait_until_checked_lock.acquire() |
341 |
note('locked check and checked locks') |
|
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
342 |
|
343 |
class LockDir1(LockDir): |
|
344 |
"""Use the synchronization points for the first lock."""
|
|
345 |
||
346 |
def attempt_lock(self): |
|
347 |
# Once we have acquired the lock, it is okay for
|
|
348 |
# the other lock to check it
|
|
349 |
try: |
|
350 |
return super(LockDir1, self).attempt_lock() |
|
351 |
finally: |
|
352 |
note('lock1: releasing check lock') |
|
353 |
wait_to_check_lock.release() |
|
354 |
||
355 |
class LockDir2(LockDir): |
|
356 |
"""Use the synchronization points for the second lock."""
|
|
357 |
||
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
358 |
def attempt_lock(self): |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
359 |
note('lock2: waiting for check lock') |
360 |
wait_to_check_lock.acquire() |
|
361 |
note('lock2: acquired check lock') |
|
362 |
try: |
|
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
363 |
return super(LockDir2, self).attempt_lock() |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
364 |
finally: |
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
365 |
note('lock2: releasing checked lock') |
366 |
wait_until_checked_lock.release() |
|
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
367 |
|
368 |
t = self.get_transport() |
|
369 |
lf1 = LockDir1(t, 'test_lock') |
|
370 |
lf1.create() |
|
371 |
||
372 |
lf2 = LockDir2(t, 'test_lock') |
|
373 |
self.setup_log_reporter(lf2) |
|
374 |
||
375 |
def wait_and_switch(): |
|
376 |
lf1.attempt_lock() |
|
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
377 |
# Block until lock2 has had a chance to check
|
378 |
note('lock1: waiting 1 for checked lock') |
|
379 |
wait_until_checked_lock.acquire() |
|
380 |
note('lock1: acquired for checked lock') |
|
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
381 |
note('lock1: released lockdir') |
382 |
lf1.unlock() |
|
383 |
note('lock1: acquiring lockdir') |
|
384 |
# Create a new nonce, so the lock looks different.
|
|
385 |
lf1.nonce = osutils.rand_chars(20) |
|
386 |
lf1.lock_write() |
|
387 |
note('lock1: acquired lockdir') |
|
388 |
||
389 |
# Block until lock2 has peeked again
|
|
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
390 |
note('lock1: waiting 2 for checked lock') |
391 |
wait_until_checked_lock.acquire() |
|
392 |
note('lock1: acquired for checked lock') |
|
393 |
# Now unlock, and let lock 2 grab the lock
|
|
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
394 |
lf1.unlock() |
395 |
wait_to_check_lock.release() |
|
396 |
||
397 |
unlocker = Thread(target=wait_and_switch) |
|
398 |
unlocker.start() |
|
399 |
try: |
|
1957.1.11
by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable |
400 |
# Wait and play against the other thread
|
2709.1.1
by Martin Pool
Make TestLockDir.test_35_wait_lock_changing less timing-sensitive |
401 |
lf2.wait_lock(timeout=20.0, poll=0.01) |
1957.1.7
by John Arbash Meinel
Add the ability to report if the lock changes from underneath you |
402 |
finally: |
403 |
unlocker.join() |
|
404 |
lf2.unlock() |
|
405 |
||
406 |
# There should be 2 reports, because the lock changed
|
|
407 |
lock_base = lf2.transport.abspath(lf2.path) |
|
408 |
self.assertEqual(2, len(self._logged_reports)) |
|
1957.1.13
by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock |
409 |
|
1957.1.15
by John Arbash Meinel
Review feedback from Robert |
410 |
self.assertEqual('%s %s\n' |
411 |
'%s\n%s\n' |
|
412 |
'Will continue to try until %s\n', |
|
413 |
self._logged_reports[0][0]) |
|
414 |
args = self._logged_reports[0][1] |
|
415 |
self.assertEqual('Unable to obtain', args[0]) |
|
416 |
self.assertEqual('lock %s' % (lock_base,), args[1]) |
|
417 |
self.assertStartsWith(args[2], 'held by ') |
|
418 |
self.assertStartsWith(args[3], 'locked ') |
|
419 |
self.assertEndsWith(args[3], ' ago') |
|
420 |
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d') |
|
1957.1.13
by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock |
421 |
|
1957.1.15
by John Arbash Meinel
Review feedback from Robert |
422 |
self.assertEqual('%s %s\n' |
423 |
'%s\n%s\n' |
|
424 |
'Will continue to try until %s\n', |
|
425 |
self._logged_reports[1][0]) |
|
426 |
args = self._logged_reports[1][1] |
|
427 |
self.assertEqual('Lock owner changed for', args[0]) |
|
428 |
self.assertEqual('lock %s' % (lock_base,), args[1]) |
|
429 |
self.assertStartsWith(args[2], 'held by ') |
|
430 |
self.assertStartsWith(args[3], 'locked ') |
|
431 |
self.assertEndsWith(args[3], ' ago') |
|
432 |
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d') |
|
1957.1.2
by John Arbash Meinel
Switch the default from instantly aborting, to waiting as long as 1 minute (down from 5 minutes) |
433 |
|
1553.5.20
by Martin Pool
Start adding LockDir.confirm() method |
434 |
def test_40_confirm_easy(self): |
435 |
"""Confirm a lock that's already held"""
|
|
436 |
t = self.get_transport() |
|
437 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
438 |
lf1.create() |
1553.5.20
by Martin Pool
Start adding LockDir.confirm() method |
439 |
lf1.attempt_lock() |
440 |
lf1.confirm() |
|
441 |
||
442 |
def test_41_confirm_not_held(self): |
|
443 |
"""Confirm a lock that's already held"""
|
|
444 |
t = self.get_transport() |
|
445 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
446 |
lf1.create() |
1553.5.20
by Martin Pool
Start adding LockDir.confirm() method |
447 |
self.assertRaises(LockNotHeld, lf1.confirm) |
1553.5.23
by Martin Pool
Start LockDir.confirm method and LockBroken exception |
448 |
|
449 |
def test_42_confirm_broken_manually(self): |
|
450 |
"""Confirm a lock broken by hand"""
|
|
451 |
t = self.get_transport() |
|
452 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
453 |
lf1.create() |
1553.5.23
by Martin Pool
Start LockDir.confirm method and LockBroken exception |
454 |
lf1.attempt_lock() |
455 |
t.move('test_lock', 'lock_gone_now') |
|
456 |
self.assertRaises(LockBroken, lf1.confirm) |
|
1553.5.25
by Martin Pool
New LockDir.force_break and simple test case |
457 |
|
458 |
def test_43_break(self): |
|
459 |
"""Break a lock whose caller has forgotten it"""
|
|
460 |
t = self.get_transport() |
|
461 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
462 |
lf1.create() |
1553.5.25
by Martin Pool
New LockDir.force_break and simple test case |
463 |
lf1.attempt_lock() |
464 |
# we incorrectly discard the lock object without unlocking it
|
|
465 |
del lf1 |
|
466 |
# someone else sees it's still locked
|
|
467 |
lf2 = LockDir(t, 'test_lock') |
|
1553.5.27
by Martin Pool
Confirm that only the intended holder of a lock was broken. |
468 |
holder_info = lf2.peek() |
469 |
self.assertTrue(holder_info) |
|
470 |
lf2.force_break(holder_info) |
|
1553.5.25
by Martin Pool
New LockDir.force_break and simple test case |
471 |
# now we should be able to take it
|
472 |
lf2.attempt_lock() |
|
473 |
lf2.confirm() |
|
1553.5.26
by Martin Pool
Breaking an already-released lock should just succeed |
474 |
|
475 |
def test_44_break_already_released(self): |
|
476 |
"""Lock break races with regular release"""
|
|
477 |
t = self.get_transport() |
|
478 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
479 |
lf1.create() |
1553.5.26
by Martin Pool
Breaking an already-released lock should just succeed |
480 |
lf1.attempt_lock() |
481 |
# someone else sees it's still locked
|
|
482 |
lf2 = LockDir(t, 'test_lock') |
|
483 |
holder_info = lf2.peek() |
|
484 |
# in the interim the lock is released
|
|
485 |
lf1.unlock() |
|
486 |
# break should succeed
|
|
1553.5.27
by Martin Pool
Confirm that only the intended holder of a lock was broken. |
487 |
lf2.force_break(holder_info) |
1553.5.26
by Martin Pool
Breaking an already-released lock should just succeed |
488 |
# now we should be able to take it
|
489 |
lf2.attempt_lock() |
|
490 |
lf2.confirm() |
|
491 |
||
1553.5.27
by Martin Pool
Confirm that only the intended holder of a lock was broken. |
492 |
def test_45_break_mismatch(self): |
493 |
"""Lock break races with someone else acquiring it"""
|
|
494 |
t = self.get_transport() |
|
495 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
496 |
lf1.create() |
1553.5.27
by Martin Pool
Confirm that only the intended holder of a lock was broken. |
497 |
lf1.attempt_lock() |
498 |
# someone else sees it's still locked
|
|
499 |
lf2 = LockDir(t, 'test_lock') |
|
500 |
holder_info = lf2.peek() |
|
501 |
# in the interim the lock is released
|
|
502 |
lf1.unlock() |
|
503 |
lf3 = LockDir(t, 'test_lock') |
|
504 |
lf3.attempt_lock() |
|
505 |
# break should now *fail*
|
|
506 |
self.assertRaises(LockBreakMismatch, lf2.force_break, |
|
507 |
holder_info) |
|
508 |
lf3.unlock() |
|
1553.5.54
by Martin Pool
Add LockDir.read_lock fake method |
509 |
|
510 |
def test_46_fake_read_lock(self): |
|
511 |
t = self.get_transport() |
|
512 |
lf1 = LockDir(t, 'test_lock') |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
513 |
lf1.create() |
1553.5.54
by Martin Pool
Add LockDir.read_lock fake method |
514 |
lf1.lock_read() |
515 |
lf1.unlock() |
|
1553.5.58
by Martin Pool
Change LockDirs to format "lock-name/held/info" |
516 |
|
517 |
def test_50_lockdir_representation(self): |
|
518 |
"""Check the on-disk representation of LockDirs is as expected.
|
|
519 |
||
520 |
There should always be a top-level directory named by the lock.
|
|
521 |
When the lock is held, there should be a lockname/held directory
|
|
522 |
containing an info file.
|
|
523 |
"""
|
|
524 |
t = self.get_transport() |
|
525 |
lf1 = LockDir(t, 'test_lock') |
|
526 |
lf1.create() |
|
527 |
self.assertTrue(t.has('test_lock')) |
|
528 |
lf1.lock_write() |
|
529 |
self.assertTrue(t.has('test_lock/held/info')) |
|
530 |
lf1.unlock() |
|
531 |
self.assertFalse(t.has('test_lock/held/info')) |
|
1687.1.5
by Robert Collins
Add break_lock utility function to LockDir. |
532 |
|
533 |
def test_break_lock(self): |
|
534 |
# the ui based break_lock routine should Just Work (tm)
|
|
535 |
ld1 = self.get_lock() |
|
536 |
ld2 = self.get_lock() |
|
537 |
ld1.create() |
|
538 |
ld1.lock_write() |
|
1687.1.6
by Robert Collins
Extend LockableFiles to support break_lock() calls. |
539 |
# do this without IO redirection to ensure it doesn't prompt.
|
540 |
self.assertRaises(AssertionError, ld1.break_lock) |
|
1687.1.5
by Robert Collins
Add break_lock utility function to LockDir. |
541 |
orig_factory = bzrlib.ui.ui_factory |
542 |
# silent ui - no need for stdout
|
|
543 |
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory() |
|
544 |
bzrlib.ui.ui_factory.stdin = StringIO("y\n") |
|
545 |
try: |
|
546 |
ld2.break_lock() |
|
547 |
self.assertRaises(LockBroken, ld1.unlock) |
|
548 |
finally: |
|
549 |
bzrlib.ui.ui_factory = orig_factory |
|
1955.1.1
by John Arbash Meinel
LockDir can create the root directory if it fails to create a pending directory due to NoSuchFile. |
550 |
|
551 |
def test_create_missing_base_directory(self): |
|
552 |
"""If LockDir.path doesn't exist, it can be created
|
|
553 |
||
554 |
Some people manually remove the entire lock/ directory trying
|
|
555 |
to unlock a stuck repository/branch/etc. Rather than failing
|
|
556 |
after that, just create the lock directory when needed.
|
|
557 |
"""
|
|
558 |
t = self.get_transport() |
|
559 |
lf1 = LockDir(t, 'test_lock') |
|
560 |
||
561 |
lf1.create() |
|
562 |
self.failUnless(t.has('test_lock')) |
|
563 |
||
564 |
t.rmdir('test_lock') |
|
565 |
self.failIf(t.has('test_lock')) |
|
566 |
||
567 |
# This will create 'test_lock' if it needs to
|
|
568 |
lf1.lock_write() |
|
569 |
self.failUnless(t.has('test_lock')) |
|
570 |
self.failUnless(t.has('test_lock/held/info')) |
|
571 |
||
572 |
lf1.unlock() |
|
573 |
self.failIf(t.has('test_lock/held/info')) |
|
1957.1.6
by John Arbash Meinel
[merge] bzr.dev 2009 |
574 |
|
1957.1.5
by John Arbash Meinel
Create a helper function for formatting lock information |
575 |
def test__format_lock_info(self): |
576 |
ld1 = self.get_lock() |
|
577 |
ld1.create() |
|
578 |
ld1.lock_write() |
|
579 |
try: |
|
580 |
info_list = ld1._format_lock_info(ld1.peek()) |
|
581 |
finally: |
|
582 |
ld1.unlock() |
|
583 |
self.assertEqual('lock %s' % (ld1.transport.abspath(ld1.path),), |
|
584 |
info_list[0]) |
|
585 |
self.assertContainsRe(info_list[1], |
|
586 |
r'^held by .* on host .* \[process #\d*\]$') |
|
587 |
self.assertContainsRe(info_list[2], r'locked \d+ seconds? ago$') |
|
2055.2.1
by John Arbash Meinel
Make LockDir less sensitive to invalid configuration of email |
588 |
|
589 |
def test_lock_without_email(self): |
|
590 |
global_config = config.GlobalConfig() |
|
591 |
# Intentionally has no email address
|
|
592 |
global_config.set_user_option('email', 'User Identity') |
|
593 |
ld1 = self.get_lock() |
|
594 |
ld1.create() |
|
595 |
ld1.lock_write() |
|
596 |
ld1.unlock() |
|
1551.10.3
by Aaron Bentley
Lock attempts don't treat permission problems as lock contention |
597 |
|
598 |
def test_lock_permission(self): |
|
1551.10.4
by Aaron Bentley
Update to skip on win32 |
599 |
if not osutils.supports_posix_readonly(): |
600 |
raise tests.TestSkipped('Cannot induce a permission failure') |
|
1551.10.3
by Aaron Bentley
Lock attempts don't treat permission problems as lock contention |
601 |
ld1 = self.get_lock() |
602 |
lock_path = ld1.transport.local_abspath('test_lock') |
|
603 |
os.mkdir(lock_path) |
|
604 |
osutils.make_readonly(lock_path) |
|
2872.5.1
by Martin Pool
Avoid internal error tracebacks on failure to lock on readonly transport (#129701). |
605 |
self.assertRaises(errors.LockFailed, ld1.attempt_lock) |
2555.3.5
by Martin Pool
Return token directly from LockDir.acquire to avoid unnecessary peek() |
606 |
|
607 |
def test_lock_by_token(self): |
|
608 |
ld1 = self.get_lock() |
|
609 |
token = ld1.lock_write() |
|
610 |
self.assertNotEqual(None, token) |
|
611 |
ld2 = self.get_lock() |
|
612 |
t2 = ld2.lock_write(token) |
|
613 |
self.assertEqual(token, t2) |
|
2555.3.9
by Martin Pool
Add test and fix for locking robustly when rename of directories doesn't act as a mutex (thank John) |
614 |
|
615 |
def test_lock_with_buggy_rename(self): |
|
616 |
# test that lock acquisition handles servers which pretend they
|
|
617 |
# renamed correctly but that actually fail
|
|
618 |
t = transport.get_transport('brokenrename+' + self.get_url()) |
|
619 |
ld1 = LockDir(t, 'test_lock') |
|
620 |
ld1.create() |
|
621 |
ld1.attempt_lock() |
|
622 |
ld2 = LockDir(t, 'test_lock') |
|
2555.3.14
by Martin Pool
Better handling in LockDir of rename that moves one directory within another |
623 |
# we should fail to lock
|
624 |
e = self.assertRaises(errors.LockContention, ld2.attempt_lock) |
|
625 |
# now the original caller should succeed in unlocking
|
|
626 |
ld1.unlock() |
|
627 |
# and there should be nothing left over
|
|
628 |
self.assertEquals([], t.list_dir('test_lock')) |
|
2555.3.12
by Martin Pool
Add test for https://bugs.launchpad.net/bzr/+bug/109169 -- test_failed_lock_leaves_no_trash |
629 |
|
630 |
def test_failed_lock_leaves_no_trash(self): |
|
631 |
# if we fail to acquire the lock, we don't leave pending directories
|
|
632 |
# behind -- https://bugs.launchpad.net/bzr/+bug/109169
|
|
633 |
ld1 = self.get_lock() |
|
634 |
ld2 = self.get_lock() |
|
635 |
# should be nothing before we start
|
|
636 |
ld1.create() |
|
637 |
t = self.get_transport().clone('test_lock') |
|
638 |
def check_dir(a): |
|
639 |
self.assertEquals(a, t.list_dir('.')) |
|
640 |
check_dir([]) |
|
641 |
# when held, that's all we see
|
|
642 |
ld1.attempt_lock() |
|
643 |
check_dir(['held']) |
|
644 |
# second guy should fail
|
|
645 |
self.assertRaises(errors.LockContention, ld2.attempt_lock) |
|
646 |
# no kibble
|
|
647 |
check_dir(['held']) |