Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Type hint fixes for connection pool (or redis connnection) (#4)
Browse files Browse the repository at this point in the history
* Fix typehint for pool_or_conn to be the aioredis.commands.Redis interface

Closes #1

* Bump version to 0.0.3

* Add testing to cover a pool and an individual connection
  • Loading branch information
mattrasband authored Jun 29, 2019
1 parent a5c1f8d commit 811e5fd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
4 changes: 2 additions & 2 deletions aioredis_lock/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid
from typing import List, Optional

from aioredis import ConnectionsPool
from aioredis import Redis

from .errors import LockTimeoutError
from .lua_scripts import ACQUIRE_SCRIPT, EXTEND_SCRIPT, RELEASE_SCRIPT, RENEW_SCRIPT
Expand All @@ -25,7 +25,7 @@ class RedisLock:
"""

# The aioredis pool or connection object
pool_or_conn: ConnectionsPool
pool_or_conn: Redis

# The key to lock on in redis
key: str
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import find_packages, setup


__version__ = "0.0.2"
__version__ = "0.0.3"


with open(Path(__file__).parent / "README.md") as f:
Expand Down
39 changes: 28 additions & 11 deletions tests/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ async def redis_pool(event_loop):
await pool.wait_closed()


@pytest.fixture
async def redis_connection(event_loop):
conn = await aioredis.create_redis("redis://127.0.0.1:6380/1", loop=event_loop)
yield conn
conn.close()
await conn.wait_closed()


@pytest.fixture
async def key() -> str:
return str(uuid.uuid4())
Expand All @@ -33,33 +41,42 @@ async def test_acquire_lock(redis_pool, key):
assert await lock.is_owner()


@pytest.fixture
def red(request):
yield request.getfixturevalue(request.param)


@pytest.mark.asyncio
async def test_acquire_lock_timeout(redis_pool, key):
await redis_pool.setex(key, 10, str(uuid.uuid4()))
@pytest.mark.parametrize("red", ["redis_pool", "redis_connection"], indirect=["red"])
async def test_acquire_lock_timeout(red, key):
await red.setex(key, 10, str(uuid.uuid4()))

with pytest.raises(LockTimeoutError):
async with RedisLock(redis_pool, key, wait_timeout=0) as lock:
async with RedisLock(red, key, wait_timeout=0) as lock:
assert False, "Acquired lock when the key is set"


@pytest.mark.asyncio
async def test_extend_lock(redis_pool, key):
async with RedisLock(redis_pool, key, timeout=10) as lock:
@pytest.mark.parametrize("red", ["redis_pool", "redis_connection"], indirect=["red"])
async def test_extend_lock(red, key):
async with RedisLock(red, key, timeout=10) as lock:
await lock.extend(10)
assert (await redis_pool.ttl(key)) > 11
assert (await red.ttl(key)) > 11


@pytest.mark.asyncio
async def test_renew_lock(redis_pool, key):
async with RedisLock(redis_pool, key, timeout=10) as lock:
@pytest.mark.parametrize("red", ["redis_pool", "redis_connection"], indirect=["red"])
async def test_renew_lock(red, key):
async with RedisLock(red, key, timeout=10) as lock:
await lock.renew(10)
assert (await redis_pool.pttl(key)) > 9 * 1000
assert (await red.pttl(key)) > 9 * 1000


@pytest.mark.asyncio
async def test_acquisition_failover(event_loop, redis_pool, key):
@pytest.mark.parametrize("red", ["redis_pool", "redis_connection"], indirect=["red"])
async def test_acquisition_failover(event_loop, red, key):
async def task(sleep: float):
async with RedisLock(redis_pool, key, timeout=5):
async with RedisLock(red, key, timeout=5):
await asyncio.sleep(sleep)
return True

Expand Down

0 comments on commit 811e5fd

Please sign in to comment.