Example #1
0
 def test_42_confirm_broken_manually(self):
     """Confirm a lock broken by hand"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     t.move('test_lock', 'lock_gone_now')
     self.assertRaises(LockBroken, lf1.confirm)
    def test_30_lock_wait_fail(self):
        """Wait on a lock, then fail

        We ask to wait up to 400ms; this should fail within at most one
        second.  (Longer times are more realistic but we don't want the test
        suite to take too long, and this should do for now.)
        """
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')
        lf1.create()
        lf2 = LockDir(t, 'test_lock')
        self.setup_log_reporter(lf2)
        lf1.attempt_lock()
        try:
            before = time.time()
            self.assertRaises(LockContention, lf2.wait_lock,
                              timeout=0.4, poll=0.1)
            after = time.time()
            # it should only take about 0.4 seconds, but we allow more time in
            # case the machine is heavily loaded
            self.assertTrue(after - before <= 8.0,
                "took %f seconds to detect lock contention" % (after - before))
        finally:
            lf1.unlock()
        self.assertEqual(1, len(self._logged_reports))
        self.assertContainsRe(self._logged_reports[0][0],
            r'Unable to obtain lock .* held by jrandom@example\.com on .*'
            r' \(process #\d+\), acquired .* ago\.\n'
            r'Will continue to try until \d{2}:\d{2}:\d{2}, unless '
            r'you press Ctrl-C.\n'
            r'See "bzr help break-lock" for more.')
Example #3
0
 def test_21_peek_readonly(self):
     """Peek over a readonly transport"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
     self.assertEqual(lf2.peek(), None)
     lf1.attempt_lock()
     info2 = lf2.peek()
     self.assertTrue(info2)
     self.assertEqual(info2['nonce'], lf1.nonce)
Example #4
0
 def test_auto_break_stale_lock_configured_off(self):
     """Automatic breaking can be turned off"""
     l1 = LockDir(self.get_transport(), 'a',
         extra_holder_info={'pid': '12312313'})
     token_1 = l1.attempt_lock()
     self.addCleanup(l1.unlock)
     l2 = LockDir(self.get_transport(), 'a')
     # This fails now, because dead lock breaking is off by default.
     self.assertRaises(LockContention,
         l2.attempt_lock)
     # and it's in fact not broken
     l1.confirm()
Example #5
0
    def test_32_lock_wait_succeed(self):
        """Succeed when trying to acquire a lock that gets released

        One thread holds on a lock and then releases it; another 
        tries to lock it.
        """
        # This test sometimes fails like this:
        # Traceback (most recent call last):

        #   File "/home/pqm/bzr-pqm-workdir/home/+trunk/bzrlib/tests/
        # test_lockdir.py", line 247, in test_32_lock_wait_succeed
        #     self.assertEqual(1, len(self._logged_reports))
        # AssertionError: not equal:
        # a = 1
        # b = 0
        raise tests.TestSkipped("Test fails intermittently")
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')
        lf1.create()
        lf1.attempt_lock()

        def wait_and_unlock():
            time.sleep(0.1)
            lf1.unlock()

        unlocker = Thread(target=wait_and_unlock)
        unlocker.start()
        try:
            lf2 = LockDir(t, 'test_lock')
            self.setup_log_reporter(lf2)
            before = time.time()
            # wait and then lock
            lf2.wait_lock(timeout=0.4, poll=0.1)
            after = time.time()
            self.assertTrue(after - before <= 1.0)
        finally:
            unlocker.join()

        # There should be only 1 report, even though it should have to
        # wait for a while
        lock_base = lf2.transport.abspath(lf2.path)
        self.assertEqual(1, len(self._logged_reports))
        self.assertEqual(
            '%s %s\n'
            '%s\n%s\n'
            'Will continue to try until %s\n', self._logged_reports[0][0])
        args = self._logged_reports[0][1]
        self.assertEqual('Unable to obtain', args[0])
        self.assertEqual('lock %s' % (lock_base, ), args[1])
        self.assertStartsWith(args[2], 'held by ')
        self.assertStartsWith(args[3], 'locked ')
        self.assertEndsWith(args[3], ' ago')
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
Example #6
0
 def test_40_confirm_easy(self):
     """Confirm a lock that's already held"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     lf1.confirm()
Example #7
0
 def test_20_lock_contested(self):
     """Contention to get a lock"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     lf2 = LockDir(t, 'test_lock')
     try:
         # locking is between LockDir instances; aliases within
         # a single process are not detected
         lf2.attempt_lock()
         self.fail('Failed to detect lock collision')
     except LockContention, e:
         self.assertEqual(e.lock, lf2)
         self.assertContainsRe(str(e), r'^Could not acquire.*test_lock.*$')
