11import os
22import pytest
3- import asyncio
43
54from redisvl .redis .connection import RedisConnectionFactory
65from testcontainers .compose import DockerCompose
76
87
8+ @pytest .fixture (autouse = True )
9+ def set_tokenizers_parallelism ():
10+ """Disable tokenizers parallelism in tests to avoid deadlocks"""
11+ os .environ ["TOKENIZERS_PARALLELISM" ] = "false"
12+
913
1014@pytest .fixture (scope = "session" , autouse = True )
11- def redis_container ():
12- # Set the default Redis version if not already set
15+ def redis_container (request ):
16+ """
17+ Create a unique Compose project for each xdist worker by setting
18+ COMPOSE_PROJECT_NAME. That prevents collisions on container/volume names.
19+ """
20+ # In xdist, the config has "workerid" in workerinput
21+ worker_id = request .config .workerinput .get ("workerid" , "master" )
22+
23+ # Set the Compose project name so containers do not clash across workers
24+ os .environ ["COMPOSE_PROJECT_NAME" ] = f"redis_test_{ worker_id } "
1325 os .environ .setdefault ("REDIS_VERSION" , "edge" )
1426
15- compose = DockerCompose ("tests" , compose_file_name = "docker-compose.yml" , pull = True )
27+ compose = DockerCompose (
28+ context = "tests" ,
29+ compose_file_name = "docker-compose.yml" ,
30+ pull = True ,
31+ )
1632 compose .start ()
1733
18- redis_host , redis_port = compose .get_service_host_and_port ("redis" , 6379 )
19- redis_url = f"redis://{ redis_host } :{ redis_port } "
20- os .environ ["REDIS_URL" ] = redis_url
21-
2234 yield compose
2335
2436 compose .stop ()
2537
38+
2639@pytest .fixture (scope = "session" )
27- def redis_url ():
28- return os .getenv ("REDIS_URL" , "redis://localhost:6379" )
40+ def redis_url (redis_container ):
41+ """
42+ Use the `DockerCompose` fixture to get host/port of the 'redis' service
43+ on container port 6379 (mapped to an ephemeral port on the host).
44+ """
45+ host , port = redis_container .get_service_host_and_port ("redis" , 6379 )
46+ return f"redis://{ host } :{ port } "
2947
3048@pytest .fixture
3149async def async_client (redis_url ):
50+ """
51+ An async Redis client that uses the dynamic `redis_url`.
52+ """
3253 client = await RedisConnectionFactory .get_async_redis_connection (redis_url )
3354 yield client
3455 try :
@@ -38,8 +59,11 @@ async def async_client(redis_url):
3859 raise
3960
4061@pytest .fixture
41- def client ():
42- conn = RedisConnectionFactory .get_redis_connection (os .environ ["REDIS_URL" ])
62+ def client (redis_url ):
63+ """
64+ A sync Redis client that uses the dynamic `redis_url`.
65+ """
66+ conn = RedisConnectionFactory .get_redis_connection (redis_url )
4367 yield conn
4468 conn .close ()
4569
0 commit comments