Beispiel #1
0
    def test_clear(self):
        on_evict = MagicMock()

        c = LRUCache(3, on_evict)
        c[0] = "a"
        c[1] = "b"
        c[2] = "c"

        c.clear()
        self._check_order([], c)
        assert_eq(3, on_evict.call_count)
        assert_eq([call(0, "a"), call(1, "b"),
                   call(2, "c")], on_evict.call_args_list)
Beispiel #2
0
    def test_clear(self):
        on_evict = MagicMock()

        c = LRUCache(3, on_evict)
        c[0] = 'a'
        c[1] = 'b'
        c[2] = 'c'

        c.clear()
        self._check_order([], c)
        assert_eq(3, on_evict.call_count)
        assert_eq([call(0, 'a'), call(1, 'b'),
                   call(2, 'c')], on_evict.call_args_list)
Beispiel #3
0
    def test_get(self):
        c = LRUCache(3)
        c[0] = 'a'
        c[1] = 'b'
        c[2] = 'c'

        # Getting a value should make it MRU
        assert_in(1, c)
        assert_eq('b', c.get(1))
        self._check_order([(0, 'a'), (2, 'c'), (1, 'b')], c)

        # Missing value should have no effect
        assert_not_in(100, c)
        assert_eq(miss, c.get(100))
        self._check_order([(0, 'a'), (2, 'c'), (1, 'b')], c)
Beispiel #4
0
    def test_get(self):
        c = LRUCache(3)
        c[0] = "a"
        c[1] = "b"
        c[2] = "c"

        # Getting a value should make it MRU
        assert_in(1, c)
        assert_eq("b", c.get(1))
        self._check_order([(0, "a"), (2, "c"), (1, "b")], c)

        # Missing value should have no effect
        assert_not_in(100, c)
        assert_eq(miss, c.get(100))
        self._check_order([(0, "a"), (2, "c"), (1, "b")], c)
Beispiel #5
0
    def decorator(fn):
        cache = LRUCache(maxsize)
        argspec = inspect2.getfullargspec(get_original_fn(fn))
        arg_names = argspec.args[1:] + argspec.kwonlyargs  # remove self
        async_fun = fn.asynq
        kwargs_defaults = get_kwargs_defaults(argspec)

        cache_key = key_fn
        if cache_key is None:

            def cache_key(args, kwargs):
                return get_args_tuple(args, kwargs, arg_names, kwargs_defaults)

        @asynq()
        @functools.wraps(fn)
        def wrapper(*args, **kwargs):
            key = cache_key(args, kwargs)
            try:
                result(cache[key])
                return
            except KeyError:
                value = yield async_fun(*args, **kwargs)
                cache[key] = value
                result(value)
                return

        return wrapper
Beispiel #6
0
    def test_getitem(self):
        c = LRUCache(3)
        c[0] = 'a'
        c[1] = 'b'
        c[2] = 'c'

        assert_eq(3, c.get_capacity())
        self._check_order([(0, 'a'), (1, 'b'), (2, 'c')], c)

        # Getting a value should make it MRU
        assert_eq('b', c[1])
        self._check_order([(0, 'a'), (2, 'c'), (1, 'b')], c)

        # Missing value should fail
        with AssertRaises(KeyError):
            c[100]
Beispiel #7
0
    def test_iteration(self):
        c = LRUCache(3)
        c[0] = 'a'
        c[1] = 'b'
        c[2] = 'c'

        self._check_order([(0, 'a'), (1, 'b'), (2, 'c')], c)
Beispiel #8
0
    def test_getitem(self):
        c = LRUCache(3)
        c[0] = "a"
        c[1] = "b"
        c[2] = "c"

        assert_eq(3, c.get_capacity())
        self._check_order([(0, "a"), (1, "b"), (2, "c")], c)

        # Getting a value should make it MRU
        assert_eq("b", c[1])
        self._check_order([(0, "a"), (2, "c"), (1, "b")], c)

        # Missing value should fail
        with AssertRaises(KeyError):
            c[100]
Beispiel #9
0
    def test_iteration(self):
        c = LRUCache(3)
        c[0] = "a"
        c[1] = "b"
        c[2] = "c"

        self._check_order([(0, "a"), (1, "b"), (2, "c")], c)
Beispiel #10
0
    def test_eviction(self):
        on_eviction = MagicMock()

        c = LRUCache(1, on_eviction)
        c[0] = '0'
        c[1] = '1'
        assert_eq(1, on_eviction.call_count)
        on_eviction.assert_called_once_with(0, '0')

        on_eviction.reset_mock()
        del c[1]
        assert_eq(1, on_eviction.call_count)
        on_eviction.assert_called_once_with(1, '1')
Beispiel #11
0
    def test_eviction(self):
        on_eviction = MagicMock()

        c = LRUCache(1, on_eviction)
        c[0] = "0"
        c[1] = "1"
        assert_eq(1, on_eviction.call_count)
        on_eviction.assert_called_once_with(0, "0")

        on_eviction.reset_mock()
        del c[1]
        assert_eq(1, on_eviction.call_count)
        on_eviction.assert_called_once_with(1, "1")
Beispiel #12
0
    def test_sets(self):
        c = LRUCache(3)
        c[0] = 'a'
        c[1] = 'b'
        c[2] = 'c'

        # Updating a value should make it MRU
        c[0] = 'd'
        assert_in(0, c)
        self._check_order([(1, 'b'), (2, 'c'), (0, 'd')], c)

        # Update order and evict the LRU item
        c[3] = 'e'
        assert_in(3, c)
        self._check_order([(2, 'c'), (0, 'd'), (3, 'e')], c)
Beispiel #13
0
    def test_sets(self):
        c = LRUCache(3)
        c[0] = "a"
        c[1] = "b"
        c[2] = "c"

        # Updating a value should make it MRU
        c[0] = "d"
        assert_in(0, c)
        self._check_order([(1, "b"), (2, "c"), (0, "d")], c)

        # Update order and evict the LRU item
        c[3] = "e"
        assert_in(3, c)
        self._check_order([(2, "c"), (0, "d"), (3, "e")], c)
Beispiel #14
0
    def test_deletion(self):
        # Zero capacity cache is not allowed
        with AssertRaises(ValueError):
            c = LRUCache(0)

        # Capacity = 1
        c = LRUCache(1)
        c[0] = '0'
        c[1] = '1'
        assert_eq(1, len(c))
        assert_eq('1', c[1])
        assert_is(miss, c.get(2))
        del c[1]
        assert_eq(0, len(c))

        # Capacity = 2
        c = LRUCache(2)
        c[0] = '0'
        c[1] = '1'
        c[2] = '2'
        del c[1]
        assert_eq(1, len(c))
        assert_is(miss, c.get(1))
        assert_eq('2', c[2])

        c = LRUCache(2)
        c[0] = '0'
        c[1] = '1'
        c[2] = '2'
        del c[2]
        assert_eq(1, len(c))
        assert_eq('1', c[1])
        assert_is(miss, c.get(2))

        # Capacity = 3
        c = LRUCache(3)
        c[0] = '0'
        c[1] = '1'
        c[2] = '2'
        c[3] = '3'
        del c[2]
        assert_eq(2, len(c))
        assert_is(miss, c.get(2))
        assert_eq('1', c[1])
        assert_eq('3', c[3])

        # Deletion of invalid item
        with AssertRaises(KeyError):
            del c[15]