Example #8
0
 def test_no_lockdir_info(self):
     """We can cope with empty info files."""
     # This seems like a fairly common failure case - see
     # <https://bugs.launchpad.net/bzr/+bug/185103> and all its dupes.
     # Processes are often interrupted after opening the file
     # before the actual contents are committed.
     t = self.get_transport()
     t.mkdir('test_lock')
     t.mkdir('test_lock/held')
     t.put_bytes('test_lock/held/info', '')
     lf = LockDir(t, 'test_lock')
     info = lf.peek()
     formatted_info = info.to_readable_dict()
     self.assertEquals(
         dict(user='******', hostname='<unknown>', pid='<unknown>',
             time_ago='(unknown)'),
         formatted_info)
 def test_no_lockdir_info(self):
     """We can cope with empty info files."""
     # This seems like a fairly common failure case - see
     # <https://bugs.launchpad.net/bzr/+bug/185103> and all its dupes.
     # Processes are often interrupted after opening the file
     # before the actual contents are committed.
     t = self.get_transport()
     t.mkdir('test_lock')
     t.mkdir('test_lock/held')
     t.put_bytes('test_lock/held/info', '')
     lf = LockDir(t, 'test_lock')
     info = lf.peek()
     formatted_info = info.to_readable_dict()
     self.assertEqual(
         dict(user='******', hostname='<unknown>', pid='<unknown>',
             time_ago='(unknown)'),
         formatted_info)
Example #10
0
    def test_30_lock_wait_fail(self):
        """Wait on a lock, then fail

        We ask to wait up to 400ms; this should fail within at most one
        second.  (Longer times are more realistic but we don't want the test
        suite to take too long, and this should do for now.)
        """
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')
        lf1.create()
        lf2 = LockDir(t, 'test_lock')
        self.setup_log_reporter(lf2)
        lf1.attempt_lock()
        try:
            before = time.time()
            self.assertRaises(LockContention, lf2.wait_lock,
                              timeout=0.4, poll=0.1)
            after = time.time()
            # it should only take about 0.4 seconds, but we allow more time in
            # case the machine is heavily loaded
            self.assertTrue(after - before <= 8.0,
                "took %f seconds to detect lock contention" % (after - before))
        finally:
            lf1.unlock()
        self.assertEqual(1, len(self._logged_reports))
        self.assertContainsRe(self._logged_reports[0][0],
            r'Unable to obtain lock .* held by jrandom@example\.com on .*'
            r' \(process #\d+\), acquired .* ago\.\n'
            r'Will continue to try until \d{2}:\d{2}:\d{2}, unless '
            r'you press Ctrl-C.\n'
            r'See "bzr help break-lock" for more.')
Example #11
0
 def test_lock_with_buggy_rename(self):
     # test that lock acquisition handles servers which pretend they
     # renamed correctly but that actually fail
     t = transport.get_transport('brokenrename+' + self.get_url())
     ld1 = LockDir(t, 'test_lock')
     ld1.create()
     ld1.attempt_lock()
     ld2 = LockDir(t, 'test_lock')
     # we should fail to lock
     e = self.assertRaises(errors.LockContention, ld2.attempt_lock)
     # now the original caller should succeed in unlocking
     ld1.unlock()
     # and there should be nothing left over
     self.assertEquals([], t.list_dir('test_lock'))
 def test_missing_lockdir_info(self):
     """We can cope with absent info files."""
     t = self.get_transport()
     t.mkdir('test_lock')
     t.mkdir('test_lock/held')
     lf = LockDir(t, 'test_lock')
     # In this case we expect the 'not held' result from peek, because peek
     # cannot be expected to notice that there is a 'held' directory with no
     # 'info' file.
     self.assertEqual(None, lf.peek())
     # And lock/unlock may work or give LockContention (but not any other
     # error).
     try:
         lf.attempt_lock()
     except LockContention:
         # LockContention is ok, and expected on Windows
         pass
     else:
         # no error is ok, and expected on POSIX (because POSIX allows
         # os.rename over an empty directory).
         lf.unlock()
     # Currently raises TokenMismatch, but LockCorrupt would be reasonable
     # too.
     self.assertRaises(
         (errors.TokenMismatch, errors.LockCorrupt),
         lf.validate_token, 'fake token')
