109
105
"""Fail to create lock on readonly transport"""
110
106
t = self.get_readonly_transport()
111
107
lf = LockDir(t, 'test_lock')
112
self.assertRaises(LockFailed, lf.create)
108
self.assertRaises(UnlockableTransport, lf.create)
114
110
def test_12_lock_readonly_transport(self):
115
111
"""Fail to lock on readonly transport"""
116
112
lf = LockDir(self.get_transport(), 'test_lock')
118
114
lf = LockDir(self.get_readonly_transport(), 'test_lock')
119
self.assertRaises(LockFailed, lf.attempt_lock)
115
self.assertRaises(UnlockableTransport, lf.attempt_lock)
121
117
def test_20_lock_contested(self):
122
118
"""Contention to get a lock"""
187
181
after = time.time()
188
182
# it should only take about 0.4 seconds, but we allow more time in
189
183
# case the machine is heavily loaded
190
self.assertTrue(after - before <= 8.0,
184
self.assertTrue(after - before <= 8.0,
191
185
"took %f seconds to detect lock contention" % (after - before))
194
188
lock_base = lf2.transport.abspath(lf2.path)
195
189
self.assertEqual(1, len(self._logged_reports))
196
lock_url = lf2.transport.abspath(lf2.path)
197
190
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.',
192
'Will continue to try until %s\n',
202
193
self._logged_reports[0][0])
203
194
args = self._logged_reports[0][1]
204
195
self.assertEqual('Unable to obtain', args[0])
322
312
def test_35_wait_lock_changing(self):
323
313
"""LockDir.wait_lock() will report if the lock changes underneath.
325
315
This is the stages we want to happen:
327
317
0) Synchronization locks are created and locked.
328
318
1) Lock1 obtains the lockdir, and releases the 'check' lock.
329
319
2) Lock2 grabs the 'check' lock, and checks the lockdir.
330
It sees the lockdir is already acquired, reports the fact,
320
It sees the lockdir is already acquired, reports the fact,
331
321
and unsets the 'checked' lock.
332
322
3) Thread1 blocks on acquiring the 'checked' lock, and then tells
333
323
Lock1 to release and acquire the lockdir. This resets the 'check'
335
325
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
326
that the holder of the lock has changed, and so reports a new
338
328
5) Thread1 blocks on the 'checked' lock, this time, it completely
339
329
unlocks the lockdir, allowing Lock2 to acquire the lock.
342
raise tests.KnownFailure(
343
"timing dependency in lock tests (#213182)")
345
332
wait_to_check_lock = Lock()
346
333
wait_until_checked_lock = Lock()
660
637
# when held, that's all we see
661
638
ld1.attempt_lock()
662
self.addCleanup(ld1.unlock)
663
639
check_dir(['held'])
664
640
# second guy should fail
665
641
self.assertRaises(errors.LockContention, ld2.attempt_lock)
667
643
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)