~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 14:01:20 UTC
  • mfrom: (3280.2.5 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080316140120-i3yq8yr1l66m11h7
Start 1.4 development

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for LockDir"""
18
18
 
25
25
from bzrlib import (
26
26
    config,
27
27
    errors,
28
 
    lock,
29
28
    osutils,
30
29
    tests,
31
30
    transport,
126
125
        lf1.attempt_lock()
127
126
        lf2 = LockDir(t, 'test_lock')
128
127
        try:
129
 
            # locking is between LockDir instances; aliases within
 
128
            # locking is between LockDir instances; aliases within 
130
129
            # a single process are not detected
131
130
            lf2.attempt_lock()
132
131
            self.fail('Failed to detect lock collision')
142
141
        lf1 = LockDir(t, 'test_lock')
143
142
        lf1.create()
144
143
        lf1.attempt_lock()
145
 
        self.addCleanup(lf1.unlock)
146
144
        # lock is held, should get some info on it
147
145
        info1 = lf1.peek()
148
146
        self.assertEqual(set(info1.keys()),
162
160
        lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
163
161
        self.assertEqual(lf2.peek(), None)
164
162
        lf1.attempt_lock()
165
 
        self.addCleanup(lf1.unlock)
166
163
        info2 = lf2.peek()
167
164
        self.assertTrue(info2)
168
165
        self.assertEqual(info2['nonce'], lf1.nonce)
169
166
 
170
167
    def test_30_lock_wait_fail(self):
171
168
        """Wait on a lock, then fail
172
 
 
 
169
        
173
170
        We ask to wait up to 400ms; this should fail within at most one
174
171
        second.  (Longer times are more realistic but we don't want the test
175
172
        suite to take too long, and this should do for now.)
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))
192
189
        finally:
193
190
            lf1.unlock()
 
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')
205
 
        # skip hostname
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'
 
194
                         '%s\n%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')
209
204
 
210
205
    def test_31_lock_wait_easy(self):
211
206
        """Succeed when waiting on a lock with no contention.
226
221
    def test_32_lock_wait_succeed(self):
227
222
        """Succeed when trying to acquire a lock that gets released
228
223
 
229
 
        One thread holds on a lock and then releases it; another
 
224
        One thread holds on a lock and then releases it; another 
230
225
        tries to lock it.
231
226
        """
232
227
        # This test sometimes fails like this:
277
272
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
278
273
 
279
274
    def test_34_lock_write_waits(self):
280
 
        """LockDir.lock_write() will wait for the lock."""
 
275
        """LockDir.lock_write() will wait for the lock.""" 
281
276
        # the test suite sets the default to 0 to make deadlocks fail fast.
282
277
        # change it for this test, as we want to try a manual deadlock.
283
278
        raise tests.TestSkipped('Timing-sensitive test')
320
315
 
321
316
    def test_35_wait_lock_changing(self):
322
317
        """LockDir.wait_lock() will report if the lock changes underneath.
323
 
 
 
318
        
324
319
        This is the stages we want to happen:
325
320
 
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'
333
328
           lock.
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 
336
331
           lock holder.
337
332
        5) Thread1 blocks on the 'checked' lock, this time, it completely
338
333
           unlocks the lockdir, allowing Lock2 to acquire the lock.
339
334
        """
340
335
 
341
 
        raise tests.KnownFailure(
342
 
            "timing dependency in lock tests (#213182)")
343
 
 
344
336
        wait_to_check_lock = Lock()
345
337
        wait_until_checked_lock = Lock()
346
338
 
414
406
        # There should be 2 reports, because the lock changed
415
407
        lock_base = lf2.transport.abspath(lf2.path)
416
408
        self.assertEqual(2, len(self._logged_reports))
417
 
        lock_url = lf2.transport.abspath(lf2.path)
 
409
 
418
410
        self.assertEqual('%s %s\n'
419
411
                         '%s\n%s\n'
420
 
                         'Will continue to try until %s, unless '
421
 
                         'you press Ctrl-C.\n'
422
 
                         'See "bzr help break-lock" for more.',
 
412
                         'Will continue to try until %s\n',
423
413
                         self._logged_reports[0][0])
424
414
        args = self._logged_reports[0][1]
425
415
        self.assertEqual('Unable to obtain', args[0])
431
421
 
432
422
        self.assertEqual('%s %s\n'
433
423
                         '%s\n%s\n'
434
 
                         'Will continue to try until %s, unless '
435
 
                         'you press Ctrl-C.\n'
436
 
                         'See "bzr help break-lock" for more.',
 
424
                         'Will continue to try until %s\n',
437
425
                         self._logged_reports[1][0])
438
426
        args = self._logged_reports[1][1]
439
427
        self.assertEqual('Lock owner changed for', args[0])
449
437
        lf1 = LockDir(t, 'test_lock')
450
438
        lf1.create()
451
439
        lf1.attempt_lock()
452
 
        self.addCleanup(lf1.unlock)
453
440
        lf1.confirm()
454
441
 
455
442
    def test_41_confirm_not_held(self):
467
454
        lf1.attempt_lock()
468
455
        t.move('test_lock', 'lock_gone_now')
469
456
        self.assertRaises(LockBroken, lf1.confirm)
470
 
        # Clean up
471
 
        t.move('lock_gone_now', 'test_lock')
472
 
        lf1.unlock()
473
457
 
474
458
    def test_43_break(self):
475
459
        """Break a lock whose caller has forgotten it"""
486
470
        lf2.force_break(holder_info)
487
471
        # now we should be able to take it
488
472
        lf2.attempt_lock()
489
 
        self.addCleanup(lf2.unlock)
490
473
        lf2.confirm()
491
474
 
492
475
    def test_44_break_already_released(self):
504
487
        lf2.force_break(holder_info)
505
488
        # now we should be able to take it
506
489
        lf2.attempt_lock()
507
 
        self.addCleanup(lf2.unlock)
508
490
        lf2.confirm()
509
491
 
510
492
    def test_45_break_mismatch(self):
536
518
        """Check the on-disk representation of LockDirs is as expected.