Example #13
0
 def test_40_confirm_easy(self):
     """Confirm a lock that's already held"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     lf1.confirm()
 def test_10_lock_uncontested(self):
     """Acquire and release a lock"""
     t = self.get_transport()
     lf = LockDir(t, 'test_lock')
     lf.create()
     lf.attempt_lock()
     try:
         self.assertTrue(lf.is_held)
     finally:
         lf.unlock()
         self.assertFalse(lf.is_held)
Example #15
0
 def test_21_peek_readonly(self):
     """Peek over a readonly transport"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
     self.assertEqual(lf2.peek(), None)
     lf1.attempt_lock()
     info2 = lf2.peek()
     self.assertTrue(info2)
     self.assertEqual(info2['nonce'], lf1.nonce)
 def test_42_confirm_broken_manually(self):
     """Confirm a lock broken by hand"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     t.move('test_lock', 'lock_gone_now')
     self.assertRaises(LockBroken, lf1.confirm)
     # Clean up
     t.move('lock_gone_now', 'test_lock')
     lf1.unlock()
Example #17
0
    def test_uses_lockdir(self):
        """WorkingTreeFormat4 uses its own LockDir:

            - lock is a directory
            - when the WorkingTree is locked, LockDir can see that
        """
        # this test could be factored into a subclass of tests common to both
        # format 3 and 4, but for now its not much of an issue as there is only one in common.
        t = self.get_transport()
        tree = self.make_workingtree()
        self.assertIsDirectory('.bzr', t)
        self.assertIsDirectory('.bzr/checkout', t)
        self.assertIsDirectory('.bzr/checkout/lock', t)
        our_lock = LockDir(t, '.bzr/checkout/lock')
        self.assertEquals(our_lock.peek(), None)
        tree.lock_write()
        self.assertTrue(our_lock.peek())
        tree.unlock()
        self.assertEquals(our_lock.peek(), None)
Example #18
0
    def test_uses_lockdir(self):
        """WorkingTreeFormat4 uses its own LockDir:

            - lock is a directory
            - when the WorkingTree is locked, LockDir can see that
        """
        # this test could be factored into a subclass of tests common to both
        # format 3 and 4, but for now its not much of an issue as there is only one in common.
        t = self.get_transport()
        tree = self.make_workingtree()
        self.assertIsDirectory('.bzr', t)
        self.assertIsDirectory('.bzr/checkout', t)
        self.assertIsDirectory('.bzr/checkout/lock', t)
        our_lock = LockDir(t, '.bzr/checkout/lock')
        self.assertEquals(our_lock.peek(), None)
        tree.lock_write()
        self.assertTrue(our_lock.peek())
        tree.unlock()
        self.assertEquals(our_lock.peek(), None)
Example #19
0
    def test_34_lock_write_waits(self):
        """LockDir.lock_write() will wait for the lock."""
        # the test suite sets the default to 0 to make deadlocks fail fast.
        # change it for this test, as we want to try a manual deadlock.
        raise tests.TestSkipped('Timing-sensitive test')
        bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 300
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')
        lf1.create()
        lf1.attempt_lock()

        def wait_and_unlock():
            time.sleep(0.1)
            lf1.unlock()

        unlocker = Thread(target=wait_and_unlock)
        unlocker.start()
        try:
            lf2 = LockDir(t, 'test_lock')
            self.setup_log_reporter(lf2)
            before = time.time()
            # wait and then lock
            lf2.lock_write()
            after = time.time()
        finally:
            unlocker.join()

        # There should be only 1 report, even though it should have to
        # wait for a while
        lock_base = lf2.transport.abspath(lf2.path)
        self.assertEqual(1, len(self._logged_reports))
        self.assertEqual(
            '%s %s\n'
            '%s\n%s\n'
            'Will continue to try until %s\n', self._logged_reports[0][0])
        args = self._logged_reports[0][1]
        self.assertEqual('Unable to obtain', args[0])
        self.assertEqual('lock %s' % (lock_base, ), args[1])
        self.assertStartsWith(args[2], 'held by ')
        self.assertStartsWith(args[3], 'locked ')
        self.assertEndsWith(args[3], ' ago')
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
Example #20
0
    def test_32_lock_wait_succeed(self):
        """Succeed when trying to acquire a lock that gets released

        One thread holds on a lock and then releases it; another 
        tries to lock it.
        """
        # This test sometimes fails like this:
        # Traceback (most recent call last):

        #   File "/home/pqm/bzr-pqm-workdir/home/+trunk/bzrlib/tests/
        # test_lockdir.py", line 247, in test_32_lock_wait_succeed
        #     self.assertEqual(1, len(self._logged_reports))
        # AssertionError: not equal:
        # a = 1
        # b = 0
        raise tests.TestSkipped("Test fails intermittently")
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')
        lf1.create()
        lf1.attempt_lock()

        def wait_and_unlock():
            time.sleep(0.1)
            lf1.unlock()
        unlocker = Thread(target=wait_and_unlock)
        unlocker.start()
        try:
            lf2 = LockDir(t, 'test_lock')
            self.setup_log_reporter(lf2)
            before = time.time()
            # wait and then lock
            lf2.wait_lock(timeout=0.4, poll=0.1)
            after = time.time()
            self.assertTrue(after - before <= 1.0)
        finally:
            unlocker.join()

        # There should be only 1 report, even though it should have to
        # wait for a while
        lock_base = lf2.transport.abspath(lf2.path)
        self.assertEqual(1, len(self._logged_reports))
        self.assertEqual('%s %s\n'
                         '%s\n%s\n'
                         'Will continue to try until %s\n',
                         self._logged_reports[0][0])
        args = self._logged_reports[0][1]
        self.assertEqual('Unable to obtain', args[0])
        self.assertEqual('lock %s' % (lock_base,), args[1])
        self.assertStartsWith(args[2], 'held by ')
        self.assertStartsWith(args[3], 'locked ')
        self.assertEndsWith(args[3], ' ago')
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
 def test_20_lock_peek(self):
     """Peek at the state of a lock"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     self.addCleanup(lf1.unlock)
     # lock is held, should get some info on it
     info1 = lf1.peek()
     self.assertEqual(set(info1.info_dict.keys()),
         set(['user', 'nonce', 'hostname', 'pid', 'start_time']))
     # should get the same info if we look at it through a different
     # instance
     info2 = LockDir(t, 'test_lock').peek()
     self.assertEqual(info1, info2)
     # locks which are never used should be not-held
     self.assertEqual(LockDir(t, 'other_lock').peek(), None)
