~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: 2009-04-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for LockDir"""
18
18
 
25
25
from bzrlib import (
26
26
    config,
27
27
    errors,
 
28
    lock,
28
29
    osutils,
29
30
    tests,
 
31
    transport,
30
32
    )
31
33
from bzrlib.errors import (
32
 
        LockBreakMismatch,
33
 
        LockContention, LockError, UnlockableTransport,
34
 
        LockNotHeld, LockBroken
35
 
        )
 
34
    LockBreakMismatch,
 
35
    LockBroken,
 
36
    LockContention,
 
37
    LockError,
 
38
    LockFailed,
 
39
    LockNotHeld,
 
40
    )
36
41
from bzrlib.lockdir import LockDir
37
42
from bzrlib.tests import TestCaseWithTransport
38
43
from bzrlib.trace import note
104
109
        """Fail to create lock on readonly transport"""
105
110
        t = self.get_readonly_transport()
106
111
        lf = LockDir(t, 'test_lock')
107
 
        self.assertRaises(UnlockableTransport, lf.create)
 
112
        self.assertRaises(LockFailed, lf.create)
108
113
 
109
114
    def test_12_lock_readonly_transport(self):
110
115
        """Fail to lock on readonly transport"""
111
116
        lf = LockDir(self.get_transport(), 'test_lock')
112
117
        lf.create()
113
118
        lf = LockDir(self.get_readonly_transport(), 'test_lock')
114
 
        self.assertRaises(UnlockableTransport, lf.attempt_lock)
 
119
        self.assertRaises(LockFailed, lf.attempt_lock)
115
120
 
116
121
    def test_20_lock_contested(self):
117
122
        """Contention to get a lock"""
121
126
        lf1.attempt_lock()
122
127
        lf2 = LockDir(t, 'test_lock')
123
128
        try:
124
 
            # locking is between LockDir instances; aliases within 
 
129
            # locking is between LockDir instances; aliases within
125
130
            # a single process are not detected
126
131
            lf2.attempt_lock()
127
132
            self.fail('Failed to detect lock collision')
162
167
 
163
168
    def test_30_lock_wait_fail(self):
164
169
        """Wait on a lock, then fail
165
 
        
 
170
 
166
171
        We ask to wait up to 400ms; this should fail within at most one
167
172
        second.  (Longer times are more realistic but we don't want the test
168
173
        suite to take too long, and this should do for now.)
180
185
            after = time.time()
181
186
            # it should only take about 0.4 seconds, but we allow more time in
182
187
            # case the machine is heavily loaded
