287
def test_34_lock_write_waits(self):
288
"""LockDir.lock_write() will wait for the lock."""
289
t = self.get_transport()
290
lf1 = LockDir(t, 'test_lock')
294
def wait_and_unlock():
297
unlocker = Thread(target=wait_and_unlock)
300
lf2 = LockDir(t, 'test_lock')
301
self.setup_log_reporter(lf2)
309
# There should be only 1 report, even though it should have to
311
lock_base = lf2.transport.abspath(lf2.path)
312
self.assertEqual(1, len(self._logged_reports))
313
self.assertEqual('%s %s\n'
315
'Will continue to try until %s\n',
316
self._logged_reports[0][0])
317
args = self._logged_reports[0][1]
318
self.assertEqual('Unable to obtain', args[0])
319
self.assertEqual('lock %s' % (lock_base,), args[1])
320
self.assertStartsWith(args[2], 'held by ')
321
self.assertStartsWith(args[3], 'locked ')
322
self.assertEndsWith(args[3], ' ago')
323
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
325
def test_35_wait_lock_changing(self):
326
"""LockDir.wait_lock() will report if the lock changes underneath.
328
This is the stages we want to happen:
330
0) Synchronization locks are created and locked.
331
1) Lock1 obtains the lockdir, and releases the 'check' lock.
332
2) Lock2 grabs the 'check' lock, and checks the lockdir.
333
It sees the lockdir is already acquired, reports the fact,
334
and unsets the 'checked' lock.
335
3) Thread1 blocks on acquiring the 'checked' lock, and then tells
336
Lock1 to release and acquire the lockdir. This resets the 'check'
338
4) Lock2 acquires the 'check' lock, and checks again. It notices
339
that the holder of the lock has changed, and so reports a new
341
5) Thread1 blocks on the 'checked' lock, this time, it completely
342
unlocks the lockdir, allowing Lock2 to acquire the lock.
345
wait_to_check_lock = Lock()
346
wait_until_checked_lock = Lock()
348
wait_to_check_lock.acquire()
349
wait_until_checked_lock.acquire()
350
note('locked check and checked locks')
352
class LockDir1(LockDir):
353
"""Use the synchronization points for the first lock."""
355
def attempt_lock(self):
356
# Once we have acquired the lock, it is okay for
357
# the other lock to check it
359
return super(LockDir1, self).attempt_lock()
361
note('lock1: releasing check lock')
362
wait_to_check_lock.release()
364
class LockDir2(LockDir):
365
"""Use the synchronization points for the second lock."""
367
def attempt_lock(self):
368
note('lock2: waiting for check lock')
369
wait_to_check_lock.acquire()
370
note('lock2: acquired check lock')
372
return super(LockDir2, self).attempt_lock()
374
note('lock2: releasing checked lock')
375
wait_until_checked_lock.release()
377
t = self.get_transport()
378
lf1 = LockDir1(t, 'test_lock')
381
lf2 = LockDir2(t, 'test_lock')
382
self.setup_log_reporter(lf2)
384
def wait_and_switch():
386
# Block until lock2 has had a chance to check
387
note('lock1: waiting 1 for checked lock')
388
wait_until_checked_lock.acquire()
389
note('lock1: acquired for checked lock')
390
note('lock1: released lockdir')
392
note('lock1: acquiring lockdir')
393
# Create a new nonce, so the lock looks different.
394
lf1.nonce = osutils.rand_chars(20)
396
note('lock1: acquired lockdir')
398
# Block until lock2 has peeked again
399
note('lock1: waiting 2 for checked lock')
400
wait_until_checked_lock.acquire()
401
note('lock1: acquired for checked lock')
402
# Now unlock, and let lock 2 grab the lock
404
wait_to_check_lock.release()
406
unlocker = Thread(target=wait_and_switch)
409
# Wait and play against the other thread
410
lf2.wait_lock(timeout=1.0, poll=0.01)
415
# There should be 2 reports, because the lock changed
416
lock_base = lf2.transport.abspath(lf2.path)
417
self.assertEqual(2, len(self._logged_reports))
419
self.assertEqual('%s %s\n'
421
'Will continue to try until %s\n',
422
self._logged_reports[0][0])
423
args = self._logged_reports[0][1]
424
self.assertEqual('Unable to obtain', args[0])
425
self.assertEqual('lock %s' % (lock_base,), args[1])
426
self.assertStartsWith(args[2], 'held by ')
427
self.assertStartsWith(args[3], 'locked ')
428
self.assertEndsWith(args[3], ' ago')
429
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
431
self.assertEqual('%s %s\n'
433
'Will continue to try until %s\n',
434
self._logged_reports[1][0])
435
args = self._logged_reports[1][1]
436
self.assertEqual('Lock owner changed for', args[0])
437
self.assertEqual('lock %s' % (lock_base,), args[1])
438
self.assertStartsWith(args[2], 'held by ')
439
self.assertStartsWith(args[3], 'locked ')
440
self.assertEndsWith(args[3], ' ago')
441
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
239
443
def test_40_confirm_easy(self):
240
444
"""Confirm a lock that's already held"""
241
445
t = self.get_transport()
352
556
self.assertRaises(LockBroken, ld1.unlock)
354
558
bzrlib.ui.ui_factory = orig_factory
560
def test_create_missing_base_directory(self):
561
"""If LockDir.path doesn't exist, it can be created
563
Some people manually remove the entire lock/ directory trying
564
to unlock a stuck repository/branch/etc. Rather than failing
565
after that, just create the lock directory when needed.
567
t = self.get_transport()
568
lf1 = LockDir(t, 'test_lock')
571
self.failUnless(t.has('test_lock'))
574
self.failIf(t.has('test_lock'))
576
# This will create 'test_lock' if it needs to
578
self.failUnless(t.has('test_lock'))
579
self.failUnless(t.has('test_lock/held/info'))
582
self.failIf(t.has('test_lock/held/info'))
584
def test__format_lock_info(self):
585
ld1 = self.get_lock()
589
info_list = ld1._format_lock_info(ld1.peek())
592
self.assertEqual('lock %s' % (ld1.transport.abspath(ld1.path),),
594
self.assertContainsRe(info_list[1],
595
r'^held by .* on host .* \[process #\d*\]$')
596
self.assertContainsRe(info_list[2], r'locked \d+ seconds? ago$')
598
def test_lock_without_email(self):
599
global_config = config.GlobalConfig()
600
# Intentionally has no email address
601
global_config.set_user_option('email', 'User Identity')
602
ld1 = self.get_lock()
607
def test_lock_permission(self):
608
if not osutils.supports_posix_readonly():
609
raise tests.TestSkipped('Cannot induce a permission failure')
610
ld1 = self.get_lock()
611
lock_path = ld1.transport.local_abspath('test_lock')
613
osutils.make_readonly(lock_path)
614
self.assertRaises(errors.PermissionDenied, ld1.attempt_lock)