~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/workingtree_implementations/test_locking.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
        self.assertFalse(wt.is_locked())
54
54
        self.assertFalse(wt.branch.is_locked())
55
55
        
 
56
    def test_trivial_lock_tree_write_unlock(self):
 
57
        """Locking for tree write is ok when the branch is not locked."""
 
58
        wt = self.make_branch_and_tree('.')
 
59
 
 
60
        self.assertFalse(wt.is_locked())
 
61
        self.assertFalse(wt.branch.is_locked())
 
62
        wt.lock_tree_write()
 
63
        try:
 
64
            self.assertTrue(wt.is_locked())
 
65
            self.assertTrue(wt.branch.is_locked())
 
66
        finally:
 
67
            wt.unlock()
 
68
        self.assertFalse(wt.is_locked())
 
69
        self.assertFalse(wt.branch.is_locked())
 
70
        
 
71
    def test_trivial_lock_tree_write_branch_read_locked(self):
 
72
        """It is ok to lock_tree_write when the branch is read locked."""
 
73
        wt = self.make_branch_and_tree('.')
 
74
 
 
75
        self.assertFalse(wt.is_locked())
 
76
        self.assertFalse(wt.branch.is_locked())
 
77
        wt.branch.lock_read()
 
78
        try:
 
79
            wt.lock_tree_write()
 
80
        except errors.ReadOnlyError:
 
81
            # When ReadOnlyError is raised, it indicates that the 
 
82
            # workingtree shares its lock with the branch, which is what
 
83
            # the git/hg/bzr0.6 formats do.
 
84
            # in this case, no lock should have been taken - but the tree
 
85
            # will have been locked because they share a lock. Unlocking
 
86
            # just the branch should make everything match again correctly.
 
87
            wt.branch.unlock()
 
88
            self.assertFalse(wt.is_locked())
 
89
            self.assertFalse(wt.branch.is_locked())
 
90
            return
 
91
        try:
 
92
            self.assertTrue(wt.is_locked())
 
93
            self.assertTrue(wt.branch.is_locked())
 
94
        finally:
 
95
            wt.unlock()
 
96
        self.assertFalse(wt.is_locked())
 
97
        self.assertTrue(wt.branch.is_locked())
 
98
        wt.branch.unlock()
 
99
        
56
100
    def test_unlock_branch_failures(self):
57
101
        """If the branch unlock fails the tree must still unlock."""
58
102
        # The public interface for WorkingTree requires a branch, but
143
187
                    wt.unlock()
144
188
        finally:
145
189
            branch_copy.unlock()
 
190
 
 
191
    def test_failing_to_lock_tree_write_branch_does_not_lock(self):
 
192
        """If the branch cannot be read locked, dont lock the tree."""
 
193
        # Many implementations treat read-locks as non-blocking, but some
 
194
        # treat them as blocking with writes.. Accordingly we test this by
 
195
        # opening the branch twice, and locking the branch for write in the
 
196
        # second instance.  Our lock contract requires separate instances to
 
197
        # mutually exclude if a lock is exclusive at all: If we get no error
 
198
        # locking, the test still passes.
 
199
        wt = self.make_branch_and_tree('.')
 
200
        branch_copy = branch.Branch.open('.')
 
201
        branch_copy.lock_write()
 
202
        try:
 
203
            try:
 
204
                wt.lock_tree_write()
 
205
            except errors.LockError:
 
206
                # any error here means the locks are exclusive in some 
 
207
                # manner
 
208
                self.assertFalse(wt.is_locked())
 
209
                self.assertFalse(wt.branch.is_locked())
 
210
                return
 
211
            else:
 
212
                # no error - the branch allows read locks while writes
 
213
                # are taken, just pass.
 
214
                wt.unlock()
 
215
        finally:
 
216
            branch_copy.unlock()