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

Commit

Permalink
Clear loop usage in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
popravich committed Nov 18, 2019
1 parent e33dc2f commit de36e3f
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 102 deletions.
6 changes: 4 additions & 2 deletions tests/conftest.py
Expand Up @@ -33,7 +33,8 @@
def loop():
"""Creates new event loop."""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
if sys.version_info < (3, 8):
asyncio.set_event_loop(loop)

try:
yield loop
Expand Down Expand Up @@ -598,7 +599,8 @@ def pytest_configure(config):
bins = config.getoption('--redis-server')[:]
cmd = 'which redis-server'
if not bins:
path = os.popen(cmd).read().rstrip()
with os.popen(cmd) as pipe:
path = pipe.read().rstrip()
assert path, (
"There is no redis-server on your computer."
" Please install it first")
Expand Down
8 changes: 3 additions & 5 deletions tests/connection_test.py
Expand Up @@ -68,8 +68,7 @@ async def test_connect_inject_connection_cls_invalid(
async def test_connect_tcp_timeout(request, create_connection, loop, server):
with patch.object(loop, 'create_connection') as\
open_conn_mock:
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2,
loop=loop)
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2)
with pytest.raises(asyncio.TimeoutError):
await create_connection(
server.tcp_address, loop=loop, timeout=0.1)
Expand Down Expand Up @@ -99,8 +98,7 @@ async def test_connect_unixsocket(create_connection, loop, server):
reason="No unixsocket on Windows")
async def test_connect_unixsocket_timeout(create_connection, loop, server):
with patch.object(loop, 'create_unix_connection') as open_conn_mock:
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2,
loop=loop)
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2)
with pytest.raises(asyncio.TimeoutError):
await create_connection(
server.unixsocket, db=0, loop=loop, timeout=0.1)
Expand Down Expand Up @@ -557,7 +555,7 @@ async def test_connection_idle_close(create_connection, start_server, loop):
ok = await conn.execute("config", "set", "timeout", 1)
assert ok == b'OK'

await asyncio.sleep(3, loop=loop)
await asyncio.sleep(3)

with pytest.raises(ConnectionClosedError):
assert await conn.execute('ping') is None
Expand Down
6 changes: 3 additions & 3 deletions tests/generic_commands_test.py
Expand Up @@ -396,7 +396,7 @@ async def test_object_idletime(redis, loop, server):
res = 0
while not res:
res = await redis.object_idletime('foo')
await asyncio.sleep(.5, loop=loop)
await asyncio.sleep(.5)
assert res >= 1

res = await redis.object_idletime('non-existent-key')
Expand Down Expand Up @@ -438,7 +438,7 @@ async def test_pexpire(redis, loop):
assert res is True

# XXX: tests now looks strange to me.
await asyncio.sleep(.2, loop=loop)
await asyncio.sleep(.2)

res = await redis.exists('my-key')
assert not res
Expand Down Expand Up @@ -670,7 +670,7 @@ async def test_touch(redis, loop):
res = 0
while not res:
res = await redis.object_idletime('key')
await asyncio.sleep(.5, loop=loop)
await asyncio.sleep(.5)
assert res > 0
assert await redis.touch('key', 'key', 'key') == 3
res2 = await redis.object_idletime('key')
Expand Down
24 changes: 12 additions & 12 deletions tests/integration_test.py
Expand Up @@ -5,22 +5,22 @@


@pytest.fixture
def pool_or_redis(_closable, server, loop):
def pool_or_redis(_closable, server):
version = tuple(map(int, aioredis.__version__.split('.')[:2]))
if version >= (1, 0):
factory = aioredis.create_redis_pool
else:
factory = aioredis.create_pool

