~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-11-26 21:33:20 UTC
  • mfrom: (3015.2.15 pack.read-locks)
  • Revision ID: pqm@pqm.ubuntu.com-20071126213320-adxxra3gsie5inhw
(robertc) Many fixes to support packs on the smart server and as the
        default format. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
        bcf = b.control_files
43
43
        rcf = getattr(b.repository, 'control_files', None)
44
44
        if rcf is None:
45
 
            raise TestSkipped(
46
 
                "This tests depends on being able to instrument "
47
 
                "repository.control_files, but %r doesn't have control_files."
48
 
                % (b.repository,))
49
 
 
50
 
        # Look out for branch types that reuse their control files
51
 
        self.combined_control = bcf is rcf
52
 
 
53
 
        b.control_files = LockWrapper(self.locks, b.control_files, 'bc')
54
 
        b.repository.control_files = \
55
 
            LockWrapper(self.locks, b.repository.control_files, 'rc')
 
45
            self.combined_branch = False
 
46
        else:
 
47
            # Look out for branch types that reuse their control files
 
48
            self.combined_control = bcf is rcf
 
49
        try:
 
50
            b.control_files = LockWrapper(self.locks, b.control_files, 'bc')
 
51
        except AttributeError:
 
52
            # RemoteBranch seems to trigger this.
 
53
            raise TestSkipped("Could not instrument branch control files.")
 
54
        if self.combined_control:
 
55
            # instrument the repository control files too to ensure its worked
 
56
            # with correctly. When they are not shared, we trust the repository
 
57
            # API and only instrument the repository itself. 
 
58
            b.repository.control_files = \
 
59
                LockWrapper(self.locks, b.repository.control_files, 'rc')
56
60
        return b
57
61
 
58
62
    def test_01_lock_read(self):
70
74
        self.assertFalse(b.is_locked())
71
75
        self.assertFalse(b.repository.is_locked())
72
76
 
73
 
        self.assertEqual([('b', 'lr', True),
74
 
                          ('r', 'lr', True),
75
 
                          ('rc', 'lr', True),
76
 
                          ('bc', 'lr', True),
77
 
                          ('b', 'ul', True),
78
 
                          ('bc', 'ul', True),
79
 
                          ('r', 'ul', True),
80
 
                          ('rc', 'ul', True),
81
 
                         ], self.locks)
 
77
        if self.combined_control:
 
78
            self.assertEqual([('b', 'lr', True),
 
79
                              ('r', 'lr', True),
 
80
                              ('rc', 'lr', True),
 
81
                              ('bc', 'lr', True),
 
82
                              ('b', 'ul', True),
 
83
                              ('bc', 'ul', True),
 
84
                              ('r', 'ul', True),
 
85
                              ('rc', 'ul', True),
 
86
                             ], self.locks)
 
87
        else:
 
88
            self.assertEqual([('b', 'lr', True),
 
89
                              ('r', 'lr', True),
 
90
                              ('bc', 'lr', True),
 
91
                              ('b', 'ul', True),
 
92
                              ('bc', 'ul', True),
 
93
                              ('r', 'ul', True),
 
94
                             ], self.locks)
82
95
 
83
96
    def test_02_lock_write(self):
84
97
        # Test that locking occurs in the correct order
95
108
        self.assertFalse(b.is_locked())
96
109
        self.assertFalse(b.repository.is_locked())
97
110
 
98
 
        self.assertEqual([('b', 'lw', True),
99
 
                          ('r', 'lw', True),
100
 
                          ('rc', 'lw', True),
101
 
                          ('bc', 'lw', True),
102
 
                          ('b', 'ul', True),
103
 
                          ('bc', 'ul', True),
104
 
                          ('r', 'ul', True),
105
 
                          ('rc', 'ul', True),
106
 
                         ], self.locks)
 
111
        if self.combined_control:
 
112
            self.assertEqual([('b', 'lw', True),
 
113
                              ('r', 'lw', True),
 
114
                              ('rc', 'lw', True),
 
115
                              ('bc', 'lw', True),
 
116
                              ('b', 'ul', True),
 
117
                              ('bc', 'ul', True),
 
118
                              ('r', 'ul', True),
 
119
                              ('rc', 'ul', True),
 
120
                             ], self.locks)
 
121
        else:
 
