105
109
"""Fail to create lock on readonly transport"""
106
110
t = self.get_readonly_transport()
107
111
lf = LockDir(t, 'test_lock')
108
self.assertRaises(UnlockableTransport, lf.create)
112
self.assertRaises(LockFailed, lf.create)
110
114
def test_12_lock_readonly_transport(self):
111
115
"""Fail to lock on readonly transport"""
112
116
lf = LockDir(self.get_transport(), 'test_lock')
114
118
lf = LockDir(self.get_readonly_transport(), 'test_lock')
115
self.assertRaises(UnlockableTransport, lf.attempt_lock)
119
self.assertRaises(LockFailed, lf.attempt_lock)
117
121
def test_20_lock_contested(self):
118
122
"""Contention to get a lock"""
181
187
after = time.time()
182
188
# it should only take about 0.4 seconds, but we allow more time in
183
189
# case the machine is heavily loaded
184
self.assertTrue(after - before <= 8.0,
190
self.assertTrue(after - before <= 8.0,
185
191
"took %f seconds to detect lock contention" % (after - before))
188
lock_base = lf2.transport.abspath(lf2.path)
189
194
self.assertEqual(1, len(self._logged_reports))
190
self.assertEqual('%s %s\n'
192
'Will continue to try until %s\n',
193
self._logged_reports[0][0])
194
args = self._logged_reports[0][1]
195
self.assertEqual('Unable to obtain', args[0])
196
self.assertEqual('lock %s' % (lock_base,), args[1])
197
self.assertStartsWith(args[2], 'held by ')
198
self.assertStartsWith(args[3], 'locked ')
199
self.assertEndsWith(args[3], ' ago')
200
self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
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}')
202
210
def test_31_lock_wait_easy(self):
203
211
"""Succeed when waiting on a lock with no contention.
312
321
def test_35_wait_lock_changing(self):
313
322
"""LockDir.wait_lock() will report if the lock changes underneath.
315
324
This is the stages we want to happen:
317
326
0) Synchronization locks are created and locked.
318
327
1) Lock1 obtains the lockdir, and releases the 'check' lock.
319
328
2) Lock2 grabs the 'check' lock, and checks the lockdir.
320
It sees the lockdir is already acquired, reports the fact,
329
It sees the lockdir is already acquired, reports the fact,
321
330
and unsets the 'checked' lock.
322
331
3) Thread1 blocks on acquiring the 'checked' lock, and then tells
323
332
Lock1 to release and acquire the lockdir. This resets the 'check'
325
334
4) Lock2 acquires the 'check' lock, and checks again. It notices
326
that the holder of the lock has changed, and so reports a new
335
that the holder of the lock has changed, and so reports a new
328
337
5) Thread1 blocks on the 'checked' lock, this time, it completely
329
338
unlocks the lockdir, allowing Lock2 to acquire the lock.
341
raise tests.KnownFailure(
342
"timing dependency in lock tests (#213182)")
332
344
wait_to_check_lock = Lock()
333
345
wait_until_checked_lock = Lock()
576
596
info_list = ld1._format_lock_info(ld1.peek())
579
self.assertEqual('lock %s' % (ld1.transport.abspath(ld1.path),),
581
self.assertContainsRe(info_list[1],
582
r'^held by .* on host .* \[process #\d*\]$')
583
self.assertContainsRe(info_list[2], r'locked \d+ seconds? ago$')
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
585
604
def test_lock_without_email(self):
586
605
global_config = config.GlobalConfig()
637
658
# when held, that's all we see
638
659
ld1.attempt_lock()
660
self.addCleanup(ld1.unlock)
639
661
check_dir(['held'])
640
662
# second guy should fail
641
663
self.assertRaises(errors.LockContention, ld2.attempt_lock)
643
665
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)