Exemple #1
0
    def testExpireItemForLruHeapOrderDuringExpiry(self):
        def callback(input):
            return input * 2

        test_lru = lru.LruDict(callback, maximum_size=4)
        test_lru[100]  # will be expired.
        test_lru[200]  # will be expired.
        test_lru[300]  # will be expired.
        test_lru['400']  # will be expired.
        test_lru[10]
        test_lru[20]
        test_lru[30]
        test_lru['40']
        self.assertEqual(20, test_lru.expire_item())
        self.assertEqual(40, test_lru.expire_item())
        self.assertEqual(60, test_lru.expire_item())
        self.assertEqual('4040', test_lru.expire_item())
        test_lru[10]  # will be expired.
        test_lru[20]
        test_lru[30]
        test_lru['40']
        test_lru['50']
        self.assertEqual(40, test_lru.expire_item())
        self.assertEqual(60, test_lru.expire_item())
        self.assertEqual('4040', test_lru.expire_item())
        self.assertEqual('5050', test_lru.expire_item())
Exemple #2
0
    def testLruChangeExpireCallback(self):
        called = []

        def callback(input):
            return input * 3

        def expire(key, value):
            called[:] = []

        def expire_new(key, value):
            called.append(True)

        test_lru = lru.LruDict(callback,
                               expire_callback=expire,
                               maximum_size=2)
        self.assert_(not called)
        self.assertEqual(test_lru[5], 15)
        self.assertEqual(test_lru[10], 30)
        self.assert_(not called)
        self.assertEqual(len(test_lru), 2)
        test_lru.set_expire_callback(expire_new)
        self.assert_(not called)
        self.assertEqual(test_lru['10'], '101010')
        self.assert_(called)
        self.assertEqual(len(test_lru), 2)
Exemple #3
0
    def testSetItem(self):
        def callback(input):
            return input * 2

        test_lru = lru.LruDict(callback, maximum_size=2)
        # Override the callback function by direct setting.
        test_lru[10] = 40
        self.assertEqual(test_lru[10], 40)
        self.assertEqual(test_lru[20], 40)
Exemple #4
0
    def testLruAgeExpiry(self):
        def callback(input):
            return input * 2

        test_lru = lru.LruDict(callback, maximum_age=0.1)
        test_lru[100]
        self.assert_(100 in test_lru)
        time.sleep(0.2)
        self.assert_(100 not in test_lru)
Exemple #5
0
    def testLruGet(self):
        def callback(input):
            return input * 2

        test_lru = lru.LruDict(callback, maximum_size=4)
        test_lru[100]
        self.assertEqual(test_lru.get(100), 200)
        self.assertEqual(test_lru.get('not here'), None)
        self.assertEqual(test_lru.get('not there', default='foo'), 'foo')
Exemple #6
0
    def testExpireItemDontReturnCopy(self):
        def callback(input):
            return input * 2

        test_lru = lru.LruDict(callback, maximum_size=4)
        test_lru[100]
        item = test_lru.expire_item(return_copy=False)
        self.assertEqual(item, None)
        self.assert_(100 not in test_lru)
Exemple #7
0
    def testLruSameItem(self):
        """Item keys are only added to the cache once."""
        def callback(input):
            return input

        test_lru = lru.LruDict(callback, maximum_size=2)
        self.assertEqual(test_lru[10], 10)
        self.assertEqual(len(test_lru), 1)
        self.assertEqual(test_lru[10], 10)
        self.assertEqual(len(test_lru), 1)
Exemple #8
0
    def testExpireItem(self):
        def callback(input):
            return input * 3

        test_lru = lru.LruDict(callback, maximum_size=2)
        test_lru[5]
        test_lru[10]
        self.assertEqual(test_lru.expire_item(), 15)
        self.assertEqual(test_lru.expire_item(), 30)
        self.assertRaises(IndexError, test_lru.expire_item)
Exemple #9
0
    def testLruBasic(self):
        def callback(input):
            return input * 2

        test_lru = lru.LruDict(callback, maximum_size=2)
        test_lru[5]
        self.assertEqual(test_lru[5], 10)
        self.assertEqual(test_lru[10], 20)
        self.assertEqual(test_lru[20], 40)
        self.assertEqual(len(test_lru), 2)
Exemple #10
0
    def testExpireItemReturnCopy(self):
        def callback(input):
            return input * 2

        test_lru = lru.LruDict(callback, maximum_size=4)
        test_lru[100]
        item = test_lru.expire_item(return_copy=True)
        self.assertEqual(item, 200)
        self.assert_(100 not in test_lru)

        test_lru[200]
        item = test_lru.expire_item()  # return_copy=True is the default.
        self.assertEqual(item, 400)
        self.assert_(200 not in test_lru)
Exemple #11
0
    def testLruDontExpireSignal(self):
        def callback(input):
            return input * 3

        def expire(key, value):
            # We won't cause this item to be expired.
            raise lru.DontExpireError

        test_lru = lru.LruDict(callback,
                               expire_callback=expire,
                               maximum_size=2)
        self.assertEqual(test_lru[5], 15)
        self.assertEqual(test_lru[10], 30)
        self.assertEqual(test_lru['10'], '101010')
        self.assertEqual(len(test_lru), 3)
Exemple #12
0
    def testLruChangePopulateCallback(self):
        def callback1(input):
            return input

        def callback2(input):
            return input * 2

        my_lru = lru.LruDict(callback1, maximum_size=1)
        self.assertEqual(my_lru[10], 10)
        self.assertEqual(len(my_lru), 1)
        self.assertEqual(my_lru[10], 10)
        self.assertEqual(len(my_lru), 1)

        my_lru.set_populate_callback(callback2)
        self.assertEqual(my_lru[10], 20)
        self.assertEqual(len(my_lru), 1)
Exemple #13
0
    def testLruSameItemWithExpiry(self):
        """Item keys are re-added to the cache upon expiry."""
        def callback(input):
            return input * 2

        test_lru = lru.LruDict(callback, maximum_size=2)
        self.assertEqual(test_lru[10], 20)
        self.assertEqual(len(test_lru), 1)
        self.assertEqual(test_lru[10], 20)
        self.assertEqual(len(test_lru), 1)
        self.assertEqual(test_lru[20], 40)
        self.assertEqual(len(test_lru), 2)
        self.assertEqual(test_lru[10], 20)
        self.assertEqual(len(test_lru), 2)
        self.assertEqual(test_lru[20], 40)
        self.assertEqual(len(test_lru), 2)
        self.assertEqual(test_lru[20], 40)
        self.assertEqual(len(test_lru), 2)