Beispiel #1
0
async def redis_helper() -> AsyncGenerator[RedisHelper, None]:
    redis_helper: RedisHelper = RedisHelper()
    redis_helper.init(
        await aioredis.create_pool("redis://localhost", minsize=1, maxsize=10, encoding="utf-8"),
    )
    yield redis_helper
    await redis_helper.close()
Beispiel #2
0
 async def clear() -> None:
     redis_helper: RedisHelper = RedisHelper()
     redis_helper.init(await aioredis.create_pool("redis://localhost",
                                                  minsize=1,
                                                  maxsize=10,
                                                  encoding="utf-8"))
     await redis_helper.client.delete("fast-tools:root:():{}")
     await redis_helper.client.delete("fast-tools:user_login:123")
     await redis_helper.client.delete("fast-tools:user_login:1234")
     await redis_helper.close()
Beispiel #3
0
    async def _clear() -> None:
        redis_helper: RedisHelper = RedisHelper()
        redis_helper.init(await aioredis.create_pool("redis://localhost", minsize=1, maxsize=10, encoding="utf-8"))
        delete_key_list = []
        async for key in redis_helper.client.iscan(match="fast-tool*"):
            delete_key_list.append(key)

        if delete_key_list:
            await redis_helper.client.delete("fake", *delete_key_list)

        await redis_helper.close()
Beispiel #4
0
    async def test_execute(self) -> None:
        redis_helper: RedisHelper = RedisHelper()
        with pytest.raises(ConnectionError) as e:
            await redis_helper.execute("set", "test", "value")

        assert e.value.args[0] == "Not init RedisHelper, please run RedisHelper.init"

        redis_helper.init(
            await aioredis.create_pool("redis://localhost", minsize=1, maxsize=10, encoding="utf-8"),
        )
        await redis_helper.execute("set", "test", "value")
        with pytest.raises(errors.RedisError):
            await redis_helper.hmget_dict("test")

        await redis_helper.execute("del", "test")
        await redis_helper.close()
Beispiel #5
0
    async def test_init(self) -> None:
        redis_helper: RedisHelper = RedisHelper()
        with pytest.raises(ConnectionError) as e:
            redis_helper.init(None)  # type: ignore
        assert e.value.args[0] == "conn_pool is none"

        redis_helper.init(
            await aioredis.create_pool("redis://localhost", minsize=1, maxsize=10, encoding="utf-8"), namespace="test"
        )
        assert redis_helper.namespace == "test"

        with pytest.raises(ConnectionError) as e:
            redis_helper.init(
                await aioredis.create_pool("redis://localhost", minsize=1, maxsize=10, encoding="utf-8"),
            )
        assert e.value.args[0].startswith("Init error, RedisHelper already init")

        await redis_helper.close()
        assert redis_helper.closed()
        redis_helper.init(
            await aioredis.create_pool("redis://localhost", minsize=1, maxsize=10, encoding="utf-8"),
        )
        assert not redis_helper.closed()
Beispiel #6
0
    async def test_lock(self, redis_helper: RedisHelper) -> None:
        lock_1: Lock = redis_helper.lock("test_key")
        lock_2: Lock = redis_helper.lock("test_key")
        await lock_1.acquire()

        assert not await lock_2.do_acquire(str(int(time.time()) + 10))

        assert await lock_1.locked()
        assert await lock_2.locked()

        with pytest.raises(LockError) as e:
            await lock_2.release()

        assert e.value.args[0] == "Cannot release an unlocked lock"

        with pytest.raises(LockError) as e:
            await lock_2.do_release(int(time.time()))

        assert e.value.args[0] == "Cannot release a lock that's no longer owned"

        assert not await lock_2.acquire(1)
        await lock_1.release()
        assert not await lock_1.locked()
Beispiel #7
0
 def test_not_run_init(self) -> None:
     with pytest.raises(ConnectionError) as e:
         client: Redis = RedisHelper().client
     assert e.value.args[0] == "Not init RedisHelper, please run RedisHelper.init"