async def redis_factory(maxsize):
redis = await factory(server.tcp_address, loop=loop,
redis = await factory(server.tcp_address,
minsize=1, maxsize=maxsize)
_closable(redis)
return redis
return redis_factory


async def simple_get_set(pool, idx, loop):
async def simple_get_set(pool, idx):
"""A simple test to make sure Redis(pool) can be used as old Pool(Redis).
"""
val = 'val:{}'.format(idx)
Expand All @@ -29,15 +29,15 @@ async def simple_get_set(pool, idx, loop):
await redis.get('key', encoding='utf-8')


async def pipeline(pool, val, loop):
async def pipeline(pool, val):
val = 'val:{}'.format(val)
with await pool as redis:
f1 = redis.set('key', val)
f2 = redis.get('key', encoding='utf-8')
ok, res = await asyncio.gather(f1, f2, loop=loop)
ok, res = await asyncio.gather(f1, f2)


async def transaction(pool, val, loop):
async def transaction(pool, val):
val = 'val:{}'.format(val)
with await pool as redis:
tr = redis.multi_exec()
Expand All @@ -48,12 +48,12 @@ async def transaction(pool, val, loop):
assert res == val


async def blocking_pop(pool, val, loop):
async def blocking_pop(pool, val):

async def lpush():
with await pool as redis:
# here v0.3 has bound connection, v1.0 does not;
await asyncio.sleep(.1, loop=loop)
await asyncio.sleep(.1)
await redis.lpush('list-key', 'val')

async def blpop():
Expand All @@ -62,7 +62,7 @@ async def blpop():
res = await redis.blpop(
'list-key', timeout=2, encoding='utf-8')
assert res == ['list-key', 'val'], res
await asyncio.gather(blpop(), lpush(), loop=loop)
await asyncio.gather(blpop(), lpush())


@pytest.mark.run_loop
Expand All @@ -80,12 +80,12 @@ async def blpop():
(transaction, 10),
(blocking_pop, 10),
], ids=lambda o: getattr(o, '__name__', repr(o)))
async def test_operations(pool_or_redis, test_case, pool_size, loop):
async def test_operations(pool_or_redis, test_case, pool_size):
repeat = 100
redis = await pool_or_redis(pool_size)
done, pending = await asyncio.wait(
[asyncio.ensure_future(test_case(redis, i, loop), loop=loop)
for i in range(repeat)], loop=loop)
[asyncio.ensure_future(test_case(redis, i))
for i in range(repeat)])

assert not pending
success = 0
Expand Down
23 changes: 10 additions & 13 deletions tests/list_commands_test.py
Expand Up @@ -5,7 +5,7 @@


async def push_data_with_sleep(redis, loop, key, *values):
await asyncio.sleep(0.2, loop=loop)
await asyncio.sleep(0.2)
result = await redis.lpush(key, *values)
return result

Expand Down Expand Up @@ -51,10 +51,9 @@ async def test_blpop_blocking_features(redis, create_redis, loop, server):
# create blocking task in separate connection
consumer = other_redis.blpop(key1, key2)

producer_task = asyncio.Task(
push_data_with_sleep(redis, loop, key2, value), loop=loop)
results = await asyncio.gather(
consumer, producer_task, loop=loop)
producer_task = asyncio.ensure_future(
push_data_with_sleep(redis, loop, key2, value))
results = await asyncio.gather(consumer, producer_task)

assert results[0] == [key2, value]
assert results[1] == 1
Expand Down Expand Up @@ -107,11 +106,10 @@ async def test_brpop_blocking_features(redis, create_redis, server, loop):
# create blocking task in separate connection
consumer_task = other_redis.brpop(key1, key2)

producer_task = asyncio.Task(
push_data_with_sleep(redis, loop, key2, value), loop=loop)
producer_task = asyncio.ensure_future(
push_data_with_sleep(redis, loop, key2, value))

results = await asyncio.gather(
consumer_task, producer_task, loop=loop)
results = await asyncio.gather(consumer_task, producer_task)

assert results[0] == [key2, value]
assert results[1] == 1
Expand Down Expand Up @@ -171,10 +169,9 @@ async def test_brpoplpush_blocking_features(redis, create_redis, server, loop):
server.tcp_address, loop=loop)
# create blocking task
consumer_task = other_redis.brpoplpush(source, destkey)
producer_task = asyncio.Task(
push_data_with_sleep(redis, loop, source, value), loop=loop)
results = await asyncio.gather(
consumer_task, producer_task, loop=loop)
producer_task = asyncio.ensure_future(
push_data_with_sleep(redis, loop, source, value))
results = await asyncio.gather(consumer_task, producer_task)
assert results[0] == value
assert results[1] == 1

