From d92311f798b21dfe3c59e7129703271ff4b35047 Mon Sep 17 00:00:00 2001 From: Ivan Tyagov <ivan@nexedi.com> Date: Tue, 30 Oct 2012 14:02:38 +0200 Subject: [PATCH] Add set and get API testing and a short algorithm optimization comment. --- product/ERP5Type/Core/CacheBag.py | 5 +++ product/ERP5Type/tests/testCacheTool.py | 43 ++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/product/ERP5Type/Core/CacheBag.py b/product/ERP5Type/Core/CacheBag.py index 3ec6230804..14e2a34e6d 100644 --- a/product/ERP5Type/Core/CacheBag.py +++ b/product/ERP5Type/Core/CacheBag.py @@ -61,6 +61,11 @@ class CacheBag(CacheFactory): value = data_dict.getValue() if ram_cache_factory_plugin_list.index(cache_plugin)>0: # update first plugin as it's the one to be used + # XXX: JPS we can have different update policy here based on a project requirements. + # c0 c1 c2....cN where c0 is filled from cN + # c1.... cN-1 untouched then rotate i -> i+1 + # this way you can create "groups of caches" per date and trash old stuff + # instead of using 2x more disk space, you can use 1/N more disk space cache_duration = self.getRamCacheFactory().cache_duration ram_cache_factory_plugin_list[0].set(cache_id, DEFAULT_CACHE_SCOPE, value, cache_duration) return value diff --git a/product/ERP5Type/tests/testCacheTool.py b/product/ERP5Type/tests/testCacheTool.py index 341db12f48..b336504448 100644 --- a/product/ERP5Type/tests/testCacheTool.py +++ b/product/ERP5Type/tests/testCacheTool.py @@ -484,7 +484,8 @@ return 'a' * 1024 * 1024 * 25 print "\n\tCalculation time (3rd call)", calculation_time def test_06_CheckCacheBag(self): - """Check Cache Bag + """ + Check Cache Bag """ portal_caches = self.portal.portal_caches @@ -516,6 +517,46 @@ return 'a' * 1024 * 1024 * 25 self.assertEqual('value_for_y', cache_bag.get('y')) self.assertEqual('value_for_y', ram_cache_factory_plugin_list[0].get('y',DEFAULT_CACHE_SCOPE).getValue()) + def test_07_CheckCacheFactory(self): + """ + Check Cache Factory set and get API. + """ + portal_caches = self.portal.portal_caches + + cache_factory = portal_caches.newContent(portal_type="Cache Factory", + cache_duration=3600) + + cache_plugin1 = cache_factory.newContent(portal_type="Ram Cache") + cache_plugin1.setIntIndex(0) + + cache_bag1 = cache_factory.newContent(portal_type="Cache Bag", + cache_duration=3600) + cache_bag1.setIntIndex(1) + ram_cache1 = cache_bag1.newContent(portal_type="Ram Cache") + ram_cache2 = cache_bag1.newContent(portal_type="Ram Cache") + self.tic() + + # test get / set API + cache_factory.set('x', 'value_for_x') + self.assertEqual('value_for_x', cache_factory.get('x')) + + # test that all cache plugin have this set + self.assertEqual('value_for_x', cache_plugin1.get('x')) + self.assertEqual('value_for_x', cache_bag1.get('x')) + + # test set on individual cache plugin as this cache plugin has highest priority + # it will affect what root Cache Factory returns + cache_plugin1.set('x', 'new_value_for_x') + self.assertEqual('new_value_for_x', cache_plugin1.get('x')) + self.assertEqual('new_value_for_x', cache_factory.get('x')) + # others cache plugins will remain with old value until ... + self.assertEqual('value_for_x', cache_bag1.get('x')) + # .. root Cache Factory set will update all + cache_factory.set('x', 'new_value_for_x') + self.assertEqual(cache_factory.get('x'), cache_plugin1.get('x')) + self.assertEqual(cache_plugin1.get('x'), cache_bag1.get('x')) + self.assertEqual('new_value_for_x', cache_factory.get('x')) + def test_99_CachePluginInterface(self): """Test Class against Interface """ -- 2.30.9