Example #22
0
 def test_missing_lockdir_info(self):
     """We can cope with absent info files."""
     t = self.get_transport()
     t.mkdir('test_lock')
     t.mkdir('test_lock/held')
     lf = LockDir(t, 'test_lock')
     # In this case we expect the 'not held' result from peek, because peek
     # cannot be expected to notice that there is a 'held' directory with no
     # 'info' file.
     self.assertEqual(None, lf.peek())
     # And lock/unlock may work or give LockContention (but not any other
     # error).
     try:
         lf.attempt_lock()
     except LockContention:
         # LockContention is ok, and expected on Windows
         pass
     else:
         # no error is ok, and expected on POSIX (because POSIX allows
         # os.rename over an empty directory).
         lf.unlock()
     # Currently raises TokenMismatch, but LockCorrupt would be reasonable
     # too.
     self.assertRaises(
         (errors.TokenMismatch, errors.LockCorrupt),
         lf.validate_token, 'fake token')
Example #23
0
 def test_30_lock_wait_fail(self):
     """Wait on a lock, then fail
     
     We ask to wait up to 400ms; this should fail within at most one
     second.  (Longer times are more realistic but we don't want the test
     suite to take too long, and this should do for now.)
     """
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf2 = LockDir(t, 'test_lock')
     self.setup_log_reporter(lf2)
     lf1.attempt_lock()
     try:
         before = time.time()
         self.assertRaises(LockContention,
                           lf2.wait_lock,
                           timeout=0.4,
                           poll=0.1)
         after = time.time()
         # it should only take about 0.4 seconds, but we allow more time in
         # case the machine is heavily loaded
         self.assertTrue(
             after - before <= 8.0,
             "took %f seconds to detect lock contention" % (after - before))
     finally:
         lf1.unlock()
     lock_base = lf2.transport.abspath(lf2.path)
     self.assertEqual(1, len(self._logged_reports))
     lock_url = lf2.transport.abspath(lf2.path)
     self.assertEqual(
         '%s %s\n'
         '%s\n%s\n'
         'Will continue to try until %s, unless '
         'you press Ctrl-C\n'
         'If you\'re sure that it\'s not being '
         'modified, use bzr break-lock %s', self._logged_reports[0][0])
     args = self._logged_reports[0][1]
     self.assertEqual('Unable to obtain', args[0])
     self.assertEqual('lock %s' % (lock_base, ), args[1])
     self.assertStartsWith(args[2], 'held by ')
     self.assertStartsWith(args[3], 'locked ')
     self.assertEndsWith(args[3], ' ago')
     self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
