diff options
author | Raymond Hettinger <python@rcn.com> | 2013-03-04 07:52:50 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2013-03-04 07:52:50 (GMT) |
commit | 03923426733dcc9f6c998057114212c168c3b1cd (patch) | |
tree | b12d625b6dcaef119588e00deb8b9c79ca2920ef /Lib | |
parent | ad246bfb712f04b34e993b0e70e470f6d759e736 (diff) | |
download | cpython-03923426733dcc9f6c998057114212c168c3b1cd.zip cpython-03923426733dcc9f6c998057114212c168c3b1cd.tar.gz cpython-03923426733dcc9f6c998057114212c168c3b1cd.tar.bz2 |
Add test for RLock in the lru_cache().
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_functools.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index b3803da..db1e934 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -777,6 +777,31 @@ class TestLRU(unittest.TestCase): self.assertEqual(square.cache_info().hits, 4) self.assertEqual(square.cache_info().misses, 4) + def test_need_for_rlock(self): + # This will deadlock on an LRU cache that uses a regular lock + + @functools.lru_cache(maxsize=10) + def test_func(x): + 'Used to demonstrate a reentrant lru_cache call within a single thread' + return x + + class DoubleEq: + 'Demonstrate a reentrant lru_cache call within a single thread' + def __init__(self, x): + self.x = x + def __hash__(self): + return self.x + def __eq__(self, other): + if self.x == 2: + test_func(DoubleEq(1)) + return self.x == other.x + + test_func(DoubleEq(1)) # Load the cache + test_func(DoubleEq(2)) # Load the cache + self.assertEqual(test_func(DoubleEq(2)), # Trigger a re-entrant __eq__ call + DoubleEq(2)) # Verify the correct return value + + def test_main(verbose=None): test_classes = ( TestPartial, |