Skip to content

Commit cc4cb37

Browse files
bearritobstrausser
and
bstrausser
authoredMar 6, 2024··
feat(redis): support AsyncRedisContainer (#442)
Co-authored-by: bstrausser <bstrausser@locusrobotics.com>
1 parent 5356caf commit cc4cb37

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed
 

‎modules/redis/testcontainers/redis/__init__.py

+27
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import Optional
1515

1616
import redis
17+
from redis.asyncio import Redis as asyncRedis
1718
from testcontainers.core.container import DockerContainer
1819
from testcontainers.core.utils import raise_for_deprecated_parameter
1920
from testcontainers.core.waiting_utils import wait_container_is_ready
@@ -69,3 +70,29 @@ def start(self) -> "RedisContainer":
6970
super().start()
7071
self._connect()
7172
return self
73+
74+
75+
class AsyncRedisContainer(RedisContainer):
76+
"""
77+
Redis container.
78+
79+
Example
80+
-------
81+
.. doctest::
82+
83+
>>> from testcontainers.redis import AsyncRedisContainer
84+
85+
>>> with AsyncRedisContainer() as redis_container:
86+
... redis_client =await redis_container.get_async_client()
87+
"""
88+
89+
def __init__(self, image="redis:latest", port_to_expose=6379, password=None, **kwargs):
90+
super().__init__(image, port_to_expose, password, **kwargs)
91+
92+
async def get_async_client(self, **kwargs):
93+
return await asyncRedis(
94+
host=self.get_container_host_ip(),
95+
port=self.get_exposed_port(self.port),
96+
password=self.password,
97+
**kwargs,
98+
)

‎modules/redis/tests/test_redis.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22

3-
from testcontainers.redis import RedisContainer
3+
from testcontainers.redis import RedisContainer, AsyncRedisContainer
4+
import pytest
45

56

67
def 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+
2670
def wait_for_message(pubsub, timeout=1, ignore_subscribe_messages=True):
2771
now = time.time()
2872
timeout = now + timeout

‎poetry.lock

+39-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pytest = "7.4.3"
115115
pytest-cov = "4.1.0"
116116
sphinx = "^7.2.6"
117117
twine = "^4.0.2"
118+
anyio = "^4.3.0"
118119

119120
[[tool.poetry.source]]
120121
name = "PyPI"

0 commit comments

Comments
 (0)
Please sign in to comment.