def test_cache_expire(self): cache_expire_second = 3 test_cache_mgr = CacheMgr.instance(TestLoader, cache_expire_second) test_set_key = random_str(3) self.assertTrue(test_cache_mgr.get(test_set_key) == test_data) time.sleep(cache_expire_second * 2) self.assertTrue(test_cache_mgr.has(test_set_key) is None) self.assertTrue(test_cache_mgr.get(test_set_key) == test_data)
def test_cache_expire_extend(self): cache_expire_second = 3 test_cache_mgr = CacheMgr.instance(TestLoader, cache_expire_second) test_set_key = random_str(3) self.assertTrue(test_cache_mgr.get(test_set_key) == test_data) reget_times = 3 for _ in xrange(reget_times): time.sleep(cache_expire_second) test_cache_mgr.get(test_set_key) self.assertTrue(test_cache_mgr.has(test_set_key))
test_data = {random_str(): random_str(), random_str(): random_str()} class TestLoader(CacheLoader): @classmethod def load(cls, key): return test_data @classmethod def load_ls(cls, key_ls, **kwargs): return [test_data for _ in key_ls] @classmethod def check(cls, data): assert isinstance(data, dict) test_cache_mgr = CacheMgr.instance(TestLoader) class CacheTest(unittest.TestCase): # 初始化工作 def setUp(self): pass # 退出清理工作 def tearDown(self): pass @unittest_adaptor() def test_cache_set(self): test_set_key = random_str(3) self.assertTrue(test_cache_mgr.get(test_set_key) == test_data)