/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1711.8.5 by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper.
1
# Copyright (C) 2006 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
"""Test locks across all branch implemenations"""
18
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
19
from bzrlib import errors
20
from bzrlib.branch import BzrBranchFormat4
21
from bzrlib.tests import TestSkipped
1711.8.5 by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper.
22
from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
1711.8.7 by John Arbash Meinel
Renaming LockHelpers.py to lock_helpers.py
23
from bzrlib.tests.lock_helpers import TestPreventLocking, LockWrapper
1711.8.5 by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper.
24
25
26
class TestBranchLocking(TestCaseWithBranch):
27
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
28
    def setUp(self):
29
        TestCaseWithBranch.setUp(self)
30
        self.reduceLockdirTimeout()
31
1711.8.5 by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper.
32
    def get_instrumented_branch(self):
33
        """Get a Branch object which has been instrumented"""
34
        # TODO: jam 20060630 It may be that not all formats have a 
35
        # 'control_files' member. So we should fail gracefully if
36
        # not there. But assuming it has them lets us test the exact 
37
        # lock/unlock order.
38
        self.locks = []
39
        b = LockWrapper(self.locks, self.get_branch(), 'b')
40
        b.repository = LockWrapper(self.locks, b.repository, 'r')
41
        bcf = b.control_files
42
        rcf = b.repository.control_files
43
44
        # Look out for branch types that reuse their control files
45
        self.combined_control = bcf is rcf
46
47
        b.control_files = LockWrapper(self.locks, b.control_files, 'bc')
48
        b.repository.control_files = \
49
            LockWrapper(self.locks, b.repository.control_files, 'rc')
50
        return b
51
52
    def test_01_lock_read(self):
53
        # Test that locking occurs in the correct order
54
        b = self.get_instrumented_branch()
55
56
        self.assertFalse(b.is_locked())
57
        self.assertFalse(b.repository.is_locked())
58
        b.lock_read()
59
        try:
60
            self.assertTrue(b.is_locked())
61
            self.assertTrue(b.repository.is_locked())
62
        finally:
63
            b.unlock()
64
        self.assertFalse(b.is_locked())
65
        self.assertFalse(b.repository.is_locked())
66
67
        self.assertEqual([('b', 'lr', True),
68
                          ('r', 'lr', True),
69
                          ('rc', 'lr', True),
70
                          ('bc', 'lr', True),
71
                          ('b', 'ul', True),
72
                          ('bc', 'ul', True),
73
                          ('r', 'ul', True),
74
                          ('rc', 'ul', True),
75
                         ], self.locks)
76
77
    def test_02_lock_write(self):
78
        # Test that locking occurs in the correct order
79
        b = self.get_instrumented_branch()
80
81
        self.assertFalse(b.is_locked())
82
        self.assertFalse(b.repository.is_locked())
83
        b.lock_write()
84
        try:
85
            self.assertTrue(b.is_locked())
86
            self.assertTrue(b.repository.is_locked())
87
        finally:
88
            b.unlock()
89
        self.assertFalse(b.is_locked())
90
        self.assertFalse(b.repository.is_locked())
91
92
        self.assertEqual([('b', 'lw', True),
93
                          ('r', 'lw', True),
94
                          ('rc', 'lw', True),
95
                          ('bc', 'lw', True),
96
                          ('b', 'ul', True),
97
                          ('bc', 'ul', True),
98
                          ('r', 'ul', True),
99
                          ('rc', 'ul', True),
100
                         ], self.locks)
101
102
    def test_03_lock_fail_unlock_repo(self):
103
        # Make sure branch.unlock() is called, even if there is a
104
        # failure while unlocking the repository.
105
        b = self.get_instrumented_branch()
106
        b.repository.disable_unlock()
107
108
        self.assertFalse(b.is_locked())
109
        self.assertFalse(b.repository.is_locked())
110
        b.lock_write()
111
        try:
112
            self.assertTrue(b.is_locked())
113
            self.assertTrue(b.repository.is_locked())
114
            self.assertRaises(TestPreventLocking, b.unlock)
115
            if self.combined_control:
116
                self.assertTrue(b.is_locked())
117
            else:
118
                self.assertFalse(b.is_locked())
119
            self.assertTrue(b.repository.is_locked())
120
121
            # We unlock the branch control files, even if 
122
            # we fail to unlock the repository
123
            self.assertEqual([('b', 'lw', True),
124
                              ('r', 'lw', True),
125
                              ('rc', 'lw', True),
126
                              ('bc', 'lw', True),
127
                              ('b', 'ul', True),
128
                              ('bc', 'ul', True),
129
                              ('r', 'ul', False), 
130
                             ], self.locks)
131
132
        finally:
133
            # For cleanup purposes, make sure we are unlocked
