/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/branch_implementations/test_locking.py

  • Committer: Andrew Bennetts
  • Date: 2007-04-12 07:08:33 UTC
  • mto: (2018.18.11 hpss-faster-copy)
  • mto: This revision was merged to the branch mainline in revision 2435.
  • Revision ID: andrew.bennetts@canonical.com-20070412070833-5fio6r0fkgnf10u1
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).

Show diffs side-by-side

added added

removed removed

Lines of Context:
231
231
                          ('rc', 'ul', True),
232
232
                         ], self.locks)
233
233
 
234
 
    def test_lock_write_returns_None_refuses_tokens(self):
 
234
    def test_lock_write_returns_None_refuses_token(self):
235
235
        branch = self.make_branch('b')
236
 
        tokens = branch.lock_write()
 
236
        token = branch.lock_write()
237
237
        try:
238
 
            if tokens is not None:
 
238
            if token is not None:
239
239
                # This test does not apply, because this lockable supports
240
240
                # tokens.
241
241
                return
242
242
            self.assertRaises(errors.TokenLockingNotSupported,
243
 
                              branch.lock_write, tokens=('token','token'))
 
243
                              branch.lock_write, token='token')
244
244
        finally:
245
245
            branch.unlock()
246
246
 
247
247
    def test_reentering_lock_write_raises_on_token_mismatch(self):
248
248
        branch = self.make_branch('b')
249
 
        tokens = branch.lock_write()
 
249
        token = branch.lock_write()
250
250
        try:
251
 
            if tokens is None:
 
251
            if token is None:
252
252
                # This test does not apply, because this lockable refuses
253
253
                # tokens.
254
254
                return
255
 
            branch_token, repo_token = tokens
256
 
            different_branch_token = branch_token + 'xxx'
257
 
            different_repo_token = repo_token + 'xxx'
 
255
            different_branch_token = token + 'xxx'
258
256
            # Re-using the same lockable instance with a different branch token
259
257
            # will raise TokenMismatch.
260
258
            self.assertRaises(errors.TokenMismatch,
261
259
                              branch.lock_write,
262
 
                              tokens=(different_branch_token, repo_token))
263
 
            # Similarly for a different repository token.
264
 
            self.assertRaises(errors.TokenMismatch,
265
 
                              branch.lock_write,
266
 
                              tokens=(branch_token, different_repo_token))
 
260
                              token=different_branch_token)
267
261
        finally:
268
262
            branch.unlock()
269
263
 
270
264
    def test_lock_write_with_nonmatching_token(self):
271
265
        branch = self.make_branch('b')
272
 
        tokens = branch.lock_write()
 
266
        token = branch.lock_write()
273
267
        try:
274
 
            if tokens is None:
 
268
            if token is None:
275
269
                # This test does not apply, because this branch refuses
276
270
                # tokens.
277
271
                return
278
 
            branch_token, repo_token = tokens
279
 
            different_branch_token = branch_token + 'xxx'
280
 
            different_repo_token = repo_token + 'xxx'
 
272
            different_branch_token = token + 'xxx'
281
273
 
282
274
            new_branch = branch.bzrdir.open_branch()
283
275
            # We only want to test the relocking abilities of branch, so use the
285
277
            new_branch.repository = branch.repository
286
278
            self.assertRaises(errors.TokenMismatch,
287
279
                              new_branch.lock_write,
288
 
                              tokens=(different_branch_token, repo_token))
289
 
            self.assertRaises(errors.TokenMismatch,
290
 
                              new_branch.lock_write,
291
 
                              tokens=(branch_token, different_repo_token))
 
280
                              token=different_branch_token)
292
281
        finally:
293
282
            branch.unlock()
294
283
 
297
286
        """Test that a branch can be locked with a token, if it is already
298
287
        locked by that token."""
299
288
        branch = self.make_branch('b')
300
 
        tokens = branch.lock_write()
 
289
        token = branch.lock_write()
301
290
        try:
302
 
            if tokens is None:
 
291
            if token is None:
303
292
                # This test does not apply, because this branch refuses tokens.
304
293
                return
305
294
            # The same instance will accept a second lock_write if the specified
306
295
            # token matches.
307
 
            branch.lock_write(tokens=tokens)
 
296
            branch.lock_write(token=token)
308
297
            branch.unlock()
309
298
            # Calling lock_write on a new instance for the same lockable will
310
299
            # also succeed.
312
301
            # We only want to test the relocking abilities of branch, so use the
313
302
            # existing repository object which is already locked.
314
303
            new_branch.repository = branch.repository
315
 
            new_branch.lock_write(tokens=tokens)
 
304
            new_branch.lock_write(token=token)
316
305
            new_branch.unlock()
317
306
        finally:
318
307
            branch.unlock()
319
308
 
320
 
    def test_unlock_after_lock_write_with_tokens(self):
 
309
    def test_unlock_after_lock_write_with_token(self):