122
            self.assertEqual([('b', 'lw', True),
 
123
                              ('r', 'lw', True),
 
124
                              ('bc', 'lw', True),
 
125
                              ('b', 'ul', True),
 
126
                              ('bc', 'ul', True),
 
127
                              ('r', 'ul', True),
 
128
                             ], self.locks)
107
129
 
108
130
    def test_03_lock_fail_unlock_repo(self):
109
131
        # Make sure branch.unlock() is called, even if there is a
126
148
 
127
149
            # We unlock the branch control files, even if 
128
150
            # we fail to unlock the repository
129
 
            self.assertEqual([('b', 'lw', True),
130
 
                              ('r', 'lw', True),
131
 
                              ('rc', 'lw', True),
132
 
                              ('bc', 'lw', True),
133
 
                              ('b', 'ul', True),
134
 
                              ('bc', 'ul', True),
135
 
                              ('r', 'ul', False), 
136
 
                             ], self.locks)
 
151
            if self.combined_control:
 
152
                self.assertEqual([('b', 'lw', True),
 
153
                                  ('r', 'lw', True),
 
154
                                  ('rc', 'lw', True),
 
155
                                  ('bc', 'lw', True),
 
156
                                  ('b', 'ul', True),
 
157
                                  ('bc', 'ul', True),
 
158
                                  ('r', 'ul', False),
 
159
                                 ], self.locks)
 
160
            else:
 
161
                self.assertEqual([('b', 'lw', True),
 
162
                                  ('r', 'lw', True),
 
163
                                  ('bc', 'lw', True),
 
164
                                  ('b', 'ul', True),
 
165
                                  ('bc', 'ul', True),
 
166
                                  ('r', 'ul', False),
 
167
                                 ], self.locks)
137
168
 
138
169
        finally:
139
170
            # For cleanup purposes, make sure we are unlocked
159
190
 
160
191
            # We unlock the repository even if 
161
192
            # we fail to unlock the control files
162
 
            self.assertEqual([('b', 'lw', True),
163
 
                              ('r', 'lw', True),
164
 
                              ('rc', 'lw', True),
165
 
                              ('bc', 'lw', True),
166
 
                              ('b', 'ul', True),
167
 
                              ('bc', 'ul', False),
168
 
                              ('r', 'ul', True), 
169
 
                              ('rc', 'ul', True), 
170
 
                             ], self.locks)
 
193
            if self.combined_control:
 
194
                self.assertEqual([('b', 'lw', True),
 
195
                                  ('r', 'lw', True),
 
196
                                  ('rc', 'lw', True),
 
197
                                  ('bc', 'lw', True),
 
198
                                  ('b', 'ul', True),
 
199
                                  ('bc', 'ul', False),
 
200
                                  ('r', 'ul', True),
 
201
                                  ('rc', 'ul', True),
 
202
                                 ], self.locks)
 
203
            else:
 
204
                self.assertEqual([('b', 'lw', True),
 
205
                                  ('r', 'lw', True),
 
206
                                  ('bc', 'lw', True),
 
207
                                  ('b', 'ul', True),
 
208
                                  ('bc', 'ul', False),
 
209
                                  ('r', 'ul', True),
 
210
                                 ], self.locks)
171
211
 
172
212
        finally:
173
213
            # For cleanup purposes, make sure we are unlocked
208
248
        self.assertFalse(b.is_locked())
209
249
        self.assertFalse(b.repository.is_locked())
210
250
 
211
 
        self.assertEqual([('b', 'lr', True),
212
 
                          ('r', 'lr', True),
213
 
                          ('rc', 'lr', True),
214
 
                          ('bc', 'lr', False),
215
 
                          ('r', 'ul', True),
216
 
                          ('rc', 'ul', True),
217
 
                         ], self.locks)
 
251
        if self.combined_control:
 
252
            self.assertEqual([('b', 'lr', True),
 
253
                              ('r', 'lr', True),
 
254
                              ('rc', 'lr', True),
 
255
                              ('bc', 'lr', False),
 
256
                              ('r', 'ul', True),
 
257
                              ('rc', 'ul', True),
 
258
                             ], self.locks)
 
259
        else:
 
260
            self.assertEqual([('b', 'lr', True),
 
261
                              ('r', 'lr', True),
 
262
                              ('bc', 'lr', False),
 
263
                              ('r', 'ul', True),
 
264
                             ], self.locks)
