Exemplo n.º 1
0
    def test_lru_getitem(self):

        capacity = 4
        cases = (
            (3, [0, 1, 2], []),
            (4, [0, 1, 2, 3], []),
            (5, [0, 1, 2, 3, 4], []),
            (6, [0, 1, 2, 3, 4, 5], []),
            (7, [3, 4, 5, 6], [0, 1, 2]),
        )

        for insert_count, exist_items, cleanup_items in cases:
            lru = cacheable.LRU(capacity, 10)
            for i in range(insert_count):
                lru[i] = 'val%d' % (i)

            for i in range(insert_count):
                try:
                    val = lru[i]
                    self.assertEqual(val, 'val%d' % (i))
                    self.assertEqual(lru.tail['key'], i)
                    self.assertIn(i, exist_items)

                except KeyError:
                    self.assertIn(i, cleanup_items)
Exemplo n.º 2
0
    def test_lru_setitem(self):

        capacity = 4
        cases = (
            (3,
             [0, 1, 2]),

            (4,
             [0, 1, 2, 3]),

            (5,
             [0, 1, 2, 3, 4]),

            (6,
             [0, 1, 2, 3, 4, 5]),

            # size of lru > capacity*1.5
            # clean items from head, until size=capacity
            (7,
             [3, 4, 5, 6]),
        )

        for insert_count, expect_order_keys in cases:
            lru = cacheable.LRU(capacity, 10)
            for i in range(insert_count):
                lru[i] = 'val'
                self._assert_lru_list(lru)

            self._assert_lru_items_order(lru, expect_order_keys)
Exemplo n.º 3
0
    def test_lru_capacity(self):

        cases = (
            (0, ((0, 0), (1, 0))),
            (1, ((1, 1), (2, 1))),
            (10, ((9, 9), (10, 10), (13, 13), (15, 15), (16, 10))),
        )

        for capacity, case in cases:
            for insert_count, expect_size in case:
                lru = cacheable.LRU(capacity, 60)
                for i in range(insert_count):
                    lru[i] = 'val'

                self.assertEqual(lru.size, expect_size)
Exemplo n.º 4
0
    def test_lru_timeout(self):

        cases = (
            ('k1', 'v1', 3, True),
            ('k2', 'v2', 1, False),
        )

        lru = cacheable.LRU(10, 2)
        for key, val, sleep_time, is_timeout in cases:
            lru[key] = val
            time.sleep(sleep_time)

            try:
                lru[key]
                self.assertFalse(is_timeout)

            except KeyError:
                self._assert_lru_list(lru)
                self.assertTrue(is_timeout)