11# pylint: disable=redefined-outer-name
2+ import shutil
3+ import subprocess
24import time
35from pathlib import Path
46
57import pytest
8+ import redis
69import requests
7- from requests .exceptions import ConnectionError
10+ from requests .exceptions import RequestException
11+ from redis .exceptions import RedisError
812from sqlalchemy .exc import OperationalError
913from sqlalchemy import create_engine
1014from sqlalchemy .orm import sessionmaker , clear_mappers
@@ -48,11 +52,22 @@ def wait_for_webapp_to_come_up():
4852 while time .time () < deadline :
4953 try :
5054 return requests .get (url )
51- except ConnectionError :
55+ except RequestException :
5256 time .sleep (0.5 )
5357 pytest .fail ("API never came up" )
5458
5559
60+ def wait_for_redis_to_come_up ():
61+ deadline = time .time () + 5
62+ r = redis .Redis (** config .get_redis_host_and_port ())
63+ while time .time () < deadline :
64+ try :
65+ return r .ping ()
66+ except RedisError :
67+ time .sleep (0.5 )
68+ pytest .fail ("Redis never came up" )
69+
70+
5671@pytest .fixture (scope = "session" )
5772def postgres_db ():
5873 engine = create_engine (config .get_postgres_uri ())
@@ -78,3 +93,15 @@ def restart_api():
7893 (Path (__file__ ).parent / "../src/allocation/entrypoints/flask_app.py" ).touch ()
7994 time .sleep (0.5 )
8095 wait_for_webapp_to_come_up ()
96+
97+
98+ @pytest .fixture
99+ def restart_redis_pubsub ():
100+ wait_for_redis_to_come_up ()
101+ if not shutil .which ("docker-compose" ):
102+ print ("skipping restart, assumes running in container" )
103+ return
104+ subprocess .run (
105+ ["docker-compose" , "restart" , "-t" , "0" , "redis_pubsub" ],
106+ check = True ,
107+ )
0 commit comments