17
17
"""Tests for the (un)lock interfaces on all working tree implemenations."""
19
import bzrlib.branch as branch
20
import bzrlib.errors as errors
21
24
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
53
56
self.assertFalse(wt.is_locked())
54
57
self.assertFalse(wt.branch.is_locked())
59
def test_trivial_lock_tree_write_unlock(self):
60
"""Locking for tree write is ok when the branch is not locked."""
61
wt = self.make_branch_and_tree('.')
63
self.assertFalse(wt.is_locked())
64
self.assertFalse(wt.branch.is_locked())
67
self.assertTrue(wt.is_locked())
68
self.assertTrue(wt.branch.is_locked())
71
self.assertFalse(wt.is_locked())
72
self.assertFalse(wt.branch.is_locked())
74
def test_trivial_lock_tree_write_branch_read_locked(self):
75
"""It is ok to lock_tree_write when the branch is read locked."""
76
wt = self.make_branch_and_tree('.')
78
self.assertFalse(wt.is_locked())
79
self.assertFalse(wt.branch.is_locked())
83
except errors.ReadOnlyError:
84
# When ReadOnlyError is raised, it indicates that the
85
# workingtree shares its lock with the branch, which is what
86
# the git/hg/bzr0.6 formats do.
87
# in this case, no lock should have been taken - but the tree
88
# will have been locked because they share a lock. Unlocking
89
# just the branch should make everything match again correctly.
91
self.assertFalse(wt.is_locked())
92
self.assertFalse(wt.branch.is_locked())
95
self.assertTrue(wt.is_locked())
96
self.assertTrue(wt.branch.is_locked())
99
self.assertFalse(wt.is_locked())
100
self.assertTrue(wt.branch.is_locked())
56
103
def test_unlock_branch_failures(self):
57
104
"""If the branch unlock fails the tree must still unlock."""
58
105
# The public interface for WorkingTree requires a branch, but
135
182
branch_copy.lock_write()
138
self.assertRaises(errors.LockError, wt.lock_write)
185
orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
187
lockdir._DEFAULT_TIMEOUT_SECONDS = 1
188
self.assertRaises(errors.LockError, wt.lock_write)
190
lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
139
192
self.assertFalse(wt.is_locked())
140
193
self.assertFalse(wt.branch.is_locked())
145
198
branch_copy.unlock()
200
def test_failing_to_lock_tree_write_branch_does_not_lock(self):
201
"""If the branch cannot be read locked, dont lock the tree."""
202
# Many implementations treat read-locks as non-blocking, but some
203
# treat them as blocking with writes.. Accordingly we test this by
204
# opening the branch twice, and locking the branch for write in the
205
# second instance. Our lock contract requires separate instances to
206
# mutually exclude if a lock is exclusive at all: If we get no error
207
# locking, the test still passes.
208
wt = self.make_branch_and_tree('.')
209
branch_copy = branch.Branch.open('.')
211
branch_copy.lock_write()
215
except errors.LockError:
216
# any error here means the locks are exclusive in some
218
self.assertFalse(wt.is_locked())
219
self.assertFalse(wt.branch.is_locked())
222
# no error - the branch allows read locks while writes
223
# are taken, just pass.