def test_key_expiration(self): clock = FakeClock(0.0) cache = utils.ExpiringCache(ttl=1.0, clock=clock) cache['the answer'] = 42 clock.now = 0.999999 self.assertEqual(42, cache['the answer']) clock.now = 1.0 self.assertEqual(None, cache.get('the answer')) clock.now = 1.000001 self.assertEqual(None, cache.get('the answer'))
def test_nonzero_full_expiration(self): clock = FakeClock(0.0) cache = utils.ExpiringCache(ttl=1.0, clock=clock) ITEMS = 10 for i in range(ITEMS): cache[i] = 'foobar-%d' % i self.assertTrue(cache) clock.now = 1.1 self.assertFalse(cache)
def test_nonzero_partial_expiration(self): clock = FakeClock(0.0) cache = utils.ExpiringCache(ttl=2.0, clock=clock) cache['a'] = 1 clock.now = 1.0 self.assertTrue(cache) cache['b'] = 2 clock.now = 2.0 self.assertTrue(cache) clock.now = 3.0 self.assertFalse(cache)
def setUp(self): self.cache = utils.ExpiringCache(ttl=20)