Example #24
0
 def test_10_lock_uncontested(self):
     """Acquire and release a lock"""
     t = self.get_transport()
     lf = LockDir(t, 'test_lock')
     lf.create()
     lf.attempt_lock()
     try:
         self.assertTrue(lf.is_held)
     finally:
         lf.unlock()
         self.assertFalse(lf.is_held)
 def test_31_lock_wait_easy(self):
     """Succeed when waiting on a lock with no contention.
     """
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     self.setup_log_reporter(lf1)
     try:
         before = time.time()
         lf1.wait_lock(timeout=0.4, poll=0.1)
         after = time.time()
         self.assertTrue(after - before <= 1.0)
     finally:
         lf1.unlock()
     self.assertEqual([], self._logged_reports)
    def test_50_lockdir_representation(self):
        """Check the on-disk representation of LockDirs is as expected.

        There should always be a top-level directory named by the lock.
        When the lock is held, there should be a lockname/held directory
        containing an info file.
        """
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')
        lf1.create()
        self.assertTrue(t.has('test_lock'))
        lf1.lock_write()
        self.assertTrue(t.has('test_lock/held/info'))
        lf1.unlock()
        self.assertFalse(t.has('test_lock/held/info'))
Example #27
0
 def test_20_lock_contested(self):
     """Contention to get a lock"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     lf2 = LockDir(t, 'test_lock')
     try:
         # locking is between LockDir instances; aliases within
         # a single process are not detected
         lf2.attempt_lock()
         self.fail('Failed to detect lock collision')
     except LockContention, e:
         self.assertEqual(e.lock, lf2)
         self.assertContainsRe(str(e),
                 r'^Could not acquire.*test_lock.*$')
Example #28
0
 def test_uses_lockdir(self):
     """WorkingTreeFormat3 uses its own LockDir:
         
         - lock is a directory
         - when the WorkingTree is locked, LockDir can see that
     """
     t = self.get_transport()
     url = self.get_url()
     dir = bzrdir.BzrDirMetaFormat1().initialize(url)
     repo = dir.create_repository()
     branch = dir.create_branch()
     try:
         tree = workingtree.WorkingTreeFormat3().initialize(dir)
     except errors.NotLocalUrl:
         raise TestSkipped('Not a local URL')
     self.assertIsDirectory('.bzr', t)
     self.assertIsDirectory('.bzr/checkout', t)
     self.assertIsDirectory('.bzr/checkout/lock', t)
     our_lock = LockDir(t, '.bzr/checkout/lock')
     self.assertEquals(our_lock.peek(), None)
     tree.lock_write()
     self.assertTrue(our_lock.peek())
     tree.unlock()
     self.assertEquals(our_lock.peek(), None)
Example #29
0
    def test_uses_lockdir(self):
        """WorkingTreeFormat3 uses its own LockDir:

            - lock is a directory
            - when the WorkingTree is locked, LockDir can see that
        """
        t = self.get_transport()
        url = self.get_url()
        dir = bzrdir.BzrDirMetaFormat1().initialize(url)
        repo = dir.create_repository()
        branch = dir.create_branch()
        try:
            tree = workingtree_3.WorkingTreeFormat3().initialize(dir)
        except errors.NotLocalUrl:
            raise TestSkipped('Not a local URL')
        self.assertIsDirectory('.bzr', t)
        self.assertIsDirectory('.bzr/checkout', t)
        self.assertIsDirectory('.bzr/checkout/lock', t)
        our_lock = LockDir(t, '.bzr/checkout/lock')
        self.assertEquals(our_lock.peek(), None)
        tree.lock_write()
        self.assertTrue(our_lock.peek())
        tree.unlock()
        self.assertEquals(our_lock.peek(), None)
Example #30
0
 def test_lock_with_buggy_rename(self):
     # test that lock acquisition handles servers which pretend they
     # renamed correctly but that actually fail
     t = transport.get_transport('brokenrename+' + self.get_url())
     ld1 = LockDir(t, 'test_lock')
     ld1.create()
     ld1.attempt_lock()
     ld2 = LockDir(t, 'test_lock')
     # we should fail to lock
     e = self.assertRaises(errors.LockContention, ld2.attempt_lock)
     # now the original caller should succeed in unlocking
     ld1.unlock()
     # and there should be nothing left over
     self.assertEquals([], t.list_dir('test_lock'))
Example #31
0
    def test_50_lockdir_representation(self):
        """Check the on-disk representation of LockDirs is as expected.

        There should always be a top-level directory named by the lock.
        When the lock is held, there should be a lockname/held directory
        containing an info file.
        """
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')
        lf1.create()
        self.assertTrue(t.has('test_lock'))
        lf1.lock_write()
        self.assertTrue(t.has('test_lock/held/info'))
        lf1.unlock()
        self.assertFalse(t.has('test_lock/held/info'))
 def test_corrupt_lockdir_info(self):
     """We can cope with corrupt (and thus unparseable) info files."""
     # This seems like a fairly common failure case too - see
     # <https://bugs.launchpad.net/bzr/+bug/619872> for instance.
     # In particular some systems tend to fill recently created files with
     # nul bytes after recovering from a system crash.
     t = self.get_transport()
     t.mkdir('test_lock')
     t.mkdir('test_lock/held')
     t.put_bytes('test_lock/held/info', '\0')
     lf = LockDir(t, 'test_lock')
     self.assertRaises(errors.LockCorrupt, lf.peek)
     # Currently attempt_lock gives LockContention, but LockCorrupt would be
     # a reasonable result too.
     self.assertRaises(
         (errors.LockCorrupt, errors.LockContention), lf.attempt_lock)
     self.assertRaises(errors.LockCorrupt, lf.validate_token, 'fake token')
Example #33
0
 def test_31_lock_wait_easy(self):
     """Succeed when waiting on a lock with no contention.
     """
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     self.setup_log_reporter(lf1)
     try:
         before = time.time()
         lf1.wait_lock(timeout=0.4, poll=0.1)
         after = time.time()
         self.assertTrue(after - before <= 1.0)
     finally:
         lf1.unlock()
     self.assertEqual([], self._logged_reports)
 def test_auto_break_stale_lock_configured_off(self):
     """Automatic breaking can be turned off"""
     l1 = LockDir(self.get_transport(), 'a',
         extra_holder_info={'pid': '12312313'})
     token_1 = l1.attempt_lock()
     self.addCleanup(l1.unlock)
     l2 = LockDir(self.get_transport(), 'a')
     # This fails now, because dead lock breaking is off by default.
     self.assertRaises(LockContention,
         l2.attempt_lock)
     # and it's in fact not broken
     l1.confirm()
Example #35
0
    def test_34_lock_write_waits(self):
        """LockDir.lock_write() will wait for the lock.""" 
        # the test suite sets the default to 0 to make deadlocks fail fast.
        # change it for this test, as we want to try a manual deadlock.
        raise tests.TestSkipped('Timing-sensitive test')
        bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 300
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')
        lf1.create()
        lf1.attempt_lock()

        def wait_and_unlock():
            time.sleep(0.1)
            lf1.unlock()
        unlocker = Thread(target=wait_and_unlock)
        unlocker.start()
        try:
            lf2 = LockDir(t, 'test_lock')
            self.setup_log_reporter(lf2)
            before = time.time()
            # wait and then lock
            lf2.lock_write()
            after = time.time()
        finally:
            unlocker.join()

        # There should be only 1 report, even though it should have to
        # wait for a while
        lock_base = lf2.transport.abspath(lf2.path)
        self.assertEqual(1, len(self._logged_reports))
        self.assertEqual('%s %s\n'
                         '%s\n%s\n'
                         'Will continue to try until %s\n',
                         self._logged_reports[0][0])
        args = self._logged_reports[0][1]
        self.assertEqual('Unable to obtain', args[0])
        self.assertEqual('lock %s' % (lock_base,), args[1])
        self.assertStartsWith(args[2], 'held by ')
        self.assertStartsWith(args[3], 'locked ')
        self.assertEndsWith(args[3], ' ago')
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
    def test_auto_break_stale_lock(self):
        """Locks safely known to be stale are just cleaned up.

        This generates a warning but no other user interaction.
        """
        self.overrideAttr(lockdir, 'get_host_name',
            lambda: 'aproperhostname')
        # This is off by default at present; see the discussion in the bug.
        # If you change the default, don't forget to update the docs.
        config.GlobalStack().set('locks.steal_dead', True)
        # Create a lock pretending to come from a different nonexistent
        # process on the same machine.
        l1 = LockDir(self.get_transport(), 'a',
            extra_holder_info={'pid': '12312313'})
        token_1 = l1.attempt_lock()
        l2 = LockDir(self.get_transport(), 'a')
        token_2 = l2.attempt_lock()
        # l1 will notice its lock was stolen.
        self.assertRaises(errors.LockBroken,
            l1.unlock)
        l2.unlock()
    def test_create_missing_base_directory(self):
        """If LockDir.path doesn't exist, it can be created

        Some people manually remove the entire lock/ directory trying
        to unlock a stuck repository/branch/etc. Rather than failing
        after that, just create the lock directory when needed.
        """
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')

        lf1.create()
        self.assertTrue(t.has('test_lock'))

        t.rmdir('test_lock')
        self.assertFalse(t.has('test_lock'))

        # This will create 'test_lock' if it needs to
        lf1.lock_write()
        self.assertTrue(t.has('test_lock'))
        self.assertTrue(t.has('test_lock/held/info'))

        lf1.unlock()
        self.assertFalse(t.has('test_lock/held/info'))
Example #38
0
 def test_20_lock_peek(self):
     """Peek at the state of a lock"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     # lock is held, should get some info on it
     info1 = lf1.peek()
     self.assertEqual(set(info1.keys()),
                      set(['user', 'nonce', 'hostname', 'pid', 'start_time']))
     # should get the same info if we look at it through a different
     # instance
     info2 = LockDir(t, 'test_lock').peek()
     self.assertEqual(info1, info2)
     # locks which are never used should be not-held
     self.assertEqual(LockDir(t, 'other_lock').peek(), None)
