11import time
22
3- from testcontainers .redis import RedisContainer
3+ from testcontainers .redis import RedisContainer , AsyncRedisContainer
4+ import pytest
45
56
67def test_docker_run_redis ():
@@ -23,6 +24,49 @@ def test_docker_run_redis_with_password():
2324 assert client .get ("hello" ) == "world"
2425
2526
27+ pytest .mark .usefixtures ("anyio_backend" )
28+
29+
30+ @pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
31+ async def test_key_set_in_async_redis (anyio_backend ):
32+ with AsyncRedisContainer () as container :
33+ async_redis_client : redis .Redis = await container .get_async_client (decode_responses = True )
34+ key = "key"
35+ expected_value = 1
36+ await async_redis_client .set (key , expected_value )
37+ actual_value = await async_redis_client .get (key )
38+ assert int (actual_value ) == expected_value
39+
40+
41+ pytest .mark .usefixtures ("anyio_backend" )
42+
43+
44+ @pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
45+ @pytest .mark .skip (reason = "Need to sort out async pub/sub" )
46+ async def test_docker_run_async_redis (anyio_backend ):
47+ config = AsyncRedisContainer ()
48+ with config as container :
49+ client : redis .Redis = await container .get_async_client (decode_responses = True )
50+ p = await client .pubsub ()
51+ await p .subscribe ("test" )
52+ await client .publish ("test" , "new_msg" )
53+ msg = wait_for_message (p )
54+ assert "data" in msg
55+ assert b"new_msg" , msg ["data" ]
56+
57+
58+ pytest .mark .usefixtures ("anyio_backend" )
59+
60+
61+ @pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
62+ async def test_docker_run_async_redis_with_password (anyio_backend ):
63+ config = AsyncRedisContainer (password = "mypass" )
64+ with config as container :
65+ client : redis .Redis = await container .get_async_client (decode_responses = True )
66+ await client .set ("hello" , "world" )
67+ assert await client .get ("hello" ) == "world"
68+
69+
2670def wait_for_message (pubsub , timeout = 1 , ignore_subscribe_messages = True ):
2771 now = time .time ()
2872 timeout = now + timeout
0 commit comments