From dcee56c0401854f98e677b53a77bc371b5d0001d Mon Sep 17 00:00:00 2001 From: Daniel Greenfeld Date: Mon, 19 May 2014 08:49:00 -0700 Subject: [PATCH] Illuminating the problems with threads. #6 --- README.rst | 3 +++ tests/test_cached_property.py | 47 +++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/README.rst b/README.rst index 1086672..a447a7c 100644 --- a/README.rst +++ b/README.rst @@ -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 -------- diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index b4f062f..ef89730 100755 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -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) -- 2.30.2