Skip to content

Commit 4f1358a

Browse files
authored
Merge branch 'master' into graph_new_statistics
2 parents 6def0e4 + 6f20821 commit 4f1358a

19 files changed

+319
-278
lines changed

tests/test_asyncio/conftest.py

Lines changed: 80 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import asyncio
1+
import functools
22
import random
33
import sys
44
from typing import Union
55
from urllib.parse import urlparse
66

7-
if sys.version_info[0:2] == (3, 6):
8-
import pytest as pytest_asyncio
9-
else:
10-
import pytest_asyncio
11-
127
import pytest
138
from packaging.version import Version
149

@@ -26,6 +21,13 @@
2621

2722
from .compat import mock
2823

24+
if sys.version_info[0:2] == (3, 6):
25+
import pytest as pytest_asyncio
26+
27+
pytestmark = pytest.mark.asyncio
28+
else:
29+
import pytest_asyncio
30+
2931

3032
async def _get_info(redis_url):
3133
client = redis.Redis.from_url(redis_url)
@@ -69,11 +71,13 @@ async def _get_info(redis_url):
6971
"pool-hiredis",
7072
],
7173
)
72-
def create_redis(request, event_loop: asyncio.BaseEventLoop):
74+
async def create_redis(request):
7375
"""Wrapper around redis.create_redis."""
7476
single_connection, parser_cls = request.param
7577

76-
async def f(
78+
teardown_clients = []
79+
80+
async def client_factory(
7781
url: str = request.config.getoption("--redis-url"),
7882
cls=redis.Redis,
7983
flushdb=True,
@@ -95,56 +99,50 @@ async def f(
9599
client = client.client()
96100
await client.initialize()
97101

98-
def teardown():
99-
async def ateardown():
100-
if not cluster_mode:
101-
if "username" in kwargs:
102-
return
103-
if flushdb:
104-
try:
105-
await client.flushdb()
106-
except redis.ConnectionError:
107-
# handle cases where a test disconnected a client
108-
# just manually retry the flushdb
109-
await client.flushdb()
110-
await client.close()
111-
await client.connection_pool.disconnect()
112-
else:
113-
if flushdb:
114-
try:
115-
await client.flushdb(target_nodes="primaries")
116-
except redis.ConnectionError:
117-
# handle cases where a test disconnected a client
118-
# just manually retry the flushdb
119-
await client.flushdb(target_nodes="primaries")
120-
await client.close()
121-
122-
if event_loop.is_running():
123-
event_loop.create_task(ateardown())
102+
async def teardown():
103+
if not cluster_mode:
104+
if flushdb and "username" not in kwargs:
105+
try:
106+
await client.flushdb()
107+
except redis.ConnectionError:
108+
# handle cases where a test disconnected a client
109+
# just manually retry the flushdb
110+
await client.flushdb()
111+
await client.close()
112+
await client.connection_pool.disconnect()
124113
else:
125-
event_loop.run_until_complete(ateardown())
126-
127-
request.addfinalizer(teardown)
128-
114+
if flushdb:
115+
try:
116+
await client.flushdb(target_nodes="primaries")
117+
except redis.ConnectionError:
118+
# handle cases where a test disconnected a client
119+
# just manually retry the flushdb
120+
await client.flushdb(target_nodes="primaries")
121+
await client.close()
122+
123+
teardown_clients.append(teardown)
129124
return client
130125

131-
return f
126+
yield client_factory
127+
128+
for teardown in teardown_clients:
129+
await teardown()
132130

133131

134132
@pytest_asyncio.fixture()
135-
async def r(request, create_redis):
136-
yield await create_redis()
133+
async def r(create_redis):
134+
return await create_redis()
137135

138136

139137
@pytest_asyncio.fixture()
140138
async def r2(create_redis):
141139
"""A second client for tests that need multiple"""
142-
yield await create_redis()
140+
return await create_redis()
143141

144142

145143
@pytest_asyncio.fixture()
146144
async def modclient(request, create_redis):
147-
yield await create_redis(
145+
return await create_redis(
148146
url=request.config.getoption("--redismod-url"), decode_responses=True
149147
)
150148

@@ -222,7 +220,7 @@ async def mock_cluster_resp_slaves(create_redis, **kwargs):
222220
def master_host(request):
223221
url = request.config.getoption("--redis-url")
224222
parts = urlparse(url)
225-
yield parts.hostname
223+
return parts.hostname
226224

227225

228226
async def wait_for_command(
@@ -246,3 +244,41 @@ async def wait_for_command(
246244
return monitor_response
247245
if key in monitor_response["command"]:
248246
return None
247+
248+
249+
# python 3.6 doesn't have the asynccontextmanager decorator. Provide it here.
250+
class AsyncContextManager:
251+
def __init__(self, async_generator):
252+
self.gen = async_generator
253+
254+
async def __aenter__(self):
255+
try:
256+
return await self.gen.__anext__()
257+
except StopAsyncIteration as err:
258+
raise RuntimeError("Pickles") from err
259+
260+
async def __aexit__(self, exc_type, exc_inst, tb):
261+
if exc_type:
262+
await self.gen.athrow(exc_type, exc_inst, tb)
263+
return True
264+
try:
265+
await self.gen.__anext__()
266+
except StopAsyncIteration:
267+
return
268+
raise RuntimeError("More pickles")
269+
270+
271+
if sys.version_info[0:2] == (3, 6):
272+
273+
def asynccontextmanager(func):
274+
@functools.wraps(func)
275+
def wrapper(*args, **kwargs):
276+
return AsyncContextManager(func(*args, **kwargs))
277+
278+
return wrapper
279+
280+
else:
281+
from contextlib import asynccontextmanager as _asynccontextmanager
282+
283+
def asynccontextmanager(func):
284+
return _asynccontextmanager(func)

tests/test_asyncio/test_bloom.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import sys
2+
13
import pytest
24

35
import redis.asyncio as redis
46
from redis.exceptions import ModuleError, RedisError
57
from redis.utils import HIREDIS_AVAILABLE
68

7-
pytestmark = pytest.mark.asyncio
9+
if sys.version_info[0:2] == (3, 6):
10+
pytestmark = pytest.mark.asyncio
811

912

1013
def intlist(obj):
@@ -91,7 +94,7 @@ async def do_verify():
9194
res += rv == x
9295
assert res < 5
9396

94-
do_verify()
97+
await do_verify()
9598
cmds = []
9699
if HIREDIS_AVAILABLE:
97100
with pytest.raises(ModuleError):
@@ -120,7 +123,7 @@ async def do_verify():
120123

121124
cur_info = await modclient.bf().execute_command("bf.debug", "myBloom")
122125
assert prev_info == cur_info
123-
do_verify()
126+
await do_verify()
124127

125128
await modclient.bf().client.delete("myBloom")
126129
await modclient.bf().create("myBloom", "0.0001", "10000000")

tests/test_asyncio/test_cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
if sys.version_info[0:2] == (3, 6):
1313
import pytest as pytest_asyncio
14+
15+
pytestmark = pytest.mark.asyncio
1416
else:
1517
import pytest_asyncio
1618

@@ -39,8 +41,6 @@
3941
skip_unless_arch_bits,
4042
)
4143

42-
pytestmark = pytest.mark.asyncio
43-
4444
default_host = "127.0.0.1"
4545
default_port = 7000
4646
default_cluster_slots = [

0 commit comments

Comments
 (0)