Beispiel #1
0
    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()
Beispiel #2
0
    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:]))
Beispiel #3
0
    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:])
Beispiel #4
0
    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()
Beispiel #5
0
 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])
Beispiel #6
0
 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)
Beispiel #7
0
 def test_incr(self):
     c = LRUCache()
     c.update(a='1')
     c.incr('a')
     assert c['a'] == '2'
Beispiel #8
0
 def test_items(self):
     c = LRUCache()
     c.update(a=1, b=2, c=3)
     assert list(c.items())
Beispiel #9
0
 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]
Beispiel #10
0
 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
Beispiel #11
0
 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
Beispiel #12
0
 def test_incr(self):
     c = LRUCache()
     c.update(a='1')
     c.incr('a')
     self.assertEqual(c['a'], '2')
Beispiel #13
0
 def test_items(self):
     c = LRUCache()
     c.update(a=1, b=2, c=3)
     self.assertTrue(list(items(c)))
Beispiel #14
0
 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)
Beispiel #15
0
 def test_items(self):
     c = LRUCache()
     c.update(a=1, b=2, c=3)
     assert list(items(c))
Beispiel #16
0
 def test_items(self):
     c = LRUCache()
     c.update(a=1, b=2, c=3)
     self.assertTrue(list(items(c)))
Beispiel #17
0
 def test_incr(self):
     c = LRUCache()
     c.update(a='1')
     c.incr('a')
     self.assertEqual(c['a'], '2')
Beispiel #18
0
 def test_incr(self):
     c = LRUCache()
     c.update(a='1')
     c.incr('a')
     assert c['a'] == '2'