Illuminating the problems with threads. #6
authorDaniel Greenfeld <pydanny@gmail.com>
Mon, 19 May 2014 15:49:00 +0000 (08:49 -0700)
committerDaniel Greenfeld <pydanny@gmail.com>
Mon, 19 May 2014 15:49:00 +0000 (08:49 -0700)
README.rst
tests/test_cached_property.py

index 108667241d5e09fc6e27c8ed209bcea9a1100b5a..a447a7cb426df263fb86333912bc7df065402201 100644 (file)
@@ -104,7 +104,10 @@ Results of cached functions can be invalidated by outside forces. Let's demonstr
     >>> m.boardwalk
     600
 
+Warning
+-------
 
+This library currently doesn't work with threads. Please see https://github.com/pydanny/cached-property/issues/6.
 
 Credits
 --------
index b4f062f5a3f22562e77139f7df613f1fc2b187f8..ef89730fd958bc74554e035b6400824e5643c511 100755 (executable)
@@ -83,28 +83,37 @@ class TestCachedProperty(unittest.TestCase):
         # Run standard cache assertion
         self.assertEqual(c.add_cached, None)
 
-    # def test_threads(self):
-    #     """ How well does this implementation work with threads?"""
 
-    #     class Check(object):
+class TestThreadingIssues(unittest.TestCase):
 
-    #         def __init__(self):
-    #             self.total = 0
+    def test_threads(self):
+        """ How well does this implementation work with threads?"""
 
-    #         @cached_property
-    #         def add_cached(self):
-    #             sleep(1)
-    #             self.total += 1
-    #             return self.total
+        class Check(object):
 
-    #     c = Check()
-    #     threads = []
-    #     for x in range(10):
-    #         thread = Thread(target=lambda: c.add_cached)
-    #         thread.start()
-    #         threads.append(thread)
+            def __init__(self):
+                self.total = 0
 
-    #     for thread in threads:
-    #         thread.join()
+            @cached_property
+            def add_cached(self):
+                sleep(1)
+                self.total += 1
+                return self.total
 
-    #     self.assertEqual(c.add_cached, 1)
+        c = Check()
+        threads = []
+        for x in range(10):
+            thread = Thread(target=lambda: c.add_cached)
+            thread.start()
+            threads.append(thread)
+
+        for thread in threads:
+            thread.join()
+
+        # TODO: This assertion should be working.
+        # See https://github.com/pydanny/cached-property/issues/6
+        # self.assertEqual(c.add_cached, 1)
+
+        # TODO: This assertion should be failing.
+        # See https://github.com/pydanny/cached-property/issues/6
+        self.assertEqual(c.add_cached, 10)