/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Robert Collins
  • Date: 2009-04-30 06:16:30 UTC
  • mfrom: (3331.4.1 test-suite.lock_checking)
  • mto: This revision was merged to the branch mainline in revision 4317.
  • Revision ID: robertc@robertcollins.net-20090430061630-fj6xih0prt4cw3s8
Update lock debugging support patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
    debug,
56
56
    errors,
57
57
    hooks,
 
58
    lock as _mod_lock,
58
59
    memorytree,
59
60
    osutils,
60
61
    progress,
798
799
        self._benchcalls = []
799
800
        self._benchtime = None
800
801
        self._clear_hooks()
 
802
        self._track_locks()
801
803
        self._clear_debug_flags()
802
804
        TestCase._active_threads = threading.activeCount()
803
805
        self.addCleanup(self._check_leaked_threads)
850
852
        ui.ui_factory = ui.SilentUIFactory()
851
853
        self.addCleanup(_restore)
852
854
 
 
855
    def _check_locks(self):
 
856
        """Check that all lock take/release actions have been paired."""
 
857
        # once we have fixed all the current lock problems, we can change the
 
858
        # following code to always check for mismatched locks, but only do
 
859
        # traceback showing with -Dlock (self._lock_check_thorough is True).
 
860
        # For now, because the test suite will fail, we only assert that lock
 
861
        # matching has occured with -Dlock.
 
862
        # unhook:
 
863
        acquired_locks = [lock for action, lock in self._lock_actions
 
864
            if action == 'acquired']
 
865
        released_locks = [lock for action, lock in self._lock_actions
 
866
            if action == 'released']
 
867
        # trivially, given the tests for lock acquistion and release, if we
 
868
        # have as many in each list, it should be ok.
 
869
        if len(acquired_locks) != len(released_locks):
 
870
            message = \
 
871
                ("Different number of acquired and released locks. (%s, %s)" %
 
872
                (acquired_locks, released_locks))
 
873
            if not self._lock_check_thorough:
 
874
                # Rather than fail, just warn
 
875
                print "Broken test %s: %s" % (self, message)
 
876
                return
 
877
            self.fail(message)
 
878
 
 
879
    def _track_locks(self):
 
880
        """Track lock activity during tests."""
 
881
        self._lock_actions = []
 
882
        self._lock_check_thorough = 'lock' in debug.debug_flags
 
883
        self.addCleanup(self._check_locks)
 
884
        _mod_lock.Lock.hooks.install_named_hook('lock_acquired', self._lock_acquired, None)
 
885
        _mod_lock.Lock.hooks.install_named_hook('lock_released', self._lock_released, None)
 
886
 
 
887
    def _lock_acquired(self, result):
 
888
        self._lock_actions.append(('acquired', result))
 
889
 
 
890
    def _lock_released(self, result):
 
891
        self._lock_actions.append(('released', result))
 
892
 
853
893
    def _ndiff_strings(self, a, b):
854
894
        """Return ndiff between two strings containing lines.
855
895
 
1331
1371
                else:
1332
1372
                    result.addSuccess(self)
1333
1373
                result.stopTest(self)
1334
 
                return
 
1374
                return result
1335
1375
        try:
1336
1376
            try:
1337
1377
                result.startTest(self)
1354
1394
                    except TestSkipped, e:
1355
1395
                        self._do_skip(result, e.args[0])
1356
1396
                        self.tearDown()
1357
 
                        return
 
1397
                        return result
1358
1398
                    except:
1359
1399
                        result.addError(self, sys.exc_info())
1360
 
                        return
 
1400
                        return result
1361
1401
 
1362
1402
                    ok = False
1363
1403
                    try:
1390
1430
                    if ok: result.addSuccess(self)
1391
1431
                finally:
1392
1432
                    result.stopTest(self)
1393
 
                return
 
1433
                return result
1394
1434
            except TestNotApplicable:
1395
1435
                # Not moved from the result [yet].
1396
1436
                raise