Exemple #1
0
 def test_capacity_set(self):
     for size in SIZES:
         l = TTLRU(size)
         for i in range(size + 5):
             l[i] = str(i)
         l.set_size(size + 10)
         self.assertTrue(size + 10 == l.get_size())
         self.assertTrue(len(l) == size)
         for i in range(size + 20):
             l[i] = str(i)
         self.assertTrue(len(l) == size + 10)
         l.set_size(size + 10 - 1)
         self.assertTrue(len(l) == size + 10 - 1)
Exemple #2
0
    def test_callback(self):

        counter = [0]

        first_key = 'a'
        first_value = 1

        def callback(key, value):
            self.assertEqual(key, first_key)
            self.assertEqual(value, first_value)
            counter[0] += 1

        l = TTLRU(1, callback=callback)
        l[first_key] = first_value
        l['b'] = 1  # test calling the callback

        self.assertEqual(counter[0], 1)
        self.assertEqual(l.keys(), ['b'])

        l['b'] = 2  # doesn't call callback
        self.assertEqual(counter[0], 1)
        self.assertEqual(l.keys(), ['b'])
        self.assertEqual(l.values(), [2])

        l = TTLRU(1, callback=callback)
        l[first_key] = first_value

        l.set_callback(None)
        l['c'] = 1  # doesn't call callback
        self.assertEqual(counter[0], 1)
        self.assertEqual(l.keys(), ['c'])

        l.set_callback(callback)
        del l['c']  # doesn't call callback
        self.assertEqual(counter[0], 1)
        self.assertEqual(l.keys(), [])

        l = TTLRU(2, callback=callback)
        l['a'] = 1  # test calling the callback
        l['b'] = 2  # test calling the callback

        self.assertEqual(counter[0], 1)
        self.assertEqual(l.keys(), ['b', 'a'])
        l.set_size(1)
        self.assertEqual(counter[0], 2)  # callback invoked
        self.assertEqual(l.keys(), ['b'])