def test_lru(self): l = TTLRU(1) l['a'] = 1 l['a'] self.assertEqual(l.keys(), ['a']) l['b'] = 2 self.assertEqual(l.keys(), ['b']) l = TTLRU(2) l['a'] = 1 l['b'] = 2 self.assertEqual(len(l), 2) l['a'] # Testing the first one l['c'] = 3 self.assertEqual(sorted(l.keys()), ['a', 'c']) l['c'] self.assertEqual(sorted(l.keys()), ['a', 'c']) l = TTLRU(3) l['a'] = 1 l['b'] = 2 l['c'] = 3 self.assertEqual(len(l), 3) l['b'] # Testing the middle one l['d'] = 4 self.assertEqual(sorted(l.keys()), ['b', 'c', 'd']) l['d'] # Testing the last one self.assertEqual(sorted(l.keys()), ['b', 'c', 'd']) l['e'] = 5 self.assertEqual(sorted(l.keys()), ['b', 'd', 'e'])
def test_callback(self): counter = [0] first_key = 'a' first_value = 1 def callback(key, value): self.assertEqual(key, first_key) self.assertEqual(value, first_value) counter[0] += 1 l = TTLRU(1, callback=callback) l[first_key] = first_value l['b'] = 1 # test calling the callback self.assertEqual(counter[0], 1) self.assertEqual(l.keys(), ['b']) l['b'] = 2 # doesn't call callback self.assertEqual(counter[0], 1) self.assertEqual(l.keys(), ['b']) self.assertEqual(l.values(), [2]) l = TTLRU(1, callback=callback) l[first_key] = first_value l.set_callback(None) l['c'] = 1 # doesn't call callback self.assertEqual(counter[0], 1) self.assertEqual(l.keys(), ['c']) l.set_callback(callback) del l['c'] # doesn't call callback self.assertEqual(counter[0], 1) self.assertEqual(l.keys(), []) l = TTLRU(2, callback=callback) l['a'] = 1 # test calling the callback l['b'] = 2 # test calling the callback self.assertEqual(counter[0], 1) self.assertEqual(l.keys(), ['b', 'a']) l.set_size(1) self.assertEqual(counter[0], 2) # callback invoked self.assertEqual(l.keys(), ['b'])
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_empty(self): l = TTLRU(1) self.assertEqual([], l.keys()) self.assertEqual([], l.values())