218
265
 
219
266
    def test_08_lock_write_fail_control(self):
220
267
        # Test the repository is unlocked if we can't lock self
224
271
        self.assertRaises(TestPreventLocking, b.lock_write)
225
272
        self.assertFalse(b.is_locked())
226
273
        self.assertFalse(b.repository.is_locked())
227
 
 
228
 
        self.assertEqual([('b', 'lw', True),
229
 
                          ('r', 'lw', True),
230
 
                          ('rc', 'lw', True),
231
 
                          ('bc', 'lw', False),
232
 
                          ('r', 'ul', True),
233
 
                          ('rc', 'ul', True),
234
 
                         ], self.locks)
 
274
        if self.combined_control:
 
275
            self.assertEqual([('b', 'lw', True),
 
276
                              ('r', 'lw', True),
 
277
                              ('rc', 'lw', True),
 
278
                              ('bc', 'lw', False),
 
279
                              ('r', 'ul', True),
 
280
                              ('rc', 'ul', True),
 
281
                             ], self.locks)
 
282
        else:
 
283
            self.assertEqual([('b', 'lw', True),
 
284
                              ('r', 'lw', True),
 
285
                              ('bc', 'lw', False),
 
286
                              ('r', 'ul', True),
 
287
                             ], self.locks)
235
288
 
236
289
    def test_lock_write_returns_None_refuses_token(self):
237
290
        branch = self.make_branch('b')
398
451
            except NotImplementedError:
399
452
                # This branch doesn't support this API.
400
453
                return
401
 
            branch.repository.leave_lock_in_place()
402
 
            repo_token = branch.repository.lock_write()
403
 
            branch.repository.unlock()
 
454
            try:
 
455
                branch.repository.leave_lock_in_place()
 
456
            except NotImplementedError:
 
457
                # This repo doesn't support leaving locks around,
 
458
                # assume it is essentially lock-free.
 
459
                repo_token = None
 
460
            else:
 
461
                repo_token = branch.repository.lock_write()
 
462
                branch.repository.unlock()
404
463
        finally:
405
464
            branch.unlock()
406
465
        # Reacquire the lock (with a different branch object) by using the
407
466
        # tokens.
408
467
        new_branch = branch.bzrdir.open_branch()
409
 
        # We have to explicitly lock the repository first.
410
 
        new_branch.repository.lock_write(token=repo_token)
 
468
        if repo_token is not None:
 
469
            # We have to explicitly lock the repository first.
 
470
            new_branch.repository.lock_write(token=repo_token)
411
471
        new_branch.lock_write(token=token)
412
 
        # Now we don't need our own repository lock anymore (the branch is
413
 
        # holding it for us).
414
 
        new_branch.repository.unlock()
 
472
        if repo_token is not None:
 
473
            # Now we don't need our own repository lock anymore (the branch is
 
474
            # holding it for us).
 
475
            new_branch.repository.unlock()
415
476
        # Call dont_leave_lock_in_place, so that the lock will be released by
416
477
        # this instance, even though the lock wasn't originally acquired by it.
417
478
        new_branch.dont_leave_lock_in_place()
418
 
        new_branch.repository.dont_leave_lock_in_place()
 
479
        if repo_token is not None:
 
480
            new_branch.repository.dont_leave_lock_in_place()
419
481
        new_branch.unlock()
420
482
        # Now the branch (and repository) is unlocked.  Test this by locking it
421
483
        # without tokens.
437
499
        branch = branch.bzrdir.open_branch()
438
500
        branch.lock_write()
439
501
        try:
440
 
            # Now the branch.repository is locked, so we can't lock it with a new
441
 
            # repository without a token.
 
502
            # The branch should have asked the repository to lock.
 
503
            self.assertTrue(branch.repository.is_write_locked())
 
504
            # Does the repository type actually lock?
 
505
            if not branch.repository.get_physical_lock_status():
 
506
                # The test was successfully applied, so it was applicable.
 
507
                return
 
508
            # Now the branch.repository is physically locked, so we can't lock
 
509
            # it with a new repository instance.
442
510
            new_repo = branch.bzrdir.open_repository()
443
511
            self.assertRaises(errors.LockContention, new_repo.lock_write)
444
512
            # We can call lock_write on the original repository object though,