Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_threading_local.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 8ccb35739a96 |
| children | ee9a551a875e 6768b2350c57 |
| rev | line source |
|---|---|
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
1 import unittest |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
2 from doctest import DocTestSuite |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
3 from test import test_support |
|
44763
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
4 import threading |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
5 import weakref |
|
44764
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
6 import gc |
|
44763
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
7 |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
8 class Weak(object): |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
9 pass |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
10 |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
11 def target(local, weaklist): |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
12 weak = Weak() |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
13 local.weak = weak |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
14 weaklist.append(weakref.ref(weak)) |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
15 |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
16 class ThreadingLocalTest(unittest.TestCase): |
|
44764
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
17 |
|
44763
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
18 def test_local_refs(self): |
|
44764
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
19 self._local_refs(20) |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
20 self._local_refs(50) |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
21 self._local_refs(100) |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
22 |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
23 def _local_refs(self, n): |
|
44763
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
24 local = threading.local() |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
25 weaklist = [] |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
26 for i in range(n): |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
27 t = threading.Thread(target=target, args=(local, weaklist)) |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
28 t.start() |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
29 t.join() |
|
44764
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
30 del t |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
31 |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
32 gc.collect() |
|
44763
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
33 self.assertEqual(len(weaklist), n) |
|
44764
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
34 |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
35 # XXX threading.local keeps the local of the last stopped thread alive. |
|
44763
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
36 deadlist = [weak for weak in weaklist if weak() is None] |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
37 self.assertEqual(len(deadlist), n-1) |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
38 |
|
44764
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
39 # Assignment to the same thread local frees it sometimes (!) |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
40 local.someothervar = None |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
41 gc.collect() |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
42 deadlist = [weak for weak in weaklist if weak() is None] |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
43 self.assert_(len(deadlist) in (n-1, n), (n, len(deadlist))) |
|
52e821418a79
Update for threading.local test.
Christian Heimes <christian@cheimes.de>
parents:
44763
diff
changeset
|
44 |
|
47859
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
45 def test_derived(self): |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
46 # Issue 3088: if there is a threads switch inside the __init__ |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
47 # of a threading.local derived class, the per-thread dictionary |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
48 # is created but not correctly set on the object. |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
49 # The first member set may be bogus. |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
50 import time |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
51 class Local(threading.local): |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
52 def __init__(self): |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
53 time.sleep(0.01) |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
54 local = Local() |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
55 |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
56 def f(i): |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
57 local.x = i |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
58 # Simply check that the variable is correctly set |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
59 self.assertEqual(local.x, i) |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
60 |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
61 threads= [] |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
62 for i in range(10): |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
63 t = threading.Thread(target=f, args=(i,)) |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
64 t.start() |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
65 threads.append(t) |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
66 |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
67 for t in threads: |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
68 t.join() |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
69 |
|
8ccb35739a96
#Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44764
diff
changeset
|
70 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
71 def test_main(): |
|
44763
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
72 suite = unittest.TestSuite() |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
73 suite.addTest(DocTestSuite('_threading_local')) |
|
73b5e939e058
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?
Christian Heimes <christian@cheimes.de>
parents:
41554
diff
changeset
|
74 suite.addTest(unittest.makeSuite(ThreadingLocalTest)) |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
75 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
76 try: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
77 from thread import _local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
78 except ImportError: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
79 pass |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
80 else: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
81 import _threading_local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
82 local_orig = _threading_local.local |
|
33313
542a5efa4883
setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents:
32673
diff
changeset
|
83 def setUp(test): |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
84 _threading_local.local = _local |
|
33313
542a5efa4883
setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents:
32673
diff
changeset
|
85 def tearDown(test): |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
86 _threading_local.local = local_orig |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
87 suite.addTest(DocTestSuite('_threading_local', |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
88 setUp=setUp, tearDown=tearDown) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
89 ) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
90 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
33313
diff
changeset
|
91 test_support.run_unittest(suite) |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
92 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
93 if __name__ == '__main__': |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
diff
changeset
|
94 test_main() |