183
 
            self.assertTrue(after - before <= 8.0, 
 
188
            self.assertTrue(after - before <= 8.0,
184
189
                    "took %f seconds to detect lock contention" % (after - before))
185
190
        finally:
186
191
            lf1.unlock()
187
192
        lock_base = lf2.transport.abspath(lf2.path)
188
193
        self.assertEqual(1, len(self._logged_reports))
 
194
        lock_url = lf2.transport.abspath(lf2.path)
189
195
        self.assertEqual('%s %s\n'
190
196
                         '%s\n%s\n'
191
 
                         'Will continue to try until %s\n',
 
197
                         'Will continue to try until %s, unless '
 
198
                         'you press Ctrl-C\n'
 
199
                         'If you\'re sure that it\'s not being '
 
200
                         'modified, use bzr break-lock %s',
192
201
                         self._logged_reports[0][0])
193
202
        args = self._logged_reports[0][1]
194
203
        self.assertEqual('Unable to obtain', args[0])
217
226
    def test_32_lock_wait_succeed(self):
218
227
        """Succeed when trying to acquire a lock that gets released
219
228
 
220
 
        One thread holds on a lock and then releases it; another 
 
229
        One thread holds on a lock and then releases it; another
221
230
        tries to lock it.
222
231
        """
 
232
        # This test sometimes fails like this:
 
233
        # Traceback (most recent call last):
 
234
 
 
235
        #   File "/home/pqm/bzr-pqm-workdir/home/+trunk/bzrlib/tests/
 
236
        # test_lockdir.py", line 247, in test_32_lock_wait_succeed
 
237
        #     self.assertEqual(1, len(self._logged_reports))
 
238
        # AssertionError: not equal:
 
239
        # a = 1
 
240
        # b = 0
 
241
        raise tests.TestSkipped("Test fails intermittently")
223
242
        t = self.get_transport()
224
243
        lf1 = LockDir(t, 'test_lock')
225
244
        lf1.create()
257
276
        self.assertEndsWith(args[3], ' ago')
258
277
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
259
278
 
260
 
    def test_33_wait(self):
261
 
        """Succeed when waiting on a lock that gets released
262
 
 
263
 
        The difference from test_32_lock_wait_succeed is that the second 
264
 
        caller does not actually acquire the lock, but just waits for it
265
 
        to be released.  This is done over a readonly transport.
266
 
        """
267
 
        t = self.get_transport()
268
 
        lf1 = LockDir(t, 'test_lock')
269
 
        lf1.create()
270
 
        lf1.attempt_lock()
271
 
 
272
 
        def wait_and_unlock():
273
 
            time.sleep(0.1)
274
 
            lf1.unlock()
275
 
        unlocker = Thread(target=wait_and_unlock)
276
 
        unlocker.start()
277
 
        try:
278
 
            lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
279
 
            before = time.time()
280
 
            # wait but don't lock
281
 
            lf2.wait(timeout=0.4, poll=0.1)
282
 
            after = time.time()
283
 
            self.assertTrue(after - before <= 1.0)
284
 
        finally:
285
 
            unlocker.join()
286
 
 
287
279
    def test_34_lock_write_waits(self):
288
 
        """LockDir.lock_write() will wait for the lock.""" 
 
280
        """LockDir.lock_write() will wait for the lock."""
289
281
        # the test suite sets the default to 0 to make deadlocks fail fast.
290
282
        # change it for this test, as we want to try a manual deadlock.
 
283
        raise tests.TestSkipped('Timing-sensitive test')
291
284
        bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 300
292
285
        t = self.get_transport()
293
286
        lf1 = LockDir(t, 'test_lock')
327
320
 
328
321
    def test_35_wait_lock_changing(self):
329
322
        """LockDir.wait_lock() will report if the lock changes underneath.
330
 
        
 
323
 
331
324
        This is the stages we want to happen:
332
325
 
333
326
        0) Synchronization locks are created and locked.
334
327
        1) Lock1 obtains the lockdir, and releases the 'check' lock.
335
328
        2) Lock2 grabs the 'check' lock, and checks the lockdir.
336
 
           It sees the lockdir is already acquired, reports the fact, 
 
329
           It sees the lockdir is already acquired, reports the fact,
337
330
           and unsets the 'checked' lock.
338
331
        3) Thread1 blocks on acquiring the 'checked' lock, and then tells
339
332
           Lock1 to release and acquire the lockdir. This resets the 'check'
340
333
           lock.
341
334
        4) Lock2 acquires the 'check' lock, and checks again. It notices
342
 
           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
343
336
           lock holder.
344
337
        5) Thread1 blocks on the 'checked' lock, this time, it completely
345
338
           unlocks the lockdir, allowing Lock2 to acquire the lock.
346
339
        """
347
340
 
 
341
        raise tests.KnownFailure(
 
342
            "timing dependency in lock tests (#213182)")
 
343
 
348
344
        wait_to_check_lock = Lock()
349
345
        wait_until_checked_lock = Lock()
350
346
 
410
406
        unlocker.start()
411
407
        try:
412
408
            # Wait and play against the other thread
413
 
            lf2.wait_lock(timeout=1.0, poll=0.01)
 
409
            lf2.wait_lock(timeout=20.0, poll=0.01)
414
410
        finally:
415
411
            unlocker.join()
416
412
        lf2.unlock()
418
414
        # There should be 2 reports, because the lock changed
419
415
        lock_base = lf2.transport.abspath(lf2.path)
420
416
        self.assertEqual(2, len(self._logged_reports))
421
 
 
 
417
        lock_url = lf2.transport.abspath(lf2.path)
422
418
        self.assertEqual('%s %s\n'
423
419
                         '%s\n%s\n'
424
 
                         'Will continue to try until %s\n',
 
420
                         'Will continue to try until %s, unless '
 
421
                         'you press Ctrl-C\n'
 
422
                         'If you\'re sure that it\'s not being '
 
423
                         'modified, use bzr break-lock %s',
425
424
                         self._logged_reports[0][0])
426
425
        args = self._logged_reports[0][1]
427
426
        self.assertEqual('Unable to obtain', args[0])
433
432
 
434
433
        self.assertEqual('%s %s\n'
435
434
                         '%s\n%s\n'
436
 
                         'Will continue to try until %s\n',
 
435
                         'Will continue to try until %s, unless '
 
436
                         'you press Ctrl-C\n'
 
437
                         'If you\'re sure that it\'s not being '
 
438
                         'modified, use bzr break-lock %s',
437
439
                         self._logged_reports[1][0])
438
440
        args = self._logged_reports[1][1]
439
441
        self.assertEqual('Lock owner changed for', args[0])
530
532
        """Check the on-disk representation of LockDirs is as expected.
531
533
 
532
534
        There should always be a top-level directory named by the lock.
533
 
        When the lock is held, there should be a lockname/held directory 
 
535
        When the lock is held, there should be a lockname/held directory
534
536
        containing an info file.
535
537
        """
536
538
        t = self.get_transport()
614
616
        lock_path = ld1.transport.local_abspath('test_lock')
615
617
        os.mkdir(lock_path)
616
618
        osutils.make_readonly(lock_path)
617
 
        self.assertRaises(errors.PermissionDenied, ld1.attempt_lock)
 
619
        self.assertRaises(errors.LockFailed, ld1.attempt_lock)
 
620
 
 
621
    def test_lock_by_token(self):
 
622
        ld1 = self.get_lock()
 
623
        token = ld1.lock_write()
 
624
        self.assertNotEqual(None, token)
 
625
        ld2 = self.get_lock()
 
626
        t2 = ld2.lock_write(token)
 
627
        self.assertEqual(token, t2)
 
628
 
 
629
    def test_lock_with_buggy_rename(self):
 
630
        # test that lock acquisition handles servers which pretend they
 
631
        # renamed correctly but that actually fail
 
632
        t = transport.get_transport('brokenrename+' + self.get_url())
 
633
        ld1 = LockDir(t, 'test_lock')
 
634
        ld1.create()
 
635
        ld1.attempt_lock()
 
636
        ld2 = LockDir(t, 'test_lock')
 
637
        # we should fail to lock
 
638
        e = self.assertRaises(errors.LockContention, ld2.attempt_lock)
 
639
        # now the original caller should succeed in unlocking
 
640
        ld1.unlock()
 
641
        # and there should be nothing left over
 
642
        self.assertEquals([], t.list_dir('test_lock'))
 
643
 
 
644
    def test_failed_lock_leaves_no_trash(self):
 
645
        # if we fail to acquire the lock, we don't leave pending directories
 
646
        # behind -- https://bugs.launchpad.net/bzr/+bug/109169
 
647
        ld1 = self.get_lock()
 
648
        ld2 = self.get_lock()
 
649
        # should be nothing before we start
 
650
        ld1.create()
 
651
        t = self.get_transport().clone('test_lock')
 
652
        def check_dir(a):
 
653
            self.assertEquals(a, t.list_dir('.'))
 
654
        check_dir([])
 
655
        # when held, that's all we see
 
656
        ld1.attempt_lock()
 
657
        check_dir(['held'])
 
658
        # second guy should fail
 
659
        self.assertRaises(errors.LockContention, ld2.attempt_lock)
 
660
        # no kibble
 
661
        check_dir(['held'])
 
662
 
 
663
    def record_hook(self, result):
 
664
        self._calls.append(result)
 
665
 
 
666
    def reset_hooks(self):
 
667
        self._old_hooks = lock.Lock.hooks
 
668
        self.addCleanup(self.restore_hooks)
 
669
        lock.Lock.hooks = lock.LockHooks()
 
670
 
 
671
    def restore_hooks(self):
 
672
        lock.Lock.hooks = self._old_hooks
 
673
 
 
674
    def test_LockDir_acquired_success(self):
 
675
        # the LockDir.lock_acquired hook fires when a lock is acquired.
 
676
        self._calls = []
 
677
        self.reset_hooks()
 
678
        LockDir.hooks.install_named_hook('lock_acquired',
 
679
            self.record_hook, 'record_hook')
 
680
        ld = self.get_lock()
 
681
        ld.create()
 
682
        self.assertEqual([], self._calls)
 
683
        result = ld.attempt_lock()
 
684
        lock_path = ld.transport.abspath(ld.path)
 
685
        self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
 
686
        ld.unlock()
 
687
        self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
 
688
 
 
689
    def test_LockDir_acquired_fail(self):
 
690
        # the LockDir.lock_acquired hook does not fire on failure.
 
691
        self._calls = []
 
692
        self.reset_hooks()
 
693
        ld = self.get_lock()
 
694
        ld.create()
 
695
        ld2 = self.get_lock()
 
696
        ld2.attempt_lock()
 
697
        # install a lock hook now, when the disk lock is locked
 
698
        LockDir.hooks.install_named_hook('lock_acquired',
 
699
            self.record_hook, 'record_hook')
 
700
        self.assertRaises(errors.LockContention, ld.attempt_lock)
 
701
        self.assertEqual([], self._calls)
 
702
        ld2.unlock()
 
703
        self.assertEqual([], self._calls)
 
704
 
 
705
    def test_LockDir_released_success(self):
 
706
        # the LockDir.lock_released hook fires when a lock is acquired.
 
707
        self._calls = []
 
708
        self.reset_hooks()
 
709
        LockDir.hooks.install_named_hook('lock_released',
 
710
            self.record_hook, 'record_hook')
 
711
        ld = self.get_lock()
 
712
        ld.create()
 
713
        self.assertEqual([], self._calls)
 
714
        result = ld.attempt_lock()
 
715
        self.assertEqual([], self._calls)
 
716
        ld.unlock()
 
717
        lock_path = ld.transport.abspath(ld.path)
 
718
        self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
 
719
 
 
720
    def test_LockDir_released_fail(self):
 
721
        # the LockDir.lock_released hook does not fire on failure.
 
722
        self._calls = []
 
723
        self.reset_hooks()
 
724
        ld = self.get_lock()
 
725
        ld.create()
 
726
        ld2 = self.get_lock()
 
727
        ld.attempt_lock()
 
728
        ld2.force_break(ld2.peek())
 
729
        LockDir.hooks.install_named_hook('lock_released',
 
730
            self.record_hook, 'record_hook')
 
731
        self.assertRaises(LockBroken, ld.unlock)
 
732
        self.assertEqual([], self._calls)