Expand Down
18 changes: 9 additions & 9 deletions tests/locks_test.py
Expand Up @@ -6,27 +6,27 @@

@pytest.mark.run_loop
async def test_finished_waiter_cancelled(loop):
lock = Lock(loop=loop)
lock = Lock()

ta = asyncio.ensure_future(lock.acquire(), loop=loop)
await asyncio.sleep(0, loop=loop)
ta = asyncio.ensure_future(lock.acquire())
await asyncio.sleep(0)
assert lock.locked()

tb = asyncio.ensure_future(lock.acquire(), loop=loop)
await asyncio.sleep(0, loop=loop)
tb = asyncio.ensure_future(lock.acquire())
await asyncio.sleep(0)
assert len(lock._waiters) == 1

# Create a second waiter, wake up the first, and cancel it.
# Without the fix, the second was not woken up and the lock
# will never be locked
asyncio.ensure_future(lock.acquire(), loop=loop)
await asyncio.sleep(0, loop=loop)
asyncio.ensure_future(lock.acquire())
await asyncio.sleep(0)
lock.release()
tb.cancel()

await asyncio.sleep(0, loop=loop)
await asyncio.sleep(0)
assert ta.done()
assert tb.cancelled()

await asyncio.sleep(0, loop=loop)
await asyncio.sleep(0)
assert lock.locked()
21 changes: 9 additions & 12 deletions tests/pool_test.py
Expand Up @@ -134,8 +134,7 @@ async def test_create_constraints(create_pool, loop, server):

with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(pool.acquire(),
timeout=0.2,
loop=loop)
timeout=0.2)


@pytest.mark.run_loop
Expand All @@ -152,8 +151,7 @@ async def test_create_no_minsize(create_pool, loop, server):

with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(pool.acquire(),
timeout=0.2,
loop=loop)
timeout=0.2)
assert pool.size == 1
assert pool.freesize == 1

Expand Down Expand Up @@ -214,7 +212,7 @@ async def test_release_pending(create_pool, loop, server, caplog):
b'somekey:not:exists',
b'0'),
0.05,
loop=loop)
)
except asyncio.TimeoutError:
pass
assert pool.size == 0
Expand Down Expand Up @@ -327,8 +325,7 @@ async def test_select_and_create(create_pool, loop, server):
while True:
db = (db + 1) & 1
_, conn = await asyncio.gather(pool.select(db),
pool.acquire(),
loop=loop)
pool.acquire())
assert pool.db == db
pool.release(conn)
if conn.db == db:
Expand Down Expand Up @@ -397,7 +394,7 @@ async def task1(i):
with (await pool):
assert pool.size <= pool.maxsize
assert pool.freesize == 0
await asyncio.sleep(0.2, loop=loop)
await asyncio.sleep(0.2)
done.add(i)

async def task2():
Expand All @@ -407,9 +404,9 @@ async def task2():
assert done == {0, 1}

for _ in range(2):
tasks.append(asyncio.ensure_future(task1(_), loop=loop))
tasks.append(asyncio.ensure_future(task2(), loop=loop))
await asyncio.gather(*tasks, loop=loop)
tasks.append(asyncio.ensure_future(task1(_)))
tasks.append(asyncio.ensure_future(task2()))
await asyncio.gather(*tasks)


@pytest.mark.run_loop
Expand Down Expand Up @@ -543,7 +540,7 @@ async def test_pool_idle_close(create_pool, start_server, loop, caplog):
with caplog.at_level('DEBUG', 'aioredis'):
# wait for either disconnection logged or test timeout reached.
while len(caplog.record_tuples) < 2:
await asyncio.sleep(.5, loop=loop)
await asyncio.sleep(.5)
expected = [
('aioredis', logging.DEBUG,
'Connection has been closed by server, response: None'),
Expand Down

0 comments on commit de36e3f

Please sign in to comment.