287
def test_34_lock_write_waits(self):
288
"""LockDir.lock_write() will wait for the lock."""
289
# the test suite sets the default to 0 to make deadlocks fail fast.
290
# change it for this test, as we want to try a manual deadlock.
291
bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 300
292
t = self.get_transport()
293
lf1 = LockDir(t, 'test_lock')
297
def wait_and_unlock():
300
unlocker = Thread(target=wait_and_unlock)
303
lf2 = LockDir(t, 'test_lock')
304
self.setup_log_reporter(lf2)
312
# There should be only 1 report, even though it should have to
314
lock_base = lf2.transport.abspath(lf2.path)
315
self.assertEqual(1, len(self._logged_reports))
316
self.assertEqual('%s %s\n'
318
'Will continue to try until %s\n',
319
self._logged_reports[0][0])
320
args = self._logged_reports[0][1]
321
self.assertEqual('Unable to obtain', args[0])
322
self.assertEqual('lock %s' % (lock_base,), args[1])
323
self.assertStartsWith(args[2], 'held by ')
324
self.assertStartsWith(args[3], 'locked ')
325
self.assertEndsWith(args[3], ' ago')
326
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
328
def test_35_wait_lock_changing(self):
329
"""LockDir.wait_lock() will report if the lock changes underneath.
331
This is the stages we want to happen:
333
0) Synchronization locks are created and locked.
334
1) Lock1 obtains the lockdir, and releases the 'check' lock.
335
2) Lock2 grabs the 'check' lock, and checks the lockdir.
336
It sees the lockdir is already acquired, reports the fact,
337
and unsets the 'checked' lock.
338
3) Thread1 blocks on acquiring the 'checked' lock, and then tells
339
Lock1 to release and acquire the lockdir. This resets the 'check'
341
4) Lock2 acquires the 'check' lock, and checks again. It notices
342
that the holder of the lock has changed, and so reports a new
344
5) Thread1 blocks on the 'checked' lock, this time, it completely
345
unlocks the lockdir, allowing Lock2 to acquire the lock.
348
wait_to_check_lock = Lock()
349
wait_until_checked_lock = Lock()
351
wait_to_check_lock.acquire()
352
wait_until_checked_lock.acquire()
353
note('locked check and checked locks')
355
class LockDir1(LockDir):
356
"""Use the synchronization points for the first lock."""
358
def attempt_lock(self):
359
# Once we have acquired the lock, it is okay for
360
# the other lock to check it
362
return super(LockDir1, self).attempt_lock()
364
note('lock1: releasing check lock')
365
wait_to_check_lock.release()
367
class LockDir2(LockDir):
368
"""Use the synchronization points for the second lock."""
370
def attempt_lock(self):
371
note('lock2: waiting for check lock')
372
wait_to_check_lock.acquire()
373
note('lock2: acquired check lock')
375
return super(LockDir2, self).attempt_lock()
377
note('lock2: releasing checked lock')
378
wait_until_checked_lock.release()
380
t = self.get_transport()
381
lf1 = LockDir1(t, 'test_lock')
384
lf2 = LockDir2(t, 'test_lock')
385
self.setup_log_reporter(lf2)
387
def wait_and_switch():
389
# Block until lock2 has had a chance to check
390
note('lock1: waiting 1 for checked lock')
391
wait_until_checked_lock.acquire()
392
note('lock1: acquired for checked lock')
393
note('lock1: released lockdir')
395
note('lock1: acquiring lockdir')
396
# Create a new nonce, so the lock looks different.
397
lf1.nonce = osutils.rand_chars(20)
399
note('lock1: acquired lockdir')
401
# Block until lock2 has peeked again
402
note('lock1: waiting 2 for checked lock')
403
wait_until_checked_lock.acquire()
404
note('lock1: acquired for checked lock')
405
# Now unlock, and let lock 2 grab the lock
407
wait_to_check_lock.release()
409
unlocker = Thread(target=wait_and_switch)
412
# Wait and play against the other thread
413
lf2.wait_lock(timeout=1.0, poll=0.01)
418
# There should be 2 reports, because the lock changed
419
lock_base = lf2.transport.abspath(lf2.path)
420
self.assertEqual(2, len(self._logged_reports))
422
self.assertEqual('%s %s\n'
424
'Will continue to try until %s\n',
425
self._logged_reports[0][0])
426
args = self._logged_reports[0][1]
427
self.assertEqual('Unable to obtain', args[0])
428
self.assertEqual('lock %s' % (lock_base,), args[1])
429
self.assertStartsWith(args[2], 'held by ')
430
self.assertStartsWith(args[3], 'locked ')
431
self.assertEndsWith(args[3], ' ago')
432
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
434
self.assertEqual('%s %s\n'
436
'Will continue to try until %s\n',
437
self._logged_reports[1][0])
438
args = self._logged_reports[1][1]
439
self.assertEqual('Lock owner changed for', args[0])
440
self.assertEqual('lock %s' % (lock_base,), args[1])
441
self.assertStartsWith(args[2], 'held by ')
442
self.assertStartsWith(args[3], 'locked ')
443
self.assertEndsWith(args[3], ' ago')
444
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
239
446
def test_40_confirm_easy(self):
240
447
"""Confirm a lock that's already held"""
241
448
t = self.get_transport()
352
559
self.assertRaises(LockBroken, ld1.unlock)
354
561
bzrlib.ui.ui_factory = orig_factory
563
def test_create_missing_base_directory(self):
564
"""If LockDir.path doesn't exist, it can be created
566
Some people manually remove the entire lock/ directory trying
567
to unlock a stuck repository/branch/etc. Rather than failing
568
after that, just create the lock directory when needed.
570
t = self.get_transport()
571
lf1 = LockDir(t, 'test_lock')
574
self.failUnless(t.has('test_lock'))
577
self.failIf(t.has('test_lock'))
579
# This will create 'test_lock' if it needs to
581
self.failUnless(t.has('test_lock'))
582
self.failUnless(t.has('test_lock/held/info'))
585
self.failIf(t.has('test_lock/held/info'))
587
def test__format_lock_info(self):
588
ld1 = self.get_lock()
592
info_list = ld1._format_lock_info(ld1.peek())
595
self.assertEqual('lock %s' % (ld1.transport.abspath(ld1.path),),
597
self.assertContainsRe(info_list[1],
598
r'^held by .* on host .* \[process #\d*\]$')
599
self.assertContainsRe(info_list[2], r'locked \d+ seconds? ago$')
601
def test_lock_without_email(self):
602
global_config = config.GlobalConfig()
603
# Intentionally has no email address
604
global_config.set_user_option('email', 'User Identity')
605
ld1 = self.get_lock()
610
def test_lock_permission(self):
611
if not osutils.supports_posix_readonly():
612
raise tests.TestSkipped('Cannot induce a permission failure')
613
ld1 = self.get_lock()
614
lock_path = ld1.transport.local_abspath('test_lock')
616
osutils.make_readonly(lock_path)
617
self.assertRaises(errors.PermissionDenied, ld1.attempt_lock)