321
310
        # If lock_write did not physically acquire the lock (because it was
322
311
        # passed some tokens), then unlock should not physically release it.
323
312
        branch = self.make_branch('b')
324
 
        tokens = branch.lock_write()
 
313
        token = branch.lock_write()
325
314
        try:
326
 
            if tokens is None:
 
315
            if token is None:
327
316
                # This test does not apply, because this lockable refuses
328
317
                # tokens.
329
318
                return
331
320
            # We only want to test the relocking abilities of branch, so use the
332
321
            # existing repository object which is already locked.
333
322
            new_branch.repository = branch.repository
334
 
            new_branch.lock_write(tokens=tokens)
 
323
            new_branch.lock_write(token=token)
335
324
            new_branch.unlock()
336
325
            self.assertTrue(branch.get_physical_lock_status()) #XXX
337
326
        finally:
338
327
            branch.unlock()
339
328
 
340
 
    def test_lock_write_with_tokens_fails_when_unlocked(self):
341
 
        # Lock and unlock to get superficially valid tokens.  This mimics a
342
 
        # likely programming error, where a caller accidentally tries to lock
343
 
        # with tokens that are no longer valid (because the original lock was
344
 
        # released).
 
329
    def test_lock_write_with_token_fails_when_unlocked(self):
 
330
        # First, lock and then unlock to get superficially valid tokens.  This
 
331
        # mimics a likely programming error, where a caller accidentally tries
 
332
        # to lock with a token that is no longer valid (because the original
 
333
        # lock was released).
345
334
        branch = self.make_branch('b')
346
 
        tokens = branch.lock_write()
 
335
        token = branch.lock_write()
347
336
        branch.unlock()
348
 
        if tokens is None:
 
337
        if token is None:
349
338
            # This test does not apply, because this lockable refuses
350
339
            # tokens.
351
340
            return
352
341
 
353
342
        self.assertRaises(errors.TokenMismatch,
354
 
                          branch.lock_write, tokens=tokens)
 
343
                          branch.lock_write, token=token)
355
344
 
356
 
    def test_lock_write_reenter_with_tokens(self):
 
345
    def test_lock_write_reenter_with_token(self):
357
346
        branch = self.make_branch('b')
358
 
        tokens = branch.lock_write()
 
347
        token = branch.lock_write()
359
348
        try:
360
 
            if tokens is None:
 
349
            if token is None:
361
350
                # This test does not apply, because this lockable refuses
362
351
                # tokens.
363
352
                return
364
353
            # Relock with a token.
365
 
            branch.lock_write(tokens=tokens)
 
354
            branch.lock_write(token=token)
366
355
            branch.unlock()
367
356
        finally:
368
357
            branch.unlock()
377
366
        branch = self.make_branch('b')
378
367
        # Lock the branch, then use leave_lock_in_place so that when we
379
368
        # unlock the branch the lock is still held on disk.
380
 
        tokens = branch.lock_write()
 
369
        token = branch.lock_write()
381
370
        try:
382
 
            if tokens is None:
 
371
            if token is None:
383
372
                # This test does not apply, because this repository refuses lock
384
373
                # tokens.
385
 
                self.assertRaises(NotImplementedError, branch.leave_lock_in_place)
 
374
                self.assertRaises(NotImplementedError,
 
375
                                  branch.leave_lock_in_place)
386
376
                return
387
377
            branch.leave_lock_in_place()
388
378
        finally:
393
383
    def test_dont_leave_lock_in_place(self):
394
384
        branch = self.make_branch('b')
395
385
        # Create a lock on disk.
396
 
        tokens = branch.lock_write()
 
386
        token = branch.lock_write()
397
387
        try:
398
 
            if tokens is None:
 
388
            if token is None:
399
389
                # This test does not apply, because this branch refuses lock
400
390
                # tokens.
401
391
                self.assertRaises(NotImplementedError,
407
397
                # This branch doesn't support this API.
408
398
                return
409
399
            branch.repository.leave_lock_in_place()
 
400
            repo_token = branch.repository.lock_write()
 
401
            branch.repository.unlock()
410
402
        finally:
411
403
            branch.unlock()
412
404
        # Reacquire the lock (with a different branch object) by using the
413
405
        # tokens.
414
406
        new_branch = branch.bzrdir.open_branch()
415
 
        new_branch.lock_write(tokens=tokens)
 
407
        # We have to explicitly lock the repository first.
 
408
        new_branch.repository.lock_write(token=repo_token)
 
409
        new_branch.lock_write(token=token)
 
410
        # Now we don't need our own repository lock anymore (the branch is
 
411
        # holding it for us).
 
412
        new_branch.repository.unlock()
416
413
        # Call dont_leave_lock_in_place, so that the lock will be released by
417
414
        # this instance, even though the lock wasn't originally acquired by it.
418
415
        new_branch.dont_leave_lock_in_place()