Example #39
0
 def test_30_lock_wait_fail(self):
     """Wait on a lock, then fail
     
     We ask to wait up to 400ms; this should fail within at most one
     second.  (Longer times are more realistic but we don't want the test
     suite to take too long, and this should do for now.)
     """
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf2 = LockDir(t, 'test_lock')
     self.setup_log_reporter(lf2)
     lf1.attempt_lock()
     try:
         before = time.time()
         self.assertRaises(LockContention, lf2.wait_lock,
                           timeout=0.4, poll=0.1)
         after = time.time()
         # it should only take about 0.4 seconds, but we allow more time in
         # case the machine is heavily loaded
         self.assertTrue(after - before <= 8.0, 
                 "took %f seconds to detect lock contention" % (after - before))
     finally:
         lf1.unlock()
     lock_base = lf2.transport.abspath(lf2.path)
     self.assertEqual(1, len(self._logged_reports))
     lock_url = lf2.transport.abspath(lf2.path)
     self.assertEqual('%s %s\n'
                      '%s\n%s\n'
                      'Will continue to try until %s, unless '
                      'you press Ctrl-C\n'
                      'If you\'re sure that it\'s not being '
                      'modified, use bzr break-lock %s',
                      self._logged_reports[0][0])
     args = self._logged_reports[0][1]
     self.assertEqual('Unable to obtain', args[0])
     self.assertEqual('lock %s' % (lock_base,), args[1])
     self.assertStartsWith(args[2], 'held by ')
     self.assertStartsWith(args[3], 'locked ')
     self.assertEndsWith(args[3], ' ago')
     self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
