Rev 3408: Fix as per Robert's review. in http://code.launchpad.net/%7Evila/bzr/226769-selftest-hanging-with-strace

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed May 7 14:38:15 BST 2008


At http://code.launchpad.net/%7Evila/bzr/226769-selftest-hanging-with-strace

------------------------------------------------------------
revno: 3408
revision-id: v.ladeuil+lp at free.fr-20080507133707-hfscqf32riufeqx7
parent: v.ladeuil+lp at free.fr-20080505130727-trhinzsipvucozdu
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 226769-selftest-hanging-with-strace
timestamp: Wed 2008-05-07 15:37:07 +0200
message:
  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.
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_strace.py    test_strace.py-20070323001526-6zquhhw8leb9m6j8-2
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2008-05-05 13:07:27 +0000
+++ b/NEWS	2008-05-07 13:37:07 +0000
@@ -48,8 +48,9 @@
     * Conversion from non-rich-root to rich-root(-pack) works even when a
       parent revision has a different root id.  (Aaron Bentley, #177874)
 
-    * Disable strace testing until strace is fixed (see bug #103133).
-     (Vincent Ladeuil, #226769)
+    * Disable strace testing until strace is fixed (see bug #103133) and emit a
+      warning when selftest ends to remind us of leaking tests.
+      (Vincent Ladeuil, #226769)
 
     * Fetching all revisions from a repository does not cause pack collisions.
       (Robert Collins, Aaron Bentley, #212908)

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2008-05-05 02:06:10 +0000
+++ b/bzrlib/tests/__init__.py	2008-05-07 13:37:07 +0000
@@ -42,6 +42,7 @@
 from subprocess import Popen, PIPE
 import sys
 import tempfile
+import threading
 import time
 import unittest
 import warnings
@@ -758,6 +759,12 @@
         return password
 
 
+def _report_leaked_threads():
+    bzrlib.trace.warning('%s is leaking threads among %d leaking tests',
+                         TestCase._first_thread_leaker_id,
+                         TestCase._leaking_threads_tests)
+
+
 class TestCase(unittest.TestCase):
     """Base class for bzr unit tests.
     
@@ -779,6 +786,9 @@
     accidentally overlooked.
     """
 
+    _active_threads = None
+    _leaking_threads_tests = 0
+    _first_thread_leaker_id = None
     _log_file_name = None
     _log_contents = ''
     _keep_log_file = False
@@ -801,6 +811,21 @@
         self._benchtime = None
         self._clear_hooks()
         self._clear_debug_flags()
+        TestCase._active_threads = threading.activeCount()
+        self.addCleanup(self._check_leaked_threads)
+
+    def _check_leaked_threads(self):
+        active = threading.activeCount()
+        leaked_threads = active - TestCase._active_threads
+        TestCase._active_threads = active
+        if leaked_threads:
+            TestCase._leaking_threads_tests += 1
+            if TestCase._first_thread_leaker_id is None:
+                TestCase._first_thread_leaker_id = self.id()
+                # we're not specifically told when all tests are finished.
+                # This will do. We use a function to avoid keeping a reference
+                # to a TestCase object.
+                atexit.register(_report_leaked_threads)
 
     def _clear_debug_flags(self):
         """Prevent externally set debug flags affecting tests.

=== modified file 'bzrlib/tests/test_strace.py'
--- a/bzrlib/tests/test_strace.py	2008-05-05 13:07:27 +0000
+++ b/bzrlib/tests/test_strace.py	2008-05-07 13:37:07 +0000
@@ -19,6 +19,7 @@
 
 import errno
 import subprocess
+import threading
 
 from bzrlib import (
     tests,
@@ -52,10 +53,18 @@
     # If the following tests are activated, selftest may hang (see bug
     # #226769). This is due to strace strange behavior when required to trace
     # its own parent in the presence of threads (or something like that). One
-    # strace is fixed, we may want to activate these tests again.
+    # strace is fixed, we may want to activate these tests again. Note: running
+    # these tests in isolation is still possible.
+
+    def _check_threads(self):
+        active = threading.activeCount()
+        if active > 1: # There is always the main thread at least
+            raise tests.KnownFailure(
+                '%d active threads, bug #103133 needs to be fixed.' % active)
 
     def test_strace_callable_is_called(self):
-        raise tests.TestSkipped("bug #103133 needs to be fixed.")
+        self._check_threads()
+
         output = []
         def function(positional, *args, **kwargs):
             output.append((positional, args, kwargs))
@@ -64,7 +73,7 @@
         self.assertEqual([("a", ("b",), {"c":"c"})], output)
 
     def test_strace_callable_result(self):
-        raise tests.TestSkipped("bug #103133 needs to be fixed.")
+        self._check_threads()
 
         def function():
             return "foo"
@@ -75,7 +84,8 @@
 
     def test_strace_result_has_raw_log(self):
         """Checks that a reasonable raw strace log was found by strace."""
-        raise tests.TestSkipped("bug #103133 needs to be fixed.")
+        self._check_threads()
+
         def function():
             self.build_tree(['myfile'])
         unused, result = strace_detailed(function, [], {},



More information about the bazaar-commits mailing list