Skip to content

feat(redis): add AsyncRedis support #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/ci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ jobs:
with:
python-version: "3.9" # the pre-commit is hooked in as 3.9
- name: Install Python dependencies
run: poetry install
- name: Install pre-commit
run: pip install pre-commit
- name: Run linter
run: pre-commit run -a
run: poetry install --no-interaction
- name: Execute pre-commit handler
run: poetry run pre-commit run -a
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.9
python: python3

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand All @@ -10,13 +10,13 @@ repos:
- id: end-of-file-fixer

- repo: https://github.com/psf/black-pre-commit-mirror
rev: '24.1.1'
rev: '24.2.0'
hooks:
- id: black
args: [ '--config', 'pyproject.toml' ]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.14'
rev: 'v0.3.0'
hooks:
- id: ruff
# Explicitly setting config to prevent Ruff from using `pyproject.toml` in sub packages.
Expand Down
27 changes: 27 additions & 0 deletions modules/redis/testcontainers/redis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import Optional

import redis
from redis.asyncio import Redis as asyncRedis
from testcontainers.core.container import DockerContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.waiting_utils import wait_container_is_ready
Expand Down Expand Up @@ -69,3 +70,29 @@ def start(self) -> "RedisContainer":
super().start()
self._connect()
return self


class AsyncRedisContainer(RedisContainer):
"""
Redis container.

Example
-------
.. doctest::

>>> from testcontainers.redis import AsyncRedisContainer

>>> with AsyncRedisContainer() as redis_container:
... redis_client =await redis_container.get_async_client()
"""

def __init__(self, image="redis:latest", port_to_expose=6379, password=None, **kwargs):
super().__init__(image, port_to_expose, password, **kwargs)

async def get_async_client(self, **kwargs):
return await asyncRedis(
host=self.get_container_host_ip(),
port=self.get_exposed_port(self.port),
password=self.password,
**kwargs,
)
61 changes: 60 additions & 1 deletion modules/redis/tests/test_redis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time

from testcontainers.redis import RedisContainer
from testcontainers.redis import RedisContainer, AsyncRedisContainer
import redis
import pytest


def test_docker_run_redis():
Expand All @@ -23,6 +25,63 @@ def test_docker_run_redis_with_password():
assert client.get("hello") == "world"


pytest.mark.usefixtures("anyio_backend")


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
async def test_key_set_in_async_redis(anyio_backend):
with AsyncRedisContainer() as container:
async_redis_client: redis.Redis = await container.get_async_client(decode_responses=True)
key = "key"
expected_value = 1
await async_redis_client.set(key, expected_value)
actual_value = await async_redis_client.get(key)
assert int(actual_value) == expected_value


pytest.mark.usefixtures("anyio_backend")


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
@pytest.mark.skip(reason="Need to sort out async pub/sub")
async def test_docker_run_async_redis(anyio_backend):
config = AsyncRedisContainer()
with config as container:
client: redis.Redis = await container.get_async_client(decode_responses=True)
p = await client.pubsub()
await p.subscribe("test")
await client.publish("test", "new_msg")
msg = wait_for_message(p)
assert "data" in msg
assert b"new_msg", msg["data"]


pytest.mark.usefixtures("anyio_backend")


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
async def test_docker_run_async_redis_with_password(anyio_backend):
config = AsyncRedisContainer(password="mypass")
with config as container:
client: redis.Redis = await container.get_async_client(decode_responses=True)
await client.set("hello", "world")
assert await client.get("hello") == "world"


pytest.mark.usefixtures("anyio_backend")


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
async def test_key_set_in_async(anyio_backend):
with AsyncRedisContainer() as container:
async_redis_client: redis.Redis = await container.get_async_client(decode_responses=True)
key = "key"
expected_value = 1
await async_redis_client.set(key, expected_value)
actual_value = await async_redis_client.get(key)
assert int(actual_value) == expected_value


def wait_for_message(pubsub, timeout=1, ignore_subscribe_messages=True):
now = time.time()
timeout = now + timeout
Expand Down
Loading