def assertSafeIter(self, method, interval=0.01, size=10000): if sys.version_info >= (3, 5): raise SkipTest('Fails on Py3.5') from threading import Thread, Event from time import sleep x = LRUCache(size) x.update(zip(range(size), range(size))) class Burglar(Thread): def __init__(self, cache): self.cache = cache self.__is_shutdown = Event() self.__is_stopped = Event() Thread.__init__(self) def run(self): while not self.__is_shutdown.isSet(): try: self.cache.popitem(last=False) except KeyError: break self.__is_stopped.set() def stop(self): self.__is_shutdown.set() self.__is_stopped.wait() self.join(THREAD_TIMEOUT_MAX) burglar = Burglar(x) burglar.start() try: for _ in getattr(x, method)(): sleep(0.0001) finally: burglar.stop()
def test_update_expires(self): limit = 100 x = LRUCache(limit=limit) slots = list(range(limit * 2)) for i in slots: x.update({i: i}) self.assertListEqual(list(x.keys()), list(slots[limit:]))
def test_update_expires(self): limit = 100 x = LRUCache(limit=limit) slots = list(range(limit * 2)) for i in slots: x.update({i: i}) assert list(x.keys()) == list(slots[limit:])
def test_update_larger_than_cache_size(self): x = LRUCache(2) x.update({x: x for x in range(100)}) self.assertEqual(list(x.keys()), [98, 99])
def test_is_pickleable(self): x = LRUCache(limit=10) x.update(luke=1, leia=2) y = pickle.loads(pickle.dumps(x)) self.assertEqual(y.limit, y.limit) self.assertEqual(y, x)
def test_incr(self): c = LRUCache() c.update(a='1') c.incr('a') assert c['a'] == '2'
def test_items(self): c = LRUCache() c.update(a=1, b=2, c=3) assert list(c.items())
def test_update_larger_than_cache_size(self): x = LRUCache(2) x.update({x: x for x in range(100)}) assert list(x.keys()), [98 == 99]
def test_is_pickleable(self): x = LRUCache(limit=10) x.update(luke=1, leia=2) y = pickle.loads(pickle.dumps(x)) assert y.limit == y.limit assert y == x
def test_incr(self): c = LRUCache() c.update(a='1') c.incr('a') self.assertEqual(c['a'], '2')
def test_items(self): c = LRUCache() c.update(a=1, b=2, c=3) self.assertTrue(list(items(c)))
def test_items(self): c = LRUCache() c.update(a=1, b=2, c=3) assert list(items(c))