diff options
author | Christian Heimes <christian@cheimes.de> | 2008-01-19 13:46:06 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-01-19 13:46:06 (GMT) |
commit | 71a0451f67b41e326a14f4ae1481cb72e462881d (patch) | |
tree | 88cb1c75a5b6ee9aeca32a6660dd020e8d2d1d5e /Lib/test | |
parent | f8f4eb69ce006d89f70fe2d51204533ec8319ae8 (diff) | |
download | cpython-71a0451f67b41e326a14f4ae1481cb72e462881d.zip cpython-71a0451f67b41e326a14f4ae1481cb72e462881d.tar.gz cpython-71a0451f67b41e326a14f4ae1481cb72e462881d.tar.bz2 |
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?
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_threading_local.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/Lib/test/test_threading_local.py b/Lib/test/test_threading_local.py index 0aaedbc..7c2b9f4 100644 --- a/Lib/test/test_threading_local.py +++ b/Lib/test/test_threading_local.py @@ -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 |