~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Vincent Ladeuil
  • Date: 2008-05-07 13:37:07 UTC
  • mto: This revision was merged to the branch mainline in revision 3417.
  • Revision ID: v.ladeuil+lp@free.fr-20080507133707-hfscqf32riufeqx7
Fix as per Robert's review.

* test_strace.py:
(TestStrace._check_threads): Raise KnownFailure if there is more
than one thread active (to avoid hanging).

* __init__.py:
(TestCase): Detect and report leaked threads.

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
758
759
        return password
759
760
 
760
761
 
 
762
def _report_leaked_threads():
 
763
    bzrlib.trace.warning('%s is leaking threads among %d leaking tests',
 
764
                         TestCase._first_thread_leaker_id,
 
765
                         TestCase._leaking_threads_tests)
 
766
 
 
767
 
761
768
class TestCase(unittest.TestCase):
762
769
    """Base class for bzr unit tests.
763
770
    
779
786
    accidentally overlooked.
780
787
    """
781
788
 
 
789
    _active_threads = None
 
790
    _leaking_threads_tests = 0
 
791
    _first_thread_leaker_id = None
782
792
    _log_file_name = None
783
793
    _log_contents = ''
784
794
    _keep_log_file = False
801
811
        self._benchtime = None
802
812
        self._clear_hooks()
803
813
        self._clear_debug_flags()
 
814
        TestCase._active_threads = threading.activeCount()
 
815
        self.addCleanup(self._check_leaked_threads)
 
816
 
 
817
    def _check_leaked_threads(self):
 
818
        active = threading.activeCount()
 
819
        leaked_threads = active - TestCase._active_threads
 
820
        TestCase._active_threads = active
 
821
        if leaked_threads:
 
822
            TestCase._leaking_threads_tests += 1
 
823
            if TestCase._first_thread_leaker_id is None:
 
824
                TestCase._first_thread_leaker_id = self.id()
 
825
                # we're not specifically told when all tests are finished.
 
826
                # This will do. We use a function to avoid keeping a reference
 
827
                # to a TestCase object.
 
828
                atexit.register(_report_leaked_threads)
804
829
 
805
830
    def _clear_debug_flags(self):
806
831
        """Prevent externally set debug flags affecting tests.