def test_dupe_config(self): reg = CacheRegion() reg.configure("mock") assert_raises_message( Exception, "This region is already configured", reg.configure, "mock" )
def test_dupe_config(self): reg = CacheRegion() reg.configure("mock") assert_raises_message( exception.RegionAlreadyConfigured, "This region is already configured", reg.configure, "mock" ) eq_(reg.is_configured, True)
def test_replace_backend_config_with_custom_key_mangler(self): reg = CacheRegion(key_mangler=key_mangler) reg.configure("dogpile.cache.null") eq_(reg.is_configured, True) assert reg.key_mangler is key_mangler reg.configure("mock", replace_existing_backend=True) eq_(reg.is_configured, True) assert reg.key_mangler is key_mangler
def test_replace_backend_config(self): reg = CacheRegion() reg.configure("dogpile.cache.null") eq_(reg.is_configured, True) null_backend = _backend_loader.load("dogpile.cache.null") assert reg.key_mangler is null_backend.key_mangler reg.configure("mock", replace_existing_backend=True) eq_(reg.is_configured, True) assert isinstance(reg.backend, MockBackend) assert reg.key_mangler is MockBackend.key_mangler
def _region(self, backend=None, region_args={}, config_args={}): _region_args = {} # TODO: maybe we use a class-level naming convention instead # of a dict here so that arguments merge naturally for cls in reversed(self.__class__.__mro__): if "region_args" in cls.__dict__: _region_args.update(cls.__dict__["region_args"]) _region_args.update(**region_args) _config_args = self.config_args.copy() _config_args.update(config_args) def _store_keys(key): if existing_key_mangler: key = existing_key_mangler(key) self._keys.add(key) return key self._region_inst = reg = CacheRegion(**_region_args) existing_key_mangler = self._region_inst.key_mangler self._region_inst.key_mangler = _store_keys self._region_inst._user_defined_key_mangler = _store_keys reg.configure(backend or self.backend, **_config_args) return reg
def test_no_config(self): reg = CacheRegion() assert_raises_message( exception.RegionNotConfigured, "No backend is configured on this region.", getattr, reg, "backend" ) eq_(reg.is_configured, False)
def test_invalid_backend(self): reg = CacheRegion() assert_raises_message( exception.PluginNotFound, "Couldn't find cache plugin to load: unknown", reg.configure, 'unknown' ) eq_(reg.is_configured, False)
def test_fn_decorator_with_kw(self): acr = self._fixture() reg = CacheRegion(async_creation_runner=acr) reg.configure("mock", expiration_time=5) @reg.cache_on_arguments() def go(x, **kw): return x test_value = TestProxyValue("Decorator Test") self.assertRaises(ValueError, go, x=1, foo=test_value) @reg.cache_on_arguments() def go2(x): return x # keywords that match positional names can be passed result = go2(x=test_value) self.assertTrue(isinstance(result, TestProxyValue))
def _region(self, backend=None, region_args={}, config_args={}): _region_args = self.region_args.copy() _region_args.update(**region_args) _config_args = self.config_args.copy() _config_args.update(config_args) def _store_keys(key): if existing_key_mangler: key = existing_key_mangler(key) self._keys.add(key) return key self._region_inst = reg = CacheRegion(**_region_args) existing_key_mangler = self._region_inst.key_mangler self._region_inst.key_mangler = _store_keys reg.configure(backend or self.backend, **_config_args) return reg
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"), ) ], )
def test_get_or_create(self): acr = self._fixture() reg = CacheRegion(async_creation_runner=acr) reg.configure("mock", expiration_time=0.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(0.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 _region(self, init_args={}, config_args={}, backend="mock"): reg = CacheRegion(**init_args) reg.configure(backend, **config_args) return reg
def _region(self, init_args={}, config_args={}, backend="mock"): reg = CacheRegion(**init_args) config_args['wrap'] = [ProxyRegionTest.MockProxy] reg.configure(backend, **config_args) return reg
def _region(self, init_args={}, config_args={}, backend="mock"): reg = CacheRegion(**init_args) invalidator = self.CustomInvalidationStrategy() reg.configure(backend, region_invalidator=invalidator, **config_args) return reg