def test_max_object_size(self): from wesgi import LRUCache cache = LRUCache(maxsize=3, max_object_size=1000) # objects larger than max_object_size are ignored cache.set('a', 'a' * 2000) self.assertEquals(cache._cache, {}) # smaller objects are not cache.set('a', 'a') self.assertEquals(cache._cache, {'a': 'a'})
def test_queue_emptying_memory_leak(self): # When we empty the queue, we need to make sure that elements in our cache stay in the queue from wesgi import LRUCache cache = LRUCache(maxsize=10) cache.set('x', 'y') [cache.get(i) for i in range(100)] # fill queue over maxsize self.assertEquals(cache._cache, {'x': 'y'}) self.assertEquals(len(cache._queue), 80) self.assertEquals(list(cache._queue), ['x'] + range(21, 100)) expected_refcount = dict([(i, 1) for i in range(21, 100)]) expected_refcount['x'] = 1 self.assertEquals(cache._refcount, expected_refcount) self.assertInvariants(cache)
def test_hit_miss(self): # an LRU's biggest weakness is the sequential scan # this is what happens from wesgi import LRUCache cache = LRUCache(maxsize=3) cache.get('a') cache.set('a', 'a') self.assertEquals(cache.hits, 0) self.assertEquals(cache.misses, 1) cache.get('a') self.assertEquals(cache.hits, 1) self.assertEquals(cache.misses, 1) cache.get('b') self.assertEquals(cache.hits, 1) self.assertEquals(cache.misses, 2) cache.get('a') self.assertEquals(cache.hits, 2) self.assertEquals(cache.misses, 2) self.assertInvariants(cache)
def test_repeated_set_without_get_does_not_flushe_cache(self): from wesgi import LRUCache cache = LRUCache(maxsize=3) cache.get('a') cache.set('a', 'a') cache.get('b') cache.set('b', 'b') for i in range(100): cache.set(str(i), str(i)) self.assertEquals(cache._cache, {'99': '99', 'a': 'a', 'b': 'b'}) self.assertInvariants(cache)
def test_repeated_get_and_set_flushes_cache(self): # an LRU's biggest weakness is the sequential scan # this is what happens from wesgi import LRUCache cache = LRUCache(maxsize=3) cache.get('a') cache.set('a', 'a') cache.get('b') cache.set('b', 'b') for i in range(100): cache.get(str(i)) cache.set(str(i), str(i)) self.assertEquals(cache._cache, {'99': '99', '98': '98', '97': '97'}) self.assertEquals(cache.hits, 0) self.assertEquals(cache.misses, 102) self.assertInvariants(cache)
def test_basic(self): from wesgi import LRUCache cache = LRUCache() self.assertEquals(cache.get('a'), None) self.assertEquals(cache.get('b'), None) self.assertEquals(cache._refcount, {'a': 1, 'b': 1}) self.assertEquals(cache._cache, {}) cache.set('a', 'x') self.assertInvariants(cache) self.assertEquals(cache.get('a'), 'x') self.assertEquals(cache.get('b'), None) self.assertEquals(cache._refcount, {'a': 3, 'b': 2}) self.assertEquals(cache._cache, {'a': 'x'}) cache.set('b', 'y') self.assertInvariants(cache) self.assertEquals(cache.get('a'), 'x') self.assertEquals(cache.get('b'), 'y') self.assertEquals(cache._refcount, {'a': 4, 'b': 4}) self.assertEquals(cache._cache, {'a': 'x', 'b': 'y'}) cache.set('b', 'z') self.assertInvariants(cache) self.assertEquals(cache.get('a'), 'x') self.assertEquals(cache.get('b'), 'z') self.assertEquals(cache._refcount, {'a': 5, 'b': 6}) self.assertEquals(cache._cache, {'a': 'x', 'b': 'z'}) cache.delete('b') self.assertInvariants(cache) self.assertEquals(cache._refcount, {'a': 5, 'b': 6}) self.assertEquals(cache.get('a'), 'x') self.assertEquals(cache.get('b'), None) self.assertEquals(cache._cache, {'a': 'x'}) cache.delete('a') self.assertInvariants(cache) self.assertEquals(cache._refcount, {'a': 6, 'b': 7}) self.assertEquals(cache.get('a'), None) self.assertEquals(cache.get('b'), None) self.assertEquals(cache._cache, {})