Example #40
0
    def test_create_missing_base_directory(self):
        """If LockDir.path doesn't exist, it can be created

        Some people manually remove the entire lock/ directory trying
        to unlock a stuck repository/branch/etc. Rather than failing
        after that, just create the lock directory when needed.
        """
        t = self.get_transport()
        lf1 = LockDir(t, 'test_lock')

        lf1.create()
        self.assertTrue(t.has('test_lock'))

        t.rmdir('test_lock')
        self.assertFalse(t.has('test_lock'))

        # This will create 'test_lock' if it needs to
        lf1.lock_write()
        self.assertTrue(t.has('test_lock'))
        self.assertTrue(t.has('test_lock/held/info'))

        lf1.unlock()
        self.assertFalse(t.has('test_lock/held/info'))
Example #41
0
    def test_auto_break_stale_lock(self):
        """Locks safely known to be stale are just cleaned up.

        This generates a warning but no other user interaction.
        """
        self.overrideAttr(lockdir, 'get_host_name',
            lambda: 'aproperhostname')
        # This is off by default at present; see the discussion in the bug.
        # If you change the default, don't forget to update the docs.
        config.GlobalConfig().set_user_option('locks.steal_dead', True)
        # Create a lock pretending to come from a different nonexistent
        # process on the same machine.
        l1 = LockDir(self.get_transport(), 'a',
            extra_holder_info={'pid': '12312313'})
        token_1 = l1.attempt_lock()
        l2 = LockDir(self.get_transport(), 'a')
        token_2 = l2.attempt_lock()
        # l1 will notice its lock was stolen.
        self.assertRaises(errors.LockBroken,
            l1.unlock)
        l2.unlock()
Example #42
0
 def test_03_readonly_peek(self):
     lf = LockDir(self.get_readonly_transport(), 'test_lock')
     self.assertEqual(lf.peek(), None)
Example #43
0
 def test_46_fake_read_lock(self):
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.lock_read()
     lf1.unlock()
