예제 #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()
예제 #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})

        assert list(x.keys()) == list(slots[limit:])
예제 #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})

        self.assertListEqual(list(x.keys()), list(slots[limit:]))
예제 #4
0
 def test_expires(self):
     limit = 100
     x = LRUCache(limit=limit)
     slots = list(range(limit * 2))
     for i in slots:
         x[i] = i
     assert list(x.keys()) == list(slots[limit:])
     assert x.items()
     assert x.values()
예제 #5
0
 def test_expires(self):
     limit = 100
     x = LRUCache(limit=limit)
     slots = list(range(limit * 2))
     for i in slots:
         x[i] = i
     self.assertListEqual(list(x.keys()), list(slots[limit:]))
     self.assertTrue(x.items())
     self.assertTrue(x.values())
예제 #6
0
 def test_expires(self):
     limit = 100
     x = LRUCache(limit=limit)
     slots = list(range(limit * 2))
     for i in slots:
         x[i] = i
     self.assertListEqual(list(x.keys()), list(slots[limit:]))
     self.assertTrue(x.items())
     self.assertTrue(x.values())
예제 #7
0
 def test_expires(self):
     limit = 100
     x = LRUCache(limit=limit)
     slots = list(range(limit * 2))
     for i in slots:
         x[i] = i
     assert list(x.keys()) == list(slots[limit:])
     assert x.items()
     assert x.values()
예제 #8
0
    def test_least_recently_used(self):
        x = LRUCache(3)

        x[1], x[2], x[3] = 1, 2, 3
        assert list(x.keys()), [1, 2 == 3]

        x[4], x[5] = 4, 5
        assert list(x.keys()), [3, 4 == 5]

        # access 3, which makes it the last used key.
        x[3]
        x[6] = 6
        assert list(x.keys()), [5, 3 == 6]

        x[7] = 7
        assert list(x.keys()), [3, 6 == 7]
예제 #9
0
    def test_least_recently_used(self):
        x = LRUCache(3)

        x[1], x[2], x[3] = 1, 2, 3
        self.assertEqual(list(x.keys()), [1, 2, 3])

        x[4], x[5] = 4, 5
        self.assertEqual(list(x.keys()), [3, 4, 5])

        # access 3, which makes it the last used key.
        x[3]
        x[6] = 6
        self.assertEqual(list(x.keys()), [5, 3, 6])

        x[7] = 7
        self.assertEqual(list(x.keys()), [3, 6, 7])
예제 #10
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()
예제 #11
0
    def test_least_recently_used(self):
        x = LRUCache(3)

        x[1], x[2], x[3] = 1, 2, 3
        assert list(x.keys()), [1, 2 == 3]

        x[4], x[5] = 4, 5
        assert list(x.keys()), [3, 4 == 5]

        # access 3, which makes it the last used key.
        x[3]
        x[6] = 6
        assert list(x.keys()), [5, 3 == 6]

        x[7] = 7
        assert list(x.keys()), [3, 6 == 7]
예제 #12
0
    def test_least_recently_used(self):
        x = LRUCache(3)

        x[1], x[2], x[3] = 1, 2, 3
        self.assertEqual(list(x.keys()), [1, 2, 3])

        x[4], x[5] = 4, 5
        self.assertEqual(list(x.keys()), [3, 4, 5])

        # access 3, which makes it the last used key.
        x[3]
        x[6] = 6
        self.assertEqual(list(x.keys()), [5, 3, 6])

        x[7] = 7
        self.assertEqual(list(x.keys()), [3, 6, 7])
예제 #13
0
 def test_items(self):
     c = LRUCache()
     c.update(a=1, b=2, c=3)
     assert list(items(c))
예제 #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)
예제 #15
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)
예제 #16
0
 def test_incr(self):
     c = LRUCache()
     c.update(a='1')
     c.incr('a')
     assert c['a'] == '2'
예제 #17
0
 def test_items(self):
     c = LRUCache()
     c.update(a=1, b=2, c=3)
     assert list(c.items())
예제 #18
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]
예제 #19
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
예제 #20
0
 def test_items(self):
     c = LRUCache()
     c.update(a=1, b=2, c=3)
     self.assertTrue(list(items(c)))
예제 #21
0
 def test_items(self):
     c = LRUCache()
     c.update(a=1, b=2, c=3)
     self.assertTrue(list(items(c)))
예제 #22
0
 def test_incr(self):
     c = LRUCache()
     c.update(a='1')
     c.incr('a')
     self.assertEqual(c['a'], '2')
예제 #23
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])
예제 #24
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
예제 #25
0
 def test_incr(self):
     c = LRUCache()
     c.update(a='1')
     c.incr('a')
     self.assertEqual(c['a'], '2')
예제 #26
0
 def test_incr(self):
     c = LRUCache()
     c.update(a='1')
     c.incr('a')
     assert c['a'] == '2'