Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to aioredis 2 #611

Merged
merged 18 commits into from
Aug 30, 2021
52 changes: 14 additions & 38 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import asyncio
import gc
import socket
import sys
import time
import uuid
from typing import Iterator
from typing import AsyncIterator, Iterator

import aiomcache
import aioredis
Expand Down Expand Up @@ -37,21 +35,6 @@ def unused_port() -> int:
return port


@pytest.fixture(scope='session')
def loop() -> Iterator[asyncio.AbstractEventLoop]:
loop = asyncio.new_event_loop()
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
asyncio.set_event_loop(None)

yield loop

if not loop.is_closed():
loop.call_soon(loop.stop)
loop.run_forever()
loop.close()
gc.collect()
asyncio.set_event_loop(None)


@pytest.fixture(scope='session')
def session_id() -> str:
"""Unique session identifier, random string."""
Expand All @@ -65,11 +48,10 @@ def docker() -> DockerClient: # type: ignore[misc] # No docker types.


@pytest.fixture(scope='session')
def redis_server( # type: ignore[misc] # No docker types.
async def redis_server( # type: ignore[misc] # No docker types.
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
docker: DockerClient,
session_id: str,
loop: asyncio.AbstractEventLoop,
) -> Iterator[_ContainerInfo]:
) -> AsyncIterator[_ContainerInfo]:
image = 'redis:{}'.format('latest')

if sys.platform.startswith('darwin'):
Expand Down Expand Up @@ -100,10 +82,8 @@ def redis_server( # type: ignore[misc] # No docker types.
delay = 0.1
for _i in range(20):
try:
conn = loop.run_until_complete(
aioredis.from_url("redis://{}:{}".format(host, port)) # type: ignore[no-untyped-call] # noqa: B950
)
loop.run_until_complete(conn.execute('SET', 'foo', 'bar'))
conn = await aioredis.from_url("redis://{}:{}".format(host, port)) # type: ignore[no-untyped-call] # noqa: B950
await conn.execute('SET', 'foo', 'bar')
break
except ConnectionRefusedError:
time.sleep(delay)
Expand All @@ -123,26 +103,23 @@ def redis_url(redis_server: _ContainerInfo) -> str: # type: ignore[misc]


@pytest.fixture
def redis(
loop: asyncio.AbstractEventLoop,
redis_url: str,
) -> Iterator[aioredis.Redis]:
async def redis(redis_url: str) -> AsyncIterator[aioredis.Redis]:
async def start(pool: aioredis.ConnectionPool) -> aioredis.Redis:
return aioredis.Redis(connection_pool=pool)

pool = aioredis.ConnectionPool.from_url(redis_url)
redis = loop.run_until_complete(start(pool))
redis = await start(pool)
yield redis
if redis is not None:
redis.close() # type: ignore[no-untyped-call]
loop.run_until_complete(pool.disconnect())
await pool.disconnect()


@pytest.fixture(scope='session')
def memcached_server( # type: ignore[misc] # No docker types.
async def memcached_server( # type: ignore[misc] # No docker types.
docker: DockerClient,
session_id: str, loop: asyncio.AbstractEventLoop,
) -> Iterator[_ContainerInfo]:
session_id: str,
) -> AsyncIterator[_ContainerInfo]:

image = 'memcached:{}'.format('latest')

Expand Down Expand Up @@ -174,8 +151,8 @@ def memcached_server( # type: ignore[misc] # No docker types.
delay = 0.1
for _i in range(20):
try:
conn = aiomcache.Client(host, port, loop=loop)
loop.run_until_complete(conn.set(b'foo', b'bar'))
conn = aiomcache.Client(host, port)
await conn.set(b'foo', b'bar')
break
except ConnectionRefusedError:
time.sleep(delay)
Expand All @@ -196,9 +173,8 @@ def memcached_params(memcached_server: _ContainerInfo) -> _MemcachedParams: # t

@pytest.fixture
def memcached( # type: ignore[misc]
loop: asyncio.AbstractEventLoop,
memcached_params: _MemcachedParams
) -> Iterator[aiomcache.Client]:
conn = aiomcache.Client(loop=loop, **memcached_params)
conn = aiomcache.Client(**memcached_params)
yield conn
conn.close()