Example #44
0
 def test_12_lock_readonly_transport(self):
     """Fail to lock on readonly transport"""
     lf = LockDir(self.get_transport(), 'test_lock')
     lf.create()
     lf = LockDir(self.get_readonly_transport(), 'test_lock')
     self.assertRaises(LockFailed, lf.attempt_lock)
Example #45
0
 def test_41_confirm_not_held(self):
     """Confirm a lock that's already held"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     self.assertRaises(LockNotHeld, lf1.confirm)
 def test_02_unlocked_peek(self):
     lf = LockDir(self.get_transport(), 'test_lock')
     self.assertEqual(lf.peek(), None)
 def test_43_break(self):
     """Break a lock whose caller has forgotten it"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     # we incorrectly discard the lock object without unlocking it
     del lf1
     # someone else sees it's still locked
     lf2 = LockDir(t, 'test_lock')
     holder_info = lf2.peek()
     self.assertTrue(holder_info)
     lf2.force_break(holder_info)
     # now we should be able to take it
     lf2.attempt_lock()
     self.addCleanup(lf2.unlock)
     lf2.confirm()
Example #48
0
 def test_45_break_mismatch(self):
     """Lock break races with someone else acquiring it"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     # someone else sees it's still locked
     lf2 = LockDir(t, 'test_lock')
     holder_info = lf2.peek()
     # in the interim the lock is released
     lf1.unlock()
     lf3 = LockDir(t, 'test_lock')
     lf3.attempt_lock()
     # break should now *fail*
     self.assertRaises(LockBreakMismatch, lf2.force_break,
                       holder_info)
     lf3.unlock()
Example #49
0
 def test_43_break(self):
     """Break a lock whose caller has forgotten it"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     # we incorrectly discard the lock object without unlocking it
     del lf1
     # someone else sees it's still locked
     lf2 = LockDir(t, 'test_lock')
     holder_info = lf2.peek()
     self.assertTrue(holder_info)
     lf2.force_break(holder_info)
     # now we should be able to take it
     lf2.attempt_lock()
     self.addCleanup(lf2.unlock)
     lf2.confirm()
 def test_00_lock_creation(self):
     """Creation of lock file on a transport"""
     t = self.get_transport()
     lf = LockDir(t, 'test_lock')
     self.assertFalse(lf.is_held)
 def test_01_lock_repr(self):
     """Lock string representation"""
     lf = LockDir(self.get_transport(), 'test_lock')
     r = repr(lf)
     self.assertContainsRe(r, r'^LockDir\(.*/test_lock\)$')
 def test_03_readonly_peek(self):
     lf = LockDir(self.get_readonly_transport(), 'test_lock')
     self.assertEqual(lf.peek(), None)
 def test_44_break_already_released(self):
     """Lock break races with regular release"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     # someone else sees it's still locked
     lf2 = LockDir(t, 'test_lock')
     holder_info = lf2.peek()
     # in the interim the lock is released
     lf1.unlock()
     # break should succeed
     lf2.force_break(holder_info)
     # now we should be able to take it
     lf2.attempt_lock()
     self.addCleanup(lf2.unlock)
     lf2.confirm()
Example #54
0
 def test_02_unlocked_peek(self):
     lf = LockDir(self.get_transport(), 'test_lock')
     self.assertEqual(lf.peek(), None)
 def get_lock(self):
     return LockDir(self.get_transport(), 'test_lock')
 def test_45_break_mismatch(self):
     """Lock break races with someone else acquiring it"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     # someone else sees it's still locked
     lf2 = LockDir(t, 'test_lock')
     holder_info = lf2.peek()
     # in the interim the lock is released
     lf1.unlock()
     lf3 = LockDir(t, 'test_lock')
     lf3.attempt_lock()
     # break should now *fail*
     self.assertRaises(LockBreakMismatch, lf2.force_break,
                       holder_info)
     lf3.unlock()
Example #57
0
 def test_44_break_already_released(self):
     """Lock break races with regular release"""
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.attempt_lock()
     # someone else sees it's still locked
     lf2 = LockDir(t, 'test_lock')
     holder_info = lf2.peek()
     # in the interim the lock is released
     lf1.unlock()
     # break should succeed
     lf2.force_break(holder_info)
     # now we should be able to take it
     lf2.attempt_lock()
     self.addCleanup(lf2.unlock)
     lf2.confirm()
 def test_46_fake_read_lock(self):
     t = self.get_transport()
     lf1 = LockDir(t, 'test_lock')
     lf1.create()
     lf1.lock_read()
     lf1.unlock()