277
254
self.assertEndsWith(args[3], ' ago')
278
255
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
257
def test_33_wait(self):
258
"""Succeed when waiting on a lock that gets released
260
The difference from test_32_lock_wait_succeed is that the second
261
caller does not actually acquire the lock, but just waits for it
262
to be released. This is done over a readonly transport.
264
t = self.get_transport()
265
lf1 = LockDir(t, 'test_lock')
269
def wait_and_unlock():
272
unlocker = Thread(target=wait_and_unlock)
275
lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
277
# wait but don't lock
278
lf2.wait(timeout=0.4, poll=0.1)
280
self.assertTrue(after - before <= 1.0)
280
284
def test_34_lock_write_waits(self):
281
"""LockDir.lock_write() will wait for the lock."""
282
# the test suite sets the default to 0 to make deadlocks fail fast.
283
# change it for this test, as we want to try a manual deadlock.
284
raise tests.TestSkipped('Timing-sensitive test')
285
bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 300
285
"""LockDir.lock_write() will wait for the lock."""
286
286
t = self.get_transport()
287
287
lf1 = LockDir(t, 'test_lock')
322
322
def test_35_wait_lock_changing(self):
323
323
"""LockDir.wait_lock() will report if the lock changes underneath.
325
325
This is the stages we want to happen:
327
327
0) Synchronization locks are created and locked.
328
328
1) Lock1 obtains the lockdir, and releases the 'check' lock.
329
329
2) Lock2 grabs the 'check' lock, and checks the lockdir.
330
It sees the lockdir is already acquired, reports the fact,
330
It sees the lockdir is already acquired, reports the fact,
331
331
and unsets the 'checked' lock.
332
332
3) Thread1 blocks on acquiring the 'checked' lock, and then tells
333
333
Lock1 to release and acquire the lockdir. This resets the 'check'
335
335
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
336
that the holder of the lock has changed, and so reports a new
338
338
5) Thread1 blocks on the 'checked' lock, this time, it completely
339
339
unlocks the lockdir, allowing Lock2 to acquire the lock.
342
raise tests.KnownFailure(
343
"timing dependency in lock tests (#213182)")
345
342
wait_to_check_lock = Lock()
346
343
wait_until_checked_lock = Lock()
615
def test_lock_permission(self):
616
if not osutils.supports_posix_readonly():
617
raise tests.TestSkipped('Cannot induce a permission failure')
618
ld1 = self.get_lock()
619
lock_path = ld1.transport.local_abspath('test_lock')
621
osutils.make_readonly(lock_path)
622
self.assertRaises(errors.LockFailed, ld1.attempt_lock)
624
def test_lock_by_token(self):
625
ld1 = self.get_lock()
626
token = ld1.lock_write()
627
self.addCleanup(ld1.unlock)
628
self.assertNotEqual(None, token)
629
ld2 = self.get_lock()
630
t2 = ld2.lock_write(token)
631
self.addCleanup(ld2.unlock)
632
self.assertEqual(token, t2)
634
def test_lock_with_buggy_rename(self):
635
# test that lock acquisition handles servers which pretend they
636
# renamed correctly but that actually fail
637
t = transport.get_transport('brokenrename+' + self.get_url())
638
ld1 = LockDir(t, 'test_lock')
641
ld2 = LockDir(t, 'test_lock')
642
# we should fail to lock
643
e = self.assertRaises(errors.LockContention, ld2.attempt_lock)
644
# now the original caller should succeed in unlocking
646
# and there should be nothing left over
647
self.assertEquals([], t.list_dir('test_lock'))
649
def test_failed_lock_leaves_no_trash(self):
650
# if we fail to acquire the lock, we don't leave pending directories
651
# behind -- https://bugs.launchpad.net/bzr/+bug/109169
652
ld1 = self.get_lock()
653
ld2 = self.get_lock()
654
# should be nothing before we start
656
t = self.get_transport().clone('test_lock')
658
self.assertEquals(a, t.list_dir('.'))
660
# when held, that's all we see
662
self.addCleanup(ld1.unlock)
664
# second guy should fail
665
self.assertRaises(errors.LockContention, ld2.attempt_lock)
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)