From 9557d08757da827642d5a6115033b6de26b223e7 Mon Sep 17 00:00:00 2001 From: George Sakkis Date: Mon, 20 Apr 2015 01:19:17 +0300 Subject: [PATCH] Fix threaded_cached_property_with_ttl. --- cached_property.py | 7 ----- tests/test_cached_property_ttl.py | 52 ++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/cached_property.py b/cached_property.py index 50b40ac..f05947e 100644 --- a/cached_property.py +++ b/cached_property.py @@ -121,13 +121,6 @@ class threaded_cached_property_with_ttl(cached_property_with_ttl): def __get__(self, obj, cls): with self.lock: - # Double check if the value was computed before the lock was - # acquired. - prop_name = self.__name__ - if hasattr(obj, '_cache') and prop_name in obj._cache: - return obj._cache[prop_name][0] - - # If not, do the calculation and release the lock. return super(threaded_cached_property_with_ttl, self).__get__(obj, cls) diff --git a/tests/test_cached_property_ttl.py b/tests/test_cached_property_ttl.py index 343ca2e..26f637e 100644 --- a/tests/test_cached_property_ttl.py +++ b/tests/test_cached_property_ttl.py @@ -1,12 +1,6 @@ # -*- coding: utf-8 -*- -""" -test_threaded_cache_property.py ----------------------------------- - -Tests for `cached-property` module, cached_property_with_ttl. -Tests for `cached-property` module, threaded_cache_property_with_ttl. -""" +"""Tests for cached_property_with_ttl and threaded_cache_property_with_ttl""" import unittest from freezegun import freeze_time @@ -194,6 +188,14 @@ class TestThreadedCachedPropertyWithTTL(unittest.TestCase): self.assertEqual(c.add_cached, 1) self.assertEqual(c.add_cached, 1) + # Cannot expire the cache. + with freeze_time("9999-01-01"): + self.assertEqual(c.add_cached, 1) + + # It's customary for descriptors to return themselves if accessed + # though the class, rather than through an instance. + self.assertTrue(isinstance(Check.add_cached, threaded_cached_property_with_ttl)) + def test_reset_cached_property(self): class Check(object): @@ -264,3 +266,39 @@ class TestThreadedCachedPropertyWithTTL(unittest.TestCase): thread.join() self.assertEqual(c.add_cached, 1) + + def test_ttl_expiry(self): + + class Check(object): + + def __init__(self): + self.total = 0 + self.lock = Lock() + + @threaded_cached_property_with_ttl(ttl=100000) + def add_cached(self): + sleep(1) + # Need to guard this since += isn't atomic. + with self.lock: + self.total += 1 + return self.total + + def run_threads(check, num_threads=10): + threads = [] + for _ in range(num_threads): + thread = Thread(target=lambda: check.add_cached) + thread.start() + threads.append(thread) + for thread in threads: + thread.join() + + c = Check() + run_threads(c) + self.assertEqual(c.add_cached, 1) + + # Expire the cache. + with freeze_time("9999-01-01"): + run_threads(c) + self.assertEqual(c.add_cached, 2) + + self.assertEqual(c.add_cached, 2) -- 2.30.2