Esempio n. 1
0
async def test_connection_pool_pop():
    """
    Makes sure that the connection pool does not return closed connections
    """

    # Setup scenario
    connection_pool = ConnectionPool({"address": TEST_HOSTS[0]})
    conn = await connection_pool.pop()

    # Emualte a disconnect and return it to the pool
    conn.close()
    assert conn.closed
    connection_pool.push(conn)

    # Ensure the closed connection is inside the pool
    conn_map_values = list(connection_pool.conn_map.values())
    assert len(conn_map_values) == 1
    conns = conn_map_values[0]
    assert len(conns) == 1
    assert conns[0].closed

    # Retrieve new connection
    conn = await connection_pool.pop()
    assert not conn.closed
Esempio n. 2
0
logger = logging.getLogger(__name__)

try:
    import aioredis
except ImportError:
    use_redis = False
else:
    from channels_redis.core import ConnectionPool

    # set use_redis to true, if there is a value for REDIS_ADDRESS in the settings
    redis_address = getattr(settings, "REDIS_ADDRESS", "")
    use_redis = bool(redis_address)
    if use_redis:
        logger.info(f"Redis address {redis_address}")

    pool = ConnectionPool({"address": redis_address})
    counter = 0


class RedisConnectionContextManager:
    """
    Async context manager for connections
    """

    # TODO: contextlib.asynccontextmanager can be used in python 3.7
    async def __aenter__(self) -> "aioredis.RedisConnection":
        global counter
        while counter > 100:
            await asyncio.sleep(0.1)
        counter += 1