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 threading import Thread
22
from bzrlib.errors import (
24
LockContention, LockError, UnlockableTransport,
25
LockNotHeld, LockBroken
27
from bzrlib.lockdir import LockDir
28
from bzrlib.tests import TestCaseWithTransport
30
# These tests sometimes use threads to test the behaviour of lock files with
31
# concurrent actors. This is not a typical (or necessarily supported) use;
32
# they're really meant for guarding between processes.
34
class TestLockDir(TestCaseWithTransport):
35
"""Test LockDir operations"""
37
def test_00_lock_creation(self):
38
"""Creation of lock file on a transport"""
39
t = self.get_transport()
40
lf = LockDir(t, 'test_lock')
41
self.assertFalse(lf.is_held)
43
def test_01_lock_repr(self):
44
"""Lock string representation"""
45
lf = LockDir(self.get_transport(), 'test_lock')
47
self.assertContainsRe(r, r'^LockDir\(.*/test_lock\)$')
49
def test_02_unlocked_peek(self):
50
lf = LockDir(self.get_transport(), 'test_lock')
51
self.assertEqual(lf.peek(), None)
53
def test_03_readonly_peek(self):
54
lf = LockDir(self.get_readonly_transport(), 'test_lock')
55
self.assertEqual(lf.peek(), None)
57
def test_10_lock_uncontested(self):
58
"""Acquire and release a lock"""
59
t = self.get_transport()
60
lf = LockDir(t, 'test_lock')
63
self.assertTrue(lf.is_held)
66
self.assertFalse(lf.is_held)
68
def test_11_lock_readonly_transport(self):
69
"""Fail to lock on readonly transport"""
70
t = self.get_readonly_transport()
71
lf = LockDir(t, 'test_lock')
72
self.assertRaises(UnlockableTransport, lf.attempt_lock)
74
def test_20_lock_contested(self):
75
"""Contention to get a lock"""
76
t = self.get_transport()
77
lf1 = LockDir(t, 'test_lock')
79
lf2 = LockDir(t, 'test_lock')
81
# locking is between LockDir instances; aliases within
82
# a single process are not detected
84
self.fail('Failed to detect lock collision')
85
except LockContention, e:
86
self.assertEqual(e.lock, lf2)
87
self.assertContainsRe(str(e),
88
r'^Could not acquire.*test_lock.*$')
91
def test_20_lock_peek(self):
92
"""Peek at the state of a lock"""
93
t = self.get_transport()
94
lf1 = LockDir(t, 'test_lock')
96
# lock is held, should get some info on it
98
self.assertEqual(set(info1.keys()),
99
set(['user', 'nonce', 'hostname', 'pid', 'start_time']))
100
# should get the same info if we look at it through a different
102
info2 = LockDir(t, 'test_lock').peek()
103
self.assertEqual(info1, info2)
104
# locks which are never used should be not-held
105
self.assertEqual(LockDir(t, 'other_lock').peek(), None)
107
def test_21_peek_readonly(self):
108
"""Peek over a readonly transport"""
109
t = self.get_transport()
110
lf1 = LockDir(t, 'test_lock')
111
lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
112
self.assertEqual(lf2.peek(), None)
115
self.assertTrue(info2)
116
self.assertEqual(info2['nonce'], lf1.nonce)
118
def test_30_lock_wait_fail(self):
119
"""Wait on a lock, then fail
121
We ask to wait up to 400ms; this should fail within at most one
122
second. (Longer times are more realistic but we don't want the test
123
suite to take too long, and this should do for now.)
125
t = self.get_transport()
126
lf1 = LockDir(t, 'test_lock')
127
lf2 = LockDir(t, 'test_lock')
131
self.assertRaises(LockContention, lf2.wait_lock,
132
timeout=0.4, poll=0.1)
134
self.assertTrue(after - before <= 1.0)
138
def test_31_lock_wait_easy(self):
139
"""Succeed when waiting on a lock with no contention.
141
t = self.get_transport()
142
lf2 = LockDir(t, 'test_lock')
145
lf2.wait_lock(timeout=0.4, poll=0.1)
147
self.assertTrue(after - before <= 1.0)
151
def test_32_lock_wait_succeed(self):
152
"""Succeed when trying to acquire a lock that gets released
154
One thread holds on a lock and then releases it; another tries to lock it.
156
t = self.get_transport()
157
lf1 = LockDir(t, 'test_lock')
160
def wait_and_unlock():
163
unlocker = Thread(target=wait_and_unlock)
166
lf2 = LockDir(t, 'test_lock')
169
lf2.wait_lock(timeout=0.4, poll=0.1)
171
self.assertTrue(after - before <= 1.0)
175
def test_33_wait(self):
176
"""Succeed when waiting on a lock that gets released
178
The difference from test_32_lock_wait_succeed is that the second
179
caller does not actually acquire the lock, but just waits for it
180
to be released. This is done over a readonly transport.
182
t = self.get_transport()
183
lf1 = LockDir(t, 'test_lock')
186
def wait_and_unlock():
189
unlocker = Thread(target=wait_and_unlock)
192
lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
194
# wait but don't lock
195
lf2.wait(timeout=0.4, poll=0.1)
197
self.assertTrue(after - before <= 1.0)
201
def test_40_confirm_easy(self):
202
"""Confirm a lock that's already held"""
203
t = self.get_transport()
204
lf1 = LockDir(t, 'test_lock')
208
def test_41_confirm_not_held(self):
209
"""Confirm a lock that's already held"""
210
t = self.get_transport()
211
lf1 = LockDir(t, 'test_lock')
212
self.assertRaises(LockNotHeld, lf1.confirm)
214
def test_42_confirm_broken_manually(self):
215
"""Confirm a lock broken by hand"""
216
t = self.get_transport()
217
lf1 = LockDir(t, 'test_lock')
219
t.move('test_lock', 'lock_gone_now')
220
self.assertRaises(LockBroken, lf1.confirm)
222
def test_43_break(self):
223
"""Break a lock whose caller has forgotten it"""
224
t = self.get_transport()
225
lf1 = LockDir(t, 'test_lock')
227
# we incorrectly discard the lock object without unlocking it
229
# someone else sees it's still locked
230
lf2 = LockDir(t, 'test_lock')
231
holder_info = lf2.peek()
232
self.assertTrue(holder_info)
233
lf2.force_break(holder_info)
234
# now we should be able to take it
238
def test_44_break_already_released(self):
239
"""Lock break races with regular release"""
240
t = self.get_transport()
241
lf1 = LockDir(t, 'test_lock')
243
# someone else sees it's still locked
244
lf2 = LockDir(t, 'test_lock')
245
holder_info = lf2.peek()
246
# in the interim the lock is released
248
# break should succeed
249
lf2.force_break(holder_info)
250
# now we should be able to take it
254
def test_45_break_mismatch(self):
255
"""Lock break races with someone else acquiring it"""
256
t = self.get_transport()
257
lf1 = LockDir(t, 'test_lock')
259
# someone else sees it's still locked
260
lf2 = LockDir(t, 'test_lock')
261
holder_info = lf2.peek()
262
# in the interim the lock is released
264
lf3 = LockDir(t, 'test_lock')
266
# break should now *fail*
267
self.assertRaises(LockBreakMismatch, lf2.force_break,