22import functools
33import os
44import sys
5+ from typing import Generator
56
67import msgpack
78import pytest
89import redis .exceptions
910from redis .asyncio .retry import Retry
1011from redis .backoff import NoBackoff
12+ from testcontainers .redis import RedisContainer
1113
12- from arq .connections import ArqRedis , create_pool
14+ from arq .connections import ArqRedis , RedisSettings , create_pool
1315from arq .worker import Worker
1416
1517
1618@pytest .fixture (name = 'loop' )
17- def _fix_loop (event_loop ) :
19+ def _fix_loop (event_loop : asyncio . AbstractEventLoop ) -> asyncio . AbstractEventLoop :
1820 return event_loop
1921
2022
23+ @pytest .fixture (scope = 'session' )
24+ def redis_version () -> str :
25+ return os .getenv ('ARQ_TEST_REDIS_VERSION' , 'latest' )
26+
27+
28+ @pytest .fixture (scope = 'session' )
29+ def redis_container (redis_version : str ) -> Generator [RedisContainer , None , None ]:
30+ with RedisContainer (f'redis:{ redis_version } ' ) as redis :
31+ yield redis
32+
33+
34+ @pytest .fixture (scope = 'session' )
35+ def test_redis_host (redis_container : RedisContainer ) -> str :
36+ return redis_container .get_container_host_ip ()
37+
38+
39+ @pytest .fixture (scope = 'session' )
40+ def test_redis_port (redis_container : RedisContainer ) -> int :
41+ return redis_container .get_exposed_port (redis_container .port_to_expose )
42+
43+
44+ @pytest .fixture (scope = 'session' )
45+ def test_redis_settings (test_redis_host : str , test_redis_port : int ) -> RedisSettings :
46+ return RedisSettings (host = test_redis_host , port = test_redis_port )
47+
48+
2149@pytest .fixture
22- async def arq_redis (loop ):
50+ async def arq_redis (test_redis_host : str , test_redis_port : int ):
2351 redis_ = ArqRedis (
24- host = 'localhost' ,
25- port = 6379 ,
52+ host = test_redis_host ,
53+ port = test_redis_port ,
2654 encoding = 'utf-8' ,
2755 )
2856
@@ -34,10 +62,10 @@ async def arq_redis(loop):
3462
3563
3664@pytest .fixture
37- async def arq_redis_msgpack (loop ):
65+ async def arq_redis_msgpack (test_redis_host : str , test_redis_port : int ):
3866 redis_ = ArqRedis (
39- host = 'localhost' ,
40- port = 6379 ,
67+ host = test_redis_host ,
68+ port = test_redis_port ,
4169 encoding = 'utf-8' ,
4270 job_serializer = msgpack .packb ,
4371 job_deserializer = functools .partial (msgpack .unpackb , raw = False ),
@@ -48,10 +76,10 @@ async def arq_redis_msgpack(loop):
4876
4977
5078@pytest .fixture
51- async def arq_redis_retry (loop ):
79+ async def arq_redis_retry (test_redis_host : str , test_redis_port : int ):
5280 redis_ = ArqRedis (
53- host = 'localhost' ,
54- port = 6379 ,
81+ host = test_redis_host ,
82+ port = test_redis_port ,
5583 encoding = 'utf-8' ,
5684 retry = Retry (backoff = NoBackoff (), retries = 3 ),
5785 retry_on_timeout = True ,
0 commit comments