Esempio n. 1
0
 def test_evict_get(self, time):
     """ Cache .get() evicts value after expiration """
     cache = util.TimedCache(5)
     time.time.return_value = 0
     cache["a"] = 1
     time.time.return_value = 8
     self.assertEqual(cache.get("a", 5), 5)
Esempio n. 2
0
 def test_set_expire_clear(self, time):
     """ set_expire with 0 will evict value """
     cache = util.TimedCache(5)
     time.time.return_value = 0
     cache["a"] = 1
     cache.set_expire("a", None, 0)
     self.assertTrue("a" not in cache)
     cache.set_expire("b", None, 0)
     self.assertTrue("b" not in cache)
Esempio n. 3
0
 def test_evict(self, time):
     """ Cache evicts value after expiration """
     cache = util.TimedCache(5)
     time.time.return_value = 0
     cache["a"] = 1
     time.time.return_value = 3
     self.assertEqual(cache["a"], 1)
     time.time.return_value = 8
     with self.assertRaises(KeyError):
         cache["a"]  # pylint: disable=W0104
Esempio n. 4
0
 def test_set_expire(self, time):
     """ set_expire is calculated from now """
     cache = util.TimedCache(5)
     time.time.return_value = 0
     cache.set_expire("a", 1, 30)
     self.assertEqual(cache["a"], 1)
     time.time.return_value = 29
     self.assertEqual(cache["a"], 1)
     time.time.return_value = 31
     self.assertTrue("a" not in cache)
Esempio n. 5
0
 def test_factory_get(self):
     """ Factory function populates cache from .get() """
     cache = util.TimedCache(None, lambda a: a)
     self.assertEqual(cache.get("a"), "a")
Esempio n. 6
0
 def test_factory_none(self):
     """ When factory function returns None, no value """
     cache = util.TimedCache(None, lambda a: None)
     with self.assertRaises(KeyError):
         cache["a"]  # pylint: disable=W0104
Esempio n. 7
0
 def test_factory(self):
     """ Factory function populates cache """
     cache = util.TimedCache(None, lambda a: a)
     self.assertEqual(cache["a"], "a")
Esempio n. 8
0
 def test_cache_time_zero(self, time):
     """ Cache time of 0 never caches """
     cache = util.TimedCache(0)
     time.time.return_value = 0
     cache["a"] = 1
     self.assertTrue("a" not in cache)
Esempio n. 9
0
 def test_negative_cache_time(self):
     """ cache_time cannot be negative """
     with self.assertRaises(ValueError):
         util.TimedCache(-4)
Esempio n. 10
0
 def test_set_no_expire(self, time):
     """ set_expire with None will never expire value """
     cache = util.TimedCache(5)
     cache.set_expire("a", 1, None)
     time.time.return_value = 8
     self.assertEqual(cache["a"], 1)
Esempio n. 11
0
 def test_get(self):
     """ .get() functions as per dict """
     cache = util.TimedCache(None)
     cache["a"] = 1
     self.assertEqual(cache.get("a"), 1)
     self.assertEqual(cache.get("b", 6), 6)
Esempio n. 12
0
 def test_get(self):
     """ .get() functions as per dict """
     cache = util.TimedCache(None)
     cache['a'] = 1
     self.assertEqual(cache.get('a'), 1)
     self.assertEqual(cache.get('b', 6), 6)
Esempio n. 13
0
 def test_factory_get_none(self):
     """ Factory function populates cache from .get() """
     cache = util.TimedCache(None, lambda a: None)
     self.assertEqual(cache.get('a', 'f'), 'f')