1
# Copyright (C) 2005, 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
from StringIO import StringIO
20
from bzrlib.branch import Branch
21
import bzrlib.errors as errors
22
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
23
from bzrlib.lockable_files import LockableFiles, TransportLock
24
from bzrlib import lockdir
25
from bzrlib.lockdir import LockDir
26
from bzrlib.tests import TestCaseInTempDir
27
from bzrlib.tests.test_smart import TestCaseWithSmartMedium
28
from bzrlib.tests.test_transactions import DummyWeave
29
from bzrlib.transactions import (PassThroughTransaction,
33
from bzrlib.transport import get_transport
36
# these tests are applied in each parameterized suite for LockableFiles
37
class _TestLockableFiles_mixin(object):
40
self.reduceLockdirTimeout()
42
def test_read_write(self):
43
self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
44
self.assertRaises(NoSuchFile, self.lockable.get_utf8, 'foo')
45
self.lockable.lock_write()
47
unicode_string = u'bar\u1234'
48
self.assertEqual(4, len(unicode_string))
49
byte_string = unicode_string.encode('utf-8')
50
self.assertEqual(6, len(byte_string))
51
self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo',
52
StringIO(unicode_string))
53
self.lockable.put('foo', StringIO(byte_string))
54
self.assertEqual(byte_string,
55
self.lockable.get('foo').read())
56
self.assertEqual(unicode_string,
57
self.lockable.get_utf8('foo').read())
58
self.assertRaises(BzrBadParameterNotString,
59
self.lockable.put_utf8,
61
StringIO(unicode_string)
63
self.lockable.put_utf8('bar', unicode_string)
64
self.assertEqual(unicode_string,
65
self.lockable.get_utf8('bar').read())
66
self.assertEqual(byte_string,
67
self.lockable.get('bar').read())
69
self.lockable.unlock()
72
self.lockable.lock_read()
74
self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
75
StringIO('bar\u1234'))
77
self.lockable.unlock()
79
def test_transactions(self):
80
self.assertIs(self.lockable.get_transaction().__class__,
81
PassThroughTransaction)
82
self.lockable.lock_read()
84
self.assertIs(self.lockable.get_transaction().__class__,
87
self.lockable.unlock()
88
self.assertIs(self.lockable.get_transaction().__class__,
89
PassThroughTransaction)
90
self.lockable.lock_write()
91
self.assertIs(self.lockable.get_transaction().__class__,
93
# check that finish is called:
95
self.lockable.get_transaction().register_dirty(vf)
96
self.lockable.unlock()
97
self.assertTrue(vf.finished)
99
def test__escape(self):
100
self.assertEqual('%25', self.lockable._escape('%'))
102
def test__escape_empty(self):
103
self.assertEqual('', self.lockable._escape(''))
105
def test_break_lock(self):
106
# some locks are not breakable
107
self.lockable.lock_write()
109
self.assertRaises(AssertionError, self.lockable.break_lock)
110
except NotImplementedError:
111
# this lock cannot be broken
112
self.lockable.unlock()
114
l2 = self.get_lockable()
115
orig_factory = bzrlib.ui.ui_factory
116
# silent ui - no need for stdout
117
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
118
bzrlib.ui.ui_factory.stdin = StringIO("y\n")
122
bzrlib.ui.ui_factory = orig_factory
127
self.assertRaises(errors.LockBroken, self.lockable.unlock)
128
self.assertFalse(self.lockable.is_locked())
130
def test_lock_write_returns_None_refuses_token(self):
131
token = self.lockable.lock_write()
133
if token is not None:
134
# This test does not apply, because this lockable supports
137
self.assertRaises(errors.TokenLockingNotSupported,
138
self.lockable.lock_write, token='token')
140
self.lockable.unlock()
142
def test_lock_write_returns_token_when_given_token(self):
143
token = self.lockable.lock_write()
146
# This test does not apply, because this lockable refuses
149
new_lockable = self.get_lockable()
150
token_from_new_lockable = new_lockable.lock_write(token=token)
152
self.assertEqual(token, token_from_new_lockable)
154
new_lockable.unlock()
156
self.lockable.unlock()
158
def test_lock_write_raises_on_token_mismatch(self):
159
token = self.lockable.lock_write()
162
# This test does not apply, because this lockable refuses
165
different_token = token + 'xxx'
166
# Re-using the same lockable instance with a different token will
167
# raise TokenMismatch.
168
self.assertRaises(errors.TokenMismatch,
169
self.lockable.lock_write, token=different_token)
170
# A seperate instance for the same lockable will also raise
172
# This detects the case where a caller claims to have a lock (via
173
# the token) for an external resource, but doesn't (the token is
174
# different). Clients need a seperate lock object to make sure the
175
# external resource is probed, whereas the existing lock object
177
new_lockable = self.get_lockable()
178
self.assertRaises(errors.TokenMismatch,
179
new_lockable.lock_write, token=different_token)
181
self.lockable.unlock()
183
def test_lock_write_with_matching_token(self):
184
# If the token matches, so no exception is raised by lock_write.
185
token = self.lockable.lock_write()
188
# This test does not apply, because this lockable refuses
191
# The same instance will accept a second lock_write if the specified
193
self.lockable.lock_write(token=token)
194
self.lockable.unlock()
195
# Calling lock_write on a new instance for the same lockable will
197
new_lockable = self.get_lockable()
198
new_lockable.lock_write(token=token)
199
new_lockable.unlock()
201
self.lockable.unlock()
203
def test_unlock_after_lock_write_with_token(self):
204
# If lock_write did not physically acquire the lock (because it was
205
# passed a token), then unlock should not physically release it.
206
token = self.lockable.lock_write()
209
# This test does not apply, because this lockable refuses
212
new_lockable = self.get_lockable()
213
new_lockable.lock_write(token=token)
214
new_lockable.unlock()
215
self.assertTrue(self.lockable.get_physical_lock_status())
217
self.lockable.unlock()
219
def test_lock_write_with_token_fails_when_unlocked(self):
220
# Lock and unlock to get a superficially valid token. This mimics a
221
# likely programming error, where a caller accidentally tries to lock
222
# with a token that is no longer valid (because the original lock was
224
token = self.lockable.lock_write()
225
self.lockable.unlock()
227
# This test does not apply, because this lockable refuses
231
self.assertRaises(errors.TokenMismatch,
232
self.lockable.lock_write, token=token)
234
def test_lock_write_reenter_with_token(self):
235
token = self.lockable.lock_write()
238
# This test does not apply, because this lockable refuses
241
# Relock with a token.
242
token_from_reentry = self.lockable.lock_write(token=token)
244
self.assertEqual(token, token_from_reentry)
246
self.lockable.unlock()
248
self.lockable.unlock()
249
# The lock should be unlocked on disk. Verify that with a new lock
251
new_lockable = self.get_lockable()
252
# Calling lock_write now should work, rather than raise LockContention.
253
new_lockable.lock_write()
254
new_lockable.unlock()
256
def test_leave_in_place(self):
257
token = self.lockable.lock_write()
260
# This test does not apply, because this lockable refuses
263
self.lockable.leave_in_place()
265
self.lockable.unlock()
266
# At this point, the lock is still in place on disk
267
self.assertRaises(errors.LockContention, self.lockable.lock_write)
268
# But should be relockable with a token.
269
self.lockable.lock_write(token=token)
270
self.lockable.unlock()
272
def test_dont_leave_in_place(self):
273
token = self.lockable.lock_write()
276
# This test does not apply, because this lockable refuses
279
self.lockable.leave_in_place()
281
self.lockable.unlock()
282
# At this point, the lock is still in place on disk.
283
# Acquire the existing lock with the token, and ask that it is removed
284
# when this object unlocks, and unlock to trigger that removal.
285
new_lockable = self.get_lockable()
286
new_lockable.lock_write(token=token)
287
new_lockable.dont_leave_in_place()
288
new_lockable.unlock()
289
# At this point, the lock is no longer on disk, so we can lock it.
290
third_lockable = self.get_lockable()
291
third_lockable.lock_write()
292
third_lockable.unlock()
295
# This method of adapting tests to parameters is different to
296
# the TestProviderAdapters used elsewhere, but seems simpler for this
298
class TestLockableFiles_TransportLock(TestCaseInTempDir,
299
_TestLockableFiles_mixin):
302
TestCaseInTempDir.setUp(self)
303
_TestLockableFiles_mixin.setUp(self)
304
transport = get_transport('.')
305
transport.mkdir('.bzr')
306
self.sub_transport = transport.clone('.bzr')
307
self.lockable = self.get_lockable()
308
self.lockable.create_lock()
311
super(TestLockableFiles_TransportLock, self).tearDown()
312
# free the subtransport so that we do not get a 5 second
313
# timeout due to the SFTP connection cache.
314
del self.sub_transport
316
def get_lockable(self):
317
return LockableFiles(self.sub_transport, 'my-lock', TransportLock)
320
class TestLockableFiles_LockDir(TestCaseInTempDir,
321
_TestLockableFiles_mixin):
322
"""LockableFile tests run with LockDir underneath"""
325
TestCaseInTempDir.setUp(self)
326
_TestLockableFiles_mixin.setUp(self)
327
self.transport = get_transport('.')
328
self.lockable = self.get_lockable()
329
# the lock creation here sets mode - test_permissions on branch
330
# tests that implicitly, but it might be a good idea to factor
331
# out the mode checking logic and have it applied to loackable files
332
# directly. RBC 20060418
333
self.lockable.create_lock()
335
def get_lockable(self):
336
return LockableFiles(self.transport, 'my-lock', LockDir)
338
def test_lock_created(self):
339
self.assertTrue(self.transport.has('my-lock'))
340
self.lockable.lock_write()
341
self.assertTrue(self.transport.has('my-lock/held/info'))
342
self.lockable.unlock()
343
self.assertFalse(self.transport.has('my-lock/held/info'))
344
self.assertTrue(self.transport.has('my-lock'))
347
# TODO: Test the lockdir inherits the right file and directory permissions
348
# from the LockableFiles.
351
class TestLockableFiles_RemoteLockDir(TestCaseWithSmartMedium,
352
_TestLockableFiles_mixin):
353
"""LockableFile tests run with RemoteLockDir on a branch."""
356
TestCaseWithSmartMedium.setUp(self)
357
_TestLockableFiles_mixin.setUp(self)
358
# can only get a RemoteLockDir with some RemoteObject...
359
# use a branch as thats what we want. These mixin tests test the end
360
# to end behaviour, so stubbing out the backend and simulating would
361
# defeat the purpose. We test the protocol implementation separately
362
# in test_remote and test_smart as usual.
363
self.make_branch('foo')
364
self.transport = get_transport('.')
365
self.lockable = self.get_lockable()
367
def get_lockable(self):
368
# getting a new lockable involves opening a new instance of the branch
369
branch = bzrlib.branch.Branch.open(self.get_url('foo'))
370
return branch.control_files
372
def test_lock_write_returns_None_refuses_token(self):
373
# this test is not relevant for RemoteBranchLockableFiles as remote
374
# locks are done directly from the remote branch object.
377
def test_lock_write_raises_on_token_mismatch(self):
378
# See test_lock_write_returns_None_refuses_token.
381
def test_lock_write_with_matching_token(self):
382
# See test_lock_write_returns_None_refuses_token.
385
def test_unlock_after_lock_write_with_token(self):
386
# See test_lock_write_returns_None_refuses_token.
389
def test_lock_write_with_token_fails_when_unlocked(self):
390
# See test_lock_write_returns_None_refuses_token.
393
def test_lock_write_reenter_with_token(self):
394
# See test_lock_write_returns_None_refuses_token.
397
def test_leave_in_place(self):
398
# See test_lock_write_returns_None_refuses_token.
401
def test_dont_leave_in_place(self):
402
# See test_lock_write_returns_None_refuses_token.
405
def test_lock_write_returns_token_when_given_token(self):
406
# See test_lock_write_returns_None_refuses_token.