summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2018-12-11 20:57:05 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2018-12-11 21:01:27 -0500
commit0ef761550ef623506f3cd050b4120e78193d1780 (patch)
treea1169bda392fe617a94728505b30fa5ef991f205 /tests
parent01bc28dba446b29edee313b652e2884bbf895c2d (diff)
downloaddogpile-cache-0ef761550ef623506f3cd050b4120e78193d1780.tar.gz
Restore the API for async_creation_runner in all cases
Fixed regression in 0.7.0 caused by :ticket:`136` where the assumed arguments for the :paramref:`.CacheRegion.async_creation_runner` expanded to include the new :paramref:`.CacheRegion.get_or_create.creator_args` parameter, as it was not tested that the async runner would be implicitly called with these arguments when the :meth:`.CacheRegion.cache_on_arguments` decorator was used. The exact signature of ``async_creation_runner`` is now restored to have the same arguments in all cases. Fixes: #139 Change-Id: I91c95b183e01d1bbe1c42b3375fcfa65866cc97d
Diffstat (limited to 'tests')
-rw-r--r--tests/cache/test_region.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/cache/test_region.py b/tests/cache/test_region.py
index e042c89..c04ea3e 100644
--- a/tests/cache/test_region.py
+++ b/tests/cache/test_region.py
@@ -11,6 +11,7 @@ from . import eq_, is_, assert_raises_message, io, configparser
import time
import datetime
import itertools
+import mock
from collections import defaultdict
from ._fixtures import MockBackend
@@ -528,6 +529,79 @@ class CustomInvalidationStrategyTest(RegionTest):
return reg
+class AsyncCreatorTest(TestCase):
+ def _fixture(self):
+
+ def async_creation_runner(cache, somekey, creator, mutex):
+ try:
+ value = creator()
+ cache.set(somekey, value)
+ finally:
+ mutex.release()
+
+ return mock.Mock(side_effect=async_creation_runner)
+
+ def test_get_or_create(self):
+ acr = self._fixture()
+ reg = CacheRegion(async_creation_runner=acr)
+ reg.configure("mock", expiration_time=.2)
+
+ def some_value():
+ return "some value"
+
+ def some_new_value():
+ return "some new value"
+
+ eq_(reg.get_or_create("some key", some_value), "some value")
+ time.sleep(.5)
+ eq_(
+ reg.get_or_create("some key", some_new_value),
+ "some value")
+ eq_(
+ reg.get_or_create("some key", some_new_value),
+ "some new value")
+ eq_(
+ acr.mock_calls, [
+ mock.call(reg, "some key",
+ some_new_value, reg._mutex("some key"))
+ ])
+
+ def test_fn_decorator(self):
+ acr = self._fixture()
+ reg = CacheRegion(async_creation_runner=acr)
+ reg.configure("mock", expiration_time=5)
+
+ canary = mock.Mock()
+
+ @reg.cache_on_arguments()
+ def go(x, y):
+ canary(x, y)
+ return x + y
+
+ eq_(go(1, 2), 3)
+ eq_(go(1, 2), 3)
+
+ eq_(canary.mock_calls, [mock.call(1, 2)])
+
+ eq_(go(3, 4), 7)
+
+ eq_(canary.mock_calls, [mock.call(1, 2), mock.call(3, 4)])
+
+ reg.invalidate(hard=False)
+
+ eq_(go(1, 2), 3)
+
+ eq_(canary.mock_calls, [
+ mock.call(1, 2), mock.call(3, 4), mock.call(1, 2)])
+
+ eq_(
+ acr.mock_calls, [
+ mock.call(reg, "tests.cache.test_region:go|1 2",
+ mock.ANY,
+ reg._mutex("tests.cache.test_region:go|1 2"))
+ ])
+
+
class ProxyBackendTest(TestCase):
class GetCounterProxy(ProxyBackend):