def test_peek_last_item_ttl(self): l = TTLRU(2) l.set_with_ttl(0, 0, int(80e6)) l.set_with_ttl(1, 1, int(20e6)) self.assertEqual(l.peek_first_item(), (1, 1)) self.assertEqual(l.peek_last_item(), (0, 0)) time.sleep(0.01) # approximately 0.01s self.assertEqual(l.peek_last_item(), (0, 0)) time.sleep(0.01) # approximately 0.02s self.assertEqual(l.peek_last_item(), (0, 0)) time.sleep(0.05) # approximately 0.07s self.assertEqual(l.peek_last_item(), (0, 0)) time.sleep(0.01) # approximately 0.08s self.assertEqual(l.peek_first_item(), None) l.set_with_ttl(0, 0, int(10e6)) l.set_with_ttl(1, 1, int(20e6)) self.assertEqual(l.peek_last_item(), (0, 0)) time.sleep(0.01) # approximately 0.01s self.assertEqual(l.peek_last_item(), (1, 1)) time.sleep(0.01) # approximately 0.02s self.assertEqual(l.peek_first_item(), None)
def test_update(self): l = TTLRU(2) l['a'] = 1 self.assertEqual(l['a'], 1) l.update(a=2) self.assertEqual(l['a'], 2) l['b'] = 2 self.assertEqual(l['b'], 2) l.update(b=3) self.assertEqual(('b', 3), l.peek_first_item()) self.assertEqual(l['a'], 2) self.assertEqual(l['b'], 3) l.update({'a': 1, 'b': 2}) self.assertEqual(('b', 2), l.peek_first_item()) self.assertEqual(l['a'], 1) self.assertEqual(l['b'], 2) l.update() self.assertEqual(('b', 2), l.peek_first_item()) l.update(a=2) self.assertEqual(('a', 2), l.peek_first_item())
def test_ref_count(self): l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l.get(1) self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l.peek_first_item() self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l.peek_last_item() self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l.keys() self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l[1] = 2 self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) 1 in l self.assertEqual(sys.getrefcount(x), 2) l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) try: l[1] except: pass self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) l[1] = 2 self.assertEqual(sys.getrefcount(x), 2)
def test_peek_first_item(self): l = TTLRU(2) self.assertEqual(None, l.peek_first_item()) l[1] = '1' l[2] = '2' self.assertEqual((2, '2'), l.peek_first_item())