Esempio n. 1
0
async def test_cache_decorators_on_redis_down():
    mock = Mock(return_value="val")
    cache = Cache()
    cache._add_backend(Redis,
                       safe=True,
                       address="redis://localhost:9223",
                       hash_key=None)

    @cache(ttl=1)
    @cache.fail(1)
    @cache.hit(ttl=1, cache_hits=1)
    @cache.perf(ttl=1)
    @cache.circuit_breaker(ttl=1, errors_rate=1, period=1)
    @cache.rate_limit(ttl=1, limit=1, period=1)
    @cache.early(ttl=1)
    @cache.dynamic()
    @cache.locked(ttl=1)
    async def func():
        return mock()

    assert await func() == "val"
    assert mock.call_count == 1

    assert await func() == "val"
    assert mock.call_count == 2
Esempio n. 2
0
async def test_smoke_cmds(cache: Cache, target):
    await cache.set(key="key", value={"any": True}, expire=60, exist=None)
    target.set.assert_called_once_with(key="key",
                                       value={"any": True},
                                       expire=60,
                                       exist=None)

    await cache.get("key")  # -> Any
    target.get.assert_called_once_with(key="key", default=None)

    await cache.get_many("key1", "key2")
    target.get_many.assert_called_once_with("key1", "key2", default=None)

    await cache.incr("key_incr")  # -> int
    target.incr.assert_called_once_with(key="key_incr")

    await cache.delete("key")
    target.delete.assert_called_once_with(key="key")

    await cache.expire(key="key", timeout=10)
    target.expire.assert_called_once_with(key="key", timeout=10)

    await cache.get_expire(key="key")  # -> int seconds to expire
    target.get_expire.assert_called_once()

    await cache.ping(message=b"test")  # -> bytes
    target.ping.assert_called_once_with(message=b"test")

    await cache.clear()
    target.clear.assert_called_once_with()

    await cache.is_locked("key", wait=60)  # -> bool
    target.is_locked.assert_called_once_with(key="key", wait=60, step=0.1)

    await cache.set_lock("key", "value", expire=60)  # -> bool
    target.set_lock.assert_called_once_with(key="key",
                                            value="value",
                                            expire=60)

    await cache.unlock("key", "value")  # -> bool
    target.unlock.assert_called_once_with(key="key", value="value")

    await cache.exists("key")
    target.exists.assert_called_once_with(key="key")

    await cache.get_bits("key", 1, 2, 3, size=2)
    target.get_bits.assert_called_once_with("key", 1, 2, 3, size=2)

    await cache.incr_bits("key", 1, 2, 3, size=2)
    target.incr_bits.assert_called_once_with("key", 1, 2, 3, size=2, by=1)

    [key async for key in cache.keys_match("key:*")]
    target.keys_match.assert_called_once_with("key:*")

    [key_value async for key_value in cache.get_match("key:*")]
    target.get_match.assert_called_once_with("key:*",
                                             batch_size=100,
                                             default=None)
Esempio n. 3
0
async def test_disable_decorators_get(cache: Cache):
    data = (i for i in range(10))
    await cache.init("mem://localhost")

    @cache(ttl=1)
    async def func():
        return next(data)

    assert cache.is_enable()
    assert cache.is_enable("set", prefix="cache")
    assert cache.is_enable("get", prefix="cache")
    assert cache.is_enable("set", prefix="")
    assert await func() == 0
    assert await func() == 0

    cache.disable("get")

    assert not cache.is_enable("get")
    assert cache.is_enable("set")

    assert await func() == 1
    assert await func() == 2

    cache.enable("get")
    assert await func() == 2
Esempio n. 4
0
async def test_disable_decorators_set(cache: Cache):
    data = (i for i in range(10))
    cache.disable("set")

    @cache(ttl=1)
    async def func():
        return next(data)

    assert await func() == 0
    assert await func() == 1

    cache.enable("set")
    assert await func() == 2
    assert await func() == 2
Esempio n. 5
0
async def test_cache_lock():
    m = Mock()
    cache = Cache()
    cache.setup("mem://")

    @cache(ttl=5, lock=True)
    async def my_func(val=1):
        await asyncio.sleep(0)  # for task switching
        m(val)
        return val

    await asyncio.gather(my_func(), my_func(), my_func())

    m.assert_called_once_with(1)
Esempio n. 6
0
async def test_disable_bloom(cache: Cache, target):
    cache.disable()

    @cache.bloom(index_size=10, number_of_hashes=1)
    async def func():
        return True

    await func.set()
    assert await func()
    target.incr_bits.assert_not_called()
    target.get_bits.assert_not_called()

    cache.enable()
    await func.set()
    assert await func()
    target.incr_bits.assert_called()
    target.get_bits.assert_called()
Esempio n. 7
0
async def test_all_keys_lower(cache: Cache, target):
    cache._backends[""] = cache._backends[""][0], (all_keys_lower(), )
    await cache.get(key="KEY")
    target.get.assert_called_once_with(key="key", default=None)

    await cache.set(key="KEY", value="value")
    target.set.assert_called_once_with(
        key="key",
        value="value",
        exist=None,
        expire=None,
    )
    await cache.ping()
    target.ping.assert_called_once_with(message=b"PING")
Esempio n. 8
0
async def test_disable_decorators(cache: Cache, target):
    cache.disable()
    data = (i for i in range(10))

    @cache(ttl=1)
    @cache.fail(ttl=1)
    @cache.hit(ttl=1, cache_hits=1)
    @cache.perf(ttl=1)
    @cache.circuit_breaker(ttl=1, errors_rate=1, period=1)
    @cache.rate_limit(ttl=1, limit=1, period=1)
    @cache.early(ttl=1)
    @cache.dynamic()
    @cache.locked(ttl=1)
    async def func():
        return next(data)

    assert await func() == 0
    assert await func() == 1
    target.get.assert_not_called()
    target.set.assert_not_called()

    cache.enable()
    assert await func() == 2
    assert await func() == 2
Esempio n. 9
0
async def test_add_prefix(cache: Cache, target):
    cache._backends[""] = cache._backends[""][0], (add_prefix("prefix!"), )

    await cache.get(key="key")
    target.get.assert_called_once_with(key="prefix!key", default=None)

    await cache.set(key="key", value="value")
    target.set.assert_called_once_with(
        key="prefix!key",
        value="value",
        exist=None,
        expire=None,
    )
    await cache.ping()
    target.ping.assert_called_once_with(message=b"PING")
Esempio n. 10
0
def __cache(target):
    _cache = Cache()
    _cache._add_backend(Memory)
    _cache._backends[""] = (target, _cache._backends[""][1])
    return _cache
Esempio n. 11
0
async def test_add_prefix_delete_match(cache: Cache, target):
    cache._backends[""] = cache._backends[""][0], (add_prefix("prefix!"), )
    await cache.delete_match("key")
    target.delete_match.assert_called_once_with(pattern="prefix!key")
Esempio n. 12
0
async def test_add_prefix_get_many(cache: Cache, target):
    cache._backends[""] = cache._backends[""][0], (add_prefix("prefix!"), )
    await cache.get_many("key")
    target.get_many.assert_called_once_with("prefix!key")