Skip to content

Commit

Permalink
Add a test demonstrating the need for RLock in func.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
thetorpedodog authored and tkem committed Dec 9, 2020
1 parent 6d2692f commit 5085c57
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ def test_decorator_user_function(self):
self.assertEqual(cached(1.0), 1.0)
self.assertEqual(cached.cache_info(), (2, 1, 128, 1))

def test_decorator_needs_rlock(self):
cached = self.decorator(lambda n: n)

class RecursiveEquals:
def __init__(self, use_cache):
self._use_cache = use_cache

def __hash__(self):
return hash(self._use_cache)

def __eq__(self, other):
if self._use_cache:
# This call will happen while the cache-lock is held,
# requiring a reentrant lock to avoid deadlock.
cached(self)
return self._use_cache == other._use_cache

# Prime the cache.
cached(RecursiveEquals(False))
cached(RecursiveEquals(True))
# Then do a call which will cause a deadlock with a non-reentrant lock.
self.assertEqual(cached(RecursiveEquals(True)), RecursiveEquals(True))


class FIFODecoratorTest(unittest.TestCase, DecoratorTestMixin):

Expand Down

0 comments on commit 5085c57

Please sign in to comment.