Fix threaded_cached_property_with_ttl.
authorGeorge Sakkis <george.sakkis@gmail.com>
Sun, 19 Apr 2015 22:19:17 +0000 (01:19 +0300)
committerGeorge Sakkis <george.sakkis@gmail.com>
Mon, 20 Apr 2015 01:06:40 +0000 (04:06 +0300)
cached_property.py
tests/test_cached_property_ttl.py

index 50b40ac09f114f374e9fec73d8c98c3fc6469786..f05947e10e4783ed2808ba68e044adb4e4062c16 100644 (file)
@@ -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)
 
index 343ca2e8278c6963544c7f13229999d58916a576..26f637e335d91fb4eae8e633862d23da4bb82f65 100644 (file)
@@ -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)