187
184
after = time.time()
188
185
# it should only take about 0.4 seconds, but we allow more time in
189
186
# case the machine is heavily loaded
190
self.assertTrue(after - before <= 8.0,
187
self.assertTrue(after - before <= 8.0,
191
188
"took %f seconds to detect lock contention" % (after - before))
191
lock_base = lf2.transport.abspath(lf2.path)
194
192
self.assertEqual(1, len(self._logged_reports))
195
self.assertEqual(self._logged_reports[0][0],
196
'%s lock %s held by %s\n'
197
'at %s [process #%s], acquired %s.\n'
198
'Will continue to try until %s, unless '
199
'you press Ctrl-C.\n'
200
'See "bzr help break-lock" for more.')
201
start, lock_url, user, hostname, pid, time_ago, deadline_str = \
202
self._logged_reports[0][1]
203
self.assertEqual(start, u'Unable to obtain')
204
self.assertEqual(user, u'jrandom@example.com')
206
self.assertContainsRe(pid, r'\d+')
207
self.assertContainsRe(time_ago, r'.* ago')
208
self.assertContainsRe(deadline_str, r'\d{2}:\d{2}:\d{2}')
193
self.assertEqual('%s %s\n'
195
'Will continue to try until %s\n',
196
self._logged_reports[0][0])
197
args = self._logged_reports[0][1]
198
self.assertEqual('Unable to obtain', args[0])
199
self.assertEqual('lock %s' % (lock_base,), args[1])
200
self.assertStartsWith(args[2], 'held by ')
201
self.assertStartsWith(args[3], 'locked ')
202
self.assertEndsWith(args[3], ' ago')
203
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
210
205
def test_31_lock_wait_easy(self):
211
206
"""Succeed when waiting on a lock with no contention.
321
316
def test_35_wait_lock_changing(self):
322
317
"""LockDir.wait_lock() will report if the lock changes underneath.
324
319
This is the stages we want to happen:
326
321
0) Synchronization locks are created and locked.
327
322
1) Lock1 obtains the lockdir, and releases the 'check' lock.
328
323
2) Lock2 grabs the 'check' lock, and checks the lockdir.
329
It sees the lockdir is already acquired, reports the fact,
324
It sees the lockdir is already acquired, reports the fact,
330
325
and unsets the 'checked' lock.
331
326
3) Thread1 blocks on acquiring the 'checked' lock, and then tells
332
327
Lock1 to release and acquire the lockdir. This resets the 'check'
334
329
4) Lock2 acquires the 'check' lock, and checks again. It notices
335
that the holder of the lock has changed, and so reports a new
330
that the holder of the lock has changed, and so reports a new
337
332
5) Thread1 blocks on the 'checked' lock, this time, it completely
338
333
unlocks the lockdir, allowing Lock2 to acquire the lock.
341
raise tests.KnownFailure(
342
"timing dependency in lock tests (#213182)")
344
336
wait_to_check_lock = Lock()
345
337
wait_until_checked_lock = Lock()
596
580
info_list = ld1._format_lock_info(ld1.peek())
599
self.assertEqual(info_list[0], u'jrandom@example.com')
600
# info_list[1] is hostname. we skip this.
601
self.assertContainsRe(info_list[2], '^\d+$') # pid
602
self.assertContainsRe(info_list[3], r'^\d+ seconds? ago$') # time_ago
583
self.assertEqual('lock %s' % (ld1.transport.abspath(ld1.path),),
585
self.assertContainsRe(info_list[1],
586
r'^held by .* on host .* \[process #\d*\]$')
587
self.assertContainsRe(info_list[2], r'locked \d+ seconds? ago$')
604
589
def test_lock_without_email(self):
605
590
global_config = config.GlobalConfig()
658
641
# when held, that's all we see
659
642
ld1.attempt_lock()
660
self.addCleanup(ld1.unlock)
661
643
check_dir(['held'])
662
644
# second guy should fail
663
645
self.assertRaises(errors.LockContention, ld2.attempt_lock)
665
647
check_dir(['held'])
667
def test_no_lockdir_info(self):
668
"""We can cope with empty info files."""
669
# This seems like a fairly common failure case - see
670
# <https://bugs.launchpad.net/bzr/+bug/185103> and all its dupes.
671
# Processes are often interrupted after opening the file
672
# before the actual contents are committed.
673
t = self.get_transport()
675
t.mkdir('test_lock/held')
676
t.put_bytes('test_lock/held/info', '')
677
lf = LockDir(t, 'test_lock')
679
formatted_info = lf._format_lock_info(info)
681
['<unknown>', '<unknown>', '<unknown>', '(unknown)'],
685
class TestLockDirHooks(TestCaseWithTransport):
688
super(TestLockDirHooks, self).setUp()
692
return LockDir(self.get_transport(), 'test_lock')
694
def record_hook(self, result):
695
self._calls.append(result)
697
def test_LockDir_acquired_success(self):
698
# the LockDir.lock_acquired hook fires when a lock is acquired.
699
LockDir.hooks.install_named_hook('lock_acquired',
700
self.record_hook, 'record_hook')
703
self.assertEqual([], self._calls)
704
result = ld.attempt_lock()
705
lock_path = ld.transport.abspath(ld.path)
706
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
708
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
710
def test_LockDir_acquired_fail(self):
711
# the LockDir.lock_acquired hook does not fire on failure.
714
ld2 = self.get_lock()
716
# install a lock hook now, when the disk lock is locked
717
LockDir.hooks.install_named_hook('lock_acquired',
718
self.record_hook, 'record_hook')
719
self.assertRaises(errors.LockContention, ld.attempt_lock)
720
self.assertEqual([], self._calls)
722
self.assertEqual([], self._calls)
724
def test_LockDir_released_success(self):
725
# the LockDir.lock_released hook fires when a lock is acquired.
726
LockDir.hooks.install_named_hook('lock_released',
727
self.record_hook, 'record_hook')
730
self.assertEqual([], self._calls)
731
result = ld.attempt_lock()
732
self.assertEqual([], self._calls)
734
lock_path = ld.transport.abspath(ld.path)
735
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
737
def test_LockDir_released_fail(self):
738
# the LockDir.lock_released hook does not fire on failure.
741
ld2 = self.get_lock()
743
ld2.force_break(ld2.peek())
744
LockDir.hooks.install_named_hook('lock_released',
745
self.record_hook, 'record_hook')
746
self.assertRaises(LockBroken, ld.unlock)
747
self.assertEqual([], self._calls)
749
def test_LockDir_broken_success(self):
750
# the LockDir.lock_broken hook fires when a lock is broken.
753
ld2 = self.get_lock()
754
result = ld.attempt_lock()
755
LockDir.hooks.install_named_hook('lock_broken',
756
self.record_hook, 'record_hook')
757
ld2.force_break(ld2.peek())
758
lock_path = ld.transport.abspath(ld.path)
759
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
761
def test_LockDir_broken_failure(self):
762
# the LockDir.lock_broken hook does not fires when a lock is already
766
ld2 = self.get_lock()
767
result = ld.attempt_lock()
768
holder_info = ld2.peek()
770
LockDir.hooks.install_named_hook('lock_broken',
771
self.record_hook, 'record_hook')
772
ld2.force_break(holder_info)
773
lock_path = ld.transport.abspath(ld.path)
774
self.assertEqual([], self._calls)