# -*- coding: utf-8 -*-
"""
@File    : test_acquire_random_container.py
@Author  : Jay
@Date    : 2026/3/23 13:59
@Desc    :
"""

from redis_xsync import register_redis


def test_redis_rate_limit_sync(limiter):
    with limiter(redis_key="crawler:identity:flow_limit") as lt:
        while True:
            print(lt.acquire_random_container())


async def test_redis_rate_limit(limiter):
    async with limiter(redis_key="crawler:identity:flow_limit") as lt:
        while True:
            print(await lt.acquire_random_containers(quantity=2))


if __name__ == '__main__':
    import asyncio, threading

    cfg = {
        'host': '127.0.0.1',
        'port': 6379,
        'db': 0,
        'password': '',
        'decode_responses': True
    }
    redisXsync = register_redis(label='redis_xsync', **cfg)
    Limited = redisXsync.Limited
    threading.Thread(target=test_redis_rate_limit_sync, args=(Limited,)).start()
    asyncio.run(test_redis_rate_limit(Limited))
