1
# Copyright (C) 2006 Canonical Ltd
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.
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.
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
17
"""Tests for LockDir"""
19
from cStringIO import StringIO
20
from threading import Thread
24
from bzrlib.errors import (
26
LockContention, LockError, UnlockableTransport,
27
LockNotHeld, LockBroken
29
from bzrlib.lockdir import LockDir
30
from bzrlib.tests import TestCaseWithTransport
32
# These tests sometimes use threads to test the behaviour of lock files with
33
# concurrent actors. This is not a typical (or necessarily supported) use;
34
# they're really meant for guarding between processes.
36
# These tests are run on the default transport provided by the test framework
37
# (typically a local disk transport). That can be changed by the --transport
38
# option to bzr selftest. The required properties of the transport
39
# implementation are tested separately. (The main requirement is just that
40
# they don't allow overwriting nonempty directories.)
42
class TestLockDir(TestCaseWithTransport):
43
"""Test LockDir operations"""
45
def test_00_lock_creation(self):
46
"""Creation of lock file on a transport"""
47
t = self.get_transport()
48
lf = LockDir(t, 'test_lock')
49
self.assertFalse(lf.is_held)
51
def test_01_lock_repr(self):
52
"""Lock string representation"""
53
lf = LockDir(self.get_transport(), 'test_lock')
55
self.assertContainsRe(r, r'^LockDir\(.*/test_lock\)$')
57
def test_02_unlocked_peek(self):
58
lf = LockDir(self.get_transport(), 'test_lock')
59
self.assertEqual(lf.peek(), None)
62
return LockDir(self.get_transport(), 'test_lock')
64
def test_unlock_after_break_raises(self):
69
ld2.force_break(ld2.peek())
70
self.assertRaises(LockBroken, ld.unlock)
72
def test_03_readonly_peek(self):
73
lf = LockDir(self.get_readonly_transport(), 'test_lock')
74
self.assertEqual(lf.peek(), None)
76
def test_10_lock_uncontested(self):
77
"""Acquire and release a lock"""
78
t = self.get_transport()
79
lf = LockDir(t, 'test_lock')
83
self.assertTrue(lf.is_held)
86
self.assertFalse(lf.is_held)
88
def test_11_create_readonly_transport(self):
89
"""Fail to create lock on readonly transport"""
90
t = self.get_readonly_transport()
91
lf = LockDir(t, 'test_lock')
92
self.assertRaises(UnlockableTransport, lf.create)
94
def test_12_lock_readonly_transport(self):
95
"""Fail to lock on readonly transport"""
96
lf = LockDir(self.get_transport(), 'test_lock')
98
lf = LockDir(self.get_readonly_transport(), 'test_lock')
99
self.assertRaises(UnlockableTransport, lf.attempt_lock)
101
def test_20_lock_contested(self):
102
"""Contention to get a lock"""
103
t = self.get_transport()
104
lf1 = LockDir(t, 'test_lock')
107
lf2 = LockDir(t, 'test_lock')
109
# locking is between LockDir instances; aliases within
110
# a single process are not detected
112
self.fail('Failed to detect lock collision')
113
except LockContention, e:
114
self.assertEqual(e.lock, lf2)
115
self.assertContainsRe(str(e),
116
r'^Could not acquire.*test_lock.*$')
119
def test_20_lock_peek(self):
120
"""Peek at the state of a lock"""
121
t = self.get_transport()
122
lf1 = LockDir(t, 'test_lock')
125
# lock is held, should get some info on it
127
self.assertEqual(set(info1.keys()),
128
set(['user', 'nonce', 'hostname', 'pid', 'start_time']))
129
# should get the same info if we look at it through a different
131
info2 = LockDir(t, 'test_lock').peek()
132
self.assertEqual(info1, info2)
133
# locks which are never used should be not-held
134
self.assertEqual(LockDir(t, 'other_lock').peek(), None)
136
def test_21_peek_readonly(self):
137
"""Peek over a readonly transport"""
138
t = self.get_transport()
139
lf1 = LockDir(t, 'test_lock')
141
lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
142
self.assertEqual(lf2.peek(), None)
145
self.assertTrue(info2)
146
self.assertEqual(info2['nonce'], lf1.nonce)
148
def test_30_lock_wait_fail(self):
149
"""Wait on a lock, then fail
151
We ask to wait up to 400ms; this should fail within at most one
152
second. (Longer times are more realistic but we don't want the test
153
suite to take too long, and this should do for now.)
155
t = self.get_transport()
156
lf1 = LockDir(t, 'test_lock')
158
lf2 = LockDir(t, 'test_lock')
162
self.assertRaises(LockContention, lf2.wait_lock,
163
timeout=0.4, poll=0.1)
165
# it should only take about 0.4 seconds, but we allow more time in
166
# case the machine is heavily loaded
167
self.assertTrue(after - before <= 8.0,
168
"took %f seconds to detect lock contention" % (after - before))
172
def test_31_lock_wait_easy(self):
173
"""Succeed when waiting on a lock with no contention.
175
t = self.get_transport()
176
lf1 = LockDir(t, 'test_lock')
180
lf1.wait_lock(timeout=0.4, poll=0.1)
182
self.assertTrue(after - before <= 1.0)
186
def test_32_lock_wait_succeed(self):
187
"""Succeed when trying to acquire a lock that gets released
189
One thread holds on a lock and then releases it; another
192
t = self.get_transport()
193
lf1 = LockDir(t, 'test_lock')
197
def wait_and_unlock():
200
unlocker = Thread(target=wait_and_unlock)
203
lf2 = LockDir(t, 'test_lock')
206
lf2.wait_lock(timeout=0.4, poll=0.1)
208
self.assertTrue(after - before <= 1.0)
212
def test_33_wait(self):
213
"""Succeed when waiting on a lock that gets released
215
The difference from test_32_lock_wait_succeed is that the second
216
caller does not actually acquire the lock, but just waits for it
217
to be released. This is done over a readonly transport.
219
t = self.get_transport()
220
lf1 = LockDir(t, 'test_lock')
224
def wait_and_unlock():
227
unlocker = Thread(target=wait_and_unlock)
230
lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
232
# wait but don't lock
233
lf2.wait(timeout=0.4, poll=0.1)
235
self.assertTrue(after - before <= 1.0)
239
def test_40_confirm_easy(self):
240
"""Confirm a lock that's already held"""
241
t = self.get_transport()
242
lf1 = LockDir(t, 'test_lock')
247
def test_41_confirm_not_held(self):
248
"""Confirm a lock that's already held"""
249
t = self.get_transport()
250
lf1 = LockDir(t, 'test_lock')
252
self.assertRaises(LockNotHeld, lf1.confirm)
254
def test_42_confirm_broken_manually(self):
255
"""Confirm a lock broken by hand"""
256
t = self.get_transport()
257
lf1 = LockDir(t, 'test_lock')
260
t.move('test_lock', 'lock_gone_now')
261
self.assertRaises(LockBroken, lf1.confirm)
263
def test_43_break(self):
264
"""Break a lock whose caller has forgotten it"""
265
t = self.get_transport()
266
lf1 = LockDir(t, 'test_lock')
269
# we incorrectly discard the lock object without unlocking it
271
# someone else sees it's still locked
272
lf2 = LockDir(t, 'test_lock')
273
holder_info = lf2.peek()
274
self.assertTrue(holder_info)
275
lf2.force_break(holder_info)
276
# now we should be able to take it
280
def test_44_break_already_released(self):
281
"""Lock break races with regular release"""
282
t = self.get_transport()
283
lf1 = LockDir(t, 'test_lock')
286
# someone else sees it's still locked
287
lf2 = LockDir(t, 'test_lock')
288
holder_info = lf2.peek()
289
# in the interim the lock is released
291
# break should succeed
292
lf2.force_break(holder_info)
293
# now we should be able to take it
297
def test_45_break_mismatch(self):
298
"""Lock break races with someone else acquiring it"""
299
t = self.get_transport()
300
lf1 = LockDir(t, 'test_lock')
303
# someone else sees it's still locked
304
lf2 = LockDir(t, 'test_lock')
305
holder_info = lf2.peek()
306
# in the interim the lock is released
308
lf3 = LockDir(t, 'test_lock')
310
# break should now *fail*
311
self.assertRaises(LockBreakMismatch, lf2.force_break,
315
def test_46_fake_read_lock(self):
316
t = self.get_transport()
317
lf1 = LockDir(t, 'test_lock')
322
def test_50_lockdir_representation(self):
323
"""Check the on-disk representation of LockDirs is as expected.
325
There should always be a top-level directory named by the lock.
326
When the lock is held, there should be a lockname/held directory
327
containing an info file.
329
t = self.get_transport()
330
lf1 = LockDir(t, 'test_lock')
332
self.assertTrue(t.has('test_lock'))
334
self.assertTrue(t.has('test_lock/held/info'))
336
self.assertFalse(t.has('test_lock/held/info'))
338
def test_break_lock(self):
339
# the ui based break_lock routine should Just Work (tm)
340
ld1 = self.get_lock()
341
ld2 = self.get_lock()
344
# do this without IO redirection to ensure it doesn't prompt.
345
self.assertRaises(AssertionError, ld1.break_lock)
346
orig_factory = bzrlib.ui.ui_factory
347
# silent ui - no need for stdout
348
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
349
bzrlib.ui.ui_factory.stdin = StringIO("y\n")
352
self.assertRaises(LockBroken, ld1.unlock)
354
bzrlib.ui.ui_factory = orig_factory
356
def test_create_missing_base_directory(self):
357
"""If LockDir.path doesn't exist, it can be created
359
Some people manually remove the entire lock/ directory trying
360
to unlock a stuck repository/branch/etc. Rather than failing
361
after that, just create the lock directory when needed.
363
t = self.get_transport()
364
lf1 = LockDir(t, 'test_lock')
367
self.failUnless(t.has('test_lock'))
370
self.failIf(t.has('test_lock'))
372
# This will create 'test_lock' if it needs to
374
self.failUnless(t.has('test_lock'))
375
self.failUnless(t.has('test_lock/held/info'))
378
self.failIf(t.has('test_lock/held/info'))