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))
194
191
lock_base = lf2.transport.abspath(lf2.path)
195
192
self.assertEqual(1, len(self._logged_reports))
196
lock_url = lf2.transport.abspath(lf2.path)
197
193
self.assertEqual('%s %s\n'
199
'Will continue to try until %s, unless '
200
'you press Ctrl-C.\n'
201
'See "bzr help break-lock" for more.',
195
'Will continue to try until %s\n',
202
196
self._logged_reports[0][0])
203
197
args = self._logged_reports[0][1]
204
198
self.assertEqual('Unable to obtain', args[0])
322
316
def test_35_wait_lock_changing(self):
323
317
"""LockDir.wait_lock() will report if the lock changes underneath.
325
319
This is the stages we want to happen:
327
321
0) Synchronization locks are created and locked.
328
322
1) Lock1 obtains the lockdir, and releases the 'check' lock.
329
323
2) Lock2 grabs the 'check' lock, and checks the lockdir.
330
It sees the lockdir is already acquired, reports the fact,
324
It sees the lockdir is already acquired, reports the fact,
331
325
and unsets the 'checked' lock.
332
326
3) Thread1 blocks on acquiring the 'checked' lock, and then tells
333
327
Lock1 to release and acquire the lockdir. This resets the 'check'
335
329
4) Lock2 acquires the 'check' lock, and checks again. It notices
336
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
338
332
5) Thread1 blocks on the 'checked' lock, this time, it completely
339
333
unlocks the lockdir, allowing Lock2 to acquire the lock.
342
raise tests.KnownFailure(
343
"timing dependency in lock tests (#213182)")
345
336
wait_to_check_lock = Lock()
346
337
wait_until_checked_lock = Lock()
660
641
# when held, that's all we see
661
642
ld1.attempt_lock()
662
self.addCleanup(ld1.unlock)
663
643
check_dir(['held'])
664
644
# second guy should fail
665
645
self.assertRaises(errors.LockContention, ld2.attempt_lock)
667
647
check_dir(['held'])
669
def test_no_lockdir_info(self):
670
"""We can cope with empty info files."""
671
# This seems like a fairly common failure case - see
672
# <https://bugs.edge.launchpad.net/bzr/+bug/185103> and all its dupes.
673
# Processes are often interrupted after opening the file
674
# before the actual contents are committed.
675
t = self.get_transport()
677
t.mkdir('test_lock/held')
678
t.put_bytes('test_lock/held/info', '')
679
lf = LockDir(t, 'test_lock')
681
formatted_info = lf._format_lock_info(info)
683
['lock %s' % t.abspath('test_lock'),
684
'held by <unknown> on host <unknown> [process #<unknown>]',
689
class TestLockDirHooks(TestCaseWithTransport):
692
super(TestLockDirHooks, self).setUp()
696
return LockDir(self.get_transport(), 'test_lock')
698
def record_hook(self, result):
699
self._calls.append(result)
701
def test_LockDir_acquired_success(self):
702
# the LockDir.lock_acquired hook fires when a lock is acquired.
703
LockDir.hooks.install_named_hook('lock_acquired',
704
self.record_hook, 'record_hook')
707
self.assertEqual([], self._calls)
708
result = ld.attempt_lock()
709
lock_path = ld.transport.abspath(ld.path)
710
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
712
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
714
def test_LockDir_acquired_fail(self):
715
# the LockDir.lock_acquired hook does not fire on failure.
718
ld2 = self.get_lock()
720
# install a lock hook now, when the disk lock is locked
721
LockDir.hooks.install_named_hook('lock_acquired',
722
self.record_hook, 'record_hook')
723
self.assertRaises(errors.LockContention, ld.attempt_lock)
724
self.assertEqual([], self._calls)
726
self.assertEqual([], self._calls)
728
def test_LockDir_released_success(self):
729
# the LockDir.lock_released hook fires when a lock is acquired.
730
LockDir.hooks.install_named_hook('lock_released',
731
self.record_hook, 'record_hook')
734
self.assertEqual([], self._calls)
735
result = ld.attempt_lock()
736
self.assertEqual([], self._calls)
738
lock_path = ld.transport.abspath(ld.path)
739
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
741
def test_LockDir_released_fail(self):
742
# the LockDir.lock_released hook does not fire on failure.
745
ld2 = self.get_lock()
747
ld2.force_break(ld2.peek())
748
LockDir.hooks.install_named_hook('lock_released',
749
self.record_hook, 'record_hook')
750
self.assertRaises(LockBroken, ld.unlock)
751
self.assertEqual([], self._calls)
753
def test_LockDir_broken_success(self):
754
# the LockDir.lock_broken hook fires when a lock is broken.
757
ld2 = self.get_lock()
758
result = ld.attempt_lock()
759
LockDir.hooks.install_named_hook('lock_broken',
760
self.record_hook, 'record_hook')
761
ld2.force_break(ld2.peek())
762
lock_path = ld.transport.abspath(ld.path)
763
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
765
def test_LockDir_broken_failure(self):
766
# the LockDir.lock_broken hook does not fires when a lock is already
770
ld2 = self.get_lock()
771
result = ld.attempt_lock()
772
holder_info = ld2.peek()
774
LockDir.hooks.install_named_hook('lock_broken',
775
self.record_hook, 'record_hook')
776
ld2.force_break(holder_info)
777
lock_path = ld.transport.abspath(ld.path)
778
self.assertEqual([], self._calls)