/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2006-10-06 05:53:44 UTC
  • mfrom: (2063 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061006055344-e73b97b7c6ca6e72
[merge] bzr.dev 2063

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the (un)lock interfaces on all working tree implemenations."""
18
18
 
19
 
import bzrlib.branch as branch
20
 
import bzrlib.errors as errors
 
19
from bzrlib import (
 
20
    branch,
 
21
    errors,
 
22
    lockdir,
 
23
    )
21
24
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
22
25
 
23
26
 
53
56
        self.assertFalse(wt.is_locked())
54
57
        self.assertFalse(wt.branch.is_locked())
55
58
        
 
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('.')
 
62
 
 
63
        self.assertFalse(wt.is_locked())
 
64
        self.assertFalse(wt.branch.is_locked())
 
65
        wt.lock_tree_write()
 
66
        try:
 
67
            self.assertTrue(wt.is_locked())
 
68
            self.assertTrue(wt.branch.is_locked())
 
69
        finally:
 
70
            wt.unlock()
 
71
        self.assertFalse(wt.is_locked())
 
72
        self.assertFalse(wt.branch.is_locked())
 
73
        
 
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('.')
 
77
 
 
78
        self.assertFalse(wt.is_locked())
 
79
        self.assertFalse(wt.branch.is_locked())
 
80
        wt.branch.lock_read()
 
81
        try:
 
82
            wt.lock_tree_write()
 
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.
 
90
            wt.branch.unlock()
 
91
            self.assertFalse(wt.is_locked())
 
92
            self.assertFalse(wt.branch.is_locked())
 
93
            return
 
94
        try:
 
95
            self.assertTrue(wt.is_locked())
 
96
            self.assertTrue(wt.branch.is_locked())
 
97
        finally:
 
98
            wt.unlock()
 
99
        self.assertFalse(wt.is_locked())
 
100
        self.assertTrue(wt.branch.is_locked())
 
101
        wt.branch.unlock()
 
102
        
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()
136
183
        try:
137
184
            try:
138
 
                self.assertRaises(errors.LockError, wt.lock_write)
 
185
                orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
 
186
                try:
 
187
                    lockdir._DEFAULT_TIMEOUT_SECONDS = 1
 
188
                    self.assertRaises(errors.LockError, wt.lock_write)
 
189
                finally:
 
190
                    lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
 
191
 
139
192
                self.assertFalse(wt.is_locked())
140
193
                self.assertFalse(wt.branch.is_locked())
141
194
            finally:
143
196
                    wt.unlock()
144
197
        finally:
145
198
            branch_copy.unlock()
 
199
 
 
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('.')
 
210
 
 
211
        branch_copy.lock_write()
 
212
        try:
 
213
            try:
 
214
                wt.lock_tree_write()
 
215
            except errors.LockError:
 
216
                # any error here means the locks are exclusive in some 
 
217
                # manner
 
218
                self.assertFalse(wt.is_locked())
 
219
                self.assertFalse(wt.branch.is_locked())
 
220
                return
 
221
            else:
 
222
                # no error - the branch allows read locks while writes
 
223
                # are taken, just pass.
 
224
                wt.unlock()
 
225
        finally:
 
226
            branch_copy.unlock()