2008-08-16 23:26:59 +08:00
|
|
|
import py
|
2009-10-03 07:47:39 +08:00
|
|
|
from _py.path import cacheutil
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
class BasicCacheAPITest:
|
|
|
|
cache = None
|
|
|
|
def test_getorbuild(self):
|
|
|
|
val = self.cache.getorbuild(-42, lambda: 42)
|
|
|
|
assert val == 42
|
|
|
|
val = self.cache.getorbuild(-42, lambda: 23)
|
|
|
|
assert val == 42
|
|
|
|
|
|
|
|
def test_cache_get_key_error(self):
|
2009-08-25 22:14:20 +08:00
|
|
|
py.test.raises(KeyError, "self.cache._getentry(-23)")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_delentry_non_raising(self):
|
|
|
|
val = self.cache.getorbuild(100, lambda: 100)
|
|
|
|
self.cache.delentry(100)
|
2009-08-25 22:14:20 +08:00
|
|
|
py.test.raises(KeyError, "self.cache._getentry(100)")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_delentry_raising(self):
|
|
|
|
val = self.cache.getorbuild(100, lambda: 100)
|
|
|
|
self.cache.delentry(100)
|
|
|
|
py.test.raises(KeyError, "self.cache.delentry(100, raising=True)")
|
|
|
|
|
|
|
|
class TestBuildcostAccess(BasicCacheAPITest):
|
2009-08-25 22:14:20 +08:00
|
|
|
cache = cacheutil.BuildcostAccessCache(maxentries=128)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-08-25 22:14:20 +08:00
|
|
|
def test_cache_works_somewhat_simple(self, monkeypatch):
|
|
|
|
cache = cacheutil.BuildcostAccessCache()
|
|
|
|
# the default gettime
|
2008-10-03 00:19:04 +08:00
|
|
|
# BuildcostAccessCache.build can
|
|
|
|
# result into time()-time() == 0 which makes the below
|
|
|
|
# test fail randomly. Let's rather use incrementing
|
|
|
|
# numbers instead.
|
2009-09-01 01:51:25 +08:00
|
|
|
l = [0]
|
|
|
|
def counter():
|
|
|
|
l[0] = l[0] + 1
|
|
|
|
return l[0]
|
|
|
|
monkeypatch.setattr(cacheutil, 'gettime', counter)
|
2008-08-16 23:26:59 +08:00
|
|
|
for x in range(cache.maxentries):
|
|
|
|
y = cache.getorbuild(x, lambda: x)
|
|
|
|
assert x == y
|
|
|
|
for x in range(cache.maxentries):
|
|
|
|
assert cache.getorbuild(x, None) == x
|
2009-09-01 01:51:25 +08:00
|
|
|
halfentries = int(cache.maxentries / 2)
|
|
|
|
for x in range(halfentries):
|
2008-08-16 23:26:59 +08:00
|
|
|
assert cache.getorbuild(x, None) == x
|
|
|
|
assert cache.getorbuild(x, None) == x
|
2009-08-25 22:14:20 +08:00
|
|
|
# evict one entry
|
|
|
|
val = cache.getorbuild(-1, lambda: 42)
|
2008-08-16 23:26:59 +08:00
|
|
|
assert val == 42
|
|
|
|
# check that recently used ones are still there
|
|
|
|
# and are not build again
|
2009-09-01 01:51:25 +08:00
|
|
|
for x in range(halfentries):
|
2008-08-16 23:26:59 +08:00
|
|
|
assert cache.getorbuild(x, None) == x
|
2009-08-25 22:14:20 +08:00
|
|
|
assert cache.getorbuild(-1, None) == 42
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestAging(BasicCacheAPITest):
|
2009-02-27 18:18:27 +08:00
|
|
|
maxsecs = 0.10
|
2009-08-25 22:14:20 +08:00
|
|
|
cache = cacheutil.AgingCache(maxentries=128, maxseconds=maxsecs)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_cache_eviction(self):
|
|
|
|
self.cache.getorbuild(17, lambda: 17)
|
|
|
|
endtime = py.std.time.time() + self.maxsecs * 10
|
|
|
|
while py.std.time.time() < endtime:
|
2009-08-25 22:14:20 +08:00
|
|
|
try:
|
|
|
|
self.cache._getentry(17)
|
|
|
|
except KeyError:
|
2008-08-16 23:26:59 +08:00
|
|
|
break
|
|
|
|
py.std.time.sleep(self.maxsecs*0.3)
|
|
|
|
else:
|
|
|
|
py.test.fail("waiting for cache eviction failed")
|
|
|
|
|
|
|
|
def test_prune_lowestweight():
|
|
|
|
maxsecs = 0.05
|
2009-08-25 22:14:20 +08:00
|
|
|
cache = cacheutil.AgingCache(maxentries=10, maxseconds=maxsecs)
|
2008-08-16 23:26:59 +08:00
|
|
|
for x in range(cache.maxentries):
|
|
|
|
cache.getorbuild(x, lambda: x)
|
|
|
|
py.std.time.sleep(maxsecs*1.1)
|
|
|
|
cache.getorbuild(cache.maxentries+1, lambda: 42)
|