fix: list_family_test works with async client (#992)

This commit is contained in:
Roman Gershman 2023-03-25 21:28:29 +03:00 committed by GitHub
parent 3939da1069
commit 32f2f4b72a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,42 +1,23 @@
from threading import Thread
import asyncio
import aioredis
import pytest
import redis
class BLPopWorkerThread:
def __init__(self):
self.result = None
self.thread = None
def async_blpop(self, client: redis.Redis):
self.result = None
def blpop_task(self, client):
self.result = client.blpop(
['list1{t}', 'list2{t}', 'list2{t}', 'list1{t}'], 0.5)
self.thread = Thread(target=blpop_task, args=(self, client))
self.thread.start()
def wait(self, timeout):
self.thread.join(timeout)
return not self.thread.is_alive()
@pytest.mark.parametrize('index', range(50))
class TestBlPop:
def test_blpop_multiple_keys(self, client, index):
wt_blpop = BLPopWorkerThread()
wt_blpop.async_blpop(client)
async def async_blpop(client: aioredis.Redis):
return await client.blpop(
['list1{t}', 'list2{t}', 'list2{t}', 'list1{t}'], 0.5)
client.lpush('list1{t}', 'a')
assert wt_blpop.wait(2)
assert wt_blpop.result[1] == 'a'
watched = client.execute_command('DEBUG WATCHED')
async def blpop_mult_keys(async_client: aioredis.Redis, key: str, val: str):
task = asyncio.create_task(TestBlPop.async_blpop(async_client))
await async_client.lpush(key, val)
result = await asyncio.wait_for(task, 3)
assert result[1] == val
watched = await async_client.execute_command('DEBUG WATCHED')
assert watched == ['awaked', [], 'watched', []]
wt_blpop.async_blpop(client)
client.lpush('list2{t}', 'b')
assert wt_blpop.wait(2)
assert wt_blpop.result[1] == 'b'
async def test_blpop_multiple_keys(self, async_client: aioredis.Redis, index):
await TestBlPop.blpop_mult_keys(async_client, 'list1{t}', 'a')
await TestBlPop.blpop_mult_keys(async_client, 'list2{t}', 'b')