[Python-checkins] r60075 - python/trunk/Lib/test/test_threading_local.py

christian.heimes python-checkins at python.org
Sat Jan 19 14:46:07 CET 2008


Author: christian.heimes
Date: Sat Jan 19 14:46:06 2008
New Revision: 60075

Modified:
   python/trunk/Lib/test/test_threading_local.py
Log:
Added unit test to verify that threading.local doesn't cause ref leaks. It seems that the thread local storage always keeps the storage of the last stopped thread alive. Can anybody comment on it, please?

Modified: python/trunk/Lib/test/test_threading_local.py
==============================================================================
--- python/trunk/Lib/test/test_threading_local.py	(original)
+++ python/trunk/Lib/test/test_threading_local.py	Sat Jan 19 14:46:06 2008
@@ -1,9 +1,35 @@
 import unittest
 from doctest import DocTestSuite
 from test import test_support
+import threading
+import weakref
+
+class Weak(object):
+    pass
+
+def target(local, weaklist):
+    weak = Weak()
+    local.weak = weak
+    weaklist.append(weakref.ref(weak))
+
+class ThreadingLocalTest(unittest.TestCase):
+    def test_local_refs(self):
+        local = threading.local()
+        weaklist = []
+        n = 20
+        for i in range(n):
+            t = threading.Thread(target=target, args=(local, weaklist))
+            t.start()
+            t.join()
+        self.assertEqual(len(weaklist), n)
+        deadlist = [weak for weak in weaklist if weak() is None]
+        # XXX threading.local keeps the local of the last stopped thread alive
+        self.assertEqual(len(deadlist), n-1)
 
 def test_main():
-    suite = DocTestSuite('_threading_local')
+    suite = unittest.TestSuite()
+    suite.addTest(DocTestSuite('_threading_local'))
+    suite.addTest(unittest.makeSuite(ThreadingLocalTest))
 
     try:
         from thread import _local


More information about the Python-checkins mailing list