134
            b.repository._other.unlock()
135
136
    def test_04_lock_fail_unlock_control(self):
137
        # Make sure repository.unlock() is called, if we fail to unlock self
138
        b = self.get_instrumented_branch()
139
        b.control_files.disable_unlock()
140
141
        self.assertFalse(b.is_locked())
142
        self.assertFalse(b.repository.is_locked())
143
        b.lock_write()
144
        try:
145
            self.assertTrue(b.is_locked())
146
            self.assertTrue(b.repository.is_locked())
147
            self.assertRaises(TestPreventLocking, b.unlock)
148
            self.assertTrue(b.is_locked())
149
            if self.combined_control:
150
                self.assertTrue(b.repository.is_locked())
151
            else:
152
                self.assertFalse(b.repository.is_locked())
153
154
            # We unlock the repository even if 
155
            # we fail to unlock the control files
156
            self.assertEqual([('b', 'lw', True),
157
                              ('r', 'lw', True),
158
                              ('rc', 'lw', True),
159
                              ('bc', 'lw', True),
160
                              ('b', 'ul', True),
161
                              ('bc', 'ul', False),
162
                              ('r', 'ul', True), 
163
                              ('rc', 'ul', True), 
164
                             ], self.locks)
165
166
        finally:
167
            # For cleanup purposes, make sure we are unlocked
168
            b.control_files._other.unlock()
169
170
    def test_05_lock_read_fail_repo(self):
171
        # Test that the branch is not locked if it cannot lock the repository
172
        b = self.get_instrumented_branch()
173
        b.repository.disable_lock_read()
174
175
        self.assertRaises(TestPreventLocking, b.lock_read)
176
        self.assertFalse(b.is_locked())
177
        self.assertFalse(b.repository.is_locked())
178
179
        self.assertEqual([('b', 'lr', True),
180
                          ('r', 'lr', False), 
181
                         ], self.locks)
182
183
    def test_06_lock_write_fail_repo(self):
184
        # Test that the branch is not locked if it cannot lock the repository
185
        b = self.get_instrumented_branch()
186
        b.repository.disable_lock_write()
187
188
        self.assertRaises(TestPreventLocking, b.lock_write)
189
        self.assertFalse(b.is_locked())
190
        self.assertFalse(b.repository.is_locked())
191
192
        self.assertEqual([('b', 'lw', True),
193
                          ('r', 'lw', False), 
194
                         ], self.locks)
195
196
    def test_07_lock_read_fail_control(self):
197
        # Test the repository is unlocked if we can't lock self
198
        b = self.get_instrumented_branch()
199
        b.control_files.disable_lock_read()
200
201
        self.assertRaises(TestPreventLocking, b.lock_read)
202
        self.assertFalse(b.is_locked())
203
        self.assertFalse(b.repository.is_locked())
204
205
        self.assertEqual([('b', 'lr', True),
206
                          ('r', 'lr', True),
207
                          ('rc', 'lr', True),
208
                          ('bc', 'lr', False),
209
                          ('r', 'ul', True),
210
                          ('rc', 'ul', True),
211
                         ], self.locks)
212
213
    def test_08_lock_write_fail_control(self):
214
        # Test the repository is unlocked if we can't lock self
215
        b = self.get_instrumented_branch()
216
        b.control_files.disable_lock_write()
217
218
        self.assertRaises(TestPreventLocking, b.lock_write)
219
        self.assertFalse(b.is_locked())
220
        self.assertFalse(b.repository.is_locked())
221
222
        self.assertEqual([('b', 'lw', True),
223
                          ('r', 'lw', True),
224
                          ('rc', 'lw', True),
225
                          ('bc', 'lw', False),
226
                          ('r', 'ul', True),
227
                          ('rc', 'ul', True),
228
                         ], self.locks)
229
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
230
    def test_lock_read_then_unlock(self):
231
        # Calling lock_read then unlocking should work without errors.
232
        branch = self.make_branch('b')
233
        branch.lock_read()
234
        branch.unlock()
235
236
    def test_lock_write_locks_repo_too(self):
237
        if isinstance(self.branch_format, BzrBranchFormat4):
238
            # Branch format 4 is combined with the repository, so this test
239
            # doesn't apply.
240
            return
241
        branch = self.make_branch('b')
242
        branch = branch.bzrdir.open_branch()
243
        branch.lock_write()
244
        try:
245
            # Now the branch.repository is locked, so we can't lock it with a new
246
            # repository without a token.
247
            new_repo = branch.bzrdir.open_repository()
248
            self.assertRaises(errors.LockContention, new_repo.lock_write)
249
            # We can call lock_write on the original repository object though,
250
            # because it is already locked.
251
            branch.repository.lock_write()
252
            branch.repository.unlock()
253
        finally:
254
            branch.unlock()