summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_functools.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-01-12 17:12:21 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2017-01-12 17:12:21 (GMT)
commit42e1ea9a10aa6c3df40af3b7561d6251c8e2ac9c (patch)
treed54140f730a38805fa55a7a5cc90b0e894d91318 /Lib/test/test_functools.py
parent12c4aba1a0fbd934a66d6b97c29c36d7de14e755 (diff)
parent67796521dd22b3008788a75108b45f33d06f85dd (diff)
downloadcpython-42e1ea9a10aa6c3df40af3b7561d6251c8e2ac9c.zip
cpython-42e1ea9a10aa6c3df40af3b7561d6251c8e2ac9c.tar.gz
cpython-42e1ea9a10aa6c3df40af3b7561d6251c8e2ac9c.tar.bz2
Issue #28969: Fixed race condition in C implementation of functools.lru_cache.
KeyError could be raised when cached function with full cache was simultaneously called from differen threads with the same uncached arguments.
Diffstat (limited to 'Lib/test/test_functools.py')
-rw-r--r--Lib/test/test_functools.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 27eb576..824549b 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -7,6 +7,7 @@ import pickle
from random import choice
import sys
from test import support
+import time
import unittest
from weakref import proxy
import contextlib
@@ -1401,6 +1402,20 @@ class TestLRU:
pause.reset()
self.assertEqual(f.cache_info(), (0, (i+1)*n, m*n, i+1))
+ @unittest.skipUnless(threading, 'This test requires threading.')
+ def test_lru_cache_threaded3(self):
+ @self.module.lru_cache(maxsize=2)
+ def f(x):
+ time.sleep(.01)
+ return 3 * x
+ def test(i, x):
+ with self.subTest(thread=i):
+ self.assertEqual(f(x), 3 * x, i)
+ threads = [threading.Thread(target=test, args=(i, v))
+ for i, v in enumerate([1, 2, 2, 3, 2])]
+ with support.start_threads(threads):
+ pass
+
def test_need_for_rlock(self):
# This will deadlock on an LRU cache that uses a regular lock