~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

(vila) Fix bug #226769: Don't run strace tests if more than one
        thread is active

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
from subprocess import Popen, PIPE
43
43
import sys
44
44
import tempfile
 
45
import threading
45
46
import time
46
47
import unittest
47
48
import warnings
755
756
        return password
756
757
 
757
758
 
 
759
def _report_leaked_threads():
 
760
    bzrlib.trace.warning('%s is leaking threads among %d leaking tests',
 
761
                         TestCase._first_thread_leaker_id,
 
762
                         TestCase._leaking_threads_tests)
 
763
 
 
764
 
758
765
class TestCase(unittest.TestCase):
759
766
    """Base class for bzr unit tests.
760
767
    
776
783
    accidentally overlooked.
777
784
    """
778
785
 
 
786
    _active_threads = None
 
787
    _leaking_threads_tests = 0
 
788
    _first_thread_leaker_id = None
779
789
    _log_file_name = None
780
790
    _log_contents = ''
781
791
    _keep_log_file = False
798
808
        self._benchtime = None
799
809
        self._clear_hooks()
800
810
        self._clear_debug_flags()
 
811
        TestCase._active_threads = threading.activeCount()
 
812
        self.addCleanup(self._check_leaked_threads)
 
813
 
 
814
    def _check_leaked_threads(self):
 
815
        active = threading.activeCount()
 
816
        leaked_threads = active - TestCase._active_threads
 
817
        TestCase._active_threads = active
 
818
        if leaked_threads:
 
819
            TestCase._leaking_threads_tests += 1
 
820
            if TestCase._first_thread_leaker_id is None:
 
821
                TestCase._first_thread_leaker_id = self.id()
 
822
                # we're not specifically told when all tests are finished.
 
823
                # This will do. We use a function to avoid keeping a reference
 
824
                # to a TestCase object.
 
825
                atexit.register(_report_leaked_threads)
801
826
 
802
827
    def _clear_debug_flags(self):
803
828
        """Prevent externally set debug flags affecting tests.