537
519
 
538
520
        There should always be a top-level directory named by the lock.
539
 
        When the lock is held, there should be a lockname/held directory
 
521
        When the lock is held, there should be a lockname/held directory 
540
522
        containing an info file.
541
523
        """
542
524
        t = self.get_transport()
557
539
        # do this without IO redirection to ensure it doesn't prompt.
558
540
        self.assertRaises(AssertionError, ld1.break_lock)
559
541
        orig_factory = bzrlib.ui.ui_factory
560
 
        bzrlib.ui.ui_factory = bzrlib.ui.CannedInputUIFactory([True])
 
542
        # silent ui - no need for stdout
 
543
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
544
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
561
545
        try:
562
546
            ld2.break_lock()
563
547
            self.assertRaises(LockBroken, ld1.unlock)
596
580
            info_list = ld1._format_lock_info(ld1.peek())
597
581
        finally:
598
582
            ld1.unlock()
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),),
 
584
                         info_list[0])
 
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$')
603
588
 
604
589
    def test_lock_without_email(self):
605
590
        global_config = config.GlobalConfig()
622
607
    def test_lock_by_token(self):
623
608
        ld1 = self.get_lock()
624
609
        token = ld1.lock_write()
625
 
        self.addCleanup(ld1.unlock)
626
610
        self.assertNotEqual(None, token)
627
611
        ld2 = self.get_lock()
628
612
        t2 = ld2.lock_write(token)
629
 
        self.addCleanup(ld2.unlock)
630
613
        self.assertEqual(token, t2)
631
614
 
632
615
    def test_lock_with_buggy_rename(self):
657
640
        check_dir([])
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)
664
646
        # no kibble
665
647
        check_dir(['held'])
666
 
 
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()
674
 
        t.mkdir('test_lock')
675
 
        t.mkdir('test_lock/held')
676
 
        t.put_bytes('test_lock/held/info', '')
677
 
        lf = LockDir(t, 'test_lock')
678
 
        info = lf.peek()
679
 
        formatted_info = lf._format_lock_info(info)
680
 
        self.assertEquals(
681
 
            ['<unknown>', '<unknown>', '<unknown>', '(unknown)'],
682
 
            formatted_info)
683
 
 
684
 
 
685
 
class TestLockDirHooks(TestCaseWithTransport):
686
 
 
687
 
    def setUp(self):
688
 
        super(TestLockDirHooks, self).setUp()
689
 
        self._calls = []
690
 
 
691
 
    def get_lock(self):
692
 
        return LockDir(self.get_transport(), 'test_lock')
693
 
 
694
 
    def record_hook(self, result):
695
 
        self._calls.append(result)
696
 
 
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')
701
 
        ld = self.get_lock()
702
 
        ld.create()
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)
707
 
        ld.unlock()
708
 
        self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
709
 
 
710
 
    def test_LockDir_acquired_fail(self):
711
 
        # the LockDir.lock_acquired hook does not fire on failure.
712
 
        ld = self.get_lock()
713
 
        ld.create()
714
 
        ld2 = self.get_lock()
715
 
        ld2.attempt_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)
721
 
        ld2.unlock()
722
 
        self.assertEqual([], self._calls)
723
 
 
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')
728
 
        ld = self.get_lock()
729
 
        ld.create()
730
 
        self.assertEqual([], self._calls)
731
 
        result = ld.attempt_lock()
732
 
        self.assertEqual([], self._calls)
733
 
        ld.unlock()
734
 
        lock_path = ld.transport.abspath(ld.path)
735
 
        self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
736
 
 
737
 
    def test_LockDir_released_fail(self):
738
 
        # the LockDir.lock_released hook does not fire on failure.
739
 
        ld = self.get_lock()
740
 
        ld.create()
741
 
        ld2 = self.get_lock()
742
 
        ld.attempt_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)
748
 
 
749
 
    def test_LockDir_broken_success(self):
750
 
        # the LockDir.lock_broken hook fires when a lock is broken.
751
 
        ld = self.get_lock()
752
 
        ld.create()
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)
760
 
 
761
 
    def test_LockDir_broken_failure(self):
762
 
        # the LockDir.lock_broken hook does not fires when a lock is already
763
 
        # released.
764
 
        ld = self.get_lock()
765
 
        ld.create()
766
 
        ld2 = self.get_lock()
767
 
        result = ld.attempt_lock()
768
 
        holder_info = ld2.peek()
769
 